@flighthq/image-codec 0.3.1-next.887.9f59a84 → 0.3.1-next.970.9691e7b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flighthq/image-codec",
|
|
3
|
-
"version": "0.3.1-next.
|
|
3
|
+
"version": "0.3.1-next.970.9691e7b",
|
|
4
4
|
"author": "Joshua Granick and other contributors",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"clean:dist": "tsx ../../scripts/clean-package-dist.ts"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@flighthq/types": "0.3.1-next.
|
|
41
|
+
"@flighthq/types": "0.3.1-next.970.9691e7b"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"typescript": "^5.3.0"
|
|
@@ -6,6 +6,12 @@ import { registerWebImageDecoders } from './registerWebImageDecoders';
|
|
|
6
6
|
// jsdom / node lack createImageBitmap + OffscreenCanvas + Blob; these minimal stand-ins let the
|
|
7
7
|
// canvas decoder run. getImageData always returns straight (non-premultiplied) RGBA, matching the browser.
|
|
8
8
|
const straightPixels = [200, 100, 50, 128];
|
|
9
|
+
let bitmapCloseMock: ReturnType<typeof vi.fn>;
|
|
10
|
+
let createImageBitmapMock: ReturnType<typeof vi.fn>;
|
|
11
|
+
|
|
12
|
+
class MockBlob {
|
|
13
|
+
constructor(readonly parts: readonly unknown[]) {}
|
|
14
|
+
}
|
|
9
15
|
|
|
10
16
|
class MockOffscreenCanvas {
|
|
11
17
|
constructor(
|
|
@@ -13,7 +19,8 @@ class MockOffscreenCanvas {
|
|
|
13
19
|
readonly height: number,
|
|
14
20
|
) {}
|
|
15
21
|
|
|
16
|
-
getContext(): unknown {
|
|
22
|
+
getContext(contextId: string): unknown {
|
|
23
|
+
if (contextId !== '2d') return null;
|
|
17
24
|
return {
|
|
18
25
|
drawImage: vi.fn(),
|
|
19
26
|
getImageData: () => ({ data: new Uint8ClampedArray(straightPixels) }),
|
|
@@ -22,17 +29,11 @@ class MockOffscreenCanvas {
|
|
|
22
29
|
}
|
|
23
30
|
|
|
24
31
|
beforeEach(() => {
|
|
25
|
-
vi.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
);
|
|
32
|
+
bitmapCloseMock = vi.fn();
|
|
33
|
+
createImageBitmapMock = vi.fn(async () => ({ width: 1, height: 1, close: bitmapCloseMock }));
|
|
34
|
+
vi.stubGlobal('createImageBitmap', createImageBitmapMock);
|
|
29
35
|
vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas);
|
|
30
|
-
vi.stubGlobal(
|
|
31
|
-
'Blob',
|
|
32
|
-
class {
|
|
33
|
-
constructor(readonly parts: readonly unknown[]) {}
|
|
34
|
-
},
|
|
35
|
-
);
|
|
36
|
+
vi.stubGlobal('Blob', MockBlob);
|
|
36
37
|
});
|
|
37
38
|
|
|
38
39
|
afterEach(() => {
|
|
@@ -55,6 +56,18 @@ describe('registerWebImageDecoders', () => {
|
|
|
55
56
|
expect(result.width).toBe(1);
|
|
56
57
|
expect(result.height).toBe(1);
|
|
57
58
|
expect(Array.from(result.data)).toEqual(straightPixels);
|
|
59
|
+
expect(bitmapCloseMock).toHaveBeenCalledOnce();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('forwards only the bytes inside the supplied view to createImageBitmap', async () => {
|
|
63
|
+
registerWebImageDecoders();
|
|
64
|
+
const decoder = getImageDecoder('image/png');
|
|
65
|
+
const backing = new Uint8Array([1, 2, 3, 4, 5, 6]);
|
|
66
|
+
|
|
67
|
+
await decoder!(backing.subarray(1, 5));
|
|
68
|
+
|
|
69
|
+
const blob = createImageBitmapMock.mock.calls[0][0] as MockBlob;
|
|
70
|
+
expect(Array.from(blob.parts[0] as Uint8Array)).toEqual([2, 3, 4, 5]);
|
|
58
71
|
});
|
|
59
72
|
|
|
60
73
|
it('premultiplies the straight result when premultiplyAlpha is requested', async () => {
|
|
@@ -16,7 +16,8 @@ class MockOffscreenCanvas {
|
|
|
16
16
|
readonly height: number,
|
|
17
17
|
) {}
|
|
18
18
|
|
|
19
|
-
getContext(): unknown {
|
|
19
|
+
getContext(contextId: string): unknown {
|
|
20
|
+
if (contextId !== '2d') return null;
|
|
20
21
|
return { putImageData: vi.fn() };
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -25,22 +26,25 @@ class MockOffscreenCanvas {
|
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
class MockImageData {
|
|
30
|
+
constructor(
|
|
31
|
+
readonly data: Uint8ClampedArray,
|
|
32
|
+
readonly width: number,
|
|
33
|
+
readonly height: number,
|
|
34
|
+
) {
|
|
35
|
+
if (data.length !== width * height * 4) {
|
|
36
|
+
throw new DOMException('ImageData data length must equal width * height * 4', 'IndexSizeError');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
function fakeImage(): DecodedImage {
|
|
29
42
|
return { data: new Uint8ClampedArray([10, 20, 30, 255]), width: 1, height: 1 };
|
|
30
43
|
}
|
|
31
44
|
|
|
32
45
|
beforeEach(() => {
|
|
33
46
|
vi.stubGlobal('OffscreenCanvas', MockOffscreenCanvas);
|
|
34
|
-
vi.stubGlobal(
|
|
35
|
-
'ImageData',
|
|
36
|
-
class {
|
|
37
|
-
constructor(
|
|
38
|
-
readonly data: Uint8ClampedArray,
|
|
39
|
-
readonly width: number,
|
|
40
|
-
readonly height: number,
|
|
41
|
-
) {}
|
|
42
|
-
},
|
|
43
|
-
);
|
|
47
|
+
vi.stubGlobal('ImageData', MockImageData);
|
|
44
48
|
});
|
|
45
49
|
|
|
46
50
|
afterEach(() => {
|