PhotoCraft: An open-source, photo editor written by scratch in pure Rust
test(io): cover PSD palette boundaries and bitmap row packing
Aditya Vishwakarmaff8dc65c8208
diff --git a/crates/io/src/palette_map.rs b/crates/io/src/palette_map.rsindex b268efcb9..a41cdc782 100644--- a/crates/io/src/palette_map.rs+++ b/crates/io/src/palette_map.rs@@ -124,3 +124,144 @@ fn bits(doc: &Document) -> Vec<u8> {});out}++#[cfg(test)]+mod tests {+ use super::*;+ use photocraft_color::{Color, SampleType};+ use photocraft_doc::Size;++ fn document(width: u32, height: u32, depth: SampleType, mode: ColorMode, pixels: &[f32]) -> Document {+ let mut doc = Document::with_background("palette test", Size::new(width, height), ColorMode::Rgb, depth, Color::rgba(0.0, 0.0, 0.0, 1.0));+ let bounds = doc.bounds();+ doc.layers[0].surface_mut().unwrap().write_region(bounds, pixels);+ doc.mode = mode;+ doc+ }++ fn round_trip(doc: &Document) -> (PsdFile, Document) {+ let (file, _) = document_to_psd(doc, false).unwrap();+ let bytes = file.to_bytes().unwrap();+ let file = PsdFile::from_bytes(&bytes).unwrap();+ let back = crate::import("test.psd", &bytes).unwrap().document;+ (file, back)+ }++ #[test]+ fn full_palette_endpoints_round_trip_with_either_transparent_endpoint() {+ let colors: Vec<_> = (0..=255).map(|v| [v, 255 - v, v / 2]).collect();+ for transparent in [0, 255] {+ let opaque = 255 - transparent;+ let rgb = colors[usize::from(opaque)].map(|v| f32::from(v) / 255.0);+ let mut doc = document(2, 1, SampleType::U8, ColorMode::Indexed, &[rgb[0], rgb[1], rgb[2], 1.0, 0.0, 0.0, 0.0, 0.0]);+ doc.color_table = Some(ColorTable { colors: colors.clone(), transparent: Some(transparent) });+ let (file, back) = round_trip(&doc);+ assert_eq!((file.header.color_mode, file.header.depth, file.header.channels), (PsdMode::Indexed, 8, 1));+ assert_eq!(file.decode_merged().unwrap(), [opaque, transparent]);+ assert_eq!(file.resource(COLOR_TABLE_COUNT).unwrap().data, [1, 0]);+ assert_eq!(file.color_mode_data.len(), 768);+ for (i, c) in colors.iter().enumerate() {+ assert_eq!([file.color_mode_data[i], file.color_mode_data[256 + i], file.color_mode_data[512 + i]], *c);+ }+ assert_eq!(back.color_table, doc.color_table);+ let surface = back.layers[0].surface().unwrap();+ assert_eq!(surface.pixel(0, 0), vec![rgb[0], rgb[1], rgb[2], 1.0]);+ assert_eq!(surface.pixel(1, 0)[3], 0.0);+ }+ }++ #[test]+ fn indexed_alpha_threshold_uses_stored_samples_at_all_depths() {+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {+ let mut doc = document(3, 1, depth, ColorMode::Indexed, &[0.0, 0.0, 0.0, 0.49, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.51]);+ doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: Some(1) });+ let (file, back) = round_trip(&doc);+ assert_eq!(file.decode_merged().unwrap(), [1, 0, 0], "{depth:?}");+ let surface = back.layers[0].surface().unwrap();+ assert_eq!((0..3).map(|x| surface.pixel(x, 0)[3]).collect::<Vec<_>>(), [0.0, 1.0, 1.0]);+ }+ }++ #[test]+ fn small_palette_zero_pads_unused_entries_and_rejects_invalid_lengths() {+ let mut doc = document(1, 1, SampleType::U8, ColorMode::Indexed, &[1.0, 0.0, 0.0, 1.0]);+ doc.color_table = Some(ColorTable { colors: vec![[255, 0, 0]], transparent: None });+ let (file, back) = round_trip(&doc);+ assert_eq!(file.color_mode_data[0], 255);+ assert!(file.color_mode_data[1..].iter().all(|&v| v == 0));+ assert_eq!(back.color_table, doc.color_table);+ for count in [0, 257] {+ doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0]; count], transparent: None });+ assert!(document_to_psd(&doc, false).is_none(), "{count}");+ }+ doc.color_table = None;+ assert!(document_to_psd(&doc, false).is_none());+ }++ #[test]+ fn indexed_import_bounds_malformed_count_and_transparency_resources() {+ let mut doc = document(1, 1, SampleType::U8, ColorMode::Indexed, &[0.0, 0.0, 0.0, 1.0]);+ doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: None });+ let (original, _) = document_to_psd(&doc, false).unwrap();+ for (count, expected) in [(vec![0, 0], 1), (vec![1, 1], 256), (vec![255, 255], 256), (vec![0], 256)] {+ let mut file = original.clone();+ file.resources.retain(|r| r.id != COLOR_TABLE_COUNT);+ file.resources.push(ImageResource::new(COLOR_TABLE_COUNT, count));+ file.resources.push(ImageResource::new(TRANSPARENCY_INDEX, vec![255, 255]));+ let bytes = file.to_bytes().unwrap();+ let back = crate::import("malformed.psd", &bytes).unwrap().document;+ let table = back.color_table.unwrap();+ assert_eq!(table.colors.len(), expected);+ assert_eq!(table.transparent, None);+ assert_eq!(back.layers[0].surface().unwrap().pixel(0, 0)[3], 1.0);+ }+ }++ #[test]+ fn bitmap_rows_are_msb_first_and_independently_byte_padded() {+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {+ for width in [1u32, 7, 8, 9, 15, 17] {+ // Opposite rows expose padding carried into the next row or reversed bit order.+ let pixels: Vec<_> = (0..2)+ .flat_map(|y| {+ (0..width).flat_map(move |x| {+ let v = if (x + y) % 2 == 0 { 0.0 } else { 1.0 };+ [v, v, v, 1.0]+ })+ })+ .collect();+ let doc = document(width, 2, depth, ColorMode::Bitmap, &pixels);+ let (file, back) = round_trip(&doc);+ assert_eq!((file.header.color_mode, file.header.depth, file.header.channels), (PsdMode::Bitmap, 1, 1));+ let mut expected = Vec::new();+ for y in 0..2 {+ for byte in 0..width.div_ceil(8) {+ let mut packed = 0;+ for bit in 0..8 {+ let x = byte * 8 + bit;+ if x < width && (x + y) % 2 == 0 {+ packed |= 0x80 >> bit;+ }+ }+ expected.push(packed);+ }+ }+ assert_eq!(file.decode_merged().unwrap(), expected, "{depth:?}, width={width}");+ assert!(file.color_mode_data.is_empty());+ assert_eq!(back.mode, ColorMode::Bitmap);+ let gray_alpha: Vec<_> = pixels.as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[3]]).collect();+ assert_eq!(back.layers[0].surface().unwrap().read_region(back.bounds()), gray_alpha);+ }+ }+ }++ #[test]+ fn bitmap_alpha_is_flattened_over_white_before_thresholding() {+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {+ let doc = document(3, 1, depth, ColorMode::Bitmap, &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.49, 0.0, 0.0, 0.0, 0.51]);+ let (file, back) = round_trip(&doc);+ assert_eq!(file.decode_merged().unwrap(), [0x20], "{depth:?}");+ assert_eq!(back.layers[0].surface().unwrap().read_region(back.bounds()), [1.0, 1.0, 1.0, 1.0, 0.0, 1.0]);+ }+ }+}