Skip to content

Commit ea01eac

Browse files
Bind callback to image instance (#1335)
* Misc doc updates * Bind scan callbacks
1 parent 11cd408 commit ea01eac

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

packages/utils/src/index.test.ts

+39-12
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
import { expect, test, describe } from "vitest";
22

3-
import { colorDiff } from "./index.js";
3+
import { colorDiff, scan } from "./index.js";
4+
import { JimpClass } from "@jimp/types";
45

56
// Convert [0..1] float to a percent value with only one decimal.
67
const pct = (n: number) => ((n * 1000) << 0) / 10;
78

89
describe("colorDiff", () => {
910
test("totally opaque (no alpha defined)", () => {
1011
expect(colorDiff({ r: 255, g: 0, b: 0 }, { r: 255, g: 0, b: 0 })).toEqual(
11-
0,
12+
0
1213
);
1314

1415
expect(
15-
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 })),
16+
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 }))
1617
).toEqual(33.3);
1718

1819
expect(
19-
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 })),
20+
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 }))
2021
).toEqual(66.6);
2122

2223
expect(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 255 })).toEqual(
23-
1,
24+
1
2425
);
2526

2627
expect(colorDiff({ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 })).toEqual(
27-
1,
28+
1
2829
);
2930
});
3031

3132
test("totally transparent", () => {
3233
expect(
33-
colorDiff({ r: 255, g: 0, b: 0, a: 0 }, { r: 255, g: 0, b: 0, a: 0 }),
34+
colorDiff({ r: 255, g: 0, b: 0, a: 0 }, { r: 255, g: 0, b: 0, a: 0 })
3435
).toEqual(0);
3536

3637
expect(
37-
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 0 }),
38+
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 0 })
3839
).toEqual(1);
3940
});
4041

@@ -43,13 +44,39 @@ describe("colorDiff", () => {
4344
pct(
4445
colorDiff(
4546
{ r: 255, g: 0, b: 0, a: 100 },
46-
{ r: 255, g: 0, b: 0, a: 150 },
47-
),
48-
),
47+
{ r: 255, g: 0, b: 0, a: 150 }
48+
)
49+
)
4950
).toEqual(3.8);
5051

5152
expect(
52-
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 255 }),
53+
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 255 })
5354
).toEqual(1);
5455
});
5556
});
57+
58+
describe("scan", () => {
59+
class TestImage {
60+
public bitmap = {
61+
height: 3,
62+
width: 3,
63+
data: Buffer.from([
64+
0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
65+
0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
66+
]),
67+
};
68+
}
69+
70+
const img = new TestImage() as JimpClass;
71+
72+
test("scan", () => {
73+
let count = 0;
74+
75+
scan(img, function (_, __, idx) {
76+
count++;
77+
expect(img.bitmap.data[idx]).toBe(this.bitmap.data[idx]);
78+
});
79+
80+
expect(count).toBe(img.bitmap.width * img.bitmap.height);
81+
});
82+
});

packages/utils/src/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function clone<I extends JimpClass>(image: I): I {
1515
export function scan<I extends JimpClass>(
1616
image: I,
1717
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18-
f: (this: I, x: number, y: number, idx: number) => any,
18+
f: (this: I, x: number, y: number, idx: number) => any
1919
): I;
2020
export function scan<I extends { bitmap: Bitmap }>(
2121
image: I,
@@ -24,7 +24,7 @@ export function scan<I extends { bitmap: Bitmap }>(
2424
w: number,
2525
h: number,
2626
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27-
cb: (x: number, y: number, idx: number) => any,
27+
cb: (x: number, y: number, idx: number) => any
2828
): I;
2929
export function scan<I extends { bitmap: Bitmap }>(
3030
image: I,
@@ -34,7 +34,7 @@ export function scan<I extends { bitmap: Bitmap }>(
3434
wArg?: number,
3535
hArg?: number,
3636
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37-
cbArg?: (x: number, y: number, idx: number) => any,
37+
cbArg?: (x: number, y: number, idx: number) => any
3838
): I {
3939
let x: number;
4040
let y: number;
@@ -67,10 +67,13 @@ export function scan<I extends { bitmap: Bitmap }>(
6767
w = Math.round(w);
6868
h = Math.round(h);
6969

70+
const bound = cb.bind(image);
71+
7072
for (let _y = y; _y < y + h; _y++) {
7173
for (let _x = x; _x < x + w; _x++) {
7274
const idx = (image.bitmap.width * _y + _x) << 2;
73-
cb(_x, _y, idx);
75+
// Bind the images so this.bitmap works
76+
bound(_x, _y, idx);
7477
}
7578
}
7679

@@ -82,7 +85,7 @@ export function* scanIterator<I extends JimpClass>(
8285
x: number,
8386
y: number,
8487
w: number,
85-
h: number,
88+
h: number
8689
) {
8790
// round input
8891
x = Math.round(x);
@@ -125,14 +128,14 @@ export function intToRGBA(i: number) {
125128
rgba.g = Math.floor((i - rgba.r * Math.pow(256, 3)) / Math.pow(256, 2));
126129
rgba.b = Math.floor(
127130
(i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2)) /
128-
Math.pow(256, 1),
131+
Math.pow(256, 1)
129132
);
130133
rgba.a = Math.floor(
131134
(i -
132135
rgba.r * Math.pow(256, 3) -
133136
rgba.g * Math.pow(256, 2) -
134137
rgba.b * Math.pow(256, 1)) /
135-
Math.pow(256, 0),
138+
Math.pow(256, 0)
136139
);
137140

138141
return rgba;
@@ -216,7 +219,7 @@ export function rgbaToInt(r: number, g: number, b: number, a: number) {
216219
*/
217220
export function colorDiff(
218221
rgba1: RGBAColor | RGBColor,
219-
rgba2: RGBAColor | RGBColor,
222+
rgba2: RGBAColor | RGBColor
220223
) {
221224
const sq = (n: number) => Math.pow(n, 2);
222225
const { max } = Math;

plugins/plugin-print/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export const methods = {
213213
y += font.common.lineHeight;
214214
});
215215

216-
cb({ x: x + longestLine, y });
216+
cb.bind(image)({ x: x + longestLine, y });
217217

218218
return image;
219219
},

0 commit comments

Comments
 (0)