Skip to main content

pdf2_render/
png.rs

1//! PNG encoding: RGBA8, filter 0, zlib through `miniz_oxide`.
2//!
3//! The goldens are compared byte-for-byte, so the encoder must be
4//! deterministic — which a fixed filter choice, no ancillary chunks and a
5//! fixed compression level give.
6
7use crate::canvas::Canvas;
8
9const SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
10
11/// Encode the canvas as a PNG.
12pub fn encode_png(canvas: &Canvas) -> Vec<u8> {
13    let width = canvas.width();
14    let height = canvas.height();
15    let mut raw = Vec::with_capacity((width as usize * 4 + 1) * height as usize);
16    for row in 0..height {
17        raw.push(0);
18        let start = row as usize * width as usize * 4;
19        let end = start + width as usize * 4;
20        raw.extend_from_slice(&canvas.pixels()[start..end]);
21    }
22    let compressed = miniz_oxide::deflate::compress_to_vec_zlib(&raw, 6);
23
24    let mut png = Vec::new();
25    png.extend_from_slice(&SIGNATURE);
26    let mut header = Vec::with_capacity(13);
27    header.extend_from_slice(&width.to_be_bytes());
28    header.extend_from_slice(&height.to_be_bytes());
29    header.extend_from_slice(&[8, 6, 0, 0, 0]);
30    write_chunk(&mut png, b"IHDR", &header);
31    write_chunk(&mut png, b"IDAT", &compressed);
32    write_chunk(&mut png, b"IEND", &[]);
33    png
34}
35
36fn write_chunk(out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
37    out.extend_from_slice(&(data.len() as u32).to_be_bytes());
38    out.extend_from_slice(kind);
39    out.extend_from_slice(data);
40    let mut crc_input = Vec::with_capacity(4 + data.len());
41    crc_input.extend_from_slice(kind);
42    crc_input.extend_from_slice(data);
43    out.extend_from_slice(&crc32(&crc_input).to_be_bytes());
44}
45
46fn crc32(data: &[u8]) -> u32 {
47    let mut crc = 0xffff_ffff_u32;
48    for byte in data {
49        crc ^= u32::from(*byte);
50        for _ in 0..8 {
51            let mask = (crc & 1).wrapping_neg();
52            crc = (crc >> 1) ^ (0xedb8_8320 & mask);
53        }
54    }
55    !crc
56}
57
58#[cfg(test)]
59mod tests {
60    use super::{crc32, encode_png};
61    use crate::canvas::{Canvas, Color};
62
63    #[test]
64    fn crc32_matches_the_known_check_value() {
65        // The standard check value: CRC-32 of "123456789".
66        assert_eq!(crc32(b"123456789"), 0xcbf4_3926);
67    }
68
69    #[test]
70    fn a_png_has_the_expected_framing() {
71        let canvas = Canvas::new(2, 1, Color::BLACK);
72        let png = encode_png(&canvas);
73        assert_eq!(&png[..8], b"\x89PNG\r\n\x1a\n");
74        // IHDR length is always 13 and comes first.
75        assert_eq!(&png[8..12], &13_u32.to_be_bytes());
76        assert_eq!(&png[12..16], b"IHDR");
77        assert_eq!(&png[16..20], &2_u32.to_be_bytes(), "width");
78        assert_eq!(&png[20..24], &1_u32.to_be_bytes(), "height");
79        assert!(png.ends_with(b"IEND\xaeB`\x82"), "trailing IEND chunk");
80    }
81
82    #[test]
83    fn encoding_is_deterministic() {
84        let canvas = Canvas::new(3, 3, Color::rgb(0.25, 0.5, 0.75));
85        assert_eq!(encode_png(&canvas), encode_png(&canvas));
86    }
87}