Skip to content

Commit 3fea829

Browse files
r10siequidoo
andauthored
feat: better avatar quality (#6822)
this PR scaled avatars using the Triangle-filter, resulting in often better image quality and smaller files (5%). it comes at high costs, therefore, we do not do that unconditionally for each image sent, see comment in the code and #6815 --------- Co-authored-by: iequidoo <117991069+iequidoo@users.noreply.github.com>
1 parent 6dba141 commit 3fea829

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/blob.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,20 @@ impl<'a> BlobObject<'a> {
432432
if mem::take(&mut add_white_bg) {
433433
self::add_white_bg(&mut img);
434434
}
435-
let new_img = img.thumbnail(img_wh, img_wh);
435+
436+
// resize() results in often slightly better quality,
437+
// however, comes at high price of being 4+ times slower than thumbnail().
438+
// for a typical camera image that is sent, this may be a change from "instant" (500ms) to "long time waiting" (3s).
439+
// as we do not have recoding in background while chat has already a preview,
440+
// we vote for speed.
441+
// exception is the avatar image: this is far more often sent than recoded,
442+
// usually has less pixels by cropping, UI that needs to wait anyways,
443+
// and also benefits from slightly better (5%) encoding of Triangle-filtered images.
444+
let new_img = if is_avatar {
445+
img.resize(img_wh, img_wh, image::imageops::FilterType::Triangle)
446+
} else {
447+
img.thumbnail(img_wh, img_wh)
448+
};
436449

437450
if encoded_img_exceeds_bytes(
438451
context,

src/blob/blob_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ async fn test_selfavatar_outside_blobdir() {
174174
let avatar_blob = t.get_config(Config::Selfavatar).await.unwrap().unwrap();
175175
let avatar_path = Path::new(&avatar_blob);
176176
assert!(
177-
avatar_blob.ends_with("1e08d1c9398297c21dd3820f7db2324.jpg"),
177+
avatar_blob.ends_with("7dde69e06b5ae6c27520a436bbfd65b.jpg"),
178178
"The avatar filename should be its hash, put instead it's {avatar_blob}"
179179
);
180180
let scaled_avatar_size = file_size(avatar_path).await;
@@ -226,7 +226,7 @@ async fn test_selfavatar_in_blobdir() {
226226
.unwrap();
227227
let avatar_cfg = t.get_config(Config::Selfavatar).await.unwrap().unwrap();
228228
assert!(
229-
avatar_cfg.ends_with("ec054c444a5755adf2b0aaea40209f2.png"),
229+
avatar_cfg.ends_with("d57cb5ce5f371531b6e1fb17b6dd1af.png"),
230230
"Avatar file name {avatar_cfg} should end with its hash"
231231
);
232232

0 commit comments

Comments
 (0)