Skip to content

Set sourceSize in for images, improving memory usage and smoothing

Joshua Goins requested to merge work/redstrate/better-image-delegate into master

In some cases (where we don't get a thumbnail) we load the entire image, which might be large. Even when we get a thumbnail, the smoothing we apply doesn't get the best results and the only other option is mipmapping which is more memory intensive - it stores progressively smaller images but keeps all of them in memory.

Now the ImageDelegate sets the sourceSize, which helps reduce the memory footprint (since Qt will throw away whatever parts of the image we don't need) and makes the images look smoother when scaled. However, AnimatedImages do not support this so this change introduces a new property to tell if the image could possibly be animated. It will erroneously pick up formats that could support animation (e.g. WebP) but other image formats will still benefit so it's a net positive. The ImageDelegate will load the correct image component depending on this property.

I wasn't kidding about the memory footprint, if you have a a 4K image in a message (with no thumbnail), there's a 30 megabyte savings with this change. Yay! I'm not sure how often Qt will recycle unused images once they're resized though.

Before After
image image

There's one caveat though - if the image has no width/height given to us then it will be incorrectly scaled to 16x9. This doesn't hurt the aspect cropping, but it's something to be aware of. The only real way to fix this (as far I can tell) is to read the image ahead of time. Image's QML API is kinda dumb, there's no way to get the intrinsic size of the image after you resize it.

Fixes #321 (closed)

Merge request reports