Skip to content

Commit

Permalink
Update to png 0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Nov 8, 2022
1 parent 474a7d0 commit 0bc696c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "ico"
version = "0.1.1"
version = "0.2.0"
authors = ["Matthew D. Steele <mdsteele@alum.mit.edu>"]
description = "A library for encoding/decoding ICO image files"
repository = "https://github.com/mdsteele/rust-ico"
Expand Down
61 changes: 35 additions & 26 deletions src/lib.rs
Expand Up @@ -465,34 +465,37 @@ impl IconImage {
/// is out of range.
pub fn read_png<R: Read>(reader: R) -> io::Result<IconImage> {
let decoder = png::Decoder::new(reader);
let (info, mut reader) = match decoder.read_info() {
let mut reader = match decoder.read_info() {
Ok(tuple) => tuple,
Err(error) => invalid_data!("Malformed PNG data: {}", error),
};
if info.width < MIN_WIDTH || info.width > MAX_WIDTH {
invalid_data!("Invalid PNG width (was {}, but range is {}-{})",
info.width,
MIN_WIDTH,
MAX_WIDTH);
}
if info.height < MIN_HEIGHT || info.height > MAX_HEIGHT {
invalid_data!("Invalid PNG height (was {}, but range is {}-{})",
info.height,
MIN_HEIGHT,
MAX_HEIGHT);
}
if info.bit_depth != png::BitDepth::Eight {
// TODO: Support other bit depths.
invalid_data!("Unsupported PNG bit depth: {:?}", info.bit_depth);
{
let info = reader.info();
if info.width < MIN_WIDTH || info.width > MAX_WIDTH {
invalid_data!("Invalid PNG width (was {}, but range is {}-{})",
info.width,
MIN_WIDTH,
MAX_WIDTH);
}
if info.height < MIN_HEIGHT || info.height > MAX_HEIGHT {
invalid_data!("Invalid PNG height (was {}, but range is {}-{})",
info.height,
MIN_HEIGHT,
MAX_HEIGHT);
}
if info.bit_depth != png::BitDepth::Eight {
// TODO: Support other bit depths.
invalid_data!("Unsupported PNG bit depth: {:?}", info.bit_depth);
}
}
let mut buffer = vec![0u8; info.buffer_size()];
let mut buffer = vec![0u8; reader.output_buffer_size()];
match reader.next_frame(&mut buffer) {
Ok(()) => {}
Ok(_) => {}
Err(error) => invalid_data!("Malformed PNG data: {}", error),
}
let rgba_data = match info.color_type {
png::ColorType::RGBA => buffer,
png::ColorType::RGB => {
let rgba_data = match reader.info().color_type {
png::ColorType::Rgba => buffer,
png::ColorType::Rgb => {
let num_pixels = buffer.len() / 3;
let mut rgba = Vec::with_capacity(num_pixels * 4);
for i in 0..num_pixels {
Expand Down Expand Up @@ -527,10 +530,10 @@ impl IconImage {
png::ColorType::Indexed => {
// TODO: Implement ColorType::Indexed conversion
invalid_data!("Unsupported PNG color type: {:?}",
info.color_type);
reader.info().color_type);
}
};
Ok(IconImage::from_rgba_data(info.width, info.height, rgba_data))
Ok(IconImage::from_rgba_data(reader.info().width, reader.info().height, rgba_data))
}

/// Encodes the image as a PNG file.
Expand All @@ -546,10 +549,16 @@ impl IconImage {
-> io::Result<u16> {
match self.write_png_internal_enc(stats, writer) {
Ok(bits_per_pixel) => Ok(bits_per_pixel),
Err(png::EncodingError::IoError(error)) => return Err(error),
Err(png::EncodingError::IoError(error)) => Err(error),
Err(png::EncodingError::Format(error)) => {
invalid_input!("PNG format error: {}", error);
}
Err(png::EncodingError::LimitsExceeded) => {
invalid_input!("PNG limits exceeded");
}
Err(png::EncodingError::Parameter(error)) => {
invalid_input!("PNG parameter error: {}", error);
}
}
}

Expand All @@ -561,9 +570,9 @@ impl IconImage {
// TODO: Detect if we can use grayscale.
encoder.set_depth(png::BitDepth::Eight);
if stats.has_alpha {
encoder.set_color(png::ColorType::RGBA);
encoder.set_color(png::ColorType::Rgba);
} else {
encoder.set_color(png::ColorType::RGB);
encoder.set_color(png::ColorType::Rgb);
}
let mut writer = encoder.write_header()?;
if stats.has_alpha {
Expand Down

0 comments on commit 0bc696c

Please sign in to comment.