Skip to content

Commit abdfeb9

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 increase aliasing effects on each step. Better limit the numerator 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 6a3ef20 commit abdfeb9

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
@@ -435,6 +435,7 @@ impl<'a> BlobObject<'a> {
435435
});
436436

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

0 commit comments

Comments
 (0)