0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-19 03:43:00 -05:00
denoland-deno/tests/unit/image_bitmap_test.ts

364 lines
13 KiB
TypeScript
Raw Normal View History

2024-01-22 12:08:01 +01:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "./test_util.ts";
2024-01-22 12:08:01 +01:00
const prefix = "tests/testdata/image";
2024-01-22 12:08:01 +01:00
function generateNumberedData(n: number): Uint8ClampedArray {
return new Uint8ClampedArray(
Array.from({ length: n }, (_, i) => [i + 1, 0, 0, 1]).flat(),
);
}
Deno.test(async function imageBitmapDirect() {
const data = generateNumberedData(3);
const imageData = new ImageData(data, 3, 1);
const imageBitmap = await createImageBitmap(imageData);
assertEquals(
// @ts-ignore: Deno[Deno.internal].core allowed
Deno[Deno.internal].getBitmapData(imageBitmap),
new Uint8Array(data.buffer),
);
});
Deno.test(async function imageBitmapCrop() {
const data = generateNumberedData(3 * 3);
const imageData = new ImageData(data, 3, 3);
const imageBitmap = await createImageBitmap(imageData, 1, 1, 1, 1);
assertEquals(
// @ts-ignore: Deno[Deno.internal].core allowed
Deno[Deno.internal].getBitmapData(imageBitmap),
new Uint8Array([5, 0, 0, 1]),
);
});
Deno.test(async function imageBitmapCropPartialNegative() {
const data = generateNumberedData(3 * 3);
const imageData = new ImageData(data, 3, 3);
const imageBitmap = await createImageBitmap(imageData, -1, -1, 2, 2);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 1
]));
});
Deno.test(async function imageBitmapCropGreater() {
const data = generateNumberedData(3 * 3);
const imageData = new ImageData(data, 3, 3);
const imageBitmap = await createImageBitmap(imageData, -1, -1, 5, 5);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 4, 0, 0, 1, 5, 0, 0, 1, 6, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 7, 0, 0, 1, 8, 0, 0, 1, 9, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]));
});
Deno.test(async function imageBitmapScale() {
const data = generateNumberedData(3);
const imageData = new ImageData(data, 3, 1);
const imageBitmap = await createImageBitmap(imageData, {
resizeHeight: 5,
resizeWidth: 5,
resizeQuality: "pixelated",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
1, 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1, 3, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1, 3, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1, 3, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1, 3, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1, 3, 0, 0, 1
]));
});
Deno.test(async function imageBitmapFlipY() {
const data = generateNumberedData(9);
const imageData = new ImageData(data, 3, 3);
const imageBitmap = await createImageBitmap(imageData, {
imageOrientation: "flipY",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
7, 0, 0, 1, 8, 0, 0, 1, 9, 0, 0, 1,
4, 0, 0, 1, 5, 0, 0, 1, 6, 0, 0, 1,
1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1,
]));
});
2024-09-05 18:47:37 +09:00
Deno.test("imageBitmapPremultiplyAlpha", async (t) => {
const imageData = new ImageData(
new Uint8ClampedArray([
255,
255,
0,
153,
]),
1,
1,
);
2024-09-05 18:47:37 +09:00
await t.step('"ImageData" premultiplyAlpha: "default"', async () => {
const imageBitmap = await createImageBitmap(imageData, {
premultiplyAlpha: "default",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
255, 255, 0, 153,
]));
2024-09-05 18:47:37 +09:00
});
await t.step('"ImageData" premultiplyAlpha: "premultiply"', async () => {
const imageBitmap = await createImageBitmap(imageData, {
premultiplyAlpha: "premultiply",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
153, 153, 0, 153
]));
2024-09-05 18:47:37 +09:00
});
await t.step('"ImageData" premultiplyAlpha: "none"', async () => {
const imageBitmap = await createImageBitmap(imageData, {
premultiplyAlpha: "none",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
255, 255, 0, 153,
]));
2024-09-05 18:47:37 +09:00
});
await t.step('"Blob" premultiplyAlpha: "none"', async () => {
const imageData = new Blob(
[await Deno.readFile(`${prefix}/2x2-transparent8.png`)],
{ type: "image/png" },
);
const imageBitmap = await createImageBitmap(imageData, {
premultiplyAlpha: "none",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([
255, 0, 0, 255, 0, 255, 0, 255,
0, 0, 255, 255, 255, 0, 0, 127
]));
2024-09-05 18:47:37 +09:00
});
});
2024-09-05 18:47:37 +09:00
Deno.test("imageBitmapFromBlob", async (t) => {
await t.step("8-bit png", async () => {
2024-08-21 12:30:20 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red8.png`)],
{ type: "image/png" },
);
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("16-bit png", async () => {
2024-08-22 11:45:05 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red16.png`)],
{ type: "image/png" },
);
2024-08-29 16:58:55 +09:00
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap),
// deno-fmt-ignore
2024-08-29 16:58:55 +09:00
new Uint8Array(
[
255, 255, // R
0, 0, // G
0, 0, // B
255, 255 // A
]
)
);
2024-09-05 18:47:37 +09:00
});
await t.step("8-bit jpeg", async () => {
2024-08-21 12:30:20 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red8.jpeg`)],
{ type: "image/jpeg" },
);
2024-08-21 05:47:41 +09:00
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([254, 0, 0]));
2024-09-05 18:47:37 +09:00
});
await t.step("8-bit bmp", async () => {
2024-08-21 12:30:20 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red8.bmp`)],
{ type: "image/bmp" },
);
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("8-bit gif", async () => {
2024-08-21 12:30:20 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red8.gif`)],
{ type: "image/gif" },
);
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("8-bit webp", async () => {
2024-08-21 12:30:20 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red8.webp`)],
{ type: "image/webp" },
);
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("8-bit ico", async () => {
2024-08-21 12:30:20 +09:00
const imageData = new Blob(
[await Deno.readFile(`${prefix}/1x1-red8.ico`)],
{ type: "image/x-icon" },
);
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("flotat-32-bit exr", async () => {
// image/x-exr is a known mimetype for OpenEXR
// https://www.digipres.org/formats/sources/fdd/formats/#fdd000583
2024-08-21 12:30:20 +09:00
const imageData = new Blob([
await Deno.readFile(`${prefix}/1x1-red32f.exr`),
], { type: "image/x-exr" });
await assertRejects(() => createImageBitmap(imageData), DOMException);
2024-09-05 18:47:37 +09:00
});
});
2024-08-29 22:18:44 +09:00
2024-09-05 18:47:37 +09:00
Deno.test("imageBitmapFromBlobAnimatedImage", async (t) => {
await t.step("animated png has a default image", async () => {
// the chunk of animated apng is below (2 frames, 1x1, 8-bit, RGBA), default [255, 0, 0, 255] image
2024-08-29 22:18:44 +09:00
// [ 0, 255, 0, 255,
// 0, 0, 255, 255 ]
const imageData = new Blob([
await Deno.readFile(`${prefix}/1x1-2f-animated-has-def.png`),
], { type: "image/png" });
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("animated png does not have any default image", async () => {
// the chunk of animated apng is below (3 frames, 1x1, 8-bit, RGBA)
2024-08-29 22:18:44 +09:00
// [ 255, 0, 0, 255,
// 0, 255, 0, 255,
// 0, 0, 255, 255 ]
const imageData = new Blob([
await Deno.readFile(`${prefix}/1x1-3f-animated-no-def.png`),
], { type: "image/png" });
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step("animated webp", async () => {
2024-08-30 10:10:17 +09:00
// the chunk of animated webp is below (3 frames, 1x1, 8-bit, RGBA)
//
2024-08-29 22:18:44 +09:00
// [ 255, 0, 0, 127,
// 0, 255, 0, 127,
// 0, 0, 255, 127 ]
const imageData = new Blob([
2024-08-30 10:10:17 +09:00
await Deno.readFile(
`${prefix}/1x1-3f-lossless-animated-semi-transparent.webp`,
),
2024-08-29 22:18:44 +09:00
], { type: "image/webp" });
2024-08-30 10:10:17 +09:00
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 127]));
2024-09-05 18:47:37 +09:00
});
await t.step("animated gif", async () => {
2024-08-30 14:00:05 +09:00
// the chunk of animated gif is below (3 frames, 1x1, 8-bit, RGBA)
// [ 255, 0, 0, 255,
// 0, 255, 0, 255,
// 0, 0, 255, 255 ]
const imageData = new Blob([
await Deno.readFile(`${prefix}/1x1-3f-animated.gif`),
], { type: "image/gif" });
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
2024-08-29 22:18:44 +09:00
});
Deno.test(async function imageBitmapImageDataColorspaceConversion() {
2024-09-05 18:47:37 +09:00
const imageData = new ImageData(
new Uint8ClampedArray([
255,
0,
0,
255,
]),
1,
1,
{
colorSpace: "display-p3",
},
);
const imageBitmap = await createImageBitmap(imageData);
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
assertEquals(Deno[Deno.internal].getBitmapData(imageBitmap), new Uint8Array([234, 51, 35, 255]));
});
/**
* extract high bytes from Uint16Array
*/
function extractHighBytes(array: Uint8Array): Uint8Array {
const highBytes = new Uint8Array(array.length / 2);
for (let i = 0, j = 1; i < array.length; i++, j += 2) {
highBytes[i] = array[j];
}
return highBytes;
}
2024-09-05 18:47:37 +09:00
Deno.test("imageBitmapFromBlobColorspaceConversion", async (t) => {
// reference:
// https://github.com/web-platform-tests/wpt/blob/d575dc75ede770df322fbc5da3112dcf81f192ec/html/canvas/element/manual/imagebitmap/createImageBitmap-colorSpaceConversion.html#L18
// https://wpt.fyi/results/html/canvas/element/manual/imagebitmap/createImageBitmap-colorSpaceConversion.html?label=experimental&label=master&aligned
2024-09-05 18:47:37 +09:00
await t.step('"Blob" colorSpaceConversion: "none"', async () => {
const imageData = new Blob([
await Deno.readFile(`${prefix}/wide-gamut-pattern.png`),
], { type: "image/png" });
const imageBitmap = await createImageBitmap(imageData, {
colorSpaceConversion: "none",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
2024-09-05 18:47:37 +09:00
const firstPixel = extractHighBytes(Deno[Deno.internal].getBitmapData(imageBitmap)).slice(0, 4);
// picking the high bytes of the first pixel
assertEquals(firstPixel, new Uint8Array([123, 0, 27, 255]));
2024-09-05 18:47:37 +09:00
});
await t.step('"Blob" colorSpaceConversion: "default"', async () => {
const imageData = new Blob([
await Deno.readFile(`${prefix}/wide-gamut-pattern.png`),
], { type: "image/png" });
const imageBitmap = await createImageBitmap(imageData, {
colorSpaceConversion: "default",
});
// @ts-ignore: Deno[Deno.internal].core allowed
// deno-fmt-ignore
2024-09-05 18:47:37 +09:00
const firstPixel = extractHighBytes(Deno[Deno.internal].getBitmapData(imageBitmap)).slice(0, 4);
// picking the high bytes of the first pixel
assertEquals(firstPixel, new Uint8Array([255, 0, 0, 255]));
2024-09-05 18:47:37 +09:00
});
});