Debian Codegraph @

.cargo/vendor/png/src/test_utils.rs

raw
1 //! A set of test utilities.
2 //!
3 //! There is some overlap between this module and `src/encoder.rs` module, but:
4 //!
5 //! * This module (unlike `src/encoder.rs`) performs no validation of the data being written - this
6 //! allows building testcases that use arbitrary, potentially invalid PNGs as input.
7 //! * This module can be reused from `benches/decoder.rs` (a separate crate).
8
9 use byteorder::WriteBytesExt;
10 use std::io::Write;
11
12 /// Generates a store-only, non-compressed image:
13 ///
14 /// * `00` compression mode (i.e.`BTYPE` = `00` = no compression) is used
15 /// * No filter is applied to the image rows
16 ///
17 /// Currently the image always has the following properties:
18 ///
19 /// * Single `IDAT` chunk
20 /// * Zlib chunks of maximum possible size
21 /// * 8-bit RGBA
22 ///
23 /// These images are somewhat artificial, but may be useful for benchmarking performance of parts
24 /// outside of `fdeflate` crate and/or the `unfilter` function (e.g. these images were originally
25 /// used to evaluate changes to minimize copying of image pixels between various buffers - see
26 /// [this
27 /// discussion](https://github.com/image-rs/image-png/discussions/416#discussioncomment-7436871)
28 /// for more details).
29 pub fn write_noncompressed_png(w: &mut impl Write, size: u32, idat_bytes: usize) {
30 write_png_sig(w);
31 write_rgba8_ihdr_with_width(w, size);
32 write_rgba8_idats(w, size, idat_bytes);
33 write_iend(w);
34 }
35
36 /// Writes PNG signature.
37 /// See http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html#PNG-file-signature
38 pub fn write_png_sig(w: &mut impl Write) {
39 const SIG: [u8; 8] = [137, 80, 78, 71, 13, 10, 26, 10];
40 w.write_all(&SIG).unwrap();
41 }
42
43 /// Writes an arbitrary PNG chunk.
44 pub fn write_chunk(w: &mut impl Write, chunk_type: &[u8], data: &[u8]) {
45 assert_eq!(chunk_type.len(), 4);
46 let crc = {
47 let input = chunk_type
48 .iter()
49 .copied()
50 .chain(data.iter().copied())
51 .collect::<Vec<_>>();
52 crc32fast::hash(input.as_slice())
53 };
54 w.write_u32::<byteorder::BigEndian>(data.len() as u32)
55 .unwrap();
56 w.write_all(chunk_type).unwrap();
57 w.write_all(data).unwrap();
58 w.write_u32::<byteorder::BigEndian>(crc).unwrap();
59 }
60
61 /// Writes an IHDR chunk that indicates a non-interlaced RGBA8 that uses the same height and
62 /// `width`. See http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.IHDR
63 pub fn write_rgba8_ihdr_with_width(w: &mut impl Write, width: u32) {
64 let mut data = Vec::new();
65 data.write_u32::<byteorder::BigEndian>(width).unwrap();
66 data.write_u32::<byteorder::BigEndian>(width).unwrap(); // height
67 data.write_u8(8).unwrap(); // bit depth = always 8-bits per channel
68 data.write_u8(6).unwrap(); // color type = color + alpha
69 data.write_u8(0).unwrap(); // compression method (0 is the only allowed value)
70 data.write_u8(0).unwrap(); // filter method (0 is the only allowed value)
71 data.write_u8(0).unwrap(); // interlace method = no interlacing
72 write_chunk(w, b"IHDR", &data);
73 }
74
75 /// Generates RGBA8 `width` x `height` image and wraps it in a store-only zlib container.
76 pub fn generate_rgba8_with_width_and_height(width: u32, height: u32) -> Vec<u8> {
77 // Generate arbitrary test pixels.
78 let image_pixels = {
79 let mut row = Vec::new();
80 row.write_u8(0).unwrap(); // filter = no filter
81
82 let row_pixels = (0..width).flat_map(|i| {
83 let color: u8 = (i * 255 / width) as u8;
84 let alpha: u8 = 0xff;
85 [color, 255 - color, color / 2, alpha]
86 });
87 row.extend(row_pixels);
88
89 std::iter::repeat(row)
90 .take(height as usize)
91 .flatten()
92 .collect::<Vec<_>>()
93 };
94
95 let mut zlib_data = Vec::new();
96 let mut store_only_compressor =
97 fdeflate::StoredOnlyCompressor::new(std::io::Cursor::new(&mut zlib_data)).unwrap();
98 store_only_compressor.write_data(&image_pixels).unwrap();
99 store_only_compressor.finish().unwrap();
100
101 zlib_data
102 }
103
104 /// Writes an IDAT chunk.
105 pub fn write_rgba8_idats(w: &mut impl Write, size: u32, idat_bytes: usize) {
106 let data = generate_rgba8_with_width_and_height(size, size);
107
108 for chunk in data.chunks(idat_bytes) {
109 write_chunk(w, b"IDAT", chunk);
110 }
111 }
112
113 /// Writes an IEND chunk.
114 /// See http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.IEND
115 pub fn write_iend(w: &mut impl Write) {
116 write_chunk(w, b"IEND", &[]);
117 }