Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,17 @@ impl<'a> BlobObject<'a> {
self::add_white_bg(&mut img);
}

// resize() results in often slightly better quality,
// however, comes at high price of being 4+ times slower than thumbnail().
// for a typical camera image that is sent, this may be a change from "instant" (500ms) to "long time waiting" (3s).
// as we do not have recoding in background while chat has already a preview,
// we vote for speed.
// exception is the avatar image: this is far more often sent than recoded,
// usually has less pixels by cropping, UI that needs to wait anyways,
// and also benefits from slightly better (5%) encoding of Triangle-filtered images.
let new_img = if is_avatar {
img.resize(target_wh, target_wh, image::imageops::FilterType::Triangle)
} else {
img.thumbnail(target_wh, target_wh)
};
// resize() results in better quality than thumbnail(); however, when using resize(),
// sending images can require about 60% more time.
// As we do not have recoding in the background, while the chat already has a preview,
// and the processing-time is proportional to the original resolution,
// we choose the faster resampling, if the original image has a high resolution.
let new_img =
if is_avatar || img.width().saturating_mul(img.height()) <= 2000 * 2000 {
img.resize(target_wh, target_wh, image::imageops::FilterType::Triangle)
} else {
img.thumbnail(target_wh, target_wh)
};

if encoded_img_exceeds_bytes(
context,
Expand Down
12 changes: 6 additions & 6 deletions src/blob/blob_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ async fn test_recode_image_balanced_png() {
extension: "png",
original_width: 1920,
original_height: 1080,
compressed_width: 848,
compressed_height: 477,
compressed_width: 904,
compressed_height: 509,
..Default::default()
}
.test()
Expand Down Expand Up @@ -497,8 +497,8 @@ async fn test_recode_image_rgba_png_to_jpeg() {
extension: "png",
original_width: 1920,
original_height: 1080,
compressed_width: 848,
compressed_height: 477,
compressed_width: 904,
compressed_height: 509,
..Default::default()
}
.test()
Expand All @@ -517,8 +517,8 @@ async fn test_recode_image_huge_jpg() {
has_exif: true,
original_width: 1920,
original_height: 1080,
compressed_width: 1704,
compressed_height: 959,
compressed_width: 1808,
compressed_height: 1017,
..Default::default()
}
.test()
Expand Down
4 changes: 2 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ pub(crate) const WORSE_AVATAR_SIZE: u32 = 256;
pub(crate) const WORSE_AVATAR_BYTES: usize = 20_000; // this also fits to Outlook servers don't allowing headers larger than 32k.

// max. width/height of images scaled down because of being too huge
pub const BALANCED_IMAGE_SIZE: u32 = 1280;
pub const WORSE_IMAGE_SIZE: u32 = 640;
pub const BALANCED_IMAGE_SIZE: u32 = 1360;
pub const WORSE_IMAGE_SIZE: u32 = 680;

/// Limit for received images size. Bigger images become `Viewtype::File` to avoid excessive memory
/// usage by UIs.
Expand Down
2 changes: 1 addition & 1 deletion src/tests/pre_messages/additional_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn test_additional_text_on_different_viewtypes() -> Result<()> {
let (pre_message, _, _) = send_large_image_message(alice, a_group_id).await?;
let msg = bob.recv_msg(&pre_message).await;
assert_eq!(msg.text, "test".to_owned());
assert_eq!(msg.get_text(), "test [Image – 228.45 KiB]".to_owned());
assert_eq!(msg.get_text(), "test [Image – 225.81 KiB]".to_owned());

Ok(())
}
6 changes: 3 additions & 3 deletions src/tests/pre_messages/receiving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ async fn test_receive_pre_message_image() -> Result<()> {
// test that metadata is correctly returned by methods
assert_eq!(msg.get_post_message_viewtype(), Some(Viewtype::Image));
// recoded image dimensions
assert_eq!(msg.get_filebytes(bob).await?, Some(233935));
assert_eq!(msg.get_height(), 1704);
assert_eq!(msg.get_width(), 959);
assert_eq!(msg.get_filebytes(bob).await?, Some(231228));
assert_eq!(msg.get_height(), 1808);
assert_eq!(msg.get_width(), 1017);

Ok(())
}
Expand Down
Loading