Skip to content

Commit

Permalink
fix(ext/web): properly handle Blob case for createImageBitmap (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hajime-san committed May 7, 2024
1 parent cbb78e1 commit e7a2317
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ext/canvas/01_image.js
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion ext/canvas/lib.rs
Expand Up @@ -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)?;

Expand Down
Binary file added tests/testdata/image/1x1-white.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/unit/image_bitmap_test.ts
Expand Up @@ -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]));
});

0 comments on commit e7a2317

Please sign in to comment.