Skip to content

Commit

Permalink
Separate PartialInfo from Info
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 20, 2024
1 parent 134613b commit eed8ce1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
14 changes: 12 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ impl ColorType {
|| self == ColorType::Rgba))
|| (bit_depth == BitDepth::Sixteen && self == ColorType::Indexed)
}

pub(crate) fn bits_per_pixel(&self, bit_depth: BitDepth) -> usize {
self.samples() * bit_depth as usize
}

pub(crate) fn bytes_per_pixel(&self, bit_depth: BitDepth) -> usize {
// If adjusting this for expansion or other transformation passes, remember to keep the old
// implementation for bpp_in_prediction, which is internal to the png specification.
self.samples() * ((bit_depth as usize + 7) >> 3)
}
}

/// Bit depth of the PNG file.
Expand Down Expand Up @@ -683,14 +693,14 @@ impl Info<'_> {

/// Returns the number of bits per pixel.
pub fn bits_per_pixel(&self) -> usize {
self.color_type.samples() * self.bit_depth as usize
self.color_type.bits_per_pixel(self.bit_depth)
}

/// Returns the number of bytes per pixel.
pub fn bytes_per_pixel(&self) -> usize {
// If adjusting this for expansion or other transformation passes, remember to keep the old
// implementation for bpp_in_prediction, which is internal to the png specification.
self.color_type.samples() * ((self.bit_depth as usize + 7) >> 3)
self.color_type.bytes_per_pixel(self.bit_depth)
}

/// Return the number of bytes for this pixel used in prediction.
Expand Down
29 changes: 8 additions & 21 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,33 +514,20 @@ impl PartialInfo {
}

fn bpp_in_prediction(&self) -> BytesPerPixel {
// Passthrough
self.to_info().bpp_in_prediction()
BytesPerPixel::from_usize(self.bytes_per_pixel())
}

fn bytes_per_pixel(&self) -> usize {
self.color_type.bytes_per_pixel(self.bit_depth)
}

fn raw_row_length(&self) -> usize {
// Passthrough
self.to_info().raw_row_length()
self.raw_row_length_from_width(self.width)
}

fn raw_row_length_from_width(&self, width: u32) -> usize {
// Passthrough
self.to_info().raw_row_length_from_width(width)
}

/// Converts this partial info to an owned Info struct,
/// setting missing values to their defaults
fn to_info(&self) -> Info<'static> {
Info {
width: self.width,
height: self.height,
bit_depth: self.bit_depth,
color_type: self.color_type,
frame_control: self.frame_control,
animation_control: self.animation_control,
compression: self.compression,
..Default::default()
}
self.color_type
.raw_row_length_from_width(self.bit_depth, width)
}
}

Expand Down

0 comments on commit eed8ce1

Please sign in to comment.