diff --git a/src/codecs/avif/encoder.rs b/src/codecs/avif/encoder.rs index 17cb77ceff..c515beb15d 100644 --- a/src/codecs/avif/encoder.rs +++ b/src/codecs/avif/encoder.rs @@ -98,10 +98,15 @@ impl ImageEncoder for AvifEncoder { /// The encoder currently requires all data to be RGBA8, it will be converted internally if /// necessary. When data is suitably aligned, i.e. u16 channels to two bytes, then the /// conversion may be more efficient. + /// + /// # Panics + /// + /// Panics if `buf.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. #[track_caller] fn write_image( mut self, - data: &[u8], + buf: &[u8], width: u32, height: u32, color: ExtendedColorType, @@ -109,9 +114,9 @@ impl ImageEncoder for AvifEncoder { let expected_buffer_len = color.buffer_size(width, height); assert_eq!( expected_buffer_len, - data.len() as u64, + buf.len() as u64, "Invalid buffer length: expected {expected_buffer_len} got {} for {width}x{height} image", - data.len(), + buf.len(), ); self.set_color(color); @@ -119,7 +124,7 @@ impl ImageEncoder for AvifEncoder { // owned version in our own buffer or zero-copy if possible by using the input buffer. // This requires going through `rgb`. let mut fallback = vec![]; // This vector is used if we need to do a color conversion. - let result = match Self::encode_as_img(&mut fallback, data, width, height, color)? { + let result = match Self::encode_as_img(&mut fallback, buf, width, height, color)? { RgbColor::Rgb8(buffer) => self.encoder.encode_rgb(buffer), RgbColor::Rgba8(buffer) => self.encoder.encode_rgba(buffer), }; diff --git a/src/codecs/bmp/encoder.rs b/src/codecs/bmp/encoder.rs index 31264a852d..6e65bb0afe 100644 --- a/src/codecs/bmp/encoder.rs +++ b/src/codecs/bmp/encoder.rs @@ -26,9 +26,8 @@ impl BmpEncoder { /// /// # Panics /// - /// 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)`. + /// Panics if `image.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. #[track_caller] pub fn encode( &mut self, @@ -45,9 +44,8 @@ impl BmpEncoder { /// /// # Panics /// - /// 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)`. + /// Panics if `image.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. #[track_caller] pub fn encode_with_palette( &mut self, diff --git a/src/codecs/gif.rs b/src/codecs/gif.rs index dea103e67f..133673c2b0 100644 --- a/src/codecs/gif.rs +++ b/src/codecs/gif.rs @@ -504,6 +504,11 @@ impl GifEncoder { } /// Encode a single image. + /// + /// # Panics + /// + /// Panics if `data.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. pub fn encode( &mut self, data: &[u8], diff --git a/src/codecs/ico/encoder.rs b/src/codecs/ico/encoder.rs index b4d9da3a58..566377bda8 100644 --- a/src/codecs/ico/encoder.rs +++ b/src/codecs/ico/encoder.rs @@ -68,6 +68,11 @@ impl<'a> IcoFrame<'a> { /// Construct a new `IcoFrame` by encoding `buf` as a PNG /// /// The `width` and `height` must be between 1 and 256 (inclusive) + /// + /// # Panics + /// + /// Panics if `buf.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. pub fn as_png( buf: &[u8], width: u32, @@ -125,12 +130,6 @@ impl IcoEncoder { } impl ImageEncoder for IcoEncoder { - /// Write an ICO image with the specified width, height, and color type. - /// - /// For color types with 16-bit per channel or larger, the contents of `buf` should be in - /// native endian. - /// - /// WARNING: In image 0.23.14 and earlier this method erroneously expected buf to be in big endian. #[track_caller] fn write_image( self, diff --git a/src/codecs/jpeg/encoder.rs b/src/codecs/jpeg/encoder.rs index 816fb2b66b..766f4b6b05 100644 --- a/src/codecs/jpeg/encoder.rs +++ b/src/codecs/jpeg/encoder.rs @@ -192,9 +192,8 @@ impl JpegEncoder { /// /// # Panics /// - /// 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)`. + /// Panics if `image.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. #[track_caller] fn encode( self, diff --git a/src/codecs/openexr.rs b/src/codecs/openexr.rs index 442a915341..6d868ff2a1 100644 --- a/src/codecs/openexr.rs +++ b/src/codecs/openexr.rs @@ -317,10 +317,6 @@ impl ImageEncoder for OpenExrEncoder where W: Write + Seek, { - /// Writes the complete image. - /// - /// Assumes the writer is buffered. In most cases, you should wrap your writer in a `BufWriter` - /// for best performance. #[track_caller] fn write_image( self, diff --git a/src/codecs/png.rs b/src/codecs/png.rs index b7cc944cdf..89fc9c7350 100644 --- a/src/codecs/png.rs +++ b/src/codecs/png.rs @@ -857,11 +857,6 @@ impl PngEncoder { } impl ImageEncoder for PngEncoder { - /// Write a PNG image with the specified width, height, and color type. - /// - /// For color types with 16-bit per channel or larger, the contents of `buf` should be in - /// native endian. `PngEncoder` will automatically convert to big endian as required by the - /// underlying PNG format. #[track_caller] fn write_image( self, diff --git a/src/codecs/tga/encoder.rs b/src/codecs/tga/encoder.rs index 6a65d164cb..259ff73e42 100644 --- a/src/codecs/tga/encoder.rs +++ b/src/codecs/tga/encoder.rs @@ -159,9 +159,8 @@ impl TgaEncoder { /// /// # Panics /// - /// 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)`. + /// Panics if `buf.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. #[track_caller] pub fn encode( mut self, diff --git a/src/codecs/tiff.rs b/src/codecs/tiff.rs index 0f020ac64e..4043135604 100644 --- a/src/codecs/tiff.rs +++ b/src/codecs/tiff.rs @@ -779,15 +779,6 @@ impl TiffEncoder { } impl ImageEncoder for TiffEncoder { - /// Encodes the image `image` that has dimensions `width` and `height` and `ColorType` `c`. - /// - /// 16-bit types assume the buffer is native endian. - /// - /// # Panics - /// - /// 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, diff --git a/src/codecs/webp/encoder.rs b/src/codecs/webp/encoder.rs index 70423d8406..fe167a1691 100644 --- a/src/codecs/webp/encoder.rs +++ b/src/codecs/webp/encoder.rs @@ -43,9 +43,8 @@ impl WebPEncoder { /// /// # Panics /// - /// 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)`. + /// Panics if `buf.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. #[track_caller] pub fn encode( self, diff --git a/src/io/encoder.rs b/src/io/encoder.rs index eb9ae816a4..b30eb7d514 100644 --- a/src/io/encoder.rs +++ b/src/io/encoder.rs @@ -32,7 +32,8 @@ pub trait ImageEncoder { /// /// # Panics /// - /// Panics if `buf.len() as u64 != color_type.buffer_size(width, height)`. + /// Panics if `buf.len() != color_type.buffer_size(width, height)`. + /// See [`ExtendedColorType::buffer_size`] for more information. fn write_image( self, buf: &[u8],