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
14 changes: 12 additions & 2 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ impl<'a> BlobObject<'a> {
// also `Viewtype::Gif` (maybe renamed to `Animation`) should be used for animated
// images.
let do_scale = exceeds_max_bytes
// Don't recode huge JPEGs w/o resizing:
// - It may be huge because of high JPEG quality, but we don't know that.
// - We don't want extra CPU work for most photos.
&& (fmt == ImageFormat::Jpeg
|| encoded_img_exceeds_bytes(
context,
&img,
ofmt.clone(),
max_bytes,
&mut encoded,
)?)
|| is_avatar
&& (exceeds_wh
|| exif.is_some() && {
Expand Down Expand Up @@ -480,8 +491,7 @@ impl<'a> BlobObject<'a> {
}
}
}

if do_scale || exif.is_some() {
if !encoded.is_empty() || exif.is_some() {
// The file format is JPEG/PNG now, we may have to change the file extension
if !matches!(fmt, ImageFormat::Jpeg)
&& matches!(ofmt, ImageOutputFormat::Jpeg { .. })
Expand Down
20 changes: 20 additions & 0 deletions src/blob/blob_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,26 @@ async fn test_recode_image_balanced_png() {
.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_recode_image_balanced_png_huge() {
let bytes = include_bytes!("../../test-data/image/screenshot-huge.png");

SendImageCheckMediaquality {
viewtype: Viewtype::Image,
media_quality_config: "0",
bytes,
extension: "jpg",
original_width: 1618,
original_height: 949,
compressed_width: 1618,
compressed_height: 949,
..Default::default()
}
.test()
.await
.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sticker_with_exif() {
let bytes = include_bytes!("../../test-data/image/logo-exif.png");
Expand Down
3 changes: 2 additions & 1 deletion src/tests/pre_messages/additional_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ 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 – 146.12 KiB]".to_owned());
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's annoying to fix this number every time we change image encoding, even JPEG quality can't be changed

assert!(msg.get_text().starts_with("test [Image – "));
assert!(msg.get_text().ends_with(" KiB]"));

Ok(())
}
9 changes: 6 additions & 3 deletions src/tests/pre_messages/receiving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use pretty_assertions::assert_eq;

use crate::EventType;
use crate::chat;
use crate::constants;
use crate::contact;
use crate::download::{DownloadState, PRE_MSG_ATTACHMENT_SIZE_THRESHOLD, PostMsgMetadata};
use crate::message::{Message, MessageState, Viewtype, delete_msgs, markseen_msgs};
Expand Down Expand Up @@ -393,9 +394,11 @@ 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(149632));
assert_eq!(msg.get_height(), 1280);
assert_eq!(msg.get_width(), 720);
let n_bytes: usize = msg.get_filebytes(bob).await?.unwrap().try_into().unwrap();
assert!(100_000 < n_bytes);
assert!(n_bytes <= constants::BALANCED_IMAGE_BYTES);
assert_eq!(msg.get_height(), 1920);
assert_eq!(msg.get_width(), 1080);

Ok(())
}
Expand Down
Binary file added test-data/image/screenshot-huge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading