Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/codecs/tiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,26 +553,30 @@ fn ycbcr_to_rgb8(ycbcr: &[[u8; 3]], lr: f32, lg: f32, lb: f32, out: &mut [[u8; 3
}

fn cmyk_to_rgb(cmyk: &[u8; 4]) -> [u8; 3] {
let c = f32::from(cmyk[0]);
let m = f32::from(cmyk[1]);
let y = f32::from(cmyk[2]);
let kf = 1. - f32::from(cmyk[3]) / 255.;
let c = cmyk[0] as u32;
let m = cmyk[1] as u32;
let y = cmyk[2] as u32;
let k = cmyk[3] as u32;

let k_inv = 255 - k;
[
((255. - c) * kf) as u8,
((255. - m) * kf) as u8,
((255. - y) * kf) as u8,
(((255 - c) * k_inv) / 255) as u8,
(((255 - m) * k_inv) / 255) as u8,
(((255 - y) * k_inv) / 255) as u8,
Comment on lines +563 to +565
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's making a good point that as used floor semantics but wouldn't we prefer rounding here? Then a bias term for 127 should be added here before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Adding a bias term is mathematically more accurate for rounding, but it again introduced pixel differences when testing against libtiff (which uses truncation by default). I have added another test case for this specific truncation behavior:

  • CMYK: [1, 1, 1, 128] with intermediate values (255 - c) = 254 and k_inv = 127
  • New logic without bias term: (254 * 127) / 255 = 32258 / 255 = 126 (integer division naturally truncates to 126)
  • With bias term: (254 * 127 + 127) / 255 = 32385 / 255 = 127 (rounds up to 127)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@197g Do you think this approach is worth pursuing?

]
}

fn cmyk_to_rgb16(cmyk: &[u16; 4]) -> [u16; 3] {
let c = f32::from(cmyk[0]);
let m = f32::from(cmyk[1]);
let y = f32::from(cmyk[2]);
let kf = 1. - f32::from(cmyk[3]) / 65535.;
let c = cmyk[0] as u64;
let m = cmyk[1] as u64;
let y = cmyk[2] as u64;
let k = cmyk[3] as u64;

let k_inv = 65535 - k;
[
((65535. - c) * kf) as u16,
((65535. - m) * kf) as u16,
((65535. - y) * kf) as u16,
(((65535 - c) * k_inv) / 65535) as u16,
(((65535 - m) * k_inv) / 65535) as u16,
(((65535 - y) * k_inv) / 65535) as u16,
]
}

Expand Down
34 changes: 34 additions & 0 deletions tests/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,37 @@ fn test_decode_8bit_ycbcr_lzw_invalid_coefficients() {
let result = TiffDecoder::new(std::io::Cursor::new(data));
assert!(result.is_err());
}

#[cfg(feature = "tiff")]
#[test]
fn test_decode_8bit_cmyk() -> Result<(), image::ImageError> {
let img_path = PathBuf::from("tests/images/tiff/testsuite/cmyk_u8_edge_case.tif");
let data = fs::read(img_path).expect("Test image missing");

let tiff_decoder = TiffDecoder::new(std::io::Cursor::new(data))?;

assert_eq!(tiff_decoder.color_type(), image::ColorType::Rgb8);

let mut buffer = vec![0u8; tiff_decoder.total_bytes() as usize];
tiff_decoder.read_image(&mut buffer)?;
assert_eq!(buffer, vec![190, 190, 190]);

Ok(())
}

#[cfg(feature = "tiff")]
#[test]
fn test_decode_8bit_cmyk_truncation() -> Result<(), image::ImageError> {
let img_path = PathBuf::from("tests/images/tiff/testsuite/cmyk_u8_trunc_case.tif");
let data = fs::read(img_path).expect("Test image missing");

let tiff_decoder = TiffDecoder::new(std::io::Cursor::new(data))?;

assert_eq!(tiff_decoder.color_type(), image::ColorType::Rgb8);

let mut buffer = vec![0u8; tiff_decoder.total_bytes() as usize];
tiff_decoder.read_image(&mut buffer)?;
assert_eq!(buffer, vec![126, 126, 126]);

Ok(())
}
Binary file added tests/images/tiff/testsuite/cmyk_u8_edge_case.tif
Binary file not shown.
Binary file added tests/images/tiff/testsuite/cmyk_u8_trunc_case.tif
Binary file not shown.
Loading