mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
PNG: name sTER modes by their specified eye order
Both sTER modes describe side-by-side subimages: mode 0 is cross-fuse (right eye first), and mode 1 is diverging-fuse (left eye first). Correct the enum and document comments without changing their serialized byte values. Add a labelled-pixel check against the literal mode bytes in PNG Extensions 1.4.0 section 3.6. Co-Authored-By: OpenAI Codex <[email protected]>
This commit is contained in:
1 parent
f4a81d3c16
commit
0689608619
4 files changed
+36
-17
No files matched your search
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>,
|
||||
}
|
||||
|
||||
@@ -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()));
|
||||
|
||||
Reference in new issue
Block a user