Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/codecs/bmp/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl<W: Write> BmpEncoder<W> {
///
/// # Panics
///
/// Panics if `width * height * c.bytes_per_pixel() != image.len()`.
/// Panics if the buffer does not hold exactly the number of bytes required for the given
/// `width`, `height`, and color type, accounting for rows padded to whole bytes for
/// sub-byte color types: `height * ((width * color_type.bits_per_pixel() as u32 + 7) / 8)`.
#[track_caller]
pub fn encode(
&mut self,
Expand All @@ -43,7 +45,9 @@ impl<W: Write> BmpEncoder<W> {
///
/// # Panics
///
/// Panics if `width * height * c.bytes_per_pixel() != image.len()`.
/// Panics if the buffer does not hold exactly the number of bytes required for the given
/// `width`, `height`, and color type, accounting for rows padded to whole bytes for
/// sub-byte color types: `height * ((width * color_type.bits_per_pixel() as u32 + 7) / 8)`.
#[track_caller]
pub fn encode_with_palette(
&mut self,
Expand Down
4 changes: 3 additions & 1 deletion src/codecs/jpeg/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ impl<W: Write> JpegEncoder<W> {
///
/// # Panics
///
/// Panics if `width * height * color_type.bytes_per_pixel() != image.len()`.
/// Panics if the buffer does not hold exactly the number of bytes required for the given
/// `width`, `height`, and `color_type`, accounting for rows padded to whole bytes for
/// sub-byte color types: `height * ((width * color_type.bits_per_pixel() as u32 + 7) / 8)`.
#[track_caller]
fn encode(
self,
Expand Down
4 changes: 3 additions & 1 deletion src/codecs/tga/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ impl<W: Write> TgaEncoder<W> {
///
/// # Panics
///
/// Panics if `width * height * color_type.bytes_per_pixel() != data.len()`.
/// Panics if the buffer does not hold exactly the number of bytes required for the given
/// `width`, `height`, and `color_type`, accounting for rows padded to whole bytes for
/// sub-byte color types: `height * ((width * color_type.bits_per_pixel() as u32 + 7) / 8)`.
#[track_caller]
pub fn encode(
mut self,
Expand Down
4 changes: 3 additions & 1 deletion src/codecs/tiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,9 @@ impl<W: Write + Seek> ImageEncoder for TiffEncoder<W> {
///
/// # Panics
///
/// Panics if `width * height * color_type.bytes_per_pixel() != data.len()`.
/// Panics if the buffer does not hold exactly the number of bytes required for the given
/// `width`, `height`, and `color_type`, accounting for rows padded to whole bytes for
/// sub-byte color types: `height * ((width * color_type.bits_per_pixel() as u32 + 7) / 8)`.
#[track_caller]
fn write_image(
self,
Expand Down
4 changes: 3 additions & 1 deletion src/codecs/webp/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ impl<W: Write> WebPEncoder<W> {
///
/// # Panics
///
/// Panics if `width * height * color.bytes_per_pixel() != data.len()`.
/// Panics if the buffer does not hold exactly the number of bytes required for the given
/// `width`, `height`, and color type, accounting for rows padded to whole bytes for
/// sub-byte color types: `height * ((width * color_type.bits_per_pixel() as u32 + 7) / 8)`.
#[track_caller]
pub fn encode(
self,
Expand Down
8 changes: 6 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,12 @@ impl ExtendedColorType {
}
}

/// Returns the number of bytes required to hold a width x height image of this color type.
pub(crate) fn buffer_size(self, width: u32, height: u32) -> u64 {
/// Returns the number of bytes required to hold a `width x height` image of this color type.
///
/// Each pixel row occupies exactly `width * bits_per_pixel()` bits, rounded **up to the
/// nearest byte** (no additional row padding is added). The total is `row_bytes * height`,
/// saturating at [`u64::MAX`] for astronomically large inputs.
pub fn buffer_size(self, width: u32, height: u32) -> u64 {
let bpp = self.bits_per_pixel() as u64;
let row_pitch = (width as u64 * bpp).div_ceil(8);
row_pitch.saturating_mul(height as u64)
Expand Down
2 changes: 1 addition & 1 deletion src/io/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait ImageEncoder {
///
/// # Panics
///
/// Panics if `width * height * color_type.bytes_per_pixel() != buf.len()`.
/// Panics if `buf.len() as u64 != color_type.buffer_size(width, height)`.
fn write_image(
self,
buf: &[u8],
Expand Down
Loading