Skip to content

Commit 35930f3

Browse files
committed
feat: Optimize avatar size multiplier for 2 << n px avatars
Instead of 2/3 which is not optimal for 512 px avatars usually passed to Core, use the sequence 3/4, 5/8, 4/8, 3/8... to reduce aliasing effects. Also divide the original avatar size, not the Core constant (which is 512 px currently), this makes sense if e.g. 640 px avatars are passed. Before, it was discussed that just 3/4 can be used. However: - If we repeat the reduction step, we get `3 << n` as a numerator and this way accumulate aliasing effects. Better limit it to 5. - If a 640 px avatar is passed, giving that `BALANCED_AVATAR_SIZE` is 512, 3/4 gives 384 i.e. 3/5 of 640 which is good but still worse than 3/4.
1 parent b72a677 commit 35930f3

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/blob.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ impl<'a> BlobObject<'a> {
434434
});
435435

436436
if do_scale {
437+
let (mut m, mut d) = (3, 4);
437438
loop {
438439
if mem::take(&mut add_white_bg) {
439440
self::add_white_bg(&mut img);
@@ -466,8 +467,17 @@ impl<'a> BlobObject<'a> {
466467
"Failed to scale image to below {max_bytes}B.",
467468
));
468469
}
469-
470-
target_wh = target_wh * 2 / 3;
470+
loop {
471+
let wh = max(img.width(), img.height()) * m / d;
472+
if wh < target_wh {
473+
target_wh = wh;
474+
break;
475+
}
476+
(m, d) = match m > 3 {
477+
true => (m - 1, d),
478+
false => (5, d * 2),
479+
};
480+
}
471481
} else {
472482
info!(
473483
context,

0 commit comments

Comments
 (0)