From 7a777e44267efd43ed593ca69858ca786d9104e6 Mon Sep 17 00:00:00 2001 From: novenary Date: Tue, 16 May 2023 12:11:00 +0300 Subject: [PATCH] ImageBuffer: add write_with_encoder() method References: #1922 --- src/buffer.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/buffer.rs b/src/buffer.rs index 4b8fe61ce9..48f669eb68 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -10,7 +10,7 @@ use crate::color::{FromColor, Luma, LumaA, Rgb, Rgba}; use crate::dynimage::{save_buffer, save_buffer_with_format, write_buffer_with_format}; use crate::error::ImageResult; use crate::flat::{FlatSamples, SampleLayout}; -use crate::image::{GenericImage, GenericImageView, ImageFormat, ImageOutputFormat}; +use crate::image::{GenericImage, GenericImageView, ImageEncoder, ImageFormat, ImageOutputFormat}; use crate::math::Rect; use crate::traits::{EncodableLayout, Pixel, PixelWithColorType}; use crate::utils::expand_packed; @@ -1063,6 +1063,28 @@ where } } +impl ImageBuffer +where + P: Pixel, + [P::Subpixel]: EncodableLayout, + Container: Deref, +{ + /// Writes the buffer with the given encoder. + pub fn write_with_encoder(&self, encoder: E) -> ImageResult<()> + where + E: ImageEncoder, + P: PixelWithColorType, + { + // This is valid as the subpixel is u8. + encoder.write_image( + self.inner_pixels().as_bytes(), + self.width(), + self.height(), +

::COLOR_TYPE, + ) + } +} + impl Default for ImageBuffer where P: Pixel,