Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save image with specified format #988

Merged
merged 4 commits into from Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/buffer.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -768,6 +768,29 @@ where
}
}

impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel<Subpixel = u8> + 'static,
Container: Deref<Target = [u8]>,
{
/// Saves the buffer to a file at the specified path in
/// the specified format.
pub fn save_with_format<Q>(&self, path: Q, format: ImageFormat) -> io::Result<()>
Bruflot marked this conversation as resolved.
Show resolved Hide resolved
where
Q: AsRef<Path>,
{
// This is valid as the subpixel is u8.
save_buffer_with_format(
path,
self,
self.width(),
self.height(),
<P as Pixel>::COLOR_TYPE,
format,
)
}
}

impl<P, Container> Deref for ImageBuffer<P, Container>
where
P: Pixel + 'static,
Expand Down
64 changes: 64 additions & 0 deletions src/dynimage.rs
Expand Up @@ -564,6 +564,17 @@ impl DynamicImage {
p.save(path)
})
}

/// Saves the buffer to a file at the specified path in
/// the specified format.
pub fn save_with_format<Q>(&self, path: Q, format: ImageFormat) -> io::Result<()>
Bruflot marked this conversation as resolved.
Show resolved Hide resolved
where
Q: AsRef<Path>,
{
dynamic_map!(*self, ref p -> {
p.save_with_format(path, format)
})
}
}

#[allow(deprecated)]
Expand Down Expand Up @@ -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<P>(
path: P,
buf: &[u8],
width: u32,
height: u32,
color: color::ColorType,
format: ImageFormat,
) -> io::Result<()>
where
P: AsRef<Path>,
{
// 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: BufRead + Seek>(r: R, format: ImageFormat) -> ImageResult<DynamicImage> {
#[allow(deprecated, unreachable_patterns)]
Expand Down