Skip to content

Commit

Permalink
Fix i1 png reading? & tests (#17)
Browse files Browse the repository at this point in the history
* fix i1 png reading
  • Loading branch information
bates64 authored Jan 8, 2024
1 parent b45a4f8 commit fcfe6c0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pigment64"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
description = "A library for handling conversion between N64 texture formats and modern image formats"
repository = "https://github.com/decompals/pigment64"
Expand Down
15 changes: 10 additions & 5 deletions src/image/png_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ impl PNGImage {
if let (ColorType::Grayscale, BitDepth::One) = (self.color_type, self.bit_depth) {
writer.write_all(&self.data).map_err(Into::into)
} else {
let mut i8_data = vec![0; self.data.len() * 8];
self.as_i8(&mut i8_data.as_mut_slice())?;
// Convert to i8 and then convert to i1
let mut i8_data = Vec::new();
self.as_i8(&mut i8_data)?;

for chunk in i8_data.chunks_exact(8) {
for pixels in i8_data.chunks_exact(8) {
// Combine the 8 pixels into a single byte
let mut byte = 0;
for (i, pixel) in chunk.iter().copied().enumerate() {
byte |= ((pixel > 127) as u8) << (7 - i);
for (i, pixel) in pixels.iter().copied().enumerate() {
// If its intensity is over half, set the bit
if pixel > u8::MAX / 2 {
byte |= 1 << (7 - i);
}
}
writer.write_u8(byte)?;
}
Expand Down
Binary file added tests/i1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/i1.png.bin
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/native_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ fn ci4() -> Result<()> {
Ok(())
}

#[test]
fn i1() -> Result<()> {
let original_bytes: &[u8] = include_bytes!("i1.png.bin");
let image = NativeImage::read(original_bytes, ImageType::I1, 72, 72)?;

let mut output: Vec<u8> = Vec::new();
image.as_png(&mut output, None)?;

// convert the png back to a native image
let image = PNGImage::read(output.as_slice())?;
let mut output_bytes: Vec<u8> = Vec::new();
image.as_i1(&mut output_bytes)?;

assert_eq!(output_bytes, original_bytes);
Ok(())
}

#[test]
fn i4() -> Result<()> {
let original_bytes: &[u8] = include_bytes!("i4.png.bin");
Expand Down
13 changes: 13 additions & 0 deletions tests/png_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ fn ci4() -> Result<()> {
Ok(())
}

#[test]
fn i1() -> Result<()> {
let input_bytes: &[u8] = include_bytes!("i1.png");
let image = PNGImage::read(input_bytes)?;

let expected_bytes = include_bytes!("i1.png.bin");
let mut output: Vec<u8> = Vec::new();
image.as_i1(&mut output)?;

assert_eq!(output, expected_bytes);
Ok(())
}

#[test]
fn i4() -> Result<()> {
let input_bytes: &[u8] = include_bytes!("i4.png");
Expand Down

0 comments on commit fcfe6c0

Please sign in to comment.