Skip to content

Commit

Permalink
Reuse encoder::write_chunk from test_utils::write_chunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
anforowicz committed Nov 14, 2023
1 parent 2869646 commit dfc989e
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
//! A set of test utilities.
//!
//! There is some overlap between the `test_utils::write_...` functions and the code in the
//! `encoder.rs` module. The main difference is that the `test_utils` function perform no
//! validation - this allows testcases to construct arbitrary, potentially invalid PNG inputs.
//! OTOH, we should try to reuse code whenever possible - e.g. `test_utils::write_chunk` is just a
//! thin wrapper around `encoder::write_chunk`.

use byteorder::WriteBytesExt;
use std::convert::TryInto;
use std::io::Write;

/// Generates a store-only, non-compressed image:
Expand Down Expand Up @@ -37,20 +44,12 @@ pub fn write_png_sig(w: &mut impl Write) {

/// Writes an arbitrary PNG chunk.
pub fn write_chunk(w: &mut impl Write, chunk_type: &[u8], data: &[u8]) {
assert_eq!(chunk_type.len(), 4);
let crc = {
let input = chunk_type
.iter()
.copied()
.chain(data.iter().copied())
.collect::<Vec<_>>();
crc32fast::hash(input.as_slice())
};
w.write_u32::<byteorder::BigEndian>(data.len() as u32)
.unwrap();
w.write_all(chunk_type).unwrap();
w.write_all(data).unwrap();
w.write_u32::<byteorder::BigEndian>(crc).unwrap();
let chunk_type = crate::chunk::ChunkType(
chunk_type
.try_into()
.expect("`chunk_type` needs to be 4 bytes long"),
);
crate::encoder::write_chunk(w, chunk_type, data).unwrap();
}

/// Writes an IHDR chunk that indicates a non-interlaced RGBA8 that uses the same height and
Expand Down

0 comments on commit dfc989e

Please sign in to comment.