Skip to content

Commit dca6ba7

Browse files
committed
feat: Use resize when resampling images with up to 2.56 megapixels
1 parent 29614ae commit dca6ba7

3 files changed

Lines changed: 11 additions & 12 deletions

File tree

src/blob.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,12 @@ impl<'a> BlobObject<'a> {
430430

431431
if do_scale {
432432
let n_px_longest_side = max(img.width(), img.height());
433+
let n_all_px = img.width() * img.height();
433434

434435
// target_wh will be used as the target-resolution for resizing the image,
435436
// so that the longest sides of the image match the target-resolution.
436437
let mut target_wh = if !is_avatar {
437-
let n_all_px_sqrt = f64::from(img.width() * img.height()).sqrt();
438+
let n_all_px_sqrt = f64::from(n_all_px).sqrt();
438439
// Limit resolution to the number of pixels that fit within max_wh * max_wh,
439440
// so that the image-quality does not depend on the aspect-ratio.
440441
let mut resolution_limit =
@@ -460,15 +461,13 @@ impl<'a> BlobObject<'a> {
460461
self::add_white_bg(&mut img);
461462
}
462463

463-
// resize() results in often slightly better quality,
464-
// however, comes at high price of being 4+ times slower than thumbnail().
465-
// for a typical camera image that is sent, this may be a change from "instant" (500ms) to "long time waiting" (3s).
466-
// as we do not have recoding in background while chat has already a preview,
467-
// we vote for speed.
468-
// exception is the avatar image: this is far more often sent than recoded,
469-
// usually has less pixels by cropping, UI that needs to wait anyways,
470-
// and also benefits from slightly better (5%) encoding of Triangle-filtered images.
471-
let new_img = if is_avatar {
464+
// resize() results in better quality than thumbnail(),
465+
// however, comes at the price of being 4+ times slower than thumbnail().
466+
// For a typical camera-image that is sent (often more than 8 megapixels),
467+
// this may be a change from "instant" (500ms) to "long time waiting" (3s).
468+
// As we do not have recoding in the background while the chat already has a preview,
469+
// we vote for speed, if the original image has a high resolution (> 2.56 megapixels).
470+
let new_img = if is_avatar || n_all_px <= 1600 * 1600 {
472471
img.resize(target_wh, target_wh, image::imageops::FilterType::Triangle)
473472
} else {
474473
img.thumbnail(target_wh, target_wh)

src/tests/pre_messages/additional_text.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async fn test_additional_text_on_different_viewtypes() -> Result<()> {
3434
let (pre_message, _, _) = send_large_image_message(alice, a_group_id).await?;
3535
let msg = bob.recv_msg(&pre_message).await;
3636
assert_eq!(msg.text, "test".to_owned());
37-
assert_eq!(msg.get_text(), "test [Image – 228.45 KiB]".to_owned());
37+
assert_eq!(msg.get_text(), "test [Image – 210.38 KiB]".to_owned());
3838

3939
Ok(())
4040
}

src/tests/pre_messages/receiving.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ async fn test_receive_pre_message_image() -> Result<()> {
323323
// test that metadata is correctly returned by methods
324324
assert_eq!(msg.get_post_message_viewtype(), Some(Viewtype::Image));
325325
// recoded image dimensions
326-
assert_eq!(msg.get_filebytes(bob).await?, Some(233935));
326+
assert_eq!(msg.get_filebytes(bob).await?, Some(215424));
327327
assert_eq!(msg.get_height(), 1704);
328328
assert_eq!(msg.get_width(), 959);
329329

0 commit comments

Comments
 (0)