Increate the quality of packed WebP favicons

This commit is contained in:
Olivier 'reivilibre' 2022-03-27 19:31:52 +01:00
parent 4e0352220d
commit bdfacc643e
1 changed files with 10 additions and 2 deletions

View File

@ -5,6 +5,7 @@ use chrono::{DateTime, FixedOffset, Utc};
use cylon::Cylon; use cylon::Cylon;
use futures_util::stream::StreamExt; use futures_util::stream::StreamExt;
use html5ever::tendril::fmt::Slice; use html5ever::tendril::fmt::Slice;
use image::imageops::FilterType;
use image::{GenericImageView, ImageFormat}; use image::{GenericImageView, ImageFormat};
use itertools::Itertools; use itertools::Itertools;
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -510,15 +511,22 @@ pub fn rake_icon(content: &[u8], content_type: &str) -> anyhow::Result<RakedIcon
let mut image = image::load(&mut cursor, *format).context("Failed to load image")?; let mut image = image::load(&mut cursor, *format).context("Failed to load image")?;
const WANTED_DIMENSIONS: u32 = 32; const WANTED_DIMENSIONS: u32 = 32;
/// Between 0 and 100.
const WEBP_QUALITY: f32 = 5.0;
let (w, h) = image.dimensions(); let (w, h) = image.dimensions();
if w.max(h) > WANTED_DIMENSIONS { if w.max(h) > WANTED_DIMENSIONS {
image = image.thumbnail(WANTED_DIMENSIONS, WANTED_DIMENSIONS); // image = image.thumbnail(WANTED_DIMENSIONS, WANTED_DIMENSIONS);
// Triangle is slightly better quality than nearest neighbour, but less expensive than
// Cubic or Lanczos.
// .thumbnail() is apparently very fast, but the artifacts were a little bit unfortunate for
// this.
image = image.resize_to_fill(WANTED_DIMENSIONS, WANTED_DIMENSIONS, FilterType::Triangle);
} }
let webp_encoder = let webp_encoder =
webp::Encoder::from_image(&image).map_err(|err| anyhow!("webp fail: {}", err))?; webp::Encoder::from_image(&image).map_err(|err| anyhow!("webp fail: {}", err))?;
let encoded = webp_encoder.encode(0.6).to_vec(); let encoded = webp_encoder.encode(WEBP_QUALITY).to_vec();
Ok(RakedIcon { Ok(RakedIcon {
original_size_in_bytes: orig_size, original_size_in_bytes: orig_size,