Skip to content

Commit

Permalink
feat: Refactoring of existing image transformation functions, various…
Browse files Browse the repository at this point in the history
… new transformation functions have been added
  • Loading branch information
yegor-pelykh committed Oct 31, 2022
1 parent 3813dd1 commit 7caa9a1
Show file tree
Hide file tree
Showing 16 changed files with 901 additions and 472 deletions.
39 changes: 39 additions & 0 deletions src/common/point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 2-dimensional point
*
* @format
*/

export class Point {
public x: number;
public y: number;

public get xt() {
return Math.trunc(this.x);
}

public get yt() {
return Math.trunc(this.y);
}

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}

public static from(other: Point) {
return new Point(other.x, other.y);
}

public mul(s: number) {
return new Point(this.x * s, this.y * s);
}

public add(rhs: Point) {
return new Point(this.x + rhs.x, this.y + rhs.y);
}

public equals(other: unknown) {
return other instanceof Point && this.x === other.x && this.y === other.y;
}
}
50 changes: 50 additions & 0 deletions src/common/rectangle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/** @format */

import { Point } from './point';

export class Rectangle {
public left: number;
public top: number;
public right: number;
public bottom: number;

public get topLeft(): Point {
return new Point(this.left, this.top);
}

public get topRight(): Point {
return new Point(this.right, this.top);
}

public get bottomRight(): Point {
return new Point(this.right, this.bottom);
}

public get bottomLeft(): Point {
return new Point(this.left, this.bottom);
}

public get width(): number {
return this.bottom - this.top;
}

public get height(): number {
return this.right - this.left;
}

constructor(left: number, top: number, right: number, bottom: number) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}

public static fromPosSize(
left: number,
top: number,
width: number,
height: number
) {
return new Rectangle(left, top, left + width, top + height);
}
}
4 changes: 2 additions & 2 deletions src/formats/gif-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ListUtils } from '../common/list-utils';
import { MemoryImage } from '../common/memory-image';
import { ImageError } from '../error/image-error';
import { HdrImage } from '../hdr/hdr-image';
import { CopyIntoTransform } from '../transform/copy-into';
import { ImageTransform } from '../transform/image-transform';
import { Decoder } from './decoder';
import { GifColorMap } from './gif/gif-color-map';
import { GifImageDesc } from './gif/gif-image-desc';
Expand Down Expand Up @@ -729,7 +729,7 @@ export class GifDecoder implements Decoder {
lastImage = MemoryImage.from(lastImage);
}

CopyIntoTransform.copyInto({
ImageTransform.copyInto({
dst: lastImage,
src: image,
dstX: frame.x,
Expand Down
4 changes: 2 additions & 2 deletions src/formats/png-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { TextCodec } from '../common/text-codec';
import { ImageError } from '../error/image-error';
import { NotImplementedError } from '../error/not-implemented-error';
import { HdrImage } from '../hdr/hdr-image';
import { CopyIntoTransform } from '../transform/copy-into';
import { ImageTransform } from '../transform/image-transform';
import { DecodeInfo } from './decode-info';
import { Decoder } from './decoder';
import { PngFrame } from './png/png-frame';
Expand Down Expand Up @@ -1097,7 +1097,7 @@ export class PngDecoder implements Decoder {
// Convert to MS
lastImage.duration = Math.trunc(frame.delay * 1000);

CopyIntoTransform.copyInto({
ImageTransform.copyInto({
dst: lastImage,
src: image,
dstX: frame.xOffset,
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ export { HdrSlice, HdrSliceInitOptions } from './hdr/hdr-slice';
export { HdrToImage } from './hdr/hdr-to-image';

// Export types from 'transform' directory
export { BakeOrientationTransform } from './transform/bake-orientation';
export { CopyIntoTransform, CopyIntoOptions } from './transform/copy-into';
export { CopyIntoOptions } from './transform/copy-into-options';
export {
CopyResizeTransform,
CopyResizeOptionsUsingWidth,
CopyResizeOptionsUsingHeight,
} from './transform/copy-resize';
export { CopyRotateTransform } from './transform/copy-rotate';
CopyResizeOptionsUsingWidth,
} from './transform/copy-resize-options';
export { FlipDirection } from './transform/flip-direction';
export { FlipTransform } from './transform/flip';
export { ImageTransform } from './transform/image-transform';
export { TrimMode } from './transform/trim-mode';
export { TrimSide } from './transform/trim-side';
export { TrimTransform } from './transform/trim';

/**
* Find a [Decoder] that is able to decode the given image [data].
Expand Down
49 changes: 0 additions & 49 deletions src/transform/bake-orientation.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/transform/copy-into-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @format */

import { MemoryImage } from '../common/memory-image';

export interface CopyIntoOptions {
dst: MemoryImage;
src: MemoryImage;
dstX?: number;
dstY?: number;
srcX?: number;
srcY?: number;
srcW?: number;
srcH?: number;
blend?: boolean;
center?: boolean;
}
95 changes: 0 additions & 95 deletions src/transform/copy-into.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/transform/copy-resize-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @format */

import { MemoryImage } from '../common/memory-image';
import { Interpolation } from '../formats/util/interpolation';

export interface CopyResizeOptionsUsingWidth {
image: MemoryImage;
width: number;
height?: number;
interpolation?: Interpolation;
}

export interface CopyResizeOptionsUsingHeight {
image: MemoryImage;
height: number;
width?: number;
interpolation?: Interpolation;
}
Loading

0 comments on commit 7caa9a1

Please sign in to comment.