diff --git a/.changeset/lemon-penguins-bathe.md b/.changeset/lemon-penguins-bathe.md new file mode 100644 index 0000000..e1bc476 --- /dev/null +++ b/.changeset/lemon-penguins-bathe.md @@ -0,0 +1,5 @@ +--- +'@masknet/stego-js': patch +--- + +fix: lanczos throwing when destination dimensions are not round diff --git a/src/utils/image.ts b/src/utils/image.ts index 2fdbfe0..25580a5 100644 --- a/src/utils/image.ts +++ b/src/utils/image.ts @@ -15,7 +15,7 @@ export function preprocessImage( if (imageData.width <= MAX_WIDTH && imageData.height <= MAX_WIDTH) return imageData const scale = MAX_WIDTH / Math.max(imageData.width, imageData.height) const [w, h] = [imageData.width * scale, imageData.height * scale] - const scaled = getScaled(w, h) + const scaled = getScaled(Math.round(w), Math.round(h)) if (scaled) { lanczos(imageData, scaled) return scaled diff --git a/tests/image.ts b/tests/image.ts new file mode 100644 index 0000000..c6d8b38 --- /dev/null +++ b/tests/image.ts @@ -0,0 +1,23 @@ +import { expect, test, vi } from 'vitest' +import { preprocessImage } from '../src/utils/image.js' +import { MAX_WIDTH } from '../src/constant.js' + +test('preprocessImage rounds the dimensions of the scaled image', async () => { + const getScaled = vi.fn().mockImplementation((width, height) => ({ + height, + width, + colorSpace: 'srgb', + data: new Uint8ClampedArray(width * height * 4), + })) + preprocessImage( + { + width: 1980, + height: 1024, + colorSpace: 'srgb', + data: new Uint8ClampedArray(1980 * 1024 * 4), + }, + getScaled, + ) + + expect(getScaled).toHaveBeenCalledWith(MAX_WIDTH, 1014) +})