From 530ec9e2086f1cd8d1b3addf71e6c1ebdbce0ed1 Mon Sep 17 00:00:00 2001 From: Bruflot Date: Thu, 11 Jul 2019 22:22:00 +0200 Subject: [PATCH 1/4] Save image with specified format --- src/buffer.rs | 27 +++++++++++++++++++-- src/dynimage.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index bdfc7cfcb6..82e385bd46 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -7,8 +7,8 @@ use std::slice::{Chunks, ChunksMut}; use color::{ColorType, FromColor, Luma, LumaA, Rgb, Rgba, Bgr, Bgra}; use flat::{FlatSamples, SampleLayout}; -use dynimage::save_buffer; -use image::{GenericImage, GenericImageView}; +use dynimage::{save_buffer, save_buffer_with_format}; +use image::{GenericImage, GenericImageView, ImageFormat}; use traits::Primitive; use utils::expand_packed; @@ -768,6 +768,29 @@ where } } +impl ImageBuffer +where + P: Pixel + 'static, + Container: Deref, +{ + /// Saves the buffer to a file at the path specified in + /// the specified format. + pub fn save_with_format(&self, path: Q, format: ImageFormat) -> io::Result<()> + where + Q: AsRef, + { + // This is valid as the subpixel is u8. + save_buffer_with_format( + path, + self, + self.width(), + self.height(), +

::COLOR_TYPE, + format, + ) + } +} + impl Deref for ImageBuffer where P: Pixel + 'static, diff --git a/src/dynimage.rs b/src/dynimage.rs index 79eb311214..9f7a78421e 100644 --- a/src/dynimage.rs +++ b/src/dynimage.rs @@ -564,6 +564,17 @@ impl DynamicImage { p.save(path) }) } + + /// Saves the buffer to a file at the path specified in + /// the specified format. + pub fn save_with_format(&self, path: Q, format: ImageFormat) -> io::Result<()> + where + Q: AsRef, + { + dynamic_map!(*self, ref p -> { + p.save_with_format(path, format) + }) + } } #[allow(deprecated)] @@ -881,6 +892,59 @@ fn save_buffer_impl( } } +/// Saves the supplied buffer to a file at the path specified +/// in the specified format. +/// +/// The buffer is assumed to have the correct format according +/// to the specified color type. +/// This will lead to corrupted files if the buffer contains +/// malformed data. Currently only jpeg, png, ico, pnm, bmp and +/// tiff files are supported. +pub fn save_buffer_with_format

( + path: P, + buf: &[u8], + width: u32, + height: u32, + color: color::ColorType, + format: ImageFormat, +) -> io::Result<()> +where + P: AsRef, +{ + // thin wrapper function to strip generics + save_buffer_with_format_impl(path.as_ref(), buf, width, height, color, format) +} + +fn save_buffer_with_format_impl( + path: &Path, + buf: &[u8], + width: u32, + height: u32, + color: color::ColorType, + format: ImageFormat, +) -> io::Result<()> { + let fout = &mut BufWriter::new(File::create(path)?); + + match format { + #[cfg(feature = "ico")] + image::ImageFormat::ICO => ico::ICOEncoder::new(fout).encode(buf, width, height, color), + #[cfg(feature = "jpeg")] + image::ImageFormat::JPEG => jpeg::JPEGEncoder::new(fout).encode(buf, width, height, color), + #[cfg(feature = "png_codec")] + image::ImageFormat::PNG => png::PNGEncoder::new(fout).encode(buf, width, height, color), + #[cfg(feature = "bmp")] + image::ImageFormat::BMP => bmp::BMPEncoder::new(fout).encode(buf, width, height, color), + #[cfg(feature = "tiff")] + image::ImageFormat::TIFF => tiff::TiffEncoder::new(fout) + .encode(buf, width, height, color) + .map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e))), + _ => Err(io::Error::new( + io::ErrorKind::InvalidInput, + &format!("Unsupported image format image/{:?}", format)[..], + )), + } +} + /// Create a new image from a Reader pub fn load(r: R, format: ImageFormat) -> ImageResult { #[allow(deprecated, unreachable_patterns)] From fc15b7964f39ca3572211aa2068c3ead06efbda5 Mon Sep 17 00:00:00 2001 From: Bruflot Date: Thu, 11 Jul 2019 22:29:35 +0200 Subject: [PATCH 2/4] typo --- src/buffer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buffer.rs b/src/buffer.rs index 82e385bd46..2885d84f05 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -773,7 +773,7 @@ where P: Pixel + 'static, Container: Deref, { - /// Saves the buffer to a file at the path specified in + /// Saves the buffer to a file at the specified path in /// the specified format. pub fn save_with_format(&self, path: Q, format: ImageFormat) -> io::Result<()> where From 281a2efa2b0239389b52c097103e48c692919baa Mon Sep 17 00:00:00 2001 From: Bruflot Date: Thu, 11 Jul 2019 22:31:16 +0200 Subject: [PATCH 3/4] typo --- src/dynimage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dynimage.rs b/src/dynimage.rs index 9f7a78421e..2fe3d4c314 100644 --- a/src/dynimage.rs +++ b/src/dynimage.rs @@ -565,7 +565,7 @@ impl DynamicImage { }) } - /// Saves the buffer to a file at the path specified in + /// Saves the buffer to a file at the specified path in /// the specified format. pub fn save_with_format(&self, path: Q, format: ImageFormat) -> io::Result<()> where From 90d30e3a33eeb5b1091f0992630fc9806e6c4faa Mon Sep 17 00:00:00 2001 From: Bruflot Date: Thu, 11 Jul 2019 23:30:31 +0200 Subject: [PATCH 4/4] Doc changes --- src/buffer.rs | 3 +++ src/dynimage.rs | 5 ++++- src/lib.rs | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 2885d84f05..d134d674a1 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -775,6 +775,9 @@ where { /// Saves the buffer to a file at the specified path in /// the specified format. + /// + /// See [`save_buffer_with_format`](fn.save_buffer_with_format.html) for + /// supported types. pub fn save_with_format(&self, path: Q, format: ImageFormat) -> io::Result<()> where Q: AsRef, diff --git a/src/dynimage.rs b/src/dynimage.rs index 2fe3d4c314..7ed818ec8b 100644 --- a/src/dynimage.rs +++ b/src/dynimage.rs @@ -567,6 +567,9 @@ impl DynamicImage { /// Saves the buffer to a file at the specified path in /// the specified format. + /// + /// See [`save_buffer_with_format`](fn.save_buffer_with_format.html) for + /// supported types. pub fn save_with_format(&self, path: Q, format: ImageFormat) -> io::Result<()> where Q: AsRef, @@ -898,7 +901,7 @@ fn save_buffer_impl( /// The buffer is assumed to have the correct format according /// to the specified color type. /// This will lead to corrupted files if the buffer contains -/// malformed data. Currently only jpeg, png, ico, pnm, bmp and +/// malformed data. Currently only jpeg, png, ico, bmp and /// tiff files are supported. pub fn save_buffer_with_format

( path: P, diff --git a/src/lib.rs b/src/lib.rs index 15acbc04da..31bcdf893b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ pub use traits::Primitive; // Opening and loading images pub use dynimage::{guess_format, load, load_from_memory, load_from_memory_with_format, open, - save_buffer, image_dimensions}; + save_buffer, save_buffer_with_format, image_dimensions}; pub use dynimage::DynamicImage::{self, ImageLuma8, ImageLumaA8, ImageRgb8, ImageRgba8, ImageBgr8, ImageBgra8};