Skip to content

Commit b71c9d8

Browse files
authored
Merge pull request #129 from oleteacher/patch-1
2 parents c7e56c8 + d9f7b18 commit b71c9d8

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src-tauri/src/commands/image_compressor.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@ pub mod images {
2626
let time = std::time::Instant::now();
2727
let path = Path::new(&image_path);
2828

29-
let img = image::open(path).map_err(|e| e.to_string()).unwrap();
29+
let img = image::open(path).map_err(|e| e.to_string())?;
3030

3131
match format {
3232
ImageFormat::Jpeg => {
33+
let rgb_img = img.into_rgb8();
3334
let mut writer = Vec::new();
3435
let mut encoder = JpegEncoder::new_with_quality(&mut writer, quality);
3536
encoder
36-
.encode(img.as_bytes(), img.width(), img.height(), ColorType::Rgb8)
37-
.map_err(|e| e.to_string())
38-
.unwrap();
37+
.encode(
38+
rgb_img.as_raw(),
39+
rgb_img.width(),
40+
rgb_img.height(),
41+
ColorType::Rgb8
42+
)
43+
.map_err(|e| e.to_string())?;
3944
println!("Time: {:?}", time.elapsed());
40-
return Ok(writer);
45+
Ok(writer)
4146
}
4247
ImageFormat::Png => {
48+
let rgb_img = img.into_rgb8();
4349
let mut writer = Vec::new();
44-
// convert quality percentage to CompressionType
4550
let compression_level = match quality {
4651
0..=33 => CompressionType::Best,
4752
34..=66 => CompressionType::Default,
@@ -54,20 +59,23 @@ pub mod images {
5459
image::codecs::png::FilterType::Sub,
5560
);
5661
encoder
57-
.write_image(img.as_bytes(), img.width(), img.height(), ColorType::Rgb8)
58-
.map_err(|e| e.to_string())
59-
.unwrap();
62+
.write_image(
63+
rgb_img.as_raw(),
64+
rgb_img.width(),
65+
rgb_img.height(),
66+
ColorType::Rgb8
67+
)
68+
.map_err(|e| e.to_string())?;
6069
println!("Time: {:?}", time.elapsed());
61-
return Ok(writer);
70+
Ok(writer)
6271
}
6372
ImageFormat::Webp => {
73+
// For WebP, we use the original image since webp::Encoder handles the conversion
6474
let x = webp::Encoder::from_image(&img)
65-
.unwrap()
75+
.map_err(|e| e.to_string())?
6676
.encode(quality as f32);
67-
// .map_err(|e| e.to_string())
68-
// .unwrap();
6977
println!("Time: {:?}", time.elapsed());
70-
return Ok(x.to_vec());
78+
Ok(x.to_vec())
7179
}
7280
}
7381
}

0 commit comments

Comments
 (0)