Skip to content

Commit

Permalink
png/decoder: Improve docs+ allocate enough storage for 16 bit pixels …
Browse files Browse the repository at this point in the history
…with tRNS chunks

Signed-off-by: caleb <etemesicaleb@gmail.com>
  • Loading branch information
etemesi254 committed Jan 25, 2023
1 parent 757b561 commit 31899d8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion zune-png/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use crate::filters::{
};
use crate::options::{default_chunk_handler, UnkownChunkHandler};

/// A palette entry.
///
/// The alpha field is used if the image has a tRNS
/// chunk and pLTE chunk.
#[derive(Copy, Clone)]
pub(crate) struct PLTEEntry
{
Expand Down Expand Up @@ -47,6 +51,11 @@ pub(crate) struct PngChunk
pub crc: u32
}

/// Represents PNG information that can be extracted
/// from a png file.
///
/// The properties are read from IHDR chunk,
/// but may be changed by decoder during decoding
#[derive(Default, Debug, Copy, Clone)]
pub struct PngInfo
{
Expand All @@ -59,6 +68,18 @@ pub struct PngInfo
pub interlace_method: InterlaceMethod
}

/// A PNG decoder instance.
///
/// This is the main decoder for png image decoding.
///
/// Instantiate the decoder with either the [new](PngDecoder::new)
/// or [new_with_options](PngDecoder::new_with_options) and
/// using either of the [`decode_raw`](PngDecoder::decode_raw) or
/// [`decode`](PngDecoder::decode) will return pixels present in that image
///
/// # Note
/// The decoder currently expands images less than 8 bits per pixels to 8 bits per pixel
/// if this is not desired, then I'd suggest another png decoder
pub struct PngDecoder<'a>
{
pub(crate) stream: ZByteReader<'a>,
Expand Down Expand Up @@ -577,7 +598,7 @@ impl<'a> PngDecoder<'a>

let info = &self.png_info;

let new_size = info.width * info.height * usize::from(info.color.num_components() + 1);
let new_size = info.width * info.height * usize::from(info.color.num_components() + 1) * 2;

let mut new_out = vec![0; new_size];
match info.color
Expand Down

0 comments on commit 31899d8

Please sign in to comment.