mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
feat(image-codecs): read four-component JPEGs as stored inks
A jpeg_inks module for apps that keep a CMYK JPEG's inks the way zune-jpeg readers gave them: every four-component file taken as Adobe-inverted, YCCK turned into CMYK as libjpeg does (truncating), only the 8-bit Huffman frames zune-jpeg decodes (arithmetic-coded and 12-bit files are refused), within Limits. Also the frame's component count and the assembled ICC profile from the headers alone. Refs #778.
This commit is contained in:
1 parent
4c254242de
commit
473753b58c
4 files changed
+264
No files matched your search
@@ -0,0 +1,80 @@
|
||||
//! A JPEG's headers and four-component samples read the way `zune-jpeg` and libjpeg-style readers
|
||||
//! give them, for apps that keep a CMYK JPEG's inks: the component count and ICC profile from the
|
||||
//! headers alone, and the ink amounts of a CMYK or YCCK JPEG.
|
||||
//!
|
||||
//! [`crate::decode`] reads the same files differently, on purpose: it keeps the samples of a
|
||||
//! four-component JPEG without Adobe's marker as they are, rounds its YCCK conversion, and hands
|
||||
//! arithmetic-coded, lossless and 12-bit files to its whole-coefficient decoder. The readers here
|
||||
//! follow the older convention instead, so a document that kept a CMYK JPEG's inks that way reads
|
||||
//! the same: every four-component file is taken to hold Adobe's inverted samples, YCCK is turned
|
||||
//! into CMYK as libjpeg does (truncating), and only the 8-bit Huffman-coded frames `zune-jpeg`
|
||||
//! decodes are read.
|
||||
|
||||
use zune_core::bytestream::ZCursor;
|
||||
use zune_core::colorspace::ColorSpace;
|
||||
use zune_core::options::DecoderOptions;
|
||||
use zune_jpeg::JpegDecoder;
|
||||
|
||||
use crate::Format;
|
||||
use crate::error::CodecError;
|
||||
use crate::image::{ChannelLayout, Image, SampleType};
|
||||
use crate::options::Limits;
|
||||
|
||||
/// A decoder of `bytes` whose headers have been read, with the default options (16384 pixels a
|
||||
/// side at most); `None` when they aren't a JPEG or its headers can't be read.
|
||||
fn headers(bytes: &[u8]) -> Option<JpegDecoder<ZCursor<&[u8]>>> {
|
||||
if !bytes.starts_with(&[0xFF, 0xD8]) {
|
||||
return None;
|
||||
}
|
||||
let mut d = JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
Some(d)
|
||||
}
|
||||
|
||||
/// The number of colour components the frame header of JPEG `bytes` declares, whatever it is;
|
||||
/// `None` when they aren't a JPEG whose headers can be read.
|
||||
pub fn jpeg_components(bytes: &[u8]) -> Option<usize> {
|
||||
Some(usize::from(headers(bytes)?.info()?.components))
|
||||
}
|
||||
|
||||
/// The ICC profile of JPEG `bytes`, put together from all its numbered `ICC_PROFILE` APP2
|
||||
/// segments; `None` without one, when it is empty, and when the headers can't be read.
|
||||
pub fn jpeg_icc_profile(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
headers(bytes)?.icc_profile().filter(|icc| !icc.is_empty())
|
||||
}
|
||||
|
||||
/// The ink amounts of a four-component (CMYK or YCCK) JPEG: C, M, Y and K bytes a pixel, 0 for
|
||||
/// no ink, as a [`ChannelLayout::Cmyk`] image. The samples are taken to be Adobe's inverted ones
|
||||
/// whether or not the file has Adobe's marker; YCCK is first turned into CMYK as libjpeg does
|
||||
/// (the complement of the YCbCr colour's RGB, each value truncated).
|
||||
///
|
||||
/// Errors: bytes that aren't a JPEG with readable headers ([`CodecError::InvalidImage`]); one of
|
||||
/// other than four components, or a frame `zune-jpeg` doesn't decode, such as an arithmetic-coded
|
||||
/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; and data that doesn't decode.
|
||||
pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
let bad = |m: &str| CodecError::InvalidImage(format!("JPEG inks: {m}"));
|
||||
let mut d = headers(bytes).ok_or_else(|| bad("not a JPEG with readable headers"))?;
|
||||
let space = d.input_colorspace().ok_or_else(|| bad("no colour space"))?;
|
||||
let components = d.info().map_or(0, |i| i.components);
|
||||
if components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) {
|
||||
return Err(CodecError::unsupported(Format::Jpeg, format!("inks of a {components}-component {space:?} image")));
|
||||
}
|
||||
let (w, h) = d.dimensions().ok_or_else(|| bad("no size"))?;
|
||||
let (w, h) = (u32::try_from(w).map_err(|_| bad("width"))?, u32::try_from(h).map_err(|_| bad("height"))?);
|
||||
limits.check(w, h, ChannelLayout::Cmyk, SampleType::U8)?;
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
let mut data = d.decode().map_err(|e| CodecError::unsupported(Format::Jpeg, format!("inks: {e}")))?;
|
||||
for p in data.as_chunks_mut::<4>().0 {
|
||||
if space == ColorSpace::YCCK {
|
||||
let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from);
|
||||
// libjpeg's YCCK → CMYK: the complement of the YCbCr colour's RGB.
|
||||
p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
for v in p.iter_mut() {
|
||||
*v = 255 - *v;
|
||||
}
|
||||
}
|
||||
Image::from_u8(w, h, ChannelLayout::Cmyk, data)
|
||||
}
|
||||
@@ -26,6 +26,7 @@ mod fidelity;
|
||||
mod format;
|
||||
mod image;
|
||||
mod inspection;
|
||||
pub mod jpeg_inks;
|
||||
pub mod jpeg_sef;
|
||||
mod lab;
|
||||
mod options;
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
//! `jpeg_inks`: four-component JPEGs read as Adobe-inverted inks, as `zune-jpeg`-based readers
|
||||
//! kept them, beside `decode`'s own reading.
|
||||
|
||||
use craft_image_codecs::jpeg_inks::{decode_jpeg_inks, jpeg_components, jpeg_icc_profile};
|
||||
use craft_image_codecs::*;
|
||||
|
||||
/// A `w` × `h` four-component JPEG of `samples` at quality 100, written by `jpeg-encoder` as
|
||||
/// `ty`, with ICC profile `icc` when given.
|
||||
fn four(w: u16, h: u16, samples: &[u8], ty: jpeg_encoder::ColorType, icc: Option<&[u8]>) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
let mut enc = jpeg_encoder::Encoder::new(&mut out, 100);
|
||||
enc.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_4_4);
|
||||
if let Some(icc) = icc {
|
||||
enc.add_icc_profile(icc).unwrap();
|
||||
}
|
||||
enc.encode(samples, w, h, ty).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
/// Flat 8×8 blocks of ink, so the coding keeps each block's value.
|
||||
fn blocks(w: usize, h: usize) -> Vec<u8> {
|
||||
(0..w * h)
|
||||
.flat_map(|i| {
|
||||
let b = ((i % w) / 8 + (i / w) / 8 * 7) as u8;
|
||||
[b.wrapping_mul(37), b.wrapping_mul(91), 255 - b.wrapping_mul(13), b.wrapping_mul(53)]
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The segments of `bytes` up to the scan without Adobe's APP14 marker.
|
||||
fn without_adobe(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut out = bytes[..2].to_vec();
|
||||
let mut at = 2;
|
||||
loop {
|
||||
let marker = bytes[at + 1];
|
||||
if marker == 0xDA {
|
||||
out.extend_from_slice(&bytes[at..]);
|
||||
return out;
|
||||
}
|
||||
let len = usize::from(u16::from_be_bytes([bytes[at + 2], bytes[at + 3]]));
|
||||
if !(marker == 0xEE && &bytes[at + 4..at + 9] == b"Adobe") {
|
||||
out.extend_from_slice(&bytes[at..at + 2 + len]);
|
||||
}
|
||||
at += 2 + len;
|
||||
}
|
||||
}
|
||||
|
||||
/// Adobe CMYK reads to the inks it was written with, as `decode` reads it; without the marker
|
||||
/// the samples are still taken as inverted (where `decode` keeps them as they are), and YCCK
|
||||
/// turns into the same inks closely, truncating where `decode` rounds.
|
||||
#[test]
|
||||
fn four_component_jpegs_read_as_inverted_inks() {
|
||||
let (w, h) = (32u16, 16u16);
|
||||
let inks = blocks(usize::from(w), usize::from(h));
|
||||
let cmyk = four(w, h, &inks, jpeg_encoder::ColorType::Cmyk, None);
|
||||
let read = decode_jpeg_inks(&cmyk, &Limits::default()).unwrap();
|
||||
assert_eq!((read.dimensions(), read.layout()), ((32, 16), ChannelLayout::Cmyk));
|
||||
assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 2), "Adobe CMYK keeps its inks");
|
||||
assert_eq!(read.data(), decode(&cmyk).unwrap().data(), "as decode reads Adobe CMYK");
|
||||
// No marker: inverted all the same, where decode keeps the stored samples.
|
||||
let bare = without_adobe(&cmyk);
|
||||
assert_ne!(bare, cmyk);
|
||||
let unmarked = decode_jpeg_inks(&bare, &Limits::default()).unwrap();
|
||||
assert_eq!(unmarked.data(), read.data());
|
||||
let kept = decode(&bare).unwrap();
|
||||
assert!(kept.data().iter().zip(read.data()).all(|(k, r)| *k == 255 - *r), "decode keeps them as stored");
|
||||
// YCCK: libjpeg's conversion, truncated.
|
||||
let ycck = four(w, h, &inks, jpeg_encoder::ColorType::CmykAsYcck, None);
|
||||
let read = decode_jpeg_inks(&ycck, &Limits::default()).unwrap();
|
||||
assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 3), "YCCK keeps its inks closely");
|
||||
let rounded = decode(&ycck).unwrap();
|
||||
let differ = read.data().iter().zip(rounded.data()).filter(|(a, b)| a != b).count();
|
||||
assert!(read.data().iter().zip(rounded.data()).all(|(a, b)| a.abs_diff(*b) <= 1), "within a step of decode's rounding");
|
||||
assert!(differ > 0, "truncation differs from rounding somewhere");
|
||||
}
|
||||
|
||||
/// The headers alone give the component count of any JPEG and its ICC profile, put together from
|
||||
/// its numbered segments.
|
||||
#[test]
|
||||
fn headers_give_components_and_icc() {
|
||||
let icc: Vec<u8> = (0..200_000u32).map(|i| (i * 7 % 251) as u8).collect();
|
||||
let cmyk = four(8, 8, &[9; 256], jpeg_encoder::ColorType::Cmyk, Some(&icc));
|
||||
assert_eq!(jpeg_components(&cmyk), Some(4));
|
||||
assert_eq!(jpeg_icc_profile(&cmyk).as_deref(), Some(icc.as_slice()), "three APP2 segments");
|
||||
let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap();
|
||||
assert_eq!((jpeg_components(&gray), jpeg_icc_profile(&gray)), (Some(1), None));
|
||||
for not in [&b""[..], b"\xFF\xD8", b"\x89PNG\r\n\x1a\n"] {
|
||||
assert_eq!((jpeg_components(not), jpeg_icc_profile(not)), (None, None));
|
||||
}
|
||||
}
|
||||
|
||||
/// What isn't a four-component image `zune-jpeg` decodes is refused: other component counts, an
|
||||
/// arithmetic-coded or 12-bit frame (`decode` hands those to craft-jpeg), a size past the limits,
|
||||
/// and data that isn't a JPEG.
|
||||
#[test]
|
||||
fn other_jpegs_are_refused() {
|
||||
let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap();
|
||||
assert!(matches!(decode_jpeg_inks(&gray, &Limits::default()), Err(CodecError::Unsupported { .. })));
|
||||
let cmyk = four(16, 8, &[40; 16 * 8 * 4], jpeg_encoder::ColorType::Cmyk, None);
|
||||
let sof = cmyk.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap();
|
||||
for (marker, precision) in [(0xC9u8, 8u8), (0xC1, 12)] {
|
||||
let mut other = cmyk.clone();
|
||||
other[sof + 1] = marker;
|
||||
other[sof + 4] = precision;
|
||||
assert!(decode_jpeg_inks(&other, &Limits::default()).is_err(), "SOF {marker:#x}, {precision} bits");
|
||||
}
|
||||
let tight = Limits { max_pixels: 100, ..Limits::default() };
|
||||
assert!(matches!(decode_jpeg_inks(&cmyk, &tight), Err(CodecError::LimitExceeded(_))));
|
||||
assert!(matches!(decode_jpeg_inks(b"not a jpeg", &Limits::default()), Err(CodecError::InvalidImage(_))));
|
||||
assert!(decode_jpeg_inks(&cmyk[..12], &Limits::default()).is_err(), "headers cut short");
|
||||
}
|
||||
|
||||
/// The ink reader four-component JPEGs were read with before (vectorcraft's `jpeg_inks`, on
|
||||
/// `zune-jpeg` as this crate uses it), kept as the reference the readers here match.
|
||||
fn reference_inks(bytes: &[u8]) -> Option<(usize, usize, Vec<u8>)> {
|
||||
use zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions};
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
let space = d.input_colorspace()?;
|
||||
if d.info()?.components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) {
|
||||
return None;
|
||||
}
|
||||
let (w, h) = d.dimensions()?;
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
let mut data = d.decode().ok()?;
|
||||
for p in data.as_chunks_mut::<4>().0 {
|
||||
if space == ColorSpace::YCCK {
|
||||
let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from);
|
||||
p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
for v in p.iter_mut() {
|
||||
*v = 255 - *v;
|
||||
}
|
||||
}
|
||||
Some((w, h, data))
|
||||
}
|
||||
|
||||
/// Over CMYK and YCCK JPEGs of many sizes, samplings, qualities and pictures, with and without
|
||||
/// Adobe's marker, baseline and progressive, the inks and component counts read the same as the
|
||||
/// reference reader.
|
||||
#[test]
|
||||
fn matches_the_reference_reader() {
|
||||
use jpeg_encoder::{ColorType, Encoder, SamplingFactor};
|
||||
let (mut seen, mut refused) = (0, 0);
|
||||
for (i, (w, h)) in [(1u16, 1u16), (7, 3), (8, 8), (17, 9), (33, 31), (64, 5), (100, 77)].into_iter().enumerate() {
|
||||
let (wu, n) = (usize::from(w), usize::from(w) * usize::from(h));
|
||||
let noise: Vec<u8> = (0..n * 4).map(|k| ((k * 2_654_435_761 + i * 97) >> 7) as u8).collect();
|
||||
let ramp: Vec<u8> = (0..n * 4).map(|k| (k / 4 % wu * 255 / wu.max(2)) as u8).collect();
|
||||
for samples in [&noise, &ramp] {
|
||||
for ty in [ColorType::Cmyk, ColorType::CmykAsYcck] {
|
||||
for (q, sampling, progressive) in
|
||||
[(100, SamplingFactor::R_4_4_4, false), (75, SamplingFactor::R_4_2_0, false), (40, SamplingFactor::R_4_2_2, true)]
|
||||
{
|
||||
let mut out = Vec::new();
|
||||
let mut enc = Encoder::new(&mut out, q);
|
||||
enc.set_sampling_factor(sampling);
|
||||
enc.set_progressive(progressive);
|
||||
enc.encode(samples, w, h, ty).unwrap();
|
||||
for file in [without_adobe(&out), out] {
|
||||
let read = decode_jpeg_inks(&file, &Limits::default());
|
||||
assert_eq!(jpeg_components(&file), Some(4));
|
||||
let Some((rw, rh, inks)) = reference_inks(&file) else {
|
||||
eprintln!("refused: {w}x{h} {ty:?} q{q} progressive {progressive}");
|
||||
assert!(read.is_err(), "refused by both: {w}x{h} {ty:?} q{q}");
|
||||
refused += 1;
|
||||
continue;
|
||||
};
|
||||
let read = read.unwrap();
|
||||
assert_eq!(read.dimensions(), (rw as u32, rh as u32));
|
||||
assert_eq!(read.data(), inks.as_slice(), "{w}x{h} {ty:?} q{q}");
|
||||
seen += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
eprintln!("read {seen}, refused {refused}");
|
||||
assert_eq!(seen + refused, 7 * 2 * 2 * 3 * 2);
|
||||
assert!(seen > refused);
|
||||
}
|
||||
@@ -14,6 +14,7 @@ mod exr_stream;
|
||||
mod fidelity;
|
||||
mod gif_frames;
|
||||
mod heif;
|
||||
mod jpeg_inks;
|
||||
mod jpegls_oracle;
|
||||
mod limits;
|
||||
mod malformed;
|
||||
|
||||
Reference in new issue
Block a user