[Development] Image and PreserveAspectCrop

Albert Astals Cid albert.astals at canonical.com
Mon Jul 18 10:24:11 CEST 2016


There is a problem when trying to optimally[*] show an Image with
PreserveAspectCrop fillMode.
[*]optimally => as best looking as possible while using as litte
memory as possible

You can see that problem in the screenshot at http://i.imgur.com/LSSlFEB.png
that corresponds with the code at http://paste.ubuntu.com/19480453/

As you can see when displaying a landscape (width > height) image in a
square Image Item the optimal way
is to set the source size height only, but if the image is portrait
(height > width) then the optimal way
is to set the source size width only.

The requirement my program has is to have the best rendering quality
and memory usage for Image Items using PreserveAspectCrop.
Image sources are totally arbitrary, they can be from disk, from the
internet or even from QQuickImageProviders
(since we are plugin based and plugins can bring their own QQuickImageProvider)

This can be fixed in several ways.

Workaround A
**********
Changing the Image Item source size comparing the aspect ratio of the
image file with the one Image Item.

You can see an implementation of that workaround at
http://bazaar.launchpad.net/~unity-team/unity8/trunk/view/head:/plugins/Dash/CroppedImageMinimumSourceSize.qml

The problem with this workaround is that half of the times you end up
loading the image a second time.
This means extra CPU and potentially network usage.



Workaround B
**********
Implementing your own image provider that does compare the aspect
ratios before loading the image.

You can see a partial implementation of this workaround at
https://code.launchpad.net/~aacid/unity8/croppedImageMinimumSourceSizeProvider/+merge/300176

There are two problems with this workaround:
 * You end up implementing quite a bit of duplicated functionality
from qquickpixmapcache.cpp
 * For the chained image providers (i.e. the original source was an
image provider url) you
   still have to query the image provider twice half of the times



Solution
********
Implementing the change in QtQuick internals so that when
PreserveAspectCrop fillMode is used
together with a sourceSize that has both width and height it does
return the optimal image

You can see a work in progress implementation of this solution at
https://codereview.qt-project.org/#/c/165299/
And how the previews could would look at
http://i.imgur.com/NRoXNzy.png (notice how the last column now is good
in both cases)

There are two issues with this solution:
 * It's a small behaviour change (but in my opinion for the better)
 * Needs new api for the QQuickImageProvider to be able to implement
it, so we either need the proposed
   QQuickImageProviderV2 or with a new "bool
shouldPreserveAspectRatioCrop(url, requestSize)" getter in the
   existing QQuickImageProvider API



All in all I think the solution i propose for QtQuick is acceptable
but i would like some agreeing that is fine adding new API before
finishing the patch.



More information about the Development mailing list