use crate::coder_scratch::CoderScratch;
use crate::coding::{CodingTransform, srgb_eotf};
use crate::encode_image::{
AlphaPlane, BitsPerSample, EncodeConfigImpl, MAX_DIMENSION, MIN_DISTANCE, checked_buffer_size,
encode_with_coded_planes, encode_with_config_loseless, lossy_context,
};
use crate::image::Image3F;
use crate::xyb::XybMatrix;
use crate::{EncodeConfig, EncodeError};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InkConvention {
Amount,
Inverted,
}
trait CmykSample: Copy {
fn value(self) -> u32;
}
impl CmykSample for u8 {
#[inline]
fn value(self) -> u32 {
u32::from(self)
}
}
impl CmykSample for u16 {
#[inline]
fn value(self) -> u32 {
u32::from(self)
}
}
pub fn encode_image_cmyk(
input: &[u8],
width: usize,
height: usize,
icc_profile: &[u8],
convention: InkConvention,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
encode_cmyk_impl(input, width, height, 8, icc_profile, convention, config)
}
pub fn encode_image_cmyk_16bit(
input: &[u16],
width: usize,
height: usize,
bits: u8,
icc_profile: &[u8],
convention: InkConvention,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if !matches!(bits, 10 | 12 | 16) {
return Err(EncodeError::Unsupported(
"CMYK bit depth must be 10, 12 or 16",
));
}
encode_cmyk_impl(input, width, height, bits, icc_profile, convention, config)
}
fn is_cmyk_profile(icc: &[u8]) -> bool {
icc.len() >= 128 && &icc[16..20] == b"CMYK"
}
fn encode_cmyk_impl<T: CmykSample + crate::encode_image::AsSignedInt>(
input: &[T],
width: usize,
height: usize,
bits: u8,
icc_profile: &[u8],
convention: InkConvention,
config: &EncodeConfig,
) -> Result<Vec<u8>, EncodeError> {
if width == 0 || height == 0 {
return Err(EncodeError::EmptyImage);
}
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(EncodeError::DimensionTooLarge { width, height });
}
let expected = checked_buffer_size::<T>(width, height, 4)?;
if input.len() != expected {
return Err(EncodeError::InputSizeMismatch {
expected,
actual: input.len(),
});
}
if !config.distance.is_finite() || config.distance <= 0.0 {
return Err(EncodeError::InvalidDistance(config.distance));
}
if !is_cmyk_profile(icc_profile) {
return Err(EncodeError::Unsupported("CMYK needs a CMYK ICC profile"));
}
let max = (1u32 << bits) - 1;
let stored = |v: T| -> u32 {
match convention {
InkConvention::Inverted => v.value(),
InkConvention::Amount => max - v.value().min(max),
}
};
let bps = match bits {
8 => BitsPerSample::Eight,
10 => BitsPerSample::Ten,
12 => BitsPerSample::Twelve,
_ => BitsPerSample::Sixteen,
};
let base = EncodeConfigImpl::with_distance(config.distance)
.with_icc_profile(Some(icc_profile.to_vec()))
.with_black(true)
.with_exif(config.exif.clone())
.with_xmp(config.xmp.clone())
.with_brotli_compression(config.brotli_compression.clone())
.with_orientation(config.orientation)
.with_speed(config.speed)
.with_decoding_speed(config.decoding_speed)
.with_num_threads(config.num_threads);
if config.lossless {
let stored_input: Vec<u16> = input.iter().map(|&v| stored(v) as u16).collect();
let cfg = base
.with_lossless(true)
.with_progressive(config.progressive)
.with_bits_per_sample(bps);
return if bits == 8 {
let bytes: Vec<u8> = stored_input.iter().map(|&v| v as u8).collect();
encode_with_config_loseless(&bytes, width, height, true, 8, &cfg)
} else {
encode_with_config_loseless(&stored_input, width, height, true, bits, &cfg)
};
}
let distance = config.distance.max(MIN_DISTANCE);
let mut ctx = lossy_context(config, distance, XybMatrix::SPEC, width * height);
let coding = match config.coding_transform {
CodingTransform::Xyb | CodingTransform::YCbCr => CodingTransform::YCbCr,
CodingTransform::Rgb => CodingTransform::Rgb,
};
ctx.set_coding(coding, distance);
ctx.epf_channel_scale = coding.epf_channel_scale();
let inv_max = 1.0 / max as f32;
let mut coded = Image3F::try_new(width, height)?;
let mut linear = Image3F::try_new(width, height)?;
let mut k_plane_u8 = Vec::new();
let mut k_plane_u16 = Vec::new();
for (y, row) in input.chunks_exact(width * 4).enumerate() {
let [c0, c1, c2] = coded.all_plane_rows_mut(y);
let [l0, l1, l2] = linear.all_plane_rows_mut(y);
for (x, px) in row.as_chunks::<4>().0.iter().enumerate() {
let c = stored(px[0]) as f32 * inv_max;
let m = stored(px[1]) as f32 * inv_max;
let yy = stored(px[2]) as f32 * inv_max;
let k = stored(px[3]);
[c0[x], c1[x], c2[x]] = coding.code(c, m, yy);
let kf = k as f32 * inv_max;
l0[x] = srgb_eotf(c * kf);
l1[x] = srgb_eotf(m * kf);
l2[x] = srgb_eotf(yy * kf);
if bits == 8 {
k_plane_u8.push(k as u8);
} else {
k_plane_u16.push(k as u16);
}
}
}
let mut k_plane = if bits == 8 {
AlphaPlane::U8(k_plane_u8)
} else {
AlphaPlane::U16 {
data: k_plane_u16,
bits,
}
};
let passes = crate::encode_image::progressive_schedule(
config.progressive,
config.progressive_passes,
config.progressive_shifts.as_deref(),
);
ctx.extra_squeeze_distance = BLACK_DISTANCE_SCALE * distance;
ctx.extra_learned_trees = config.decoding_speed != crate::DecodingSpeed::Fastest
&& !matches!(
config.speed,
crate::Speed::UltraFast | crate::Speed::Fastest | crate::Speed::Fast
);
ctx.extra_plane_tree = if ctx.extra_learned_trees {
Some(crate::lossless::PlaneTreeBudget::FULL)
} else {
crate::encode_image::alpha_plane_tree(config.speed, config.decoding_speed)
};
ctx.extra_context_trees = config.decoding_speed != crate::DecodingSpeed::Fastest;
if passes.len() > 1 && crate::squeeze::default_squeeze_steps(width, height, 1).is_empty() {
ctx.extra_step = black_step(distance, max);
if ctx.extra_step > 1 {
crate::modular::snap_to_lattice(&mut k_plane, ctx.extra_step);
}
}
let cfg = base
.with_progressive_from(config)
.with_bits_per_sample(bps)
.with_alpha(k_plane);
let mut scratch = Box::<CoderScratch>::default();
encode_with_coded_planes(linear, Some(coded), &cfg, &ctx, &mut scratch)
}
fn black_step(distance: f32, max: u32) -> u32 {
let step = distance * max as f32 / 255.0;
(step.round() as u32).clamp(1, max / 4)
}
const BLACK_DISTANCE_SCALE: f32 = 6.0;
#[cfg(test)]
mod tests {
use super::*;
fn cmyk_icc() -> Vec<u8> {
let mut icc = vec![0u8; 132];
icc[0..4].copy_from_slice(&132u32.to_be_bytes());
icc[8] = 2;
icc[12..16].copy_from_slice(b"prtr");
icc[16..20].copy_from_slice(b"CMYK");
icc[20..24].copy_from_slice(b"Lab ");
icc[36..40].copy_from_slice(b"acsp");
icc
}
fn ramp(width: usize, height: usize) -> Vec<u8> {
(0..width * height)
.flat_map(|i| {
let (x, y) = (i % width, i / width);
[
(x * 255 / width) as u8,
(y * 255 / height) as u8,
((x + y) % 256) as u8,
((x * y) % 200) as u8,
]
})
.collect()
}
fn has_box(bytes: &[u8], kind: &[u8; 4]) -> bool {
bytes.array_windows::<4>().any(|w| w == kind)
}
#[test]
fn rejects_non_cmyk_profiles() {
let mut icc = cmyk_icc();
icc[16..20].copy_from_slice(b"RGB ");
let px = ramp(16, 16);
let config = EncodeConfig::default();
for profile in [&icc[..], &icc[..64]] {
let err = encode_image_cmyk(&px, 16, 16, profile, InkConvention::Amount, &config)
.unwrap_err();
assert!(matches!(err, EncodeError::Unsupported(_)), "{err:?}");
}
}
#[test]
fn rejects_unsupported_bit_depths() {
let px = vec![0u16; 16 * 16 * 4];
let err = encode_image_cmyk_16bit(
&px,
16,
16,
9,
&cmyk_icc(),
InkConvention::Amount,
&EncodeConfig::default(),
)
.unwrap_err();
assert!(matches!(err, EncodeError::Unsupported(_)), "{err:?}");
}
#[test]
fn encodes_lossy_and_lossless() {
let (w, h) = (100, 70);
let px = ramp(w, h);
let icc = cmyk_icc();
let lossy = EncodeConfig::default().with_distance(1.5);
for (i, config) in [
lossy.clone(),
lossy.clone().with_progressive(true),
lossy
.clone()
.with_coding_transform(crate::CodingTransform::Rgb),
lossy.clone().with_lossy_modular(crate::LossyModular::Force),
EncodeConfig::default().with_lossless(true),
]
.into_iter()
.enumerate()
{
let convention = if i % 2 == 0 {
InkConvention::Amount
} else {
InkConvention::Inverted
};
let bytes = encode_image_cmyk(&px, w, h, &icc, convention, &config).unwrap();
assert!(has_box(&bytes, b"jxll"), "CMYK needs a level-10 box");
}
let px16: Vec<u16> = px.iter().map(|&v| u16::from(v) << 4).collect();
let bytes = encode_image_cmyk_16bit(
&px16,
w,
h,
12,
&icc,
InkConvention::Amount,
&EncodeConfig::default().with_distance(1.0),
)
.unwrap();
assert!(has_box(&bytes, b"jxll"));
}
#[test]
fn invisible_fill_never_treats_black_as_alpha() {
let pixels = ramp(16, 16);
let icc = cmyk_icc();
for coding in [crate::CodingTransform::Xyb, crate::CodingTransform::Rgb] {
for arm in [crate::LossyModular::Off, crate::LossyModular::Force] {
let on = EncodeConfig::default()
.with_num_threads(1)
.with_coding_transform(coding)
.with_lossy_modular(arm);
let off = on.clone().with_fill_invisible(false);
for convention in [InkConvention::Amount, InkConvention::Inverted] {
assert_eq!(
encode_image_cmyk(&pixels, 16, 16, &icc, convention, &on).unwrap(),
encode_image_cmyk(&pixels, 16, 16, &icc, convention, &off).unwrap()
);
}
}
}
}
}