Skip to content

Commit

Permalink
fix: lanczos throwing when destination dimensions are not round (#50)
Browse files Browse the repository at this point in the history
* fix: lanczos throwing when destination dimensions are not round

Images over `MAX_WIDTH` that don't scale to a rounded width or height
were causing lanczos to blow up with `RangeError: byte length of Uint32Array should be a multiple of 4`

This change ensures that the target dimensions are rounded and the resulting target `Uint32Array`
has a length that is a multiple of 4

* chore: add changeset

---------

Co-authored-by: Jack Works <5390719+Jack-Works@users.noreply.github.com>
  • Loading branch information
carlmw and Jack-Works authored Dec 13, 2024
1 parent f27b915 commit 40d94ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lemon-penguins-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@masknet/stego-js': patch
---

fix: lanczos throwing when destination dimensions are not round
2 changes: 1 addition & 1 deletion src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/image.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 40d94ae

Please # to comment.