feat: Use resize when resampling images with up to 4 megapixels#8001
feat: Use resize when resampling images with up to 4 megapixels#800172374 wants to merge 2 commits into
resize when resampling images with up to 4 megapixels#8001Conversation
dca6ba7 to
d8397bc
Compare
d8397bc to
ead508e
Compare
|
Just in case, the previous discussion is at #6815 |
|
One question that might be relevant for this PR: If those apply to the image, that could result in downscaling twice; first by the image-editor, to 1536x1536, and then by Core, to 1280x1280, and it would also mean that the change in this PR would apply to all images edited in the Android-app, because the upper limit is 1536x1536. I set this PR back to draft, because #7822 can change what is appropriate to do in this PR. With those changes, images with a resolution of up to 1536×10661 would not be resampled, because that is a lower resolution than 1280x12802. Footnotes |
|
I think, that the "4+ times slower" performance-test-result may be outdated, or perhaps was measured incorrectly. |
3e8b19c to
ab579e7
Compare
Not sure my computer is slow, but i'm going to recheck this. Maybe you can also try QEMU. See #6815 , the reason why it was closed wasn't only performance, but also reduction of sharpness when using EDIT: @72374 , nothing has changed for me since #6815 (comment), have ... The code comment says it's 4+ times slower, obviously it depends on hardware and i'm not sure the difference is bigger on old hardware, probably on new one you just won't notice the difference. ... Also i see more blur and less details in real images. An improvement i can notice only on synthetic ones which trigger aliasing effects on purpose. I don't want to say that ... One can use this for testing: let now = std::time::Instant::now();
let new_img = if crate::tools::time() % 60 < 30 {
info!(context, "thumbnail()...");
img.thumbnail(target_wh, target_wh)
} else {
info!(context, "resize()...");
img.resize(target_wh, target_wh, image::imageops::FilterType::Triangle)
};
info!(context, "time: {}", (std::time::Instant::now() - now).as_nanos()); |
|
Thank you; for checking, and the code to measure the time. :) I used the code, to measure the difference for sending an image in JPG-format with a resolution of 12.5 MP: Regarding the proposal to increase the resolution-limit:
The file-size-difference is probably both due to the averaging of the
See the comparison that i added to the pull-request-description. It is simply a screenshot of text and UI in a text-editor, cropped to fit within 2.56 MP, so the currently set condition to use The comparison in #6815 (comment) also shows some other visual issues (wavy lines instead of straight, angled lines; and rough lines that should be smooth). Footnotes
|
Agree, switching to Maybe a compromise also -- not to loose sharpness of real photos -- is to increase the limit not to 1440, but even more and a bit decrease the JPEG quality, say, to 65. However, this way the amount of JPEG metadata will grow, so we should be careful here. We can even use different JPEG quality for resized and not resized images. So, this is a complex topic requiring many experiments, we can't change the used algo only looking at a couple of test images which we have here. Overall, i think we should maintain the same image size as before, at least not to cause new complains from users such as loss of quality or increased traffic. If we want to optimize quality/traffic tradeoff, that should be a separate change. |
|
Here another comparison of images of text; this one in a language with more detailed characters; 日本語 - Japanese: With the Such issues with text, were also noticeable on a photo, that i compared for this. Regarding sharpness-reduction:More sharpness is not the same as better quality; for photos and anime-style art (typically based on line-art) in particular, artificial sharpening tends to result in less accurate depth-perception (for example: making things appear closer to the front than those should be, sometimes making things look like flat layers of paper, and/or visually detaching things from the place where those are supposed to be attached to). Though, #7822 increased the maximum pixel-count for images with an aspect-ratio of 4:31 by 33%, by 78% for 16:9, and more, if the image is less square; which means, that after a switch to Regarding increasing the limit above 1440:To me, that does not seem appropriately "balanced", because screens with a resolution higher than 1920 * 1080 are less common2, so the improvement would often only be relevant when zooming in. I think that the current limit is acceptable (though not good) for practical use; but if an increase is wanted, it would be good to do that before a version of Delta Chat with #7822 is published, to not increase it twice within a short time. Setting it to 1440, would increase the pixel-count by up to 25%. Regarding the size of JPEG-metadata:The proportion of it increases, not the amount; though, it is a relatively tiny amount of data either way. So it should not be practically relevant. Regarding the need for more quality-testing:I would agree, if this was about changing a reasonably good resampling-algorithm (like About performance:I think that the < 50% time-difference is not an issue, and This should be about all that i want to write about this currently. I tried to keep it short, but there is too much to consider. ^^' Footnotes
|
|
I adjusted this a little, because, according to the results of the time-measurements, the difference is practically much smaller. Though, as mentioned, i think that always using |
resize when resampling images with up to 2.56 megapixelsresize when resampling images with up to 4 megapixels
|
I've measured the whole |
|
thanks a lot for the detailed discussions! where are the images we're optimising for here are expected to come from? 4 megapixel cameras are outdated since about 20 years - and for avatars we're using resize() already. what is the expected scope? png files and diagrams? images edited by android already? otoh, as it does not affect most images, we can maybe be more relaxed :) i am wondering about the only 50% slower - at #6815 (comment) we were talking about 3-4 times slower. maybe that needs to be double checked, tho also 3-4 times slower may be fine as we're not using the expensive algorithm for large images from the camera |
I think the PR is for screenshots mostly. At least the PR description provides screenshots.
That time i only measured time spent on resizing, it's indeed still ~3 times slower with |
|
There are many sources for images with a resolution below 4 MP; for example:
Though, ideally, a better resampling-algorithm, than the
This is intended to improve the quality of images in general, including photos, screenshots, and digital art; though the visual issues are most easily seen with text (also in photos), and other fine details, which is why i used screenshots of text as an example.
Indeed. Older hardware could be much slower for some reason, so it would be good to have a practical confirmation of the time-differences. Though, theoretically, when considering a 60% longer total processing-time2 while (Time between line 2608 and line 2636 in Footnotes
|
For high-resolution images
The reason of aliasing effects is that for lower-resolution images source pixel weights are very different (from 0.25 to 1). So probably it doesn't make much sense to use |
|
Some of the quality-issues caused by the I added images that were resampled while using the lower Personally, i would not care much about any of the mentioned time-differences. |
|
I tested the performance with a PinePhone1: The time-difference, of the A release-build should be about 15% faster. I looked for some smartphones with similarly slow processors, and those seem to usually have cameras with 8-16 MP. Resampling images with the "Image"-library is apparently as slow as it is, because it does that single-threaded; including the (de)coding of JPG-images. Footnotes
|
resize when resampling images with up to 4 megapixelsresize when resampling images with up to 3840 * 2160 pixels
Thanks for testing, but my opinion as a user is that if i sent screenshots often, i'm not sure i would be fine even with +30%, so i think we should have some linear size limit. According to your measuremts, switching to |
|
i would be kind of fine to give this would not slow down processing most camera images, but already many screenshots and png would benefit. and the additional time used will be cut, as high-res images are not processed and we do not come in regions where processing takes tens of seconds longer. i am also fine to increase the image size to 1312 px - this will also slightly improve quality, by a little bit of larger images when the PR is small enough to be reverted if that makes troubles in practise. otherwise, in general, speed wins. while it is reasonable to optimise within speed constrains, so best quality using same speed, it is a non-goal to have the best thinkable image quality and being slow. if that is really needed, one can always send an image as file. for most user, it just does not matter. again, thanks a lot for all these thoughts! |
iequidoo
left a comment
There was a problem hiding this comment.
I agree with r10s about the 4 Mpx limit, we can always increase it later. Personally i don't even have a device with a 4-Mpx screen
resize when resampling images with up to 3840 * 2160 pixelsresize when resampling images with up to 4 megapixels
| 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(214468)); |
There was a problem hiding this comment.
Btw, why are the images still smaller in these tests despite they have a larger resolution? Maybe 1312 px isn't enough and we should increase it more?
There was a problem hiding this comment.
1344 would result in similar file-sizes in these tests.
I do not have much of an opinion about small changes to this.














The
thumbnail-function can resample images quickly, but with very noticeable aliasing when it is used to resample images to a close resolution (~80% of the original resolution per dimension, for example). Even if the resolution-difference is large, the quality of the resampling is rather flawed. The function seems to be for quickly resampling lots of images into tiny preview-images (less than about 2 x 2 cm , when considering that "Thumbnail" is a reference to the intended size), for which the quality is not particularly important, and the resolution-difference is usually large (~25% of the original resolution per dimension, for example).The amount of processing/time (including decoding and encoding) required to resample images, depends on the resolution of the original image.
By using the
thumbnail-function only for images with a high resolution, many images will be resampled with the better resampling of theresize-function, while images with a high resolution, will still be resampled more quickly.Quality-comparison:
Original image (2626 x 974 | 2.558 MP | 2.7MB)
main-branch:This PR:
