Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

PNG: name sTER modes by their specified eye order

Daniel Campos Ramos06896086191d
diff --git a/crates/codecs/src/codecs/png.rs b/crates/codecs/src/codecs/png.rs
index ede70d296..3fadc4eac 100644
--- a/crates/codecs/src/codecs/png.rs
+++ b/crates/codecs/src/codecs/png.rs
@@ -679,8 +679,8 @@ mod stereo_tests {
#[test]
fn reads_both_defined_views() {
- assert_eq!(stereo(&png_with(&[0])), Some(PngStereo::Horizontal));
- assert_eq!(stereo(&png_with(&[1])), Some(PngStereo::Vertical));
+ assert_eq!(stereo(&png_with(&[0])), Some(PngStereo::CrossFuse));
+ assert_eq!(stereo(&png_with(&[1])), Some(PngStereo::DivergingFuse));
assert_eq!(stereo(&png_with(&[])), None);
// Reserved and unknown mode bytes are not a pairing we can keep.
assert_eq!(stereo(&png_with(&[2])), None);
@@ -690,6 +690,24 @@ mod stereo_tests {
assert_eq!(stereo(b"not a png"), None);
}
+ #[test]
+ fn ster_mode_bytes_describe_eye_order_not_vertical_packing() {
+ // PNG Extensions 1.4.0 §3.6: both views occupy horizontal halves.
+ // Each view is eight columns, so the second starts on an eight-column boundary.
+ for (mode, left_pixel, right_pixel) in [(0, [0, 0, 255], [255, 0, 0]), (1, [255, 0, 0], [0, 0, 255])] {
+ let pixels: Vec<u8> = [left_pixel; 8].into_iter().chain([right_pixel; 8]).flatten().collect();
+ let img = Image::from_raw(16, 1, ChannelLayout::Rgb, SampleType::U8, pixels.clone()).unwrap();
+ let pairing = if mode == 0 { PngStereo::CrossFuse } else { PngStereo::DivergingFuse };
+ let opts = EncodeOptions { png_stereo: Some(pairing), ..Default::default() };
+ let bytes = crate::encode(&img, Format::Png, &opts).unwrap();
+ let chunk = bytes.windows(4).position(|w| w == b"sTER").unwrap();
+ assert_eq!(bytes[chunk + 4], mode, "the specification's literal mode byte");
+ let decoded = decode(&bytes, &Limits::default()).unwrap();
+ assert_eq!(decoded.dimensions(), (16, 1));
+ assert_eq!(decoded.data(), pixels);
+ }
+ }
+
#[test]
fn a_broken_crc_is_not_a_stereo_mark() {
let mut bytes = png_with(&[0]);
@@ -702,7 +720,7 @@ mod stereo_tests {
#[test]
fn encode_writes_the_chunk_before_idat_and_it_reads_back() {
let img = Image::from_raw(2, 1, ChannelLayout::Rgb, SampleType::U8, vec![9; 6]).unwrap();
- for view in [PngStereo::Horizontal, PngStereo::Vertical] {
+ for view in [PngStereo::CrossFuse, PngStereo::DivergingFuse] {
let opts = EncodeOptions { png_stereo: Some(view), ..Default::default() };
let bytes = crate::encode(&img, Format::Png, &opts).unwrap();
assert_eq!(stereo(&bytes), Some(view));
@@ -719,12 +737,12 @@ mod stereo_tests {
#[test]
fn interlaced_and_indexed_encoders_keep_the_mark() {
let img = Image::from_raw(2, 2, ChannelLayout::Rgb, SampleType::U8, vec![9; 12]).unwrap();
- let opts = EncodeOptions { png_stereo: Some(PngStereo::Vertical), png_interlaced: true, ..Default::default() };
+ let opts = EncodeOptions { png_stereo: Some(PngStereo::DivergingFuse), png_interlaced: true, ..Default::default() };
let bytes = crate::encode(&img, Format::Png, &opts).unwrap();
- assert_eq!(stereo(&bytes), Some(PngStereo::Vertical));
+ assert_eq!(stereo(&bytes), Some(PngStereo::DivergingFuse));
let pal = [[255, 0, 0], [0, 0, 255]];
- let bytes = encode_indexed(2, 1, &[0, 1], &pal, None, None, Some(PngStereo::Horizontal)).unwrap();
- assert_eq!(stereo(&bytes), Some(PngStereo::Horizontal));
+ let bytes = encode_indexed(2, 1, &[0, 1], &pal, None, None, Some(PngStereo::CrossFuse)).unwrap();
+ assert_eq!(stereo(&bytes), Some(PngStereo::CrossFuse));
assert_eq!(palette(&bytes).unwrap().map(|p| p.colors), Some(pal.to_vec()));
}
}
diff --git a/crates/codecs/src/options.rs b/crates/codecs/src/options.rs
index e6ca3cb94..430b412a9 100644
--- a/crates/codecs/src/options.rs
+++ b/crates/codecs/src/options.rs
@@ -103,18 +103,18 @@ pub enum PngCompression {
/// How a PNG's `sTER` chunk pairs its two views (PNG Extensions 1.4.0, §3.6).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PngStereo {
- /// Left and right views side by side, left first.
- Horizontal,
- /// Left and right views stacked, left on top.
- Vertical,
+ /// Side by side with the right-eye view first (cross-eyed viewing, mode 0).
+ CrossFuse,
+ /// Side by side with the left-eye view first (parallel viewing, mode 1).
+ DivergingFuse,
}
impl PngStereo {
/// The view for an `sTER` mode byte; reserved and unknown values are `None`.
pub fn from_mode(mode: u8) -> Option<Self> {
match mode {
- 0 => Some(Self::Horizontal),
- 1 => Some(Self::Vertical),
+ 0 => Some(Self::CrossFuse),
+ 1 => Some(Self::DivergingFuse),
_ => None,
}
}
@@ -122,8 +122,8 @@ impl PngStereo {
/// The `sTER` mode byte.
pub fn mode(self) -> u8 {
match self {
- Self::Horizontal => 0,
- Self::Vertical => 1,
+ Self::CrossFuse => 0,
+ Self::DivergingFuse => 1,
}
}
}
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index 268b0b6b4..8752bbd7b 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -933,7 +933,8 @@ pub struct FileEncoding {
#[serde(skip_serializing_if = "Option::is_none")]
pub webp_quality: Option<u8>,
/// PNG stereo pairing (the `sTER` chunk's mode byte, PNG Extensions 1.4.0 §3.6): `Some(0)`
- /// side by side, `Some(1)` stacked, so saving a stereo PNG keeps the mark (#6063).
+ /// cross-fuse (right eye first), `Some(1)` diverging-fuse (left eye first). Both are
+ /// side by side; saving a stereo PNG keeps this eye-order mark (#6063).
#[serde(skip_serializing_if = "Option::is_none")]
pub png_stereo: Option<u8>,
}
diff --git a/crates/io/tests/flat.rs b/crates/io/tests/flat.rs
index e591fbd5c..9dc27a45c 100644
--- a/crates/io/tests/flat.rs
+++ b/crates/io/tests/flat.rs
@@ -1119,7 +1119,7 @@ fn a_lab_document_with_one_layer_is_not_reported_as_flattened() {
fn a_stereo_png_keeps_its_stereo_mark_on_save() {
use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image, PngStereo, SampleType as CS};
let img = Image::from_raw(4, 2, ChannelLayout::Rgb, CS::U8, vec![40u8; 24]).unwrap();
- for view in [PngStereo::Horizontal, PngStereo::Vertical] {
+ for view in [PngStereo::CrossFuse, PngStereo::DivergingFuse] {
let bytes = photocraft_codecs::encode(&img, Format::Png, &EncodeOptions { png_stereo: Some(view), ..Default::default() }).unwrap();
let doc = import("stereo.png", &bytes).unwrap().document;
assert_eq!(doc.file_encoding.png_stereo, Some(view.mode()));