Skip to content

Commit 99babcf

Browse files
authored
fix(ext/image): handle bitmaps in createImageBitmap correctly (#34285)
Closes #34271
1 parent 0b6cd36 commit 99babcf

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

ext/image/image_ops.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ pub(crate) fn to_srgb_from_icc_profile(
496496
}
497497

498498
/// Create an image buffer from raw bytes.
499+
///
500+
/// The `width` and `height` are expected to be non-zero.
499501
fn process_image_buffer_from_raw_bytes<P, S>(
500502
width: u32,
501503
height: u32,
@@ -510,7 +512,7 @@ where
510512
for (index, buffer) in buffer.chunks_exact(bytes_per_pixel).enumerate() {
511513
let pixel = P::slice_to_pixel(buffer);
512514

513-
out.put_pixel(index as u32, index as u32, pixel);
515+
out.put_pixel((index as u32) % width, (index as u32) / width, pixel);
514516
}
515517

516518
out
@@ -633,4 +635,22 @@ mod tests {
633635
.to_rgba16();
634636
assert_eq!(image.get_pixel(0, 0), &Rgba::<u16>([65535, 0, 0, 65535]));
635637
}
638+
639+
#[test]
640+
fn test_process_wide_image_buffer_from_raw_bytes() {
641+
let buffer = &[
642+
255, 255, 255, 128, 128, 128, 255, 255, 255, 128, 128, 128, 255, 255,
643+
255, 128, 128, 128,
644+
];
645+
let color = ColorType::Rgb8;
646+
let bytes_per_pixel = color.bytes_per_pixel() as usize;
647+
let image = DynamicImage::ImageRgb8(process_image_buffer_from_raw_bytes(
648+
3,
649+
2,
650+
buffer,
651+
bytes_per_pixel,
652+
))
653+
.to_rgb8();
654+
assert_eq!(image.get_pixel(2, 1), &Rgb::<u8>([128, 128, 128]));
655+
}
636656
}

tests/unit/image_bitmap_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Deno.test(async function imageBitmapDirect() {
2323

2424
Deno.test(async function imageBitmapRecivesImageBitmap() {
2525
const imageData = new Blob(
26-
[await Deno.readFile(`${prefix}/1x1-red16.png`)],
26+
[await Deno.readFile(`${prefix}/squares_6.jpg`)],
2727
);
2828
const imageBitmap1 = await createImageBitmap(imageData);
2929
const imageBitmap2 = await createImageBitmap(imageBitmap1);

0 commit comments

Comments
 (0)