Skip to content

Commit

Permalink
Use BufWriter in imageproc (#1996)
Browse files Browse the repository at this point in the history
According to
https://docs.rs/image/0.24.3/image/enum.DynamicImage.html#method.write_to,
it assumes a buffered writer, so we should provide one.

Signed-off-by: Ana Hobden <operator@hoverbear.org>
  • Loading branch information
Hoverbear authored and Keats committed Feb 16, 2023
1 parent 9e0f400 commit f1b5c2e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions components/imageproc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ffi::OsStr;
use std::fs::{self, File};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::{collections::hash_map::DefaultHasher, io::Write};
use std::{collections::hash_map::DefaultHasher, io::{Write, BufWriter}};

use image::error::ImageResult;
use image::io::Reader as ImgReader;
Expand Down Expand Up @@ -322,14 +322,15 @@ impl ImageOp {

let img = fix_orientation(&img, &self.input_path).unwrap_or(img);

let mut f = File::create(target_path)?;
let f = File::create(target_path)?;
let mut buffered_f = BufWriter::new(f);

match self.format {
Format::Png => {
img.write_to(&mut f, ImageOutputFormat::Png)?;
img.write_to(&mut buffered_f, ImageOutputFormat::Png)?;
}
Format::Jpeg(q) => {
img.write_to(&mut f, ImageOutputFormat::Jpeg(q))?;
img.write_to(&mut buffered_f, ImageOutputFormat::Jpeg(q))?;
}
Format::WebP(q) => {
let encoder = webp::Encoder::from_image(&img)
Expand All @@ -338,7 +339,7 @@ impl ImageOp {
Some(q) => encoder.encode(q as f32),
None => encoder.encode_lossless(),
};
f.write_all(memory.as_bytes())?;
buffered_f.write_all(memory.as_bytes())?;
}
}

Expand Down

0 comments on commit f1b5c2e

Please sign in to comment.