Skip to content

Commit

Permalink
Add DynamicImage::write_with_encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed Aug 4, 2023
1 parent 31ecbe6 commit 63be6c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,11 @@ impl DynamicImage {
}
}

/// Encode this image with the provided encoder.
pub fn write_with_encoder(&self, encoder: impl ImageEncoder) -> ImageResult<()> {
dynamic_map!(self, |ref p| p.write_with_encoder(encoder))
}

/// Saves the buffer to a file at the path specified.
///
/// The image format is derived from the file extension.
Expand Down
40 changes: 34 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
//!
//! ```rust,no_run
//! # use std::io::{Write, Cursor};
//! # use image::ImageOutputFormat;
//! # use image::DynamicImage;
//! # use image::{DynamicImage, ImageOutputFormat};
//! # #[cfg(feature = "png")]
//! # fn main() -> Result<(), image::ImageError> {
//! # let img: DynamicImage = unimplemented!();
Expand Down Expand Up @@ -73,11 +72,40 @@
//!
//! # Low level encoding/decoding API
//!
//! The [`ImageDecoder`] and [`ImageDecoderRect`] traits are implemented for many image file
//! formats. They decode image data by directly on raw byte slices. Given an ImageDecoder, you can
//! produce a DynamicImage via [`DynamicImage::from_decoder`].
//! Implementations of [`ImageEncoder`] provides low level control over encoding:
//! ```rust,no_run
//! # use std::io::Write;
//! # use image::DynamicImage;
//! # use image::codecs::jpeg::JpegEncoder;
//! # use image::ImageEncoder;
//! # #[cfg(feature = "jpeg")]
//! # fn main() -> Result<(), image::ImageError> {
//! # let img: DynamicImage = unimplemented!();
//! # let writer: Box<dyn Write> = unimplemented!();
//! let encoder = JpegEncoder::new_with_quality(&mut writer, 95);
//! img.write_with_encoder(encoder)?;
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "jpeg"))] fn main() {}
//! ```
//! While [`ImageDecoder`] and [`ImageDecoderRect`] give access to more advanced decoding options:
//!
//! [`ImageEncoder`] provides the analogous functionality for encoding image data.
//! ```rust,no_run
//! # use std::io::Read;
//! # use image::DynamicImage;
//! # use image::codecs::png::PngDecoder;
//! # use image::ImageDecoder;
//! # #[cfg(feature = "png")]
//! # fn main() -> Result<(), image::ImageError> {
//! # let img: DynamicImage = unimplemented!();
//! # let reader: Box<dyn Read> = unimplemented!();
//! let decoder = PngDecoder::new(&mut reader)?;
//! let icc = decoder.icc_profile();
//! let img = DynamicImage::from_decoder(decoder)?;
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "png"))] fn main() {}
//! ```
//!
//! [`DynamicImage::from_decoder`]: enum.DynamicImage.html#method.from_decoder
//! [`ImageDecoderRect`]: trait.ImageDecoderRect.html
Expand Down

0 comments on commit 63be6c9

Please sign in to comment.