Skip to content

Commit f12afdf

Browse files
committed
feat: Make quality of images sent in chats more consistent
Currently, the resolution of a resized image that was sent in a chat, depends on the aspect-ratio. Assuming the `balanced`-quality-setting is used, a square image, that is larger than the limits for resolution and file-size, will be resized to 1280x1280 (1,638,400 pixels), an image with an aspect-ratio of 16:9, will be resized to 1280x720 (921,600 pixels), and if the aspect-ratio is 32:9, to 1280x360 (460,800 pixels). This change makes it so, that the number of pixels, in images with different aspect-ratios, will be similar.
1 parent bfbea63 commit f12afdf

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/blob.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,25 @@ impl<'a> BlobObject<'a> {
429429
});
430430

431431
if do_scale {
432+
let resolution_of_longest_side_of_image = max(img.width(), img.height());
433+
let square_root_of_number_of_pixels_in_image =
434+
f64::from(img.width() * img.height()).sqrt();
435+
432436
// target_wh will be used as the target-resolution for resizing the image,
433437
// so that the longest sides of the image match the target-resolution,
434438
// without changing the aspect-ratio.
435-
let mut target_wh = if exceeds_wh {
436-
max_wh
439+
let mut target_wh = if !is_avatar && exceeds_wh {
440+
// Limit resolution to the number of pixels that fit within max_wh * max_wh,
441+
// so that the image-quality does not depend on the aspect-ratio.
442+
(f64::from(resolution_of_longest_side_of_image)
443+
* (f64::from(max_wh) / square_root_of_number_of_pixels_in_image))
444+
as u32
437445
} else {
438-
max(img.width(), img.height())
446+
max_wh
447+
};
448+
449+
if target_wh > resolution_of_longest_side_of_image {
450+
target_wh = resolution_of_longest_side_of_image
439451
};
440452

441453
loop {

src/blob/blob_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ async fn test_recode_image_balanced_png() {
384384
extension: "png",
385385
original_width: 1920,
386386
original_height: 1080,
387-
compressed_width: constants::WORSE_IMAGE_SIZE,
388-
compressed_height: constants::WORSE_IMAGE_SIZE * 1080 / 1920,
387+
compressed_width: 853,
388+
compressed_height: 480,
389389
..Default::default()
390390
}
391391
.test()
@@ -475,8 +475,8 @@ async fn test_recode_image_rgba_png_to_jpeg() {
475475
extension: "png",
476476
original_width: 1920,
477477
original_height: 1080,
478-
compressed_width: constants::WORSE_IMAGE_SIZE,
479-
compressed_height: constants::WORSE_IMAGE_SIZE * 1080 / 1920,
478+
compressed_width: 853,
479+
compressed_height: 480,
480480
..Default::default()
481481
}
482482
.test()
@@ -495,8 +495,8 @@ async fn test_recode_image_huge_jpg() {
495495
has_exif: true,
496496
original_width: 1920,
497497
original_height: 1080,
498-
compressed_width: constants::BALANCED_IMAGE_SIZE,
499-
compressed_height: constants::BALANCED_IMAGE_SIZE * 1080 / 1920,
498+
compressed_width: 1706,
499+
compressed_height: 960,
500500
..Default::default()
501501
}
502502
.test()

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 – 146.12 KiB]".to_owned());
37+
assert_eq!(msg.get_text(), "test [Image – 227.50 KiB]".to_owned());
3838

3939
Ok(())
4040
}

src/tests/pre_messages/receiving.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ 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(149632));
327-
assert_eq!(msg.get_height(), 1280);
328-
assert_eq!(msg.get_width(), 720);
326+
assert_eq!(msg.get_filebytes(bob).await?, Some(232957));
327+
assert_eq!(msg.get_height(), 1706);
328+
assert_eq!(msg.get_width(), 960);
329329

330330
Ok(())
331331
}

0 commit comments

Comments
 (0)