1 //! The png framing, the tile split, and the frame length checks.2 3 use inf3r_image::{Format, Frame, ImageError, encode, one_pixel, split};4 5 /// The eight bytes at the front of a png.6 const SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];7 8 /// One chunk: type, data, and the CRC as it sits in the file.9 fn chunks(png: &[u8]) -> Vec<([u8; 4], &[u8], u32)> {10 let mut out = Vec::new();11 let mut at = SIGNATURE.len();12 while at < png.len() {13 let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize;14 let kind: [u8; 4] = png[at + 4..at + 8].try_into().unwrap();15 let data = &png[at + 8..at + 8 + len];16 let crc = u32::from_be_bytes(png[at + 8 + len..at + 12 + len].try_into().unwrap());17 out.push((kind, data, crc));18 at += 12 + len;19 }20 out21 }22 23 /// The CRC-32 from the bytes alone. The encoder is not consulted.24 fn crc32(bytes: impl Iterator<Item = u8>) -> u32 {25 let mut crc = 0xFFFF_FFFFu32;26 for byte in bytes {27 crc ^= u32::from(byte);28 for _ in 0..8 {29 crc = if crc & 1 == 0 {30 crc >> 131 } else {32 0xEDB8_8320 ^ (crc >> 1)33 };34 }35 }36 !crc37 }38 39 /// Decodes the stored blocks of a zlib stream. The header and Adler-32 are40 /// skipped; the point is the block framing.41 fn inflate_stored(zlib: &[u8]) -> Vec<u8> {42 let mut at = 2;43 let mut out = Vec::new();44 loop {45 let header = zlib[at];46 at += 1;47 assert_eq!(header & 0b110, 0, "the block type is stored");48 let len = u16::from_le_bytes([zlib[at], zlib[at + 1]]);49 let nlen = u16::from_le_bytes([zlib[at + 2], zlib[at + 3]]);50 at += 4;51 assert_eq!(len ^ nlen, u16::MAX, "the length has its complement");52 out.extend_from_slice(&zlib[at..at + usize::from(len)]);53 at += usize::from(len);54 if header & 1 != 0 {55 break;56 }57 }58 out59 }60 61 #[test]62 fn rgb_stride_and_pixel() {63 let frame = Frame::new(64 Format::Rgb8,65 2,66 2,67 vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],68 )69 .unwrap();70 assert_eq!(frame.stride(), 6);71 assert_eq!(frame.pixel(1, 0), Some(&[4, 5, 6][..]));72 assert_eq!(frame.pixel(2, 0), None);73 }74 75 #[test]76 fn short_frame_is_size() {77 let result = Frame::new(Format::Rgb8, 2, 2, vec![0; 11]);78 assert!(matches!(result, Err(ImageError::Size)));79 }80 81 #[test]82 fn split_keeps_the_edge_short() {83 let frame = Frame::new(Format::Gray8, 10, 10, vec![0; 100]).unwrap();84 let tiles = split(&frame, 4, 4);85 assert_eq!(tiles.len(), 9);86 assert_eq!(87 tiles[8],88 inf3r_image::Tile {89 x: 8,90 y: 8,91 w: 2,92 h: 293 }94 );95 }96 97 #[test]98 fn one_pixel_has_the_four_chunks() {99 let png = one_pixel();100 assert!(png.starts_with(&SIGNATURE));101 let chunks = chunks(&png);102 let kinds: Vec<&[u8; 4]> = chunks.iter().map(|(kind, _, _)| kind).collect();103 assert_eq!(kinds, [b"IHDR", b"IDAT", b"IEND"]);104 assert!(png.ends_with(&[0xae, 0x42, 0x60, 0x82]));105 let total: usize = SIGNATURE.len()106 + chunks107 .iter()108 .map(|(_, data, _)| 12 + data.len())109 .sum::<usize>();110 assert_eq!(total, png.len());111 }112 113 #[test]114 fn gray_idat_holds_the_filter_byte_and_the_pixel() {115 let frame = Frame::new(Format::Gray8, 1, 1, vec![0x7f]).unwrap();116 let png = encode(&frame).unwrap();117 let idat = chunks(&png)118 .into_iter()119 .find(|(kind, _, _)| kind == b"IDAT")120 .unwrap()121 .1;122 assert_eq!(inflate_stored(idat), [0x00, 0x7f]);123 }124 125 #[test]126 fn ihdr_crc_is_a_known_constant() {127 let frame = Frame::new(Format::Rgb8, 1, 1, vec![0, 0, 0]).unwrap();128 let png = encode(&frame).unwrap();129 let (_, data, stored) = chunks(&png)[0];130 let computed = crc32(b"IHDR".iter().copied().chain(data.iter().copied()));131 assert_eq!(computed, 0x9077_53de);132 assert_eq!(stored, computed);133 }