Skip to content

Commit 0801159

Browse files
requizmmatmen
andauthored
feat: export flip method (#35)
Co-authored-by: Mathis Mensing <github@matmen.dev>
1 parent a498108 commit 0801159

5 files changed

Lines changed: 51 additions & 2 deletions

File tree

ImageScript.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Image {
1717
* [Symbol.iterator](): void;
1818

1919
* iterateWithColors(): Generator<[x: number, y: number, color: number], void, unknown>;
20-
20+
2121
static rgbaToColor(r: number, g: number, b: number, a: number): number;
2222

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

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

115+
flip(direction: 'horizontal' | 'vertical'): Image;
116+
115117
private __apply__(image: Image | Frame): Image | Frame;
116118

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

164166
set disposalMode(disposalMode: string|number);
165-
167+
166168
toString(): string;
167169

168170
static from(image: Image, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: typeof Frame.DISPOSAL_KEEP | string): Frame;

ImageScript.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,17 @@ class Image {
829829
return this.__apply__(out);
830830
}
831831

832+
/**
833+
* Flips / mirrors the image horizontally or vertically
834+
* @param {'horizontal' | 'vertical'} direction The direction to flip
835+
*/
836+
flip(direction) {
837+
const frame = new v2(this.width, this.height, this.bitmap).flip(direction);
838+
839+
this.bitmap.set(frame.u8);
840+
return this;
841+
}
842+
832843
/**
833844
* @private
834845
* @param {Image|Frame} image

tests/flip.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs').promises;
2+
const {Image} = require('../ImageScript');
3+
4+
(async () => {
5+
const input = await fs.readFile('./tests/targets/external.png');
6+
7+
{
8+
const image = await Image.decode(input);
9+
image.flip('horizontal');
10+
11+
const output = await image.encode(1, {creationTime: 0, software: ''});
12+
13+
if (process.env.OVERWRITE_TEST)
14+
await fs.writeFile('./tests/targets/flip-horizontal.png', output);
15+
16+
const target = await fs.readFile('./tests/targets/flip-horizontal.png');
17+
18+
if (!Buffer.from(target).equals(Buffer.from(output)))
19+
process.exit(1);
20+
}
21+
22+
{
23+
const image = await Image.decode(input);
24+
image.flip('vertical');
25+
26+
const output = await image.encode(1, {creationTime: 0, software: ''});
27+
28+
if (process.env.OVERWRITE_TEST)
29+
await fs.writeFile('./tests/targets/flip-vertical.png', output);
30+
31+
const target = await fs.readFile('./tests/targets/flip-vertical.png');
32+
33+
if (!Buffer.from(target).equals(Buffer.from(output)))
34+
process.exit(1);
35+
}
36+
})();

tests/targets/flip-horizontal.png

832 KB
Loading

tests/targets/flip-vertical.png

832 KB
Loading

0 commit comments

Comments
 (0)