Skip to content

Commit 40d94ae

Browse files
carlmwJack-Works
andauthored
fix: lanczos throwing when destination dimensions are not round (#50)
* 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 <[email protected]>
1 parent f27b915 commit 40d94ae

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.changeset/lemon-penguins-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@masknet/stego-js': patch
3+
---
4+
5+
fix: lanczos throwing when destination dimensions are not round

src/utils/image.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function preprocessImage(
1515
if (imageData.width <= MAX_WIDTH && imageData.height <= MAX_WIDTH) return imageData
1616
const scale = MAX_WIDTH / Math.max(imageData.width, imageData.height)
1717
const [w, h] = [imageData.width * scale, imageData.height * scale]
18-
const scaled = getScaled(w, h)
18+
const scaled = getScaled(Math.round(w), Math.round(h))
1919
if (scaled) {
2020
lanczos(imageData, scaled)
2121
return scaled

tests/image.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, test, vi } from 'vitest'
2+
import { preprocessImage } from '../src/utils/image.js'
3+
import { MAX_WIDTH } from '../src/constant.js'
4+
5+
test('preprocessImage rounds the dimensions of the scaled image', async () => {
6+
const getScaled = vi.fn().mockImplementation((width, height) => ({
7+
height,
8+
width,
9+
colorSpace: 'srgb',
10+
data: new Uint8ClampedArray(width * height * 4),
11+
}))
12+
preprocessImage(
13+
{
14+
width: 1980,
15+
height: 1024,
16+
colorSpace: 'srgb',
17+
data: new Uint8ClampedArray(1980 * 1024 * 4),
18+
},
19+
getScaled,
20+
)
21+
22+
expect(getScaled).toHaveBeenCalledWith(MAX_WIDTH, 1014)
23+
})

0 commit comments

Comments
 (0)