Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #147: PDF: a damaged PNG, GIF or WebP is skipped, not fatal

aiongg8b5e1a909d3f
diff --cc crates/pdf/src/export.rs
index 51aaa721e,b004b7cd1..e10608085
--- a/crates/pdf/src/export.rs
+++ b/crates/pdf/src/export.rs
@@@ -1298,77 -1276,19 +1298,80 @@@ fn relabel_pdf_header(data: &[u8], from
Some(out)
}
-fn lossless(data: &Arc<Vec<u8>>, fmt: Option<image::ImageFormat>) -> Option<Image> {
+/// The sRGB profile (ICC v2.1) that untagged RGB images carry in PDF/X-4.
+pub(crate) static SRGB_ICC: &[u8] = include_bytes!("../../../assets/icc/sRGB-v2-magic.icc");
+
+/// How raster images are written for the export's standard.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+struct RasterMode {
+ /// `/Interpolate true`: smoother upscaling in viewers. PDF/X-4 and PDF/A don't allow it.
+ interpolate: bool,
+ /// Give RGB images with no ICC profile of their own the sRGB profile: under PDF/X-4's CMYK
+ /// output intent, RGB must be colour-managed.
+ tag_rgb: bool,
+}
+
+impl RasterMode {
+ fn for_standard(standard: Standard) -> Self {
+ Self { interpolate: standard == Standard::None, tag_rgb: standard == Standard::PdfX4 }
+ }
+}
+
+/// The ICC profile embedded in an image file, if it has one.
+fn embedded_icc(data: &[u8]) -> Option<Vec<u8>> {
+ use image::ImageDecoder;
+ let mut dec = image::ImageReader::new(std::io::Cursor::new(data)).with_guessed_format().ok()?.into_decoder().ok()?;
+ dec.icc_profile().ok().flatten()
+}
+
+/// Whether an ICC profile describes RGB (its colour space signature, header bytes 16 to 20).
+fn is_rgb_icc(icc: &[u8]) -> bool {
+ icc.get(16..20) == Some(b"RGB ".as_slice())
+}
+
+/// A JPEG, passed through. With `tag_rgb`, a JPEG keeps its own profile, and one without gets
+/// sRGB. krilla uses a profile only when its channels match the JPEG's, so a grey or CMYK JPEG
+/// without a profile stays DeviceGray or DeviceCMYK.
+fn jpeg(data: krilla::Data, icc: impl FnOnce() -> Option<Vec<u8>>, mode: RasterMode) -> Option<Image> {
+ if !mode.tag_rgb {
+ return Image::from_jpeg(data, mode.interpolate).ok();
+ }
+ let icc: krilla::Data = match icc() {
+ Some(own) => own.into(),
+ None => SRGB_ICC.into(),
+ };
+ Image::from_jpeg_with_icc(data, Some(icc), mode.interpolate).ok()
+}
+
+/// Whether krilla would write this PNG, GIF or WebP as untagged RGB: not grey, and no profile of
+/// its own.
+fn untagged_rgb(data: &[u8]) -> bool {
+ use image::ImageDecoder;
+ let Some(mut dec) = image::ImageReader::new(std::io::Cursor::new(data)).with_guessed_format().ok().and_then(|r| r.into_decoder().ok()) else {
+ return false;
+ };
+ let grey = matches!(dec.color_type(), image::ColorType::L8 | image::ColorType::La8 | image::ColorType::L16 | image::ColorType::La16);
+ !grey && dec.icc_profile().ok().flatten().is_none()
+}
+
+fn lossless(data: &Arc<Vec<u8>>, fmt: Option<image::ImageFormat>, mode: RasterMode) -> Option<Image> {
+ // krilla reads PNG, GIF and WebP lazily: a damaged file is accepted here and fails the whole
+ // export when the PDF is written. Decoding it first means it is skipped, like any other
+ // image that can't be decoded.
+ let rgba = designcraft_images::decode_rgba(data)?;
+ let i = mode.interpolate;
let direct = match fmt {
- Some(image::ImageFormat::Png) => Image::from_png(data.clone().into(), true).ok(),
- Some(image::ImageFormat::Gif) => Image::from_gif(data.clone().into(), true).ok(),
- Some(image::ImageFormat::WebP) => Image::from_webp(data.clone().into(), true).ok(),
+ Some(image::ImageFormat::Png | image::ImageFormat::Gif | image::ImageFormat::WebP) if mode.tag_rgb && untagged_rgb(data) => None,
+ Some(image::ImageFormat::Png) => Image::from_png(data.clone().into(), i).ok(),
+ Some(image::ImageFormat::Gif) => Image::from_gif(data.clone().into(), i).ok(),
+ Some(image::ImageFormat::WebP) => Image::from_webp(data.clone().into(), i).ok(),
_ => None,
};
direct.or_else(|| {
- // TIFF, BMP, PSD composites…
+ // TIFF, BMP, PSD composites… and, with `tag_rgb`, untagged RGB.
- let rgba = designcraft_images::decode_rgba(data)?;
+ if mode.tag_rgb {
+ return SrgbImage::new(rgba).and_then(|img| Image::from_custom(img, i).ok());
+ }
let (w, h) = rgba.dimensions();
Some(Image::from_rgba8(rgba.into_raw(), w, h))
})