forge
Explore
Log in

Don't you dare go Hollow

U20: ec-image drop open_info, to_rgb16, to_rgba16 (edith uses none; differential test gets its own 16-bit RGBA helper, same mapping)

Browse files at this commit

58f8d8f6acfc57dbcf59b4deb772e30194e4ae95Ahmet Kaan GümüşParent: 4e4fcb8
crates/ec-image/src/lib.rsmodified+1 -40
@@ -333,11 +333,6 @@ fn narrow(v: u16) -> u8 {
333333 ((u32::from(v) + 128) / 257) as u8
334334 }
335335
336-/// 8-bit sample widened to 16: `v * 65535 / 255`, exact.
337-fn widen(v: u8) -> u16 {
338- u16::from(v) * 257
339-}
340-
341336 impl Image {
342337 /// `(width, height)`.
343338 pub fn dimensions(&self) -> (u32, u32) {
@@ -404,34 +399,6 @@ impl Image {
404399 out
405400 }
406401
407- /// Packed 16-bit R,G,B — 8-bit sources widened exactly (`v * 257`).
408- pub fn to_rgb16(&self) -> Vec<u16> {
409- match &self.pixels {
410- Pixels::Rgb16(d) => d.clone(),
411- Pixels::Rgba16(d) => d.as_chunks::<4>().0.iter().flat_map(|p| p[..3].to_vec()).collect(),
412- Pixels::L16(d) => d.iter().flat_map(|&l| [l, l, l]).collect(),
413- Pixels::La16(d) => d.as_chunks::<2>().0.iter().flat_map(|p| [p[0], p[0], p[0]]).collect(),
414- _ => self.to_rgb8().into_iter().map(widen).collect(),
415- }
416- }
417-
418- /// Packed 16-bit R,G,B,A; opaque where the source had no alpha.
419- pub fn to_rgba16(&self) -> Vec<u16> {
420- match &self.pixels {
421- Pixels::Rgba16(d) => d.clone(),
422- Pixels::Rgb16(d) => d
423- .as_chunks::<3>().0.iter()
424- .flat_map(|p| [p[0], p[1], p[2], u16::MAX])
425- .collect(),
426- Pixels::L16(d) => d.iter().flat_map(|&l| [l, l, l, u16::MAX]).collect(),
427- Pixels::La16(d) => d
428- .as_chunks::<2>().0.iter()
429- .flat_map(|p| [p[0], p[0], p[0], p[1]])
430- .collect(),
431- _ => self.to_rgba8().into_iter().map(widen).collect(),
432- }
433- }
434-
435402 /// The image as an [`ec_core::VideoFrame`], packed RGB8 or RGBA8.
436403 ///
437404 /// The natural bridge for a caller that composes stills and decoded video
@@ -552,12 +519,6 @@ pub fn open(path: impl AsRef<std::path::Path>) -> Result<Image> {
552519 decode(&data)
553520 }
554521
555-/// Read the file at `path` and report its header dimensions.
556-pub fn open_info(path: impl AsRef<std::path::Path>) -> Result<Info> {
557- let data = std::fs::read(path).map_err(Error::Io)?;
558- info(&data)
559-}
560-
561522 #[cfg(test)]
562523 mod tests {
563524 use super::*;
@@ -582,7 +543,7 @@ mod tests {
582543 #[test]
583544 fn sample_conversion_round_trips_through_eight_bits() {
584545 for v in 0..=255u8 {
585- assert_eq!(narrow(widen(v)), v, "{v}");
546+ assert_eq!(narrow(u16::from(v) * 257), v, "{v}");
586547 }
587548 assert_eq!(narrow(0), 0);
588549 assert_eq!(narrow(u16::MAX), 255);
crates/ec-image/tests/it/differential.rsmodified+12 -1
@@ -85,6 +85,17 @@ fn psnr(a: &[u8], b: &[u8]) -> f64 {
8585 }
8686 }
8787
88+fn rgba16(img: &ec_image::Image) -> Vec<u16> {
89+ use ec_image::Pixels;
90+ match &img.pixels {
91+ Pixels::L16(d) => d.iter().flat_map(|&l| [l, l, l, u16::MAX]).collect(),
92+ Pixels::La16(d) => d.as_chunks::<2>().0.iter().flat_map(|p| [p[0], p[0], p[0], p[1]]).collect(),
93+ Pixels::Rgb16(d) => d.as_chunks::<3>().0.iter().flat_map(|p| [p[0], p[1], p[2], u16::MAX]).collect(),
94+ Pixels::Rgba16(d) => d.clone(),
95+ _ => unreachable!("only called for 16-bit pictures"),
96+ }
97+}
98+
8899 #[test]
89100 fn png_decodes_pixel_exactly() {
90101 let files = corpus("png");
@@ -102,7 +113,7 @@ fn png_decodes_pixel_exactly() {
102113 if ours.pixels.bit_depth() == 16 {
103114 // Compared at 16 bits so the comparison is not laundered through
104115 // two different 16-to-8 rounding conventions.
105- let a = ours.to_rgba16();
116+ let a = rgba16(&ours);
106117 let b = theirs.to_rgba16().into_raw();
107118 assert_eq!(a, b, "{} (16-bit samples)", path.display());
108119 } else {