Skip to content

Commit

Permalink
Bind callback to image instance (#1335)
Browse files Browse the repository at this point in the history
* Misc doc updates

* Bind scan callbacks
  • Loading branch information
hipstersmoothie authored Sep 7, 2024
1 parent 11cd408 commit ea01eac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
51 changes: 39 additions & 12 deletions packages/utils/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import { expect, test, describe } from "vitest";

import { colorDiff } from "./index.js";
import { colorDiff, scan } from "./index.js";
import { JimpClass } from "@jimp/types";

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

describe("colorDiff", () => {
test("totally opaque (no alpha defined)", () => {
expect(colorDiff({ r: 255, g: 0, b: 0 }, { r: 255, g: 0, b: 0 })).toEqual(
0,
0
);

expect(
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 })),
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 }))
).toEqual(33.3);

expect(
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 })),
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 }))
).toEqual(66.6);

expect(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 255 })).toEqual(
1,
1
);

expect(colorDiff({ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 })).toEqual(
1,
1
);
});

test("totally transparent", () => {
expect(
colorDiff({ r: 255, g: 0, b: 0, a: 0 }, { r: 255, g: 0, b: 0, a: 0 }),
colorDiff({ r: 255, g: 0, b: 0, a: 0 }, { r: 255, g: 0, b: 0, a: 0 })
).toEqual(0);

expect(
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 0 }),
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 0 })
).toEqual(1);
});

Expand All @@ -43,13 +44,39 @@ describe("colorDiff", () => {
pct(
colorDiff(
{ r: 255, g: 0, b: 0, a: 100 },
{ r: 255, g: 0, b: 0, a: 150 },
),
),
{ r: 255, g: 0, b: 0, a: 150 }
)
)
).toEqual(3.8);

expect(
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 255 }),
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 255 })
).toEqual(1);
});
});

describe("scan", () => {
class TestImage {
public bitmap = {
height: 3,
width: 3,
data: Buffer.from([
0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
]),
};
}

const img = new TestImage() as JimpClass;

test("scan", () => {
let count = 0;

scan(img, function (_, __, idx) {
count++;
expect(img.bitmap.data[idx]).toBe(this.bitmap.data[idx]);
});

expect(count).toBe(img.bitmap.width * img.bitmap.height);
});
});
19 changes: 11 additions & 8 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function clone<I extends JimpClass>(image: I): I {
export function scan<I extends JimpClass>(
image: I,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
f: (this: I, x: number, y: number, idx: number) => any,
f: (this: I, x: number, y: number, idx: number) => any
): I;
export function scan<I extends { bitmap: Bitmap }>(
image: I,
Expand All @@ -24,7 +24,7 @@ export function scan<I extends { bitmap: Bitmap }>(
w: number,
h: number,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cb: (x: number, y: number, idx: number) => any,
cb: (x: number, y: number, idx: number) => any
): I;
export function scan<I extends { bitmap: Bitmap }>(
image: I,
Expand All @@ -34,7 +34,7 @@ export function scan<I extends { bitmap: Bitmap }>(
wArg?: number,
hArg?: number,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cbArg?: (x: number, y: number, idx: number) => any,
cbArg?: (x: number, y: number, idx: number) => any
): I {
let x: number;
let y: number;
Expand Down Expand Up @@ -67,10 +67,13 @@ export function scan<I extends { bitmap: Bitmap }>(
w = Math.round(w);
h = Math.round(h);

const bound = cb.bind(image);

for (let _y = y; _y < y + h; _y++) {
for (let _x = x; _x < x + w; _x++) {
const idx = (image.bitmap.width * _y + _x) << 2;
cb(_x, _y, idx);
// Bind the images so this.bitmap works
bound(_x, _y, idx);
}
}

Expand All @@ -82,7 +85,7 @@ export function* scanIterator<I extends JimpClass>(
x: number,
y: number,
w: number,
h: number,
h: number
) {
// round input
x = Math.round(x);
Expand Down Expand Up @@ -125,14 +128,14 @@ export function intToRGBA(i: number) {
rgba.g = Math.floor((i - rgba.r * Math.pow(256, 3)) / Math.pow(256, 2));
rgba.b = Math.floor(
(i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2)) /
Math.pow(256, 1),
Math.pow(256, 1)
);
rgba.a = Math.floor(
(i -
rgba.r * Math.pow(256, 3) -
rgba.g * Math.pow(256, 2) -
rgba.b * Math.pow(256, 1)) /
Math.pow(256, 0),
Math.pow(256, 0)
);

return rgba;
Expand Down Expand Up @@ -216,7 +219,7 @@ export function rgbaToInt(r: number, g: number, b: number, a: number) {
*/
export function colorDiff(
rgba1: RGBAColor | RGBColor,
rgba2: RGBAColor | RGBColor,
rgba2: RGBAColor | RGBColor
) {
const sq = (n: number) => Math.pow(n, 2);
const { max } = Math;
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-print/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const methods = {
y += font.common.lineHeight;
});

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

return image;
},
Expand Down

0 comments on commit ea01eac

Please sign in to comment.