Skip to content

Commit

Permalink
Add support for embedded luma JPEG images (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV authored Nov 28, 2024
1 parent a53573b commit c019cec
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
20 changes: 19 additions & 1 deletion crates/resvg/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ mod raster_images {

let options = DecoderOptions::default().jpeg_set_out_colorspace(ColorSpace::RGBA);
let mut decoder = zune_jpeg::JpegDecoder::new_with_options(data, options);
let img_data = decoder.decode().ok()?;
decoder.decode_headers().ok()?;
let output_cs = decoder.get_output_colorspace()?;

let img_data = {
let data = decoder.decode().ok()?;
match output_cs {
ColorSpace::RGBA => data,
// `set_output_color_space` is not guaranteed to actually always set the output space
// to RGBA (its docs say "we do not guarantee the decoder can convert to all colorspaces").
// In particular, it seems like it doesn't work for luma JPEGs,
// so we convert them manually.
ColorSpace::Luma => data
.into_iter()
.flat_map(|p| [p, p, p, 255])
.collect::<Vec<_>>(),
_ => return None,
}
};

let info = decoder.info()?;

let size = tiny_skia::IntSize::from_wh(info.width as u32, info.height as u32)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/resvg/tests/integration/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,9 @@ use crate::render;
#[test] fn structure_image_embedded_gif() { assert_eq!(render("tests/structure/image/embedded-gif"), 0); }
#[test] fn structure_image_embedded_jpeg_as_image_jpeg() { assert_eq!(render("tests/structure/image/embedded-jpeg-as-image-jpeg"), 0); }
#[test] fn structure_image_embedded_jpeg_as_image_jpg() { assert_eq!(render("tests/structure/image/embedded-jpeg-as-image-jpg"), 0); }
#[test] fn structure_image_embedded_jpeg_luma() { assert_eq!(render("tests/structure/image/embedded-jpeg-luma"), 0); }
#[test] fn structure_image_embedded_jpeg_without_mime() { assert_eq!(render("tests/structure/image/embedded-jpeg-without-mime"), 0); }
#[test] fn structure_image_embedded_png_luma() { assert_eq!(render("tests/structure/image/embedded-png-luma"), 0); }
#[test] fn structure_image_embedded_png() { assert_eq!(render("tests/structure/image/embedded-png"), 0); }
#[test] fn structure_image_embedded_svg_with_text() { assert_eq!(render("tests/structure/image/embedded-svg-with-text"), 0); }
#[test] fn structure_image_embedded_svg_without_mime() { assert_eq!(render("tests/structure/image/embedded-svg-without-mime"), 0); }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c019cec

Please sign in to comment.