From ea01eac9ea5031d776f4cc646d6922dbbed4acd1 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Sat, 7 Sep 2024 15:02:03 -0700 Subject: [PATCH] Bind callback to image instance (#1335) * Misc doc updates * Bind scan callbacks --- packages/utils/src/index.test.ts | 51 +++++++++++++++++++++++-------- packages/utils/src/index.ts | 19 +++++++----- plugins/plugin-print/src/index.ts | 2 +- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/packages/utils/src/index.test.ts b/packages/utils/src/index.test.ts index 9f994c3b..065a7d4b 100644 --- a/packages/utils/src/index.test.ts +++ b/packages/utils/src/index.test.ts @@ -1,6 +1,7 @@ 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; @@ -8,33 +9,33 @@ 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); }); @@ -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); + }); +}); diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index e3f483b1..173cae93 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -15,7 +15,7 @@ export function clone(image: I): I { export function scan( 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( image: I, @@ -24,7 +24,7 @@ export function scan( 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( image: I, @@ -34,7 +34,7 @@ export function scan( 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; @@ -67,10 +67,13 @@ export function scan( 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); } } @@ -82,7 +85,7 @@ export function* scanIterator( x: number, y: number, w: number, - h: number, + h: number ) { // round input x = Math.round(x); @@ -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; @@ -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; diff --git a/plugins/plugin-print/src/index.ts b/plugins/plugin-print/src/index.ts index f815a3c9..6355b171 100644 --- a/plugins/plugin-print/src/index.ts +++ b/plugins/plugin-print/src/index.ts @@ -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; },