Skip to content

Commit 7786111

Browse files
authored
Increase PNG compression level (#817)
This commit increases the compression level for when we encode PNG images. We do this for Llanfair files (Gered and original) were the images are uncompressed and also for when we encounter BMP images in LiveSplit files or we scale down images. Increasing the compression level doesn't seem to really take much more time, but has a decent impact on the size of the images.
1 parent a33c167 commit 7786111

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

src/run/parser/llanfair.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010
use core::{result::Result as StdResult, str};
1111
#[cfg(feature = "std")]
12-
use image::{codecs::png, ExtendedColorType, ImageBuffer, ImageEncoder, Rgba};
12+
use image::{ExtendedColorType, ImageBuffer, ImageEncoder, Rgba};
1313
use snafu::{OptionExt, ResultExt};
1414

1515
/// The Error type for splits files that couldn't be parsed by the Llanfair
@@ -187,7 +187,7 @@ pub fn parse(source: &[u8]) -> Result<Run> {
187187
#[cfg(feature = "std")]
188188
if let Some(image) = ImageBuffer::<Rgba<u8>, _>::from_raw(width, height, _image_data) {
189189
buf.clear();
190-
if png::PngEncoder::new(&mut buf)
190+
if crate::util::image::create_reencoder(&mut buf)
191191
.write_image(image.as_ref(), width, height, ExtendedColorType::Rgba8)
192192
.is_ok()
193193
{

src/run/parser/llanfair_gered.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
RealTime, Run, Segment, Time, TimeSpan,
1919
};
2020
#[cfg(feature = "std")]
21-
use image::{codecs::png, ExtendedColorType, ImageEncoder};
21+
use image::{ExtendedColorType, ImageEncoder};
2222
#[cfg(feature = "std")]
2323
use snafu::OptionExt;
2424

@@ -123,7 +123,7 @@ where
123123
})?;
124124

125125
png_buf.clear();
126-
png::PngEncoder::new(&mut *png_buf)
126+
crate::util::image::create_reencoder(&mut *png_buf)
127127
.write_image(image, width, height, ExtendedColorType::Rgba8)
128128
.map_err(|_| Error::Image)?;
129129

src/settings/image/shrinking.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::borrow::Cow;
22
use image::{guess_format, load_from_memory_with_format, ImageEncoder, ImageFormat};
33

4-
use crate::util::image::get_dimensions;
4+
use crate::util::image::{create_reencoder, get_dimensions};
55

66
fn shrink_inner(data: &[u8], max_dim: u32) -> Option<Cow<'_, [u8]>> {
77
let format = guess_format(data).ok()?;
@@ -27,15 +27,18 @@ fn shrink_inner(data: &[u8], max_dim: u32) -> Option<Cow<'_, [u8]>> {
2727
if is_too_large {
2828
image = image.thumbnail(max_dim, max_dim);
2929
}
30+
3031
let mut data = Vec::new();
31-
image::codecs::png::PngEncoder::new(&mut data)
32+
33+
create_reencoder(&mut data)
3234
.write_image(
3335
image.as_bytes(),
3436
image.width(),
3537
image.height(),
3638
image.color().into(),
3739
)
3840
.ok()?;
41+
3942
data.into()
4043
} else {
4144
data.into()

src/util/image.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
use std::io::Cursor;
1+
use image::codecs::png::{CompressionType, FilterType, PngEncoder};
22

3-
use bytemuck_derive::{Pod, Zeroable};
4-
use image::{
5-
codecs::{bmp, farbfeld, hdr, ico, jpeg, pnm, tga, tiff, webp},
6-
ImageDecoder, ImageFormat,
7-
};
3+
pub fn create_reencoder(target_buf: &mut Vec<u8>) -> PngEncoder<&mut Vec<u8>> {
4+
PngEncoder::new_with_quality(target_buf, CompressionType::Best, FilterType::default())
5+
}
6+
7+
#[cfg(any(feature = "image-shrinking", feature = "svg-rendering"))]
8+
pub fn get_dimensions(format: image::ImageFormat, data: &[u8]) -> Option<(u32, u32)> {
9+
use std::io::Cursor;
10+
11+
use bytemuck_derive::{Pod, Zeroable};
12+
use image::{
13+
codecs::{bmp, farbfeld, hdr, ico, jpeg, pnm, tga, tiff, webp},
14+
ImageDecoder, ImageFormat,
15+
};
816

9-
use crate::util::byte_parsing::{big_endian::U32, strip_pod};
17+
use crate::util::byte_parsing::{big_endian::U32, strip_pod};
1018

11-
pub fn get_dimensions(format: ImageFormat, data: &[u8]) -> Option<(u32, u32)> {
1219
Some(match format {
1320
ImageFormat::Png => {
1421
// We encounter a lot of PNG images in splits files and decoding

src/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub(crate) mod ascii_set;
55
pub(crate) mod byte_parsing;
66
pub(crate) mod caseless;
77
mod clear_vec;
8-
#[cfg(any(feature = "image-shrinking", feature = "svg-rendering"))]
8+
#[cfg(feature = "std")]
99
pub(crate) mod image;
1010
pub(crate) mod not_nan;
1111
pub mod ordered_map;

tests/rendering.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn all_components() {
254254
&image_cache,
255255
[300, 800],
256256
"7e7aa83a3b80e1da",
257-
"39b5d1923053c5d9",
257+
"fa3c0357219389d8",
258258
"all_components",
259259
);
260260

@@ -263,7 +263,7 @@ fn all_components() {
263263
&image_cache,
264264
[150, 800],
265265
"97afa51bfd8a8597",
266-
"82b26ae781d58b78",
266+
"eda169eec3995eeb",
267267
"all_components_thin",
268268
);
269269
}
@@ -308,7 +308,7 @@ fn dark_layout() {
308308
&layout.state(&mut image_cache, &timer.snapshot()),
309309
&image_cache,
310310
"a47c590792c1bab5",
311-
"3f8dfb2da2d43648",
311+
"91a89f563eb4f43d",
312312
"dark_layout",
313313
);
314314
}

0 commit comments

Comments
 (0)