fix(codecs): adapt migrated regression tests to shared public APIs

This commit is contained in:
Dylan M. Taylor
2026-11-14 23:25:19 +00:00
parent d97cb31bb6
commit db05483915
4 changed files with 18 additions and 22 deletions
Generated
+1 -2
View File
@@ -6908,7 +6908,6 @@ dependencies = [
"image",
"jpeg-encoder",
"log",
"png 0.18.1",
"proptest",
"serde_json",
"unicode-general-category",
@@ -7032,7 +7031,7 @@ dependencies = [
name = "vectorcraft-trace"
version = "0.24.1"
dependencies = [
"image",
"craft-image-codecs",
"kurbo 0.13.1",
"serde",
"serde_json",
+3 -6
View File
@@ -232,12 +232,9 @@ fn maximum_editability_writes_groups_and_an_object_per_layer() {
assert_eq!((p.records[8].blend, p.records[8].opacity), (*b"pass", 255));
assert_eq!(p.records[6].rect, [0, 0, 6, 6]);
let psd = craft_psd::PsdFile::from_bytes(&file).unwrap();
let content_layers = psd.layers().iter().filter(|l| matches!(l.section_type(), craft_psd::layer::SectionType::Other)).count();
let groups = psd
.layers()
.iter()
.filter(|l| matches!(l.section_type(), craft_psd::layer::SectionType::OpenFolder | craft_psd::layer::SectionType::ClosedFolder))
.count();
let content_layers = psd.layers().iter().filter(|l| matches!(l.section_type(), craft_psd::SectionType::Other)).count();
let groups =
psd.layers().iter().filter(|l| matches!(l.section_type(), craft_psd::SectionType::OpenFolder | craft_psd::SectionType::ClosedFolder)).count();
assert_eq!((content_layers, groups), (3, 3));
assert_eq!(merged(&file), merged(&export(&doc, &opts(PsdOptions::default()))), "the same merged image");
+13 -13
View File
@@ -6,20 +6,20 @@ use craft_tiff::tags;
use super::jpeg::ColorModel;
use super::tiff::{self as t, ByteOrder, Compression, Image, Layout, Photometric, TiffOptions};
fn ifd0(file: &[u8]) -> craft_tiff::reader::Ifd {
fn ifd0(file: &[u8]) -> craft_tiff::Ifd {
Tiff::parse(file).expect("a TIFF").ifds.into_iter().next().expect("IFD0")
}
fn tag_u16_vec(ifd: &craft_tiff::reader::Ifd, tag: u16) -> Option<Vec<u16>> {
fn tag_u16_vec(ifd: &craft_tiff::Ifd, tag: u16) -> Option<Vec<u16>> {
ifd.value(tag).map(|v| v.to_u64_vec().into_iter().map(|x| x as u16).collect())
}
fn tag_u32_vec(ifd: &craft_tiff::reader::Ifd, tag: u16) -> Option<Vec<u32>> {
fn tag_u32_vec(ifd: &craft_tiff::Ifd, tag: u16) -> Option<Vec<u32>> {
ifd.value(tag).map(|v| v.to_u64_vec().into_iter().map(|x| x as u32).collect())
}
fn pixels(file: &[u8]) -> Vec<u8> {
craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, file).expect("decodes").data
craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, file).expect("decodes").into_data()
}
/// Straight RGBA: flat areas (as art is) with a transparent corner when `clear`.
@@ -69,7 +69,7 @@ fn lzw_is_smaller_and_decodes_the_same() {
assert_eq!(pixels(&lzw), px, "transparency kept as an alpha channel");
assert_eq!(pixels(&plain), px);
let img = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, &lzw).unwrap();
assert_eq!(img.data, px);
assert_eq!(img.data(), px);
}
#[test]
@@ -77,14 +77,14 @@ fn colour_models_write_their_samples() {
let px = art(9, 6, true);
let rgba = t::encode_samples(&px, 9, 6, 72.0, &opts(ColorModel::Rgb)).unwrap();
let rgba_ifd = ifd0(&rgba);
assert_eq!(rgba_ifd.u16(tags::PHOTOMETRIC_INTERPRETATION), Some(2));
assert_eq!(rgba_ifd.u16(tags::PHOTOMETRIC), Some(2));
assert_eq!(rgba_ifd.u16(tags::SAMPLES_PER_PIXEL), Some(4));
assert_eq!(tag_u16_vec(&rgba_ifd, tags::BITS_PER_SAMPLE).as_deref(), Some([8, 8, 8, 8].as_slice()));
assert_eq!(tag_u16_vec(&rgba_ifd, tags::EXTRA_SAMPLES).as_deref(), Some([2].as_slice()), "unassociated alpha");
let opaque = art(9, 6, false);
let rgb = t::encode_samples(&opaque, 9, 6, 72.0, &opts(ColorModel::Rgb)).unwrap();
let rgb_ifd = ifd0(&rgb);
assert_eq!(rgb_ifd.u16(tags::PHOTOMETRIC_INTERPRETATION), Some(2));
assert_eq!(rgb_ifd.u16(tags::PHOTOMETRIC), Some(2));
assert_eq!(rgb_ifd.u16(tags::SAMPLES_PER_PIXEL), Some(3));
assert!(rgb_ifd.value(tags::EXTRA_SAMPLES).is_none());
@@ -92,7 +92,7 @@ fn colour_models_write_their_samples() {
let gray_ifd = ifd0(&gray);
assert_eq!(gray_ifd.u16(tags::SAMPLES_PER_PIXEL), Some(1));
assert_eq!(tag_u16_vec(&gray_ifd, tags::BITS_PER_SAMPLE).as_deref(), Some([8].as_slice()));
assert_eq!(gray_ifd.u16(tags::PHOTOMETRIC_INTERPRETATION), Some(1), "black is zero");
assert_eq!(gray_ifd.u16(tags::PHOTOMETRIC), Some(1), "black is zero");
let g = pixels(&gray);
assert_eq!((g[0], g[8]), (super::jpeg::to_gray()([200, 30, 40]), 255), "lightness; white stays white");
let on_white = t::encode_samples(&px, 9, 6, 72.0, &opts(ColorModel::Gray)).unwrap();
@@ -105,10 +105,10 @@ fn colour_models_write_their_samples() {
let cmyk = t::encode_samples(&inks, 9, 6, 72.0, &opts(ColorModel::Cmyk)).unwrap();
let d = ifd0(&cmyk);
assert_eq!(d.u16(tags::SAMPLES_PER_PIXEL), Some(4));
assert_eq!(d.u16(tags::PHOTOMETRIC_INTERPRETATION), Some(5), "separated");
assert_eq!(d.u16(tags::PHOTOMETRIC), Some(5), "separated");
assert_eq!(d.u16(332), Some(1), "CMYK inks");
let cmyk_img = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, &cmyk).unwrap();
assert_eq!(cmyk_img.data, inks);
assert_eq!(cmyk_img.data(), inks);
}
#[test]
@@ -118,7 +118,7 @@ fn resolution_tags_and_the_profile() {
let file = t::encode_samples(&px, 4, 4, ppi, &opts(ColorModel::Rgb)).unwrap();
let d = ifd0(&file);
for tag in [tags::X_RESOLUTION, tags::Y_RESOLUTION] {
let Some(craft_tiff::reader::IfdValue::Rationals(v)) = d.value(tag) else { panic!("rational") };
let Some(craft_tiff::Value::Rational(v)) = d.value(tag) else { panic!("rational") };
assert_eq!(v.as_slice(), &[rational]);
}
assert_eq!(d.u16(tags::RESOLUTION_UNIT), Some(2), "inches");
@@ -169,8 +169,8 @@ fn packbits_and_bilevel_images_decode() {
assert_eq!(d.u16(tags::COMPRESSION), Some(32773));
// The decoder turns WhiteIsZero into black-is-zero.
let dec = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, &file).unwrap();
assert_eq!(dec.width, 10);
assert_eq!(dec.height, 3);
assert_eq!(dec.width(), 10);
assert_eq!(dec.height(), 3);
// A buffer that doesn't match the size is refused, not written.
let short = Image { data: &rows[..5], ..image };
assert!(t::write(&short, &Layout { byte_order: ByteOrder::Little, compression: Compression::None, ppi: 72.0, icc: None }).is_err());
+1 -1
View File
@@ -313,7 +313,7 @@ mod tests {
}
let rgba = Image::from_u8(4, 4, ChannelLayout::Rgba, rgba_bytes).unwrap();
let luma_a = Image::from_u8(4, 4, ChannelLayout::GrayA, luma_a_bytes).unwrap();
let rgba16 = Image::new(4, 4, ChannelLayout::Rgba, SampleType::U16, rgba16_bytes).unwrap();
let rgba16 = Image::from_raw(4, 4, ChannelLayout::Rgba, SampleType::U16, rgba16_bytes).unwrap();
for (name, img) in [("rgba", rgba), ("gray and alpha", luma_a), ("16 bits", rgba16)] {
let got = decode_alpha(&encoded(&img, Format::Png)).unwrap();
assert_eq!(got, Some((4, 4, want.clone())), "{name}");