Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exported flip method #35

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ImageScript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Image {
* [Symbol.iterator](): void;

* iterateWithColors(): Generator<[x: number, y: number, color: number], void, unknown>;

static rgbaToColor(r: number, g: number, b: number, a: number): number;

static rgbToColor(r: number, g: number, b: number): number;
Expand Down Expand Up @@ -112,6 +112,8 @@ export class Image {

rotate(angle: number, resize?: boolean): Image;

flip(direction: 'horizontal' | 'vertical'): Image;

private __apply__(image: Image | Frame): Image | Frame;

static gradient(colors: { [position: number]: number; }): ((position: number) => number);
Expand Down Expand Up @@ -162,7 +164,7 @@ export class Frame extends Image {
get disposalMode(): number;

set disposalMode(disposalMode: string|number);

toString(): string;

static from(image: Image, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: typeof Frame.DISPOSAL_KEEP | string): Frame;
Expand Down
11 changes: 11 additions & 0 deletions ImageScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,17 @@ class Image {
return this.__apply__(out);
}

/**
* Flips / mirrors the image horizontally or vertically
* @param {'horizontal' | 'vertical'} direction The direction to flip
*/
flip(direction) {
const frame = new v2(this.width, this.height, this.bitmap).flip(direction);

this.bitmap.set(frame.u8);
return this;
}

/**
* @private
* @param {Image|Frame} image
Expand Down
36 changes: 36 additions & 0 deletions tests/flip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs').promises;
const {Image} = require('../ImageScript');

(async () => {
const input = await fs.readFile('./tests/targets/external.png');

{
const image = await Image.decode(input);
image.flip('horizontal');

const output = await image.encode(1, {creationTime: 0, software: ''});

if (process.env.OVERWRITE_TEST)
await fs.writeFile('./tests/targets/flip-horizontal.png', output);

const target = await fs.readFile('./tests/targets/flip-horizontal.png');

if (!Buffer.from(target).equals(Buffer.from(output)))
process.exit(1);
}

{
const image = await Image.decode(input);
image.flip('vertical');

const output = await image.encode(1, {creationTime: 0, software: ''});

if (process.env.OVERWRITE_TEST)
await fs.writeFile('./tests/targets/flip-vertical.png', output);

const target = await fs.readFile('./tests/targets/flip-vertical.png');

if (!Buffer.from(target).equals(Buffer.from(output)))
process.exit(1);
}
})();
Binary file added tests/targets/flip-horizontal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/targets/flip-vertical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading