diff options
Diffstat (limited to 'src/layer.rs')
-rw-r--r-- | src/layer.rs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/layer.rs b/src/layer.rs index ff69f7d..2726c81 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -3,7 +3,11 @@ //! This supports OSM-style "tiled" images, but not all of the tiles have to be present. If a tile //! is not present, a default pixel is returned. The tile is allocated with the first call to a //! mutating operation. -use std::{fs::File, io::BufWriter, path::Path}; +use std::{ + fs::File, + io::{BufWriter, Write}, + path::Path, +}; use color_eyre::eyre::Result; use fnv::FnvHashMap; @@ -119,6 +123,10 @@ where pub fn compress_png<P: AsRef<Path>>(image: &RgbaImage, path: P) -> Result<()> { let outstream = BufWriter::new(File::create(path)?); + compress_png_stream(image, outstream) +} + +pub fn compress_png_stream<W: Write>(image: &RgbaImage, outstream: W) -> Result<()> { let encoder = PngEncoder::new_with_quality(outstream, CompressionType::Best, FilterType::Adaptive); @@ -127,6 +135,12 @@ pub fn compress_png<P: AsRef<Path>>(image: &RgbaImage, path: P) -> Result<()> { Ok(()) } +pub fn compress_png_as_bytes(image: &RgbaImage) -> Result<Vec<u8>> { + let mut buffer = Vec::new(); + compress_png_stream(image, &mut buffer)?; + Ok(buffer) +} + fn zero_pixel<P: Pixel>() -> P { let zeroes = vec![Zero::zero(); P::CHANNEL_COUNT as usize]; *P::from_slice(&zeroes) |