diff --git a/Cargo.toml b/Cargo.toml index dc459aa750..0070e01fc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,6 @@ craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d364177661b0ae17f48a0805e83a5f32" } craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d364177661b0ae17f48a0805e83a5f32" } craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d364177661b0ae17f48a0805e83a5f32" } -craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d364177661b0ae17f48a0805e83a5f32" } craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "e98299546f5c5c3c22388d16507f8b5e61a5f280", features = ["egui"] } # internal vectorcraft-geom = { path = "crates/geom" } @@ -122,6 +121,16 @@ ruzstd = { version = "0.9.1", default-features = false, features = ["std"] } # craft-stream-codecs' Group 3 rows with (vectorcraft-doc's and vectorcraft-engine's dev-dependencies). tiff = "0.11" zune-jpeg = "0.5" +# JPEG XL decoder (pure Rust, MIT OR Apache-2.0): File › Open and Place read `.jxl` images. It is +# built without its default `rayon` feature and decodes on one thread. +jxl-oxide = { version = "0.12.6", default-features = false } +# jxl-oxide's color conversions, run on colors after their alpha is divided out. +jxl-color = "0.11.0" +# jxl-oxide's buffer allocation error, told apart from other decoding errors. +jxl-grid = "0.6.2" +# Tests: a lossless JPEG XL encoder (pure Rust, MIT OR Apache-2.0 OR Zlib) for the `.jxl` test files. +zune-jpegxl = { version = "0.5.2", default-features = false, features = ["std"] } +zune-core = "0.5" hayro-ccitt = "0.3" jpeg-encoder = { version = "0.6", default-features = false, features = ["std"] } # Substring search for the EPS reader's PostScript `search`. diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 988798e881..cd91a0bfbb 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -16,7 +16,7 @@ bundled-fonts = ["vectorcraft-text/bundled-fonts"] # HEIC and HEIF images open and place, decoded by heic-rs (an HEVC decoder). A build without this # feature still recognizes the files and refuses them with a message. The apps list it beside # `bundled-fonts`. -heif = ["dep:craft-heif"] +heif = ["dep:heic-rs", "dep:rayon"] [dependencies] # Which paths stored in documents may be opened. @@ -45,8 +45,11 @@ vectorcraft-js = { workspace = true } vectorcraft-apischema = { workspace = true } image = { workspace = true } craft-image-codecs = { workspace = true } -craft-jxl = { workspace = true } -craft-heif = { workspace = true, optional = true } +jxl-oxide = { workspace = true } +jxl-color = { workspace = true } +jxl-grid = { workspace = true } +# Pure Rust, no unsafe; pinned at the version photocraft-heif uses. +heic-rs = { version = "=0.1.4", default-features = false, features = ["std"], optional = true } flate2 = { workspace = true } chrono = { workspace = true } # The XML parts of Word documents placed as type (place/docx.rs). @@ -69,7 +72,17 @@ rustix = { workspace = true, features = ["std", "process"] } [target.'cfg(windows)'.dependencies] same-file = "1.0.6" +# The tiles of a HEIC photo decode in parallel on native; wasm stays single-threaded. +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +heic-rs = { version = "=0.1.4", default-features = false, features = ["std", "parallel"], optional = true } +rayon = { version = "1.12", optional = true } + [dev-dependencies] +# JPEG XL files for the `.jxl` open and place tests. +zune-jpegxl = { workspace = true } +zune-core = { workspace = true } +# JPEG XL files for the import fuzz tests (tests/import_fuzz.rs). +craft-jxl = { workspace = true } # vectorcraft-doc's Group 3 TIFF reader before craft-stream-codecs, the reference # tests/group3_reference.rs compares it with. hayro-ccitt = { workspace = true } diff --git a/crates/engine/src/cmd/addons/image.rs b/crates/engine/src/cmd/addons/image.rs index ca6420bf20..5ef567e9b3 100644 --- a/crates/engine/src/cmd/addons/image.rs +++ b/crates/engine/src/cmd/addons/image.rs @@ -448,14 +448,46 @@ pub(crate) fn clear(blob: &ImageBlob, settings: &Settings, mut kept: Option<&mut Ok(Some(Cleared { blob: ImageBlob::png(png), width: w, height: h, cleared })) } +/// The image's size and the bytes a pixel of it takes to change, read from its header: its +/// samples as decoded (bitmaps as 8-bit gray, palettes as RGB, or RGBA with a transparent entry), +/// those worked on and kept for the PNG, and the work. A header that gives only the size (one +/// whose file can't hold its pixels) counts the fewest: one 8-bit sample. `None` when the header +/// doesn't say. +fn header_pixels(bytes: &[u8]) -> Option<(u32, u32, u64)> { + use craft_image_codecs::RasterColorType as T; + let Some(i) = craft_image_codecs::inspect_raster(bytes).ok().flatten() else { + return vectorcraft_doc::header_size(bytes).map(|(w, h)| (w, h, 1 + 2 * 4 + WORK_BYTES)); + }; + let deep = i.bits_per_sample > 8 && i.color_type != T::Indexed; + let channels = match i.color_type { + T::Indexed => 3 + u64::from(i.transparent_index.is_some()), + T::Bitmap => 1, + _ => u64::from(i.channels.max(1)), + }; + let samples = channels * if deep { 2 } else { 1 }; + Some((i.width, i.height, samples + 2 * if deep { 8 } else { 4 } + WORK_BYTES)) +} + /// `blob`'s pixels as straight RGBA, whether they were gray, and its ICC profile. An image that /// can't be decoded or is too large to change is an error. The pixels are kept in `kept`, when it /// is given, if a copy of them fits the decode allowance beside the work. Errors are command `cmd`'s. fn decode(blob: &ImageBlob, kept: Option<&mut DecodedImages>, cmd: &str) -> Result<(Arc, bool, Option>)> { + // The file's pixels, those worked on (as many again for the PNG made of them) and the work, + // checked from the header before anything is decoded. + let too_large = |w: u32, h: u32, per_pixel: u64| { + u64::from(w).checked_mul(u64::from(h)).and_then(|n| n.checked_mul(per_pixel)).is_none_or(|n| n > MAX_RASTER_ALLOC) + }; + let header = header_pixels(&blob.bytes); + if let Some((w, h, per_pixel)) = header + && too_large(w, h, per_pixel) + { + return Err(bad(cmd, format!("an image of {w} × {h} pixels is too large to change"))); + } let mut opts = craft_image_codecs::DecodeOptions::default(); opts.limits.max_alloc = MAX_RASTER_ALLOC; let img = craft_image_codecs::decode_with(&blob.bytes, &opts).map_err(|e| match e { - craft_image_codecs::CodecError::LimitExceeded(_) => bad(cmd, "the image is too large to change"), + // Within the allowance from its header: a file that can't hold the pixels it declares. + craft_image_codecs::CodecError::LimitExceeded(_) if header.is_none() => bad(cmd, "the image is too large to change"), _ => bad(cmd, format!("the image's pixels can't be read: {e}")), })?; let (w, h) = img.dimensions(); @@ -463,7 +495,7 @@ fn decode(blob: &ImageBlob, kept: Option<&mut DecodedImages>, cmd: &str) -> Resu let deep = img.sample_type() == craft_image_codecs::SampleType::U16; let gray = matches!(img.layout(), craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA); let per_pixel = u64::from(img.layout().channels() as u32 * img.sample_type().bytes() as u32) + 2 * if deep { 8 } else { 4 } + WORK_BYTES; - if u64::from(w).checked_mul(u64::from(h)).and_then(|n| n.checked_mul(per_pixel)).is_none_or(|n| n > MAX_RASTER_ALLOC) { + if too_large(w, h, per_pixel) { return Err(bad(cmd, format!("an image of {w} × {h} pixels is too large to change"))); } #[cfg(test)] diff --git a/crates/engine/src/cmd/addons/image/tests.rs b/crates/engine/src/cmd/addons/image/tests.rs index e5f50c5fda..30673c7677 100644 --- a/crates/engine/src/cmd/addons/image/tests.rs +++ b/crates/engine/src/cmd/addons/image/tests.rs @@ -99,6 +99,15 @@ fn pixels(s: &Session, id: NodeId) -> DynamicImage { (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => { image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) } + (layout, craft_image_codecs::SampleType::U16) => { + let v = __cimg.to_u16_samples().unwrap(); + match layout { + craft_image_codecs::ChannelLayout::Gray => image::DynamicImage::ImageLuma16(ImageBuffer::from_raw(__w, __h, v).unwrap()), + craft_image_codecs::ChannelLayout::GrayA => image::DynamicImage::ImageLumaA16(ImageBuffer::from_raw(__w, __h, v).unwrap()), + craft_image_codecs::ChannelLayout::Rgb => image::DynamicImage::ImageRgb16(ImageBuffer::from_raw(__w, __h, v).unwrap()), + _ => image::DynamicImage::ImageRgba16(ImageBuffer::from_raw(__w, __h, v).unwrap()), + } + } _ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()), } }) diff --git a/crates/engine/src/cmd/fileio/heifread.rs b/crates/engine/src/cmd/fileio/heifread.rs index df1a2ec24a..4867022ded 100644 --- a/crates/engine/src/cmd/fileio/heifread.rs +++ b/crates/engine/src/cmd/fileio/heifread.rs @@ -1,18 +1,20 @@ -//! HEIC and HEIF images for File › Open and File › Place, decoded with `craft-heif` when the -//! engine's `heif` feature is on (the default). The image is bounded before decoding and converted -//! to RGBA for the common PNG storage path. A build without the feature still recognizes the files -//! and refuses them with a message. +//! HEIC and HEIF images for File › Open and File › Place, decoded with heic-rs when the engine's +//! `heif` feature is on (the default). The image is bounded before decoding and converted to RGBA +//! for the common PNG storage path. A build without the feature still recognizes the files and +//! refuses them with a message. +#[cfg(all(test, feature = "heif"))] +pub(super) use decoder::{active_sps, picture_bytes, sps_signal, sps_size}; #[cfg(feature = "heif")] pub(super) use decoder::{decode, quarter_turned, with_exif}; -/// The brands `craft-heif` reads: HEVC-coded images and image sequences (ISO/IEC 23008-12 annex +/// The brands heic-rs 0.1.4 reads: HEVC-coded images and image sequences (ISO/IEC 23008-12 annex /// B) and the general image and sequence brands. const BRANDS: [&[u8; 4]; 7] = [b"heic", b"heix", b"heim", b"heis", b"hevc", b"mif1", b"msf1"]; /// Recognize a HEIF file by its `ftyp` box: one of [`BRANDS`] as the major brand or a compatible -/// one, and no AVIF brand (`avif`, `avis`). The box is read as JP2 boxes are -/// ([`super::jp2read::boxes`]): ISO BMFF sizes, the 64-bit one included. +/// one, and no AVIF brand (`avif`, `avis`), as heic-rs refuses a file that lists one. The box is +/// read as JP2 boxes are ([`super::jp2read::boxes`]): ISO BMFF sizes, the 64-bit one included. pub(super) fn is_heif(bytes: &[u8]) -> bool { let Some((b"ftyp", ftyp)) = super::jp2read::boxes(bytes).next() else { return false }; let Some(major) = ftyp.get(..4).and_then(|b| <&[u8; 4]>::try_from(b).ok()) else { return false }; @@ -44,72 +46,582 @@ pub(super) fn with_exif(_bytes: &[u8], _read: impl FnOnce(&[u8]) -> Option #[cfg(feature = "heif")] mod decoder { - use craft_heif::{Colour, Error, Options}; + use heic_rs::context::Context; + use heic_rs::{DecodeOptions, Error, MatrixCoefficients, Nclx, PixelLayout, Range}; /// The message for a file that isn't a readable HEIF image. const UNREADABLE: &str = "the HEIC or HEIF file can't be read"; - /// The message for an image kind that `craft-heif` does not handle. + /// The message for an image sequence, an overlay, an image that isn't HEVC-coded, a grid whose + /// tiles are cropped, turned or mirrored, or an HEVC coding tool the decoder doesn't implement. const UNSUPPORTED: &str = "the HEIC or HEIF file holds a kind of image that can't be read"; - /// Decode the primary image within `max_alloc` bytes, with the container's rotation, mirror, + /// Decode the primary image within `max_alloc` bytes, counting the decoder's planes, the RGBA + /// output and the copy that rotating or cropping makes, with the container's rotation, mirror /// and crop applied. An embedded RGB ICC profile, or `nclx` Display P3 primaries with the sRGB - /// curve, is converted to sRGB; other `nclx` values are read as sRGB. + /// curve, is converted to sRGB; other `nclx` values are read as sRGB. A panic inside the + /// decoder is reported as an unreadable file. pub(in super::super) fn decode(bytes: &[u8], max_alloc: u64) -> Result { - let refused = |e: Error| match e { - Error::Unsupported(_) => UNSUPPORTED.to_string(), - _ => UNREADABLE.to_string(), - }; - let info = craft_heif::probe(bytes).map_err(refused)?; - let too_large = || format!("an image of {} × {} pixels is too large to draw", info.coded_width, info.coded_height); - let coded = u64::from(info.coded_width).checked_mul(u64::from(info.coded_height)).ok_or_else(too_large)?; - let shown = u64::from(info.width).checked_mul(u64::from(info.height)).ok_or_else(too_large)?; - if coded == 0 || shown == 0 || coded.checked_mul(17).is_none_or(|n| n > max_alloc) { - return Err(too_large()); - } - let decoded = craft_heif::decode(bytes, &Options { max_pixels: max_alloc / 17, ..Options::default() }).map_err(|e| match e { - Error::Limit(_) => too_large(), - e => refused(e), - })?; - let channels = if decoded.has_alpha { 4usize } else { 3usize }; - let profile = source_profile(decoded.icc.as_deref(), decoded.colour); - let to_u8 = |s: u16| ((u32::from(s) * 255 + 32767) / 65535) as u8; - let mut rgba = Vec::with_capacity(decoded.width as usize * decoded.height as usize * 4); - for px in decoded.data.chunks_exact(channels) { - let a = if decoded.has_alpha { to_u8(px[3]) } else { 255 }; - let [r, g, b] = if let Some(profile) = profile.as_ref() { - let device = [px[0], px[1], px[2]].map(|v| f32::from(v) / 65535.0); - profile - .to_srgb_into(&device, vectorcraft_color::cms::Intent::RelativeColorimetric) - .map(|rgb| rgb.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)) - .unwrap_or_else(|| [to_u8(px[0]), to_u8(px[1]), to_u8(px[2])]) - } else { - [to_u8(px[0]), to_u8(px[1]), to_u8(px[2])] - }; - rgba.extend_from_slice(&[r, g, b, a]); - } - image::RgbaImage::from_raw(decoded.width, decoded.height, rgba).ok_or_else(|| UNREADABLE.into()) + crate::guard::catch_panic(|| decode_unguarded(bytes, max_alloc)).unwrap_or_else(|_| Err(UNREADABLE.into())) } - /// Whether the container's transforms swap width and height. + /// Whether the container's rotations of the primary image add up to an odd number of quarter + /// turns, so its width and height swap. The decoder applies every `irot` the item lists. pub(in super::super) fn quarter_turned(bytes: &[u8]) -> bool { - craft_heif::probe(bytes).ok().is_some_and(|i| (i.width > i.height) != (i.coded_width > i.coded_height)) + let turned = || { + let ctx = Context::open(bytes).ok()?; + let props = ctx.props(ctx.meta.primary).ok()?; + let quarters = props.transforms.iter().filter(|t| matches!(t, heic_rs::props::Transform::Rotate(r) if r.swaps_axes())).count(); + Some(quarters % 2 == 1) + }; + crate::guard::catch_panic(turned).ok().flatten().unwrap_or(false) } /// `read` of the primary image's EXIF block, a TIFF structure inside `bytes`, for its /// resolution tags. pub(in super::super) fn with_exif(bytes: &[u8], read: impl FnOnce(&[u8]) -> Option) -> Option { - let (exif, _) = craft_heif::read_metadata(bytes).ok()?; - exif.as_deref().and_then(read) + let find = || { + let ctx = Context::open(bytes).ok()?; + ctx.exif(ctx.meta.primary).ok().flatten() + }; + crate::guard::catch_panic(find).ok().flatten().and_then(read) } + fn decode_unguarded(bytes: &[u8], max_alloc: u64) -> Result { + let refused = |e: Error| match e { + Error::Unsupported(_) | Error::MissingBox("hvcC" | "meta") => UNSUPPORTED.to_string(), + _ => UNREADABLE.to_string(), + }; + let info = heic_rs::probe(bytes).map_err(refused)?; + let ctx = Context::open(bytes).map_err(refused)?; + if unsupported_items(&ctx).ok_or(UNREADABLE)? { + return Err(UNSUPPORTED.into()); + } + // The stored size, before a crop: the size the decoder holds. + let too_large = || format!("an image of {} × {} pixels is too large to draw", info.coded_width, info.coded_height); + let declared = u64::from(info.coded_width).checked_mul(u64::from(info.coded_height)).ok_or_else(too_large)?; + // A grid is composed at the size its own data gives, which need not match `ispe`. + let grid = grid_pixels(&ctx).ok_or(UNREADABLE)?; + // A single picture is converted at the size its stream gives, which need not match `ispe` + // either. + let coded = declared.max(grid).max(stream_pixels(&ctx).ok_or(UNREADABLE)?); + let shown = u64::from(info.width).checked_mul(u64::from(info.height)).ok_or_else(too_large)?; + // The RGBA output, and a second RGBA image when the decoder rotates, mirrors or crops it, + // which is no larger than the first. + let output = coded.checked_mul(8).ok_or_else(too_large)?; + let pictures = picture_bytes_in(&ctx).ok_or(UNREADABLE)?; + // An alpha grid is composed into a frame of 16-bit planes (at most three samples a pixel), + // and so is the image's grid when it is decoded with a color description of its tiles or + // its stream. A single picture is converted in place. + let composed = if grid > 0 { coded.checked_mul(if info.has_alpha { 12 } else { 6 }).ok_or_else(too_large)? } else { 0 }; + if coded == 0 || shown == 0 || output.checked_add(pictures).and_then(|n| n.checked_add(composed)).is_none_or(|n| n > max_alloc) { + return Err(too_large()); + } + let max_pixels = max_alloc / 4; + let decoded = match tile_nclx(&ctx).or_else(|| stream_nclx(&ctx)) { + Some(nclx) => decode_with_nclx(&ctx, nclx, max_pixels), + None => heic_rs::decode(bytes, &DecodeOptions::default().with_layout(PixelLayout::Rgba8).with_max_pixels(Some(max_pixels))), + } + .map_err(|e| match e { + Error::PixelLimit { .. } | Error::BoxTooLarge { .. } => too_large(), + e => refused(e), + })?; + let mut img = image::RgbaImage::from_raw(decoded.width, decoded.height, decoded.data).ok_or(UNREADABLE)?; + if let Some(profile) = source_profile(&ctx) { + for pixel in img.pixels_mut() { + let [r, g, b, a] = pixel.0; + let device = [r, g, b].map(|v| f32::from(v) / 255.0); + if let Some(rgb) = profile.to_srgb_into(&device, vectorcraft_color::cms::Intent::RelativeColorimetric) { + let [r, g, b] = rgb.map(|v| (v * 255.0).round() as u8); + pixel.0 = [r, g, b, a]; + } + } + } + Ok(img) + } + + /// The primary image and its alpha image, when it has one. + fn primary_and_alpha(ctx: &Context<'_>) -> Option> { + let primary = ctx.meta.primary; + Some(std::iter::once(primary).chain(ctx.alpha_item(primary).ok()?)) + } + + /// Whether the primary image or its alpha image is neither an HEVC-coded item (`hvc1`, `hev1`) + /// nor a grid of them, or is a grid with a tile that carries a crop, rotation or mirror. heic-rs + /// 0.1.4 decodes any item with an `hvcC` whatever its type, and composes a grid from its tiles + /// as coded, without their transforms: an encoder that pads tiles smaller than its coding block + /// (64 × 64 for x265) and crops them back with `clap` gets the wrong pixels. + fn unsupported_items(ctx: &Context<'_>) -> Option { + let hevc = |id: u32| ctx.meta.item(id).is_some_and(|info| info.is_hevc()); + for item in primary_and_alpha(ctx)? { + let Some((_, tiles)) = ctx.grid(item).ok()? else { + if !hevc(item) { + return Some(true); + } + continue; + }; + for tile in tiles { + if !hevc(tile) || !ctx.props(tile).ok()?.transforms.is_empty() { + return Some(true); + } + } + } + Some(false) + } + + /// The pixels of the larger of the primary image's grid and its alpha image's grid, at the + /// output size each grid gives; 0 when neither is a grid. + fn grid_pixels(ctx: &Context<'_>) -> Option { + let mut largest = 0u64; + for item in primary_and_alpha(ctx)? { + if let Some((grid, _)) = ctx.grid(item).ok()? { + largest = largest.max(u64::from(grid.output_width) * u64::from(grid.output_height)); + } + } + Some(largest) + } + + /// The pixels of the larger of the primary image and its alpha image when either is a single + /// picture, at the size of its largest sequence parameter set (before its conformance window); + /// 0 when both are grids. `None` when a size can't be read. + fn stream_pixels(ctx: &Context<'_>) -> Option { + let mut largest = 0u64; + for item in primary_and_alpha(ctx)? { + if ctx.grid(item).ok()?.is_some() { + continue; + } + // An item without `hvcC` isn't decoded: heic-rs refuses the file. + let Some(hvcc) = ctx.props(item).ok()?.hvcc else { continue }; + for nal in hvcc.parameter_sets().into_iter().filter(|nal| nal_type(nal) == Some(SPS)) { + let (width, height, _) = sps_size(nal)?; + largest = largest.max(width.checked_mul(height)?); + } + } + Some(largest) + } + + /// The first tile's `nclx` when the primary image is a grid without an `nclx` of its own and + /// its tiles carry one. heic-rs 0.1.4 converts a grid's samples to RGB with the grid item's + /// `nclx` alone, and with its BT.709 limited-range default when the grid has none + /// (tbraun96/heic-rs#6). + fn tile_nclx(ctx: &Context<'_>) -> Option { + let primary = ctx.meta.primary; + if ctx.props(primary).ok()?.nclx.is_some() { + return None; + } + let (_, tiles) = ctx.grid(primary).ok()??; + ctx.props(*tiles.first()?).ok()?.nclx + } + + /// The color description in the HEVC stream's video usability information (ITU-T H.265 + /// E.2.1) of the primary image, or of its grid's first tile, when neither carries an `nclx` + /// (a file whose only color box is an ICC profile, or one without a color box). heic-rs 0.1.4 + /// reads only the container's `nclx` and otherwise converts with BT.709 limited range. + fn stream_nclx(ctx: &Context<'_>) -> Option { + let primary = ctx.meta.primary; + if ctx.props(primary).ok()?.nclx.is_some() { + return None; + } + let coded = match ctx.grid(primary).ok()? { + Some((_, tiles)) => *tiles.first()?, + None => primary, + }; + let props = ctx.props(coded).ok()?; + if props.nclx.is_some() { + return None; + } + let hvcc = props.hvcc?; + let data = ctx.item_data(coded).ok()?; + let slices = hvcc.split_nals(&data).ok()?; + active_sps(&hvcc.parameter_sets(), &slices).and_then(sps_signal) + } + + /// The SPS the first slice of `slices` uses: the one its PPS names (ITU-T H.265 7.4.3.3.1). + pub(in super::super) fn active_sps<'a>(parameter_sets: &[&'a [u8]], slices: &[&[u8]]) -> Option<&'a [u8]> { + let slice = slices.iter().find(|nal| nal_type(nal).is_some_and(|t| t < 32))?; + let bits = &mut Bits { data: &payload(slice), at: 0 }; + // first_slice_segment_in_pic_flag, no_output_of_prior_pics_flag in an IRAP picture (types 16 + // to 23), slice_pic_parameter_set_id. + bits.skip(1)?; + if nal_type(slice).is_some_and(|t| (16..=23).contains(&t)) { + bits.skip(1)?; + } + let pps_id = bits.exp_golomb()?; + // pps_pic_parameter_set_id, pps_seq_parameter_set_id. + let pps_ids = |nal: &[u8]| { + let bits = &mut Bits { data: &payload(nal), at: 0 }; + Some((bits.exp_golomb()?, bits.exp_golomb()?)) + }; + let sps_id = parameter_sets.iter().filter(|nal| nal_type(nal) == Some(PPS)).find_map(|nal| pps_ids(nal).filter(|(id, _)| *id == pps_id))?.1; + let sps_ids = |nal: &[u8]| { + let rbsp = sps_payload(nal)?; + sps_head(&mut Bits { data: &rbsp, at: 0 }).map(|(_, id, ..)| id) + }; + parameter_sets.iter().copied().find(|nal| sps_ids(nal) == Some(sps_id)) + } + + /// The primary image decoded as heic-rs 0.1.4 decodes it (a grid's tiles one after another), + /// with its samples converted to RGBA with `nclx`, the alpha image applied and the image's + /// rotation, mirror and crop applied. + fn decode_with_nclx(ctx: &Context<'_>, nclx: Nclx, max_pixels: u64) -> heic_rs::Result { + use heic_rs::hevc::Frame; + + fn coded(ctx: &Context<'_>, id: u32) -> heic_rs::Result { + let props = ctx.props(id)?; + let hvcc = props.hvcc.as_ref().ok_or(Error::MissingBox("hvcC"))?; + let data = ctx.item_data(id)?; + let frame = heic_rs::hevc::decode_still(&hvcc.parameter_sets(), &hvcc.split_nals(&data)?)?; + frame.validate()?; + Ok(frame) + } + fn item(ctx: &Context<'_>, id: u32, max_pixels: u64) -> heic_rs::Result { + match ctx.grid(id)? { + Some((grid, tiles)) => { + // The tiles decode in parallel on native, as heic-rs's own decode does. + #[cfg(not(target_arch = "wasm32"))] + let frames = { + use rayon::prelude::*; + tiles.par_iter().map(|&t| coded(ctx, t)).collect::>>()? + }; + #[cfg(target_arch = "wasm32")] + let frames = tiles.iter().map(|&t| coded(ctx, t)).collect::>>()?; + heic_rs::grid::compose(&grid, &frames, max_pixels) + } + None => coded(ctx, id), + } + } + + let primary = ctx.meta.primary; + let props = ctx.props(primary)?; + let frame = item(ctx, primary, max_pixels)?; + let alpha = ctx.alpha_item(primary)?.map(|a| item(ctx, a, max_pixels)).transpose()?; + // `None`: rayon's global pool on native, where heic-rs is built with `parallel`. + let image = heic_rs::color::convert(&frame, alpha.as_ref(), nclx, PixelLayout::Rgba8, max_pixels, None)?; + heic_rs::transform::apply_all(image, &props.transforms) + } + + /// The bytes heic-rs 0.1.4 allocates for the coded pictures of `bytes` it decodes (see + /// [`picture_bytes_in`]); `None` when they can't be read. + #[cfg(test)] + pub(in super::super) fn picture_bytes(bytes: &[u8]) -> Option { + picture_bytes_in(&Context::open(bytes).ok()?) + } + + /// The bytes heic-rs 0.1.4 allocates for the coded pictures it decodes: the primary image's, + /// or each tile of its grid, and its alpha image's the same way. Each picture is sized by the + /// largest of its sequence parameter sets (the size the decoder allocates, rounded up to whole + /// 64-sample coding blocks), not by the container, and takes three sets of 16-bit planes: the + /// picture, a filtered copy and the frame returned. `None` when an item's sizes can't be read. + fn picture_bytes_in(ctx: &Context<'_>) -> Option { + let mut total = 0u64; + for item in primary_and_alpha(ctx)? { + let coded = match ctx.grid(item).ok()? { + Some((_, tiles)) => tiles, + None => vec![item], + }; + for id in coded { + let props = ctx.props(id).ok()?; + // An item without `hvcC` isn't decoded: heic-rs refuses the file. + let Some(hvcc) = props.hvcc else { continue }; + // The slices pick their SPS through the PPS, so the largest one is counted. Every + // SPS must be read. + let mut largest = None; + for nal in hvcc.parameter_sets().into_iter().filter(|nal| nal_type(nal) == Some(SPS)) { + let (width, height, chroma) = sps_size(nal)?; + let blocks = |n: u64| n.div_ceil(64) * 64; + // Bytes a sample position takes in one set of planes: luma and the two chroma + // planes. + let planes = match chroma { + 0 => 2, + 1 => 3, + 2 => 4, + _ => 6, + }; + let picture = blocks(width).checked_mul(blocks(height))?.checked_mul(planes * 3)?; + largest = largest.max(Some(picture)); + } + total = total.checked_add(largest?)?; + } + } + Some(total) + } + + /// The NAL unit type of an HEVC sequence parameter set. + const SPS: u8 = 33; + + /// The type of an HEVC NAL unit, from its header. + fn nal_type(nal: &[u8]) -> Option { + nal.first().map(|b| (b >> 1) & 0x3f) + } + + /// The NAL unit type of an HEVC picture parameter set. + const PPS: u8 = 34; + + /// A NAL unit's payload, without its two-byte header and the emulation prevention bytes (the 03 + /// of 00 00 03), up to its first 4,096 bytes. + fn payload(nal: &[u8]) -> Vec { + let mut rbsp = Vec::with_capacity(256); + let mut zeros = 0; + for &b in nal.get(2..).unwrap_or_default().iter().take(4096) { + if b == 3 && zeros >= 2 { + zeros = 0; + continue; + } + zeros = if b == 0 { zeros + 1 } else { 0 }; + rbsp.push(b); + } + rbsp + } + + /// An SPS NAL unit's [`payload`]; `None` for another NAL unit. + fn sps_payload(nal: &[u8]) -> Option> { + (nal_type(nal) == Some(SPS)).then(|| payload(nal)) + } + + /// The fields of an SPS (ITU-T H.265 7.3.2.2) up to the picture height: + /// `sps_max_sub_layers_minus1`, `sps_seq_parameter_set_id`, the width, the height and + /// `chroma_format_idc`. + fn sps_head(bits: &mut Bits<'_>) -> Option<(u64, u64, u64, u64, u64)> { + // sps_video_parameter_set_id, sps_max_sub_layers_minus1, sps_temporal_id_nesting_flag. + bits.skip(4)?; + let sub_layers = bits.read(3)?; + bits.skip(1)?; + // profile_tier_level (7.3.3): the general profile, tier and level, then the sub-layers'. + bits.skip(96)?; + let mut present = Vec::new(); + for _ in 0..sub_layers { + present.push((bits.read(1)?, bits.read(1)?)); + } + if sub_layers > 0 { + bits.skip(2 * (8 - sub_layers as usize))?; + } + for (profile, level) in present { + bits.skip(88 * profile as usize + 8 * level as usize)?; + } + let id = bits.exp_golomb()?; + let chroma = bits.exp_golomb()?; + if chroma == 3 { + bits.skip(1)?; + } + let width = bits.exp_golomb()?; + let height = bits.exp_golomb()?; + Some((sub_layers, id, width, height, chroma)) + } + + /// The picture width, height and `chroma_format_idc` of an HEVC sequence parameter set NAL + /// unit, before its conformance window; `None` for another NAL unit. + pub(in super::super) fn sps_size(nal: &[u8]) -> Option<(u64, u64, u64)> { + let rbsp = sps_payload(nal)?; + let (_, _, width, height, chroma) = sps_head(&mut Bits { data: &rbsp, at: 0 })?; + Some((width, height, chroma)) + } + + /// The video signal type of an HEVC sequence parameter set NAL unit's VUI (ITU-T H.265 E.2.1) + /// as an `nclx`: the full-range flag and, when the VUI describes them, the color primaries, + /// transfer characteristics and matrix coefficients (otherwise 2, unspecified). `None` when the + /// SPS has no VUI, the VUI has no video signal type, or the SPS can't be read that far. + pub(in super::super) fn sps_signal(nal: &[u8]) -> Option { + let rbsp = sps_payload(nal)?; + let bits = &mut Bits { data: &rbsp, at: 0 }; + let (sub_layers, ..) = sps_head(bits)?; + skip_to_vui(bits, sub_layers)?; + // vui_parameters_present_flag, then aspect_ratio_info_present_flag (an Extended_SAR + // aspect_ratio_idc of 255 adds its 32-bit ratio) and overscan_info_present_flag. + if bits.read(1)? == 0 { + return None; + } + if bits.read(1)? == 1 && bits.read(8)? == 255 { + bits.skip(32)?; + } + if bits.read(1)? == 1 { + bits.skip(1)?; + } + // video_signal_type_present_flag: video_format, video_full_range_flag and the color + // description. + if bits.read(1)? == 0 { + return None; + } + bits.skip(3)?; + let range = if bits.read(1)? == 1 { Range::Full } else { Range::Limited }; + let (primaries, transfer, matrix) = if bits.read(1)? == 1 { (bits.read(8)?, bits.read(8)?, bits.read(8)?) } else { (2, 2, 2) }; + let code = |v: u64| u16::try_from(v).ok(); + let matrix_code = code(matrix)?; + Some(Nclx { primaries: code(primaries)?, transfer: code(transfer)?, matrix: MatrixCoefficients::from_code(matrix_code), matrix_code, range }) + } + + /// Skips the fields of an SPS after [`sps_head`], up to `vui_parameters_present_flag`. + fn skip_to_vui(bits: &mut Bits<'_>, sub_layers: u64) -> Option<()> { + // conformance_window_flag and its four offsets. + if bits.read(1)? == 1 { + for _ in 0..4 { + bits.exp_golomb()?; + } + } + // bit_depth_luma_minus8, bit_depth_chroma_minus8. + bits.exp_golomb()?; + bits.exp_golomb()?; + let poc_bits = bits.exp_golomb()?.checked_add(4).filter(|&n| n <= 16)?; + // sps_sub_layer_ordering_info_present_flag, then three values for each sub-layer given. + let ordered = if bits.read(1)? == 1 { sub_layers + 1 } else { 1 }; + for _ in 0..ordered * 3 { + bits.exp_golomb()?; + } + // The coding and transform block sizes and the transform hierarchy depths. + for _ in 0..6 { + bits.exp_golomb()?; + } + // scaling_list_enabled_flag, sps_scaling_list_data_present_flag. + if bits.read(1)? == 1 && bits.read(1)? == 1 { + skip_scaling_lists(bits)?; + } + // amp_enabled_flag, sample_adaptive_offset_enabled_flag. + bits.skip(2)?; + // pcm_enabled_flag: the PCM sample depths, block sizes and loop filter flag. + if bits.read(1)? == 1 { + bits.skip(8)?; + bits.exp_golomb()?; + bits.exp_golomb()?; + bits.skip(1)?; + } + skip_reference_picture_sets(bits)?; + // long_term_ref_pics_present_flag: each picture's POC bits and used flag. + if bits.read(1)? == 1 { + let count = bits.exp_golomb()?; + if count > 32 { + return None; + } + for _ in 0..count { + bits.skip(poc_bits as usize + 1)?; + } + } + // sps_temporal_mvp_enabled_flag, strong_intra_smoothing_enabled_flag. + bits.skip(2) + } + + /// Skips `scaling_list_data()` (ITU-T H.265 7.3.4). + fn skip_scaling_lists(bits: &mut Bits<'_>) -> Option<()> { + for size in 0..4 { + // The 32 × 32 lists are given for matrices 0 and 3 only. + for _ in 0..if size == 3 { 2 } else { 6 } { + if bits.read(1)? == 0 { + // scaling_list_pred_matrix_id_delta. + bits.exp_golomb()?; + } else { + // scaling_list_dc_coef_minus8, then each scaling_list_delta_coef (se(v) codes, + // as long as ue(v) codes). + if size > 1 { + bits.exp_golomb()?; + } + for _ in 0..if size == 0 { 16 } else { 64 } { + bits.exp_golomb()?; + } + } + } + } + Some(()) + } + + /// Skips `num_short_term_ref_pic_sets` and each `st_ref_pic_set()` (ITU-T H.265 7.3.7) of an + /// SPS. A set predicted from the one before it has one or two flags for each of that set's + /// pictures and one more. + fn skip_reference_picture_sets(bits: &mut Bits<'_>) -> Option<()> { + let sets = bits.exp_golomb()?; + if sets > 64 { + return None; + } + // NumDeltaPocs of each set read so far. + let mut pictures: Vec = Vec::new(); + for index in 0..sets { + let predicted = index != 0 && bits.read(1)? == 1; + let count = if predicted { + // delta_rps_sign, abs_delta_rps_minus1. + bits.skip(1)?; + bits.exp_golomb()?; + let mut count = 0; + for _ in 0..=*pictures.last()? { + // used_by_curr_pic_flag, and use_delta_flag when it is 0. + if bits.read(1)? == 1 || bits.read(1)? == 1 { + count += 1; + } + } + count + } else { + let (negative, positive) = (bits.exp_golomb()?, bits.exp_golomb()?); + if negative > 16 || positive > 16 { + return None; + } + // Each picture's delta_poc_s0_minus1 or delta_poc_s1_minus1 and its used flag. + for _ in 0..negative + positive { + bits.exp_golomb()?; + bits.skip(1)?; + } + negative + positive + }; + pictures.push(count); + } + Some(()) + } + + /// A big-endian bit reader over an RBSP. + struct Bits<'a> { + data: &'a [u8], + at: usize, + } + + impl Bits<'_> { + fn bit(&mut self) -> Option { + let byte = self.data.get(self.at / 8)?; + let bit = (byte >> (7 - self.at % 8)) & 1; + self.at += 1; + Some(u64::from(bit)) + } + + fn read(&mut self, n: u32) -> Option { + (0..n).try_fold(0u64, |v, _| Some(v << 1 | self.bit()?)) + } + + fn skip(&mut self, n: usize) -> Option<()> { + self.at = self.at.checked_add(n)?; + (self.at <= self.data.len() * 8).then_some(()) + } + + /// An unsigned Exp-Golomb code, ue(v), of at most 32 leading zeros. + fn exp_golomb(&mut self) -> Option { + let mut zeros = 0; + while self.bit()? == 0 { + zeros += 1; + if zeros > 32 { + return None; + } + } + Some((1u64 << zeros) - 1 + self.read(zeros)?) + } + } + + /// `nclx` color primaries for Display P3 (SMPTE EG 432-1) and transfer characteristics for + /// the sRGB curve (IEC 61966-2-1), ITU-T H.273. const P3_PRIMARIES: u16 = 12; const SRGB_TRANSFER: u16 = 13; - fn source_profile(icc: Option<&[u8]>, colour: Colour) -> Option { + /// The profile the decoded RGB values are in when it isn't sRGB: the primary item's RGB ICC + /// profile, else Display P3 when its `nclx` names P3 primaries with the sRGB curve. A grid + /// without its own `colr` takes its first tile's, as cameras write it on the tiles. A profile + /// that can't be read, or isn't RGB, is left out and the values are read as sRGB. Without any + /// `colr`, the HEVC stream's color description ([`stream_nclx`]) is read the same way. + fn source_profile(ctx: &Context<'_>) -> Option { use vectorcraft_color::cms::{self, IccProfile, ProfileKind}; - if let Some(icc) = icc { + + let id = ctx.meta.primary; + let own = ctx.props(id).ok()?; + let described = |p: &heic_rs::props::ItemProps<'_>| p.icc.is_some() || p.nclx.is_some(); + let tile = || ctx.grid(id).ok().flatten().and_then(|(_, tiles)| tiles.first().and_then(|&t| ctx.props(t).ok())).filter(described); + let props = if described(&own) { Some(own) } else { tile() }; + if let Some(icc) = props.as_ref().and_then(|p| p.icc) { return IccProfile::from_bytes(None, icc).ok().filter(|p| p.kind == ProfileKind::Rgb); } - (colour.primaries == P3_PRIMARIES && colour.transfer == SRGB_TRANSFER).then(|| cms::icc::builtin_rgb(cms::DISPLAY_P3)).flatten() + let nclx = match props { + Some(props) => props.nclx?, + None => stream_nclx(ctx)?, + }; + (nclx.primaries == P3_PRIMARIES && nclx.transfer == SRGB_TRANSFER).then(|| cms::icc::builtin_rgb(cms::DISPLAY_P3)).flatten() } } diff --git a/crates/engine/src/cmd/fileio/jxlread.rs b/crates/engine/src/cmd/fileio/jxlread.rs index 37b28f63a9..1d4e3d1382 100644 --- a/crates/engine/src/cmd/fileio/jxlread.rs +++ b/crates/engine/src/cmd/fileio/jxlread.rs @@ -1,15 +1,39 @@ -//! Reading JPEG XL images (`.jxl`, ISO/IEC 18181) for File › Open and File › Place, with -//! `craft-jxl`: the bare codestream and the ISOBMFF container. The first frame of an animation is -//! read, with the orientation its header gives applied. +//! Reading JPEG XL images (`.jxl`, ISO/IEC 18181) for File › Open and File › Place, with the +//! jxl-oxide decoder: the bare codestream and the ISOBMFF container. The first frame of an +//! animation is read, with the orientation its header gives applied. +//! +//! Colors come back as sRGB whenever the decoder converts them without a color management +//! system: XYB-encoded images, every color space a JPEG XL header names, and embedded ICC +//! profiles that describe one of them. Any other image with an embedded RGB ICC profile comes +//! back with its samples as stored, as PNG and JPEG files with an embedded profile are drawn, and +//! with the profile. Gray images, and images whose profile is not an RGB one, are refused when the +//! decoder cannot convert their ICC profile. 16-bit and floating-point samples become 8 bits, alpha comes back straight, +//! and CMYK images are refused. The file is untrusted: the decoder is given only a container's +//! image boxes, the image's size is checked from its header before any pixels are decoded, and the +//! decoder's image buffers are capped. -use craft_jxl::{DecodeLimits, Error as JxlError, Layout}; +use jxl_color::ColorTransform; +use jxl_oxide::color::{ColourEncoding, TransferFunction}; +use jxl_oxide::{AllocTracker, ColorEncodingWithProfile, EnumColourEncoding, HdrType, InitializeResult, JxlImage, NullCms, RenderingIntent}; /// A bare JPEG XL codestream starts with these bytes. const CODESTREAM: [u8; 2] = [0xff, 0x0a]; /// The signature box an ISOBMFF JPEG XL container starts with. const CONTAINER: [u8; 12] = [0, 0, 0, 0x0c, b'J', b'X', b'L', b' ', 0x0d, 0x0a, 0x87, 0x0a]; -/// Buffer allowance for container box filtering. +/// How many bytes the decoder is given first to read the header. Each later try gives it twice as +/// many bytes, and each try decodes the embedded ICC profile from its start again. +const HEAD: usize = 4096; +/// The most the decoder's buffers may take while it reads the header. Its `AllocTracker` counts the +/// image buffers only: an embedded ICC profile is decoded into buffers of its own, which jxl-color +/// limits to 256 MiB. const HEADER_BUFFERS: usize = 16 << 20; +/// The most the decoder's buffers may take for the pixels: this many times the image's size as +/// RGBA, plus [`HEADER_BUFFERS`], and never more than the `max_alloc` of [`decode`], the limit the +/// `image` crate's decoders get in `raster_image`. The decoder keeps each channel as 32-bit +/// samples: a lossless 16-bit RGBA image of 1024 × 768 pixels took 12 times its size as RGBA. +const DECODER_BUFFERS: u64 = 16; + +const TRUNCATED: &str = "the JPEG XL file is cut short"; /// Is `bytes` a JPEG XL image (a bare codestream or a container)? pub fn is_jxl(bytes: &[u8]) -> bool { @@ -22,48 +46,134 @@ pub(crate) fn oriented_resolution(ppi: Option<(f64, f64)>, orientation: u32) -> } /// The first frame of a JPEG XL image as straight RGBA, with the embedded ICC profile its colors -/// are in when they are not converted to sRGB, and the header orientation. +/// are in when they are not converted to sRGB ([`output_profile`]), and the header orientation. +/// Fails, without decoding the +/// pixels, when the image would take more than `max_alloc` bytes as RGBA. The image is also too +/// large to draw when the decoder's buffers for its pixels need more than they are allowed. A +/// container whose selected boxes exceed `max_alloc` or [`HEADER_BUFFERS`], whichever is greater, +/// is refused with "the JPEG XL container is too large to read". pub fn decode(bytes: &[u8], max_alloc: u64) -> Result<(image::RgbaImage, Option>, u32), String> { let pixel_boxes = pixel_boxes(bytes, max_alloc.max(HEADER_BUFFERS as u64))?; let bytes = pixel_boxes.as_deref().unwrap_or(bytes); - let limits = DecodeLimits { max_pixels: max_alloc / 4, max_tracked_bytes: Some(max_alloc), max_input_bytes: usize::MAX }; - let decoded = craft_jxl::decode(bytes, limits).map_err(|e| match e { - JxlError::PixelLimitExceeded { width, height, .. } => too_large(width, height), - JxlError::LimitExceeded(msg) => format!("an image is too large to draw: {msg}"), - JxlError::Unsupported(msg) if msg.contains("CMYK") => "CMYK JPEG XL images can't be read".into(), - other => unreadable(other), - })?; - let (w, h) = (decoded.width, decoded.height); - if u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4) > max_alloc { + // The decoder reads the header first, with buffers for the header alone. + let (head, _) = header(bytes, HEADER_BUFFERS, None)?; + let (w, h) = (head.width(), head.height()); + let rgba = u64::from(w) * u64::from(h) * 4; + if rgba > max_alloc { return Err(too_large(w, h)); } - let profile = if !decoded.xyb_encoded && decoded.metadata.icc.as_deref() == Some(decoded.rendered_icc.as_slice()) { - if matches!(decoded.layout, Layout::Gray | Layout::GrayAlpha) { - return Err(unreadable("unsupported grayscale ICC profile")); - } - if decoded.rendered_icc.get(16..20) != Some(b"RGB ") { - return Err(unreadable("unsupported ICC profile color space")); - } - Some(decoded.rendered_icc) - } else { - None - }; - let channels = decoded.layout.channels(); - let gray = channels < 3; - let has_alpha = decoded.layout.has_alpha(); - let mut out = image::RgbaImage::new(w, h); - for (dst, src) in out.as_chunks_mut::<4>().0.iter_mut().zip(decoded.pixels.chunks_exact(channels)) { - let a = if has_alpha { src[channels - 1].clamp(0.0, 1.0) } else { 1.0 }; - let unpremul = |v: f32| if decoded.pixels_premultiplied && a > 0.0 { v / a } else { v }; - let rgb = if gray { - let v = byte(unpremul(src[0])); - [v, v, v] - } else { - [byte(unpremul(src[0])), byte(unpremul(src[1])), byte(unpremul(src[2]))] - }; - dst.copy_from_slice(&[rgb[0], rgb[1], rgb[2], byte(a)]); + if head.pixel_format().has_black() { + return Err("CMYK JPEG XL images can't be read".into()); } - Ok((out, profile, decoded.source_orientation)) + // Then it reads the file again until the first frame is loaded, with buffers for the pixels. + let budget = rgba.saturating_mul(DECODER_BUFFERS).saturating_add(HEADER_BUFFERS as u64).min(max_alloc); + let budget = usize::try_from(budget).unwrap_or(usize::MAX); + let size = Some((w, h)); + let (mut image, mut at) = header(bytes, budget, size)?; + let mut end = at; + while image.num_loaded_keyframes() == 0 { + if end == bytes.len() { + return Err(TRUNCATED.into()); + } + end = end.saturating_mul(2).max(HEAD).min(bytes.len()); + at += image.feed_bytes(&bytes[at..end]).map_err(|e| failed(e, size))?; + } + let metadata = &image.image_header().metadata; + let associated = metadata.alpha().and_then(|i| metadata.ec_info.get(i)).and_then(|ec| ec.alpha_associated()) == Some(true); + let intent = RenderingIntent::Relative; + let srgb = if metadata.grayscale() { EnumColourEncoding::gray_srgb(intent) } else { EnumColourEncoding::srgb(intent) }; + let profile = output_profile(metadata.xyb_encoded, metadata.grayscale(), image.original_icc())?; + let orientation = metadata.orientation; + // Premultiplied colors are divided by alpha in the encoding they are stored in, and converted + // to sRGB after that. XYB-encoded images come from the decoder in linear sRGB (tone mapped when + // they are HDR), which the sRGB transfer curve alone turns into sRGB. + let (request, convert) = match (to_srgb_without_cms(metadata.xyb_encoded, image.original_icc()), associated) { + (false, _) => (None, None), + (true, false) => (Some(srgb), None), + (true, true) if metadata.xyb_encoded => (Some(EnumColourEncoding { tf: TransferFunction::Linear, ..srgb }), Some(ToSrgb::Curve)), + (true, true) => { + let stored = match (&metadata.colour_encoding, image.original_icc()) { + (ColourEncoding::Enum(e), _) => ColorEncodingWithProfile::new(e.clone()), + (_, Some(icc)) => ColorEncodingWithProfile::with_icc(icc).map_err(unreadable)?, + (_, None) => return Err(unreadable("no color encoding")), + }; + let mut builder = ColorTransform::builder(); + builder.from_pq(image.hdr_type() == Some(HdrType::Pq)); + let to = ColorEncodingWithProfile::new(srgb); + let t = builder.build(&stored, &to, &metadata.opsin_inverse_matrix, &metadata.tone_mapping, &NullCms).map_err(unreadable)?; + (None, Some(ToSrgb::Transform(t))) + } + }; + if let Some(encoding) = request { + image.request_color_encoding(encoding); + } + let render = image.render_frame(0).map_err(|e| failed(e, size))?; + let mut stream = render.stream(); + let channels = stream.channels() as usize; + if !(1..=4).contains(&channels) { + return Err(unreadable(format!("{channels} channels"))); + } + let mut out = image::RgbaImage::new(stream.width(), stream.height()); + let width = out.width() as usize; + let mut row = vec![0f32; width * channels]; + let mut colors = [vec![0f32; width], vec![0f32; width], vec![0f32; width]]; + for line in out.chunks_exact_mut((width * 4).max(4)) { + if stream.write_to_buffer(&mut row) != row.len() { + return Err(TRUNCATED.into()); + } + to_rgba(&row, channels, associated, convert.as_ref(), &mut colors, line)?; + } + Ok((out, profile, orientation)) +} + +/// How colors whose alpha was divided out become sRGB. +pub(crate) enum ToSrgb { + /// A conversion from the encoding the colors are stored in. + Transform(ColorTransform), + /// The sRGB transfer curve, for colors in linear sRGB. + Curve, +} + +/// One row of samples, `channels` to a pixel (gray or RGB, then alpha when there are 2 or 4), as +/// straight RGBA bytes in `out`: colors divided by alpha when it is `associated`, then converted +/// with `convert`. `colors` holds a row of red, green and blue; a gray sample goes into all three, +/// as jxl-render's `clone_gray` does before its own conversions. +pub(crate) fn to_rgba( + row: &[f32], + channels: usize, + associated: bool, + convert: Option<&ToSrgb>, + colors: &mut [Vec; 3], + out: &mut [u8], +) -> Result<(), String> { + let alpha = |s: &[f32]| if matches!(channels, 2 | 4) { s.last().map_or(1.0, |a| a.clamp(0.0, 1.0)) } else { 1.0 }; + let gray = channels < 3; + for (x, s) in row.chunks_exact(channels).enumerate() { + let a = alpha(s); + for (i, plane) in colors.iter_mut().enumerate() { + let v = s.get(if gray { 0 } else { i }).copied().unwrap_or(0.0); + if let Some(c) = plane.get_mut(x) { + *c = if associated && a > 0.0 { v / a } else { v }; + } + } + } + // The conversion leaves gray in the first row when it gives one channel. + let gray = match convert { + Some(ToSrgb::Transform(t)) => { + let mut planes: Vec<&mut [f32]> = colors.iter_mut().map(Vec::as_mut_slice).collect(); + t.run(&mut planes).map_err(unreadable)? < 3 + } + Some(ToSrgb::Curve) => { + colors.iter_mut().flatten().for_each(|v| *v = if *v <= 0.003_130_8 { *v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }); + gray + } + None => gray, + }; + for (x, (px, s)) in out.as_chunks_mut::<4>().0.iter_mut().zip(row.chunks_exact(channels)).enumerate() { + let c = |i: usize| colors.get(if gray { 0 } else { i }).and_then(|p| p.get(x)).copied().unwrap_or(0.0); + *px = [byte(c(0)), byte(c(1)), byte(c(2)), byte(alpha(s))]; + } + Ok(()) } /// A container's signature and the boxes the decoder reads pixels from (`ftyp`, `jxll`, `jxlc` and @@ -100,6 +210,56 @@ pub(super) fn pixel_boxes(bytes: &[u8], max_copy: u64) -> Result> Ok(Some(out)) } +/// A decoder whose buffers may take `buffers` bytes, given as few of `bytes` as it needs to read +/// the header → (the decoder, how many bytes it took). `size` is the image's size when it is +/// known, for the error when the buffers need more ([`failed`]). +fn header(bytes: &[u8], buffers: usize, size: Option<(u32, u32)>) -> Result<(JxlImage, usize), String> { + let mut uninit = JxlImage::builder().alloc_tracker(AllocTracker::with_limit(buffers)).build_uninit(); + let (mut at, mut end) = (0, 0usize); + loop { + end = end.saturating_mul(2).max(HEAD).min(bytes.len()); + at += uninit.feed_bytes(&bytes[at..end]).map_err(|e| failed(e, size))?; + match uninit.try_init().map_err(|e| failed(e, size))? { + InitializeResult::Initialized(image) => return Ok((image, at)), + InitializeResult::NeedMoreData(_) if end == bytes.len() => return Err(TRUNCATED.into()), + InitializeResult::NeedMoreData(more) => uninit = more, + } + } +} + +/// Does the decoder convert to sRGB without a color management system? It does for XYB-encoded +/// images and the color spaces a header names; for an embedded ICC profile, only when the profile +/// describes one of those. +fn to_srgb_without_cms(xyb: bool, icc: Option<&[u8]>) -> bool { + xyb || icc.is_none_or(|icc| ColorEncodingWithProfile::with_icc(icc).is_ok_and(|p| p.icc_profile().is_empty())) +} + +/// The embedded RGB profile when the decoder leaves colors in it. Gray colors without a +/// conversion to sRGB cannot be stored as RGB without changing their profile, and neither can +/// colors left in a profile that is not an RGB one. +pub(crate) fn output_profile(xyb: bool, grayscale: bool, icc: Option<&[u8]>) -> Result>, String> { + let converts = to_srgb_without_cms(xyb, icc); + if grayscale && !converts { + return Err(unreadable("unsupported grayscale ICC profile")); + } + if !converts && icc.and_then(|icc| icc.get(16..20)) != Some(b"RGB ") { + return Err(unreadable("unsupported ICC profile color space")); + } + Ok(icc.filter(|_| !converts).map(<[u8]>::to_vec)) +} + +/// The decoder's error `e` as a message. When the decoder's buffers needed more than they were +/// allowed and the image's `size` is known, the image is too large to draw. Otherwise the file +/// can't be read. +fn failed(e: Box, size: Option<(u32, u32)>) -> String { + let first: &(dyn std::error::Error + 'static) = &*e; + let out_of_memory = std::iter::successors(Some(first), |c| c.source()).any(|c| c.is::()); + match size { + Some((w, h)) if out_of_memory => too_large(w, h), + _ => unreadable(e), + } +} + fn too_large(w: u32, h: u32) -> String { format!("an image of {w} × {h} pixels is too large to draw") } diff --git a/crates/engine/src/cmd/fileio/tests_heifread.rs b/crates/engine/src/cmd/fileio/tests_heifread.rs index 4b4a11b0c9..9820164da5 100644 --- a/crates/engine/src/cmd/fileio/tests_heifread.rs +++ b/crates/engine/src/cmd/fileio/tests_heifread.rs @@ -37,24 +37,7 @@ fn centers(side: u32) -> [(u32, u32); 4] { #[cfg(feature = "heif")] fn decoded(bytes: &[u8]) -> image::RgbaImage { let raster = load::raster_image(bytes).unwrap(); - craft_image_codecs::decode(&raster.blob.bytes) - .map(|__cimg| { - let (__w, __h) = __cimg.dimensions(); - match (__cimg.layout(), __cimg.sample_type()) { - (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - _ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()), - } - }) - .unwrap() - .to_rgba8() + image::load_from_memory(&raster.blob.bytes).unwrap().to_rgba8() } #[cfg(feature = "heif")] @@ -82,28 +65,7 @@ fn quadrants_open_and_place_as_their_pixels() { assert_eq!(opened["format"], "heif", "{name}"); assert_eq!(s.doc().unwrap().doc.artboards[0].rect.size(), vectorcraft_geom::Size::new(32.0, 32.0)); let blob = s.doc().unwrap().doc.images.values().next().unwrap(); - assert_quadrants( - &craft_image_codecs::decode(&blob.bytes) - .map(|__cimg| { - let (__w, __h) = __cimg.dimensions(); - match (__cimg.layout(), __cimg.sample_type()) { - (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - _ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()), - } - }) - .unwrap() - .to_rgba8(), - 32, - name, - ); + assert_quadrants(&image::load_from_memory(&blob.bytes).unwrap().to_rgba8(), 32, name); s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap(); let placed = s.execute("file.place", &json!({"name": name, "dataBase64": encoded})).unwrap(); assert_eq!(placed["format"], "heif", "{name}"); @@ -384,8 +346,8 @@ fn damaged_unsupported_and_oversized_files_are_refused() { small[at + 4..at + 8].copy_from_slice(&32u32.to_be_bytes()); let refused = super::heifread::decode(&small, 64 * 64 * 17 - 1).map(|img| img.dimensions()); assert!(refused.as_ref().is_err_and(|e| e.contains("too large to draw")), "{refused:?}"); - // A 128 × 128 grid of four 64 × 64 tiles. - let grid = 128 * 128 * 17; + // A 128 × 128 grid of four 64 × 64 tiles adds its composed frame, 128 × 128 × 6. + let grid = 128 * 128 * (8 + 6) + 4 * 64 * 64 * 9; assert!(super::heifread::decode(GRID, grid).is_ok()); assert!(super::heifread::decode(GRID, grid - 1).unwrap_err().contains("too large to draw")); // The grid's ispe (property 1) claims 16 × 16 pixels; its grid data still composes 128 × 128, @@ -397,6 +359,255 @@ fn damaged_unsupported_and_oversized_files_are_refused() { assert!(super::heifread::decode(&understated, grid - 1).unwrap_err().contains("too large to draw")); } +/// Appends `value` as `n` bits, most significant first. +#[cfg(feature = "heif")] +fn put(bits: &mut Vec, value: u64, n: u32) { + bits.extend((0..n).rev().map(|i| i < 64 && value >> i & 1 == 1)); +} + +/// Appends `value` as an unsigned Exp-Golomb code. +#[cfg(feature = "heif")] +fn put_ue(bits: &mut Vec, value: u64) { + let len = 64 - (value + 1).leading_zeros(); + put(bits, 0, len - 1); + put(bits, value + 1, len); +} + +/// The fields of SPS 0 of `width` × `height` 4:2:0 samples with `sub_layers` sub-layers, each +/// with its profile and level, up to its height. +#[cfg(feature = "heif")] +fn sps_head(width: u64, height: u64, sub_layers: u64) -> Vec { + sps_head_with_id(width, height, sub_layers, 0) +} + +/// [`sps_head`] for SPS `id`. +#[cfg(feature = "heif")] +fn sps_head_with_id(width: u64, height: u64, sub_layers: u64, id: u64) -> Vec { + let mut bits = Vec::new(); + put(&mut bits, 0, 4); + put(&mut bits, sub_layers, 3); + put(&mut bits, 1, 1); + put(&mut bits, 0, 96); + for _ in 0..sub_layers { + put(&mut bits, 0b11, 2); + } + if sub_layers > 0 { + put(&mut bits, 0, 2 * (8 - sub_layers as u32)); + } + for _ in 0..sub_layers { + put(&mut bits, 0, 88 + 8); + } + put_ue(&mut bits, id); + put_ue(&mut bits, 1); + put_ue(&mut bits, width); + put_ue(&mut bits, height); + bits +} + +/// An SPS NAL unit of `bits` (see [`nal`]). +#[cfg(feature = "heif")] +fn sps_nal(bits: Vec) -> Vec { + nal(33, bits) +} + +/// A NAL unit of type `kind` holding `bits`, with its stop bit, and emulation prevention bytes +/// inserted as a stream carries them. +#[cfg(feature = "heif")] +fn nal(kind: u8, mut bits: Vec) -> Vec { + put(&mut bits, 1, 1); + while !bits.len().is_multiple_of(8) { + bits.push(false); + } + let rbsp: Vec = bits.chunks(8).map(|b| b.iter().fold(0u8, |v, &bit| v << 1 | u8::from(bit))).collect(); + let mut nal = vec![kind << 1, 1]; + let mut zeros = 0; + for b in rbsp { + if zeros >= 2 && b <= 3 { + nal.push(3); + zeros = 0; + } + zeros = if b == 0 { zeros + 1 } else { 0 }; + nal.push(b); + } + nal +} + +#[cfg(feature = "heif")] +fn sps(width: u64, height: u64, sub_layers: u64) -> Vec { + sps_nal(sps_head(width, height, sub_layers)) +} + +/// A 64 × 64 SPS that uses every optional part before the VUI's video signal type: a conformance +/// window, sub-layer ordering for two sub-layers, explicit scaling lists, PCM, a reference picture +/// set predicted from another and a long-term picture, then an Extended_SAR aspect ratio and +/// overscan information. `signal` is the full-range flag and the color description, if any. +#[cfg(feature = "heif")] +fn sps_with_vui(signal: Option<(u64, Option<[u64; 3]>)>) -> Vec { + sps_with_vui_and_id(signal, 0) +} + +/// [`sps_with_vui`] for SPS `id`. +#[cfg(feature = "heif")] +fn sps_with_vui_and_id(signal: Option<(u64, Option<[u64; 3]>)>, id: u64) -> Vec { + let mut bits = sps_head_with_id(64, 64, 1, id); + put(&mut bits, 1, 1); + for offset in [0, 2, 0, 2] { + put_ue(&mut bits, offset); + } + put_ue(&mut bits, 0); + put_ue(&mut bits, 0); + // 8 bits of picture order count. + put_ue(&mut bits, 4); + put(&mut bits, 1, 1); + for _ in 0..2 * 3 { + put_ue(&mut bits, 1); + } + for value in [0, 3, 0, 3, 1, 1] { + put_ue(&mut bits, value); + } + // Scaling lists: matrix 1 of each size given explicitly, the others copied. + put(&mut bits, 0b11, 2); + for size in 0..4 { + for matrix in 0..if size == 3 { 2 } else { 6 } { + if matrix == 1 { + put(&mut bits, 1, 1); + if size > 1 { + put_ue(&mut bits, 5); + } + for _ in 0..if size == 0 { 16 } else { 64 } { + put_ue(&mut bits, 3); + } + } else { + put(&mut bits, 0, 1); + put_ue(&mut bits, 0); + } + } + } + // AMP off, SAO on, PCM with its depths, block sizes and loop filter flag. + put(&mut bits, 0b01, 2); + put(&mut bits, 1, 1); + put(&mut bits, 0x77, 8); + put_ue(&mut bits, 0); + put_ue(&mut bits, 1); + put(&mut bits, 0, 1); + // Two short-term sets: one negative and one positive picture, then a set predicted from it + // with three entries (unused with use_delta_flag, used, unused). + put_ue(&mut bits, 2); + put_ue(&mut bits, 1); + put_ue(&mut bits, 1); + for _ in 0..2 { + put_ue(&mut bits, 0); + put(&mut bits, 1, 1); + } + put(&mut bits, 1, 1); + put(&mut bits, 0, 1); + put_ue(&mut bits, 0); + put(&mut bits, 0b01100, 5); + // One long-term picture: 8 bits of picture order count and its used flag. + put(&mut bits, 1, 1); + put_ue(&mut bits, 1); + put(&mut bits, 0xab, 8); + put(&mut bits, 1, 1); + put(&mut bits, 0b11, 2); + // VUI: Extended_SAR 1:1 and overscan information. + put(&mut bits, 1, 1); + put(&mut bits, 1, 1); + put(&mut bits, 255, 8); + put(&mut bits, 0x0001_0001, 32); + put(&mut bits, 1, 1); + put(&mut bits, 0, 1); + match signal { + Some((full, description)) => { + put(&mut bits, 1, 1); + put(&mut bits, 5, 3); + put(&mut bits, full, 1); + put(&mut bits, u64::from(description.is_some()), 1); + for code in description.into_iter().flatten() { + put(&mut bits, code, 8); + } + } + None => put(&mut bits, 0, 1), + } + // No chroma location, field or display window information, timing or restrictions. + put(&mut bits, 0, 8); + sps_nal(bits) +} + +#[cfg(feature = "heif")] +#[test] +fn picture_sizes_come_from_the_sequence_parameter_sets() { + use super::heifread::{picture_bytes, sps_size}; + for sub_layers in [0, 1, 6] { + for (w, h) in [(32, 32), (4032, 3024), (65535, 65535)] { + let nal = sps(w, h, sub_layers); + assert!(nal.windows(3).any(|w| w == [0, 0, 3]), "the profile's zeros need emulation prevention"); + assert_eq!(sps_size(&nal), Some((w, h, 1)), "{w} × {h}, {sub_layers} sub-layers"); + } + } + let mut vps = sps(32, 32, 0); + vps[0] = 32 << 1; + assert_eq!(sps_size(&vps), None); + assert_eq!(sps_size(&sps(32, 32, 0)[..8]), None); + // 4:2:0 is 3 bytes a sample position in each of three sets of planes, in 64-sample blocks. + assert_eq!(picture_bytes(QUADRANTS), Some(64 * 64 * 9)); + assert_eq!(picture_bytes(GRID), Some(4 * 64 * 64 * 9)); + assert!(picture_bytes(ALPHA).unwrap() > 64 * 64 * 9, "the alpha image is counted"); +} + +#[cfg(feature = "heif")] +#[test] +fn the_color_description_comes_from_the_streams_video_signal_type() { + use super::heifread::{sps_signal, sps_size}; + use heic_rs::{MatrixCoefficients, Range}; + let signal = |nal: &[u8]| sps_signal(nal).map(|n| (n.primaries, n.transfer, n.matrix, n.matrix_code, n.range)); + let described = sps_with_vui(Some((1, Some([12, 13, 6])))); + assert_eq!(sps_size(&described), Some((64, 64, 1))); + assert_eq!(signal(&described), Some((12, 13, MatrixCoefficients::Bt601, 6, Range::Full))); + assert_eq!(signal(&sps_with_vui(Some((0, None)))), Some((2, 2, MatrixCoefficients::Unspecified, 2, Range::Limited))); + assert_eq!(signal(&sps_with_vui(None)), None); + assert_eq!(signal(&sps(64, 64, 0)), None, "cut short before its VUI"); + // The ICC-only fixture's stream: heif-enc writes full-range BT.601. + let ctx = heic_rs::context::Context::open(TONES_P3_ICC_ONLY).unwrap(); + let props = ctx.props(ctx.meta.primary).unwrap(); + assert!(props.nclx.is_none() && props.icc.is_some()); + let sets = props.hvcc.unwrap().parameter_sets(); + let nal = sets.into_iter().find(|nal| nal[0] >> 1 & 0x3f == 33).unwrap(); + assert_eq!(sps_signal(nal).map(|n| (n.matrix, n.range)), Some((MatrixCoefficients::Bt601, Range::Full))); +} + +#[cfg(feature = "heif")] +#[test] +fn the_slices_picture_parameter_set_picks_the_sps() { + use super::heifread::{active_sps, sps_signal}; + use heic_rs::Range; + let full = sps_with_vui_and_id(Some((1, Some([1, 13, 6]))), 0); + let limited = sps_with_vui_and_id(Some((0, Some([1, 13, 1]))), 1); + let pps = |id: u64, sps: u64| { + let mut bits = Vec::new(); + put_ue(&mut bits, id); + put_ue(&mut bits, sps); + nal(34, bits) + }; + // first_slice_segment_in_pic_flag, no_output_of_prior_pics_flag for an IDR picture (type 19), + // slice_pic_parameter_set_id. + let slice = |kind: u8, pps: u64| { + let mut bits = vec![true]; + if kind == 19 { + bits.push(false); + } + put_ue(&mut bits, pps); + nal(kind, bits) + }; + let (pps0, pps3) = (pps(0, 0), pps(3, 1)); + let sets = [full.as_slice(), limited.as_slice(), pps0.as_slice(), pps3.as_slice()]; + let (idr3, trail0, idr5) = (slice(19, 3), slice(1, 0), slice(19, 5)); + assert_eq!(active_sps(&sets, &[&idr3]), Some(limited.as_slice())); + assert_eq!(active_sps(&sets, &[&trail0]), Some(full.as_slice())); + assert_eq!(active_sps(&sets, &[&idr5]), None, "no PPS 5"); + assert_eq!(active_sps(&sets, &[&pps0]), None, "no slice"); + assert_eq!(active_sps(&sets, &[&idr3]).and_then(sps_signal).map(|n| (n.matrix_code, n.range)), Some((1, Range::Limited))); +} + #[cfg(not(feature = "heif"))] #[test] fn a_build_without_the_decoder_refuses_heif_files_with_a_message() { diff --git a/crates/engine/src/cmd/fileio/tests_jxlread.rs b/crates/engine/src/cmd/fileio/tests_jxlread.rs index 111132d689..6e6af80c82 100644 --- a/crates/engine/src/cmd/fileio/tests_jxlread.rs +++ b/crates/engine/src/cmd/fileio/tests_jxlread.rs @@ -1,39 +1,20 @@ //! Reading JPEG XL images: the bare codestream and the container, alpha, 16-bit samples, the size //! check before decoding, and damaged files refused without a panic. -use craft_jxl::{EncodeOptions, ImageRef, Layout, Samples}; use serde_json::json; +use zune_core::bit_depth::BitDepth; +use zune_core::colorspace::ColorSpace; +use zune_core::options::EncoderOptions; -use super::jxlread::{decode, is_jxl, oriented_resolution, pixel_boxes}; +use super::jxlread::{ToSrgb, decode, is_jxl, oriented_resolution, output_profile, pixel_boxes, to_rgba}; use super::*; -#[derive(Clone, Copy)] -enum BitDepth { - Eight, - Sixteen, -} - -/// A lossless JPEG XL codestream of `w` × `h` pixels: `samples` in `layout`, 8-bit, or 16-bit in +/// A lossless JPEG XL codestream of `w` × `h` pixels: `samples` in `space`, 8-bit, or 16-bit in /// native byte order. -fn jxl(w: usize, h: usize, layout: Layout, depth: BitDepth, samples: &[u8]) -> Vec { - let opts = EncodeOptions { lossless: true, ..EncodeOptions::default() }; - match depth { - BitDepth::Eight => craft_jxl::encode( - &ImageRef { width: w as u32, height: h as u32, layout, samples: Samples::U8(samples) }, - opts, - &craft_jxl::Metadata::default(), - ) - .unwrap(), - BitDepth::Sixteen => { - let u16s: Vec = samples.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]])).collect(); - craft_jxl::encode( - &ImageRef { width: w as u32, height: h as u32, layout, samples: Samples::U16(&u16s) }, - opts, - &craft_jxl::Metadata::default(), - ) - .unwrap() - } - } +fn jxl(w: usize, h: usize, space: ColorSpace, depth: BitDepth, samples: &[u8]) -> Vec { + let mut out = vec![]; + zune_jpegxl::JxlSimpleEncoder::new(samples, EncoderOptions::new(w, h, space, depth)).encode(&mut out).unwrap(); + out } /// `codestream` in the ISOBMFF container: the signature box, `ftyp` and a `jxlc` box holding it. @@ -51,7 +32,7 @@ fn container(codestream: &[u8]) -> Vec { const RGBA: [[u8; 4]; 6] = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255], [200, 100, 50, 128], [9, 9, 9, 0], [77, 77, 77, 255]]; fn rgba8() -> Vec { - jxl(3, 2, Layout::Rgba, BitDepth::Eight, RGBA.as_flattened()) + jxl(3, 2, ColorSpace::RGBA, BitDepth::Eight, RGBA.as_flattened()) } fn b64(bytes: &[u8]) -> String { @@ -64,27 +45,7 @@ fn stored_pixels(s: &Session) -> Vec<[u8; 4]> { assert_eq!(doc.images.len(), 1); let blob = doc.images.values().next().unwrap(); assert_eq!(blob.mime, "image/png", "stored as PNG"); - craft_image_codecs::decode(&blob.bytes) - .map(|__cimg| { - let (__w, __h) = __cimg.dimensions(); - match (__cimg.layout(), __cimg.sample_type()) { - (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageLuma8(image::GrayImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8) => { - image::DynamicImage::ImageRgb8(image::RgbImage::from_raw(__w, __h, __cimg.into_data()).unwrap()) - } - _ => image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(__w, __h, __cimg.to_rgba8()).unwrap()), - } - }) - .unwrap() - .to_rgba8() - .pixels() - .map(|p| p.0) - .collect() + image::load_from_memory(&blob.bytes).unwrap().to_rgba8().pixels().map(|p| p.0).collect() } #[test] @@ -183,13 +144,13 @@ fn a_copied_jxl_file_pastes_as_its_image() { fn sixteen_bit_samples_become_eight_bits() { let rgb: [[u16; 3]; 4] = [[0x0000, 0x8080, 0xffff], [0x1234, 0x80ff, 0x7f00], [0xff00, 0x00ff, 0x4000], [0xc000, 0x0101, 0xfefe]]; let samples: Vec = rgb.as_flattened().iter().flat_map(|v| v.to_ne_bytes()).collect(); - let f = jxl(2, 2, Layout::Rgb, BitDepth::Sixteen, &samples); + let f = jxl(2, 2, ColorSpace::RGB, BitDepth::Sixteen, &samples); let to8 = |v: u16| (f64::from(v) / 65535.0 * 255.0).round() as u8; let want: Vec<[u8; 4]> = rgb.iter().map(|p| [to8(p[0]), to8(p[1]), to8(p[2]), 255]).collect(); assert_eq!(decode(&f, 1 << 30).unwrap().0.pixels().map(|p| p.0).collect::>(), want); // Gray, 16 bits, in the container, opened as a document. let gray: Vec = [0xffffu16, 0x8080, 0x0000, 0x4040].iter().flat_map(|v| v.to_ne_bytes()).collect(); - let f = container(&jxl(2, 2, Layout::Gray, BitDepth::Sixteen, &gray)); + let f = container(&jxl(2, 2, ColorSpace::Luma, BitDepth::Sixteen, &gray)); let mut s = Session::new(); s.execute("document.open", &json!({"name": "gray.jxl", "dataBase64": b64(&f)})).unwrap(); assert_eq!(stored_pixels(&s), [[255, 255, 255, 255], [128, 128, 128, 255], [0, 0, 0, 255], [64, 64, 64, 255]]); @@ -231,16 +192,58 @@ fn an_image_too_large_is_refused_from_its_header() { /// the image is too large to draw, as when its size alone is. #[test] fn an_image_whose_decoder_buffers_pass_the_allowance_is_too_large() { - assert!(decode(&rgba8(), 24).unwrap_err().contains("too large to draw")); + assert_eq!(decode(&rgba8(), 24).unwrap_err(), "an image of 3 × 2 pixels is too large to draw"); let gray: Vec = (0..64 * 64).map(|i| (i % 251) as u8).collect(); - let f = container(&jxl(64, 64, Layout::Gray, BitDepth::Eight, &gray)); - assert!(decode(&f, 64 * 64 * 4).unwrap_err().contains("too large to draw")); + let f = container(&jxl(64, 64, ColorSpace::Luma, BitDepth::Eight, &gray)); + assert_eq!(decode(&f, 64 * 64 * 4).unwrap_err(), "an image of 64 × 64 pixels is too large to draw"); assert!(decode(&f, 1 << 20).is_ok()); // A file cut short is still reported as cut short or unreadable. let e = decode(&f[..f.len() - 3], 1 << 20).unwrap_err(); assert!(!e.contains("too large"), "{e}"); } +/// Premultiplied colors are divided by their alpha before they are converted to sRGB: a linear +/// 0.25 at alpha 0.5 is a straight linear 0.5, which sRGB stores as 0.735 (188 of 255). +#[test] +fn associated_alpha_is_divided_out_before_the_color_conversion() { + use jxl_oxide::{ColorEncodingWithProfile, EnumColourEncoding, NullCms, RenderingIntent}; + let srgb = EnumColourEncoding::srgb(RenderingIntent::Relative); + let linear = EnumColourEncoding { tf: jxl_oxide::color::TransferFunction::Linear, ..srgb.clone() }; + let header = jxl_oxide::JxlImage::builder().read(&rgba8()[..]).unwrap(); + let m = &header.image_header().metadata; + let to_srgb = ToSrgb::Transform( + jxl_color::ColorTransform::new( + &ColorEncodingWithProfile::new(linear), + &ColorEncodingWithProfile::new(srgb), + &m.opsin_inverse_matrix, + &m.tone_mapping, + &NullCms, + ) + .unwrap(), + ); + let mut colors = [vec![0.0; 2], vec![0.0; 2], vec![0.0; 2]]; + let mut out = [0u8; 8]; + for convert in [&to_srgb, &ToSrgb::Curve] { + to_rgba(&[0.25, 0.25, 0.25, 0.5, 0.0, 0.0, 0.0, 0.0], 4, true, Some(convert), &mut colors, &mut out).unwrap(); + assert_eq!(out, [188, 188, 188, 128, 0, 0, 0, 0]); + // Straight alpha is left as it is. + to_rgba(&[0.25, 0.25, 0.25, 0.5, 1.0, 1.0, 1.0, 1.0], 4, false, Some(convert), &mut colors, &mut out).unwrap(); + assert_eq!(out, [137, 137, 137, 128, 255, 255, 255, 255]); + } + // Gray with another white point converts through three channels and comes back as gray. + let gray_e = EnumColourEncoding { + white_point: jxl_oxide::color::WhitePoint::E, + tf: jxl_oxide::color::TransferFunction::Linear, + ..EnumColourEncoding::gray_srgb(RenderingIntent::Relative) + }; + let gray_srgb = ColorEncodingWithProfile::new(EnumColourEncoding::gray_srgb(RenderingIntent::Relative)); + let to_gray = + jxl_color::ColorTransform::new(&ColorEncodingWithProfile::new(gray_e), &gray_srgb, &m.opsin_inverse_matrix, &m.tone_mapping, &NullCms) + .unwrap(); + to_rgba(&[0.25, 0.5, 1.0, 1.0], 2, true, Some(&ToSrgb::Transform(to_gray)), &mut colors, &mut out).unwrap(); + assert_eq!(out, [188, 188, 188, 128, 255, 255, 255, 255]); +} + /// A container's Exif and Brotli-compressed boxes are left out: an unreadable `brob` box before the /// codestream doesn't stop the image from opening. #[test] @@ -258,17 +261,53 @@ fn metadata_boxes_are_not_read() { assert_eq!(decode(&f, 1 << 30).unwrap().0.pixels().map(|p| p.0).collect::>(), RGBA); } -/// The PNG carries the profile, the pixels and the resolution. +/// Colors left in their embedded RGB profile keep it: the PNG they are stored in carries the +/// profile. Colors converted to sRGB, and a gray image's colors, which come back as RGB, are stored +/// without one. #[test] fn colors_left_in_their_icc_profile_keep_it() { + use image::ImageDecoder as _; + use jxl_oxide::{EnumColourEncoding, RenderingIntent}; + // A profile jxl-color can't turn into a JPEG XL color space, so the decoder leaves the colors + // in it: the sRGB profile with the tag of its red curve renamed. + let unnamed = |encoding: &EnumColourEncoding, curve: &[u8; 4]| { + let mut icc = jxl_color::icc::colour_encoding_to_icc(encoding); + let count = u32::from_be_bytes(icc[128..132].try_into().unwrap()) as usize; + for at in (0..count).map(|i| 132 + 12 * i) { + if icc[at..at + 4] == curve[..] { + icc[at..at + 4].copy_from_slice(b"xTRC"); + } + } + icc + }; + let srgb = EnumColourEncoding::srgb(RenderingIntent::Relative); + let rgb = unnamed(&srgb, b"rTRC"); + assert_ne!(rgb, jxl_color::icc::colour_encoding_to_icc(&srgb), "a tag renamed"); + assert_eq!(output_profile(false, false, Some(&rgb)).unwrap(), Some(rgb.clone())); + assert_eq!(output_profile(true, false, Some(&rgb)).unwrap(), None, "XYB-encoded colors become sRGB"); + assert_eq!(output_profile(false, false, Some(&jxl_color::icc::colour_encoding_to_icc(&srgb))).unwrap(), None, "converted to sRGB"); + let gray = unnamed(&EnumColourEncoding::gray_srgb(RenderingIntent::Relative), b"kTRC"); + assert!(!jxl_oxide::ColorEncodingWithProfile::with_icc(&gray).unwrap().icc_profile().is_empty()); + assert_eq!(output_profile(false, true, Some(&gray)).unwrap_err(), "the JPEG XL file can't be read: unsupported grayscale ICC profile"); + assert_eq!( + output_profile(false, true, Some(&jxl_color::icc::colour_encoding_to_icc(&EnumColourEncoding::gray_srgb(RenderingIntent::Relative)))) + .unwrap(), + None + ); + assert_eq!(output_profile(false, false, None).unwrap(), None); + // A color image left in a profile that is not an RGB one can't be stored as RGB. + let mut lab = rgb.clone(); + lab[16..20].copy_from_slice(b"Lab "); + assert_eq!(output_profile(false, false, Some(&lab)).unwrap_err(), "the JPEG XL file can't be read: unsupported ICC profile color space"); + assert_eq!(output_profile(true, false, Some(&lab)).unwrap(), None, "XYB-encoded colors become sRGB"); + // The PNG carries the profile, the pixels and the resolution. let img = image::RgbaImage::from_raw(2, 1, vec![200, 100, 50, 255, 9, 9, 9, 0]).unwrap(); - let dummy_icc = vec![0u8; 128]; - for icc in [Some(dummy_icc), None] { + for icc in [Some(rgb), None] { let RasterImage { blob, width, height, .. } = super::load::as_png(img.clone(), icc.clone(), Some((144.0, 144.0))).unwrap(); assert_eq!((width, height), (2, 1)); - let decoded = craft_image_codecs::decode(&blob.bytes).unwrap(); - assert_eq!(decoded.icc, icc); - assert_eq!(decoded.data(), img.as_raw().as_slice()); + let mut png = image::codecs::png::PngDecoder::new(std::io::Cursor::new(&blob.bytes[..])).unwrap(); + assert_eq!(png.icc_profile().unwrap(), icc); + assert_eq!(image::DynamicImage::from_decoder(png).unwrap().to_rgba8(), img); assert_eq!(super::ppi::resolution(&blob.bytes).map(|(x, y)| (x.round(), y.round())), Some((144.0, 144.0))); } } diff --git a/crates/engine/src/cmd/menucmds.rs b/crates/engine/src/cmd/menucmds.rs index e533f1243d..a831f1a655 100644 --- a/crates/engine/src/cmd/menucmds.rs +++ b/crates/engine/src/cmd/menucmds.rs @@ -1103,7 +1103,8 @@ fn decode_rgba(c: &str, blob: &vectorcraft_doc::ImageBlob) -> Result crate::cmd::fileio::MAX_RASTER_ALLOC / 4 { return Err(bad(c, format!("an image of {w} × {h} pixels is too large to draw"))); @@ -1112,7 +1113,8 @@ fn decode_rgba(c: &str, blob: &vectorcraft_doc::ImageBlob) -> Result bad(c, "the image is too large to draw"), + // Within the allowance from its header: a file that can't hold the pixels it declares. + craft_image_codecs::CodecError::LimitExceeded(_) if header.is_none() => bad(c, "the image is too large to draw"), _ => bad(c, format!("the image's pixels can't be read: {e}")), })?; let (w, h) = img.dimensions(); diff --git a/crates/eps/src/import/data/filters.rs b/crates/eps/src/import/data/filters.rs index a3ad50039e..3dc9662bdf 100644 --- a/crates/eps/src/import/data/filters.rs +++ b/crates/eps/src/import/data/filters.rs @@ -367,7 +367,13 @@ pub(super) fn fax_settings(d: Option<&DictRef>) -> Res { } // Group 4 below 0, one-dimensional Group 3 at 0, mixed Group 3 above (the row's tag bit says // how it is coded, so any positive `K` reads the same); `as` saturates. - let k = if k < 0.0 { -1 } else if k == 0.0 { 0 } else { (k as i64).max(1) }; + let k = if k < 0.0 { + -1 + } else if k == 0.0 { + 0 + } else { + (k as i64).max(1) + }; Ok(CcittParams { k, columns: columns as i64,