Skip to content

Commit

Permalink
ImageBuffer: add write_with_encoder() method
Browse files Browse the repository at this point in the history
References: image-rs#1922
  • Loading branch information
9ary committed May 16, 2023
1 parent d5b8bdf commit 7a777e4
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1063,6 +1063,28 @@ where
}
}

impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
{
/// Writes the buffer with the given encoder.
pub fn write_with_encoder<E>(&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(),
<P as PixelWithColorType>::COLOR_TYPE,
)
}
}

impl<P, Container> Default for ImageBuffer<P, Container>
where
P: Pixel,
Expand Down

0 comments on commit 7a777e4

Please sign in to comment.