summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHajime-san <41257923+Hajime-san@users.noreply.github.com>2024-05-07 20:47:42 +0900
committerGitHub <noreply@github.com>2024-05-07 04:47:42 -0700
commite7a2317f5a18751ecc1a63b3464690b125839ecf (patch)
tree505bb7762e5b77ca43c71804e9af5503c1ba6edb
parentcbb78e138ffbfc92ca64f2cce56dd5c629e4f142 (diff)
fix(ext/web): properly handle `Blob` case for `createImageBitmap` (#23518)
fixes https://github.com/denoland/deno/issues/22649
-rw-r--r--ext/canvas/01_image.js4
-rw-r--r--ext/canvas/lib.rs3
-rw-r--r--tests/testdata/image/1x1-white.pngbin0 -> 109 bytes
-rw-r--r--tests/unit/image_bitmap_test.ts11
4 files changed, 16 insertions, 2 deletions
diff --git a/ext/canvas/01_image.js b/ext/canvas/01_image.js
index a738f7992..6fb1ee62f 100644
--- a/ext/canvas/01_image.js
+++ b/ext/canvas/01_image.js
@@ -238,7 +238,9 @@ function createImageBitmap(
"InvalidStateError",
);
}
- const { data: imageData, width, height } = op_image_decode_png(data);
+ const { data: imageData, width, height } = op_image_decode_png(
+ new Uint8Array(data),
+ );
const processedImage = processImage(
imageData,
width,
diff --git a/ext/canvas/lib.rs b/ext/canvas/lib.rs
index b05332c3f..72173f133 100644
--- a/ext/canvas/lib.rs
+++ b/ext/canvas/lib.rs
@@ -130,7 +130,8 @@ fn op_image_decode_png(#[buffer] buf: &[u8]) -> Result<DecodedPng, AnyError> {
)));
}
- let mut png_data = Vec::with_capacity(png.total_bytes() as usize);
+ // read_image will assert that the buffer is the correct size, so we need to fill it with zeros
+ let mut png_data = vec![0_u8; png.total_bytes() as usize];
png.read_image(&mut png_data)?;
diff --git a/tests/testdata/image/1x1-white.png b/tests/testdata/image/1x1-white.png
new file mode 100644
index 000000000..dd43faec5
--- /dev/null
+++ b/tests/testdata/image/1x1-white.png
Binary files differ
diff --git a/tests/unit/image_bitmap_test.ts b/tests/unit/image_bitmap_test.ts
index 364f2a167..006631182 100644
--- a/tests/unit/image_bitmap_test.ts
+++ b/tests/unit/image_bitmap_test.ts
@@ -90,3 +90,14 @@ Deno.test(async function imageBitmapFlipY() {
1, 0, 0, 1, 2, 0, 0, 1, 3, 0, 0, 1,
]));
});
+
+Deno.test(async function imageBitmapFromBlob() {
+ const path = "tests/testdata/image/1x1-white.png";
+ const imageData = new Blob([await Deno.readFile(path)], {
+ 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,255,255,255]));
+});