Skip to content

Commit ce5636a

Browse files
committedDec 5, 2018
Allow passing {width,height}-only size to the image cache
1 parent c1df802 commit ce5636a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
 

‎src/core/qgsimagecache.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const
115115
if ( currentEntry->image.isNull() )
116116
{
117117
long cachedDataSize = 0;
118-
cachedDataSize += currentEntry->size.width() * currentEntry->size.height() * 32;
119118
result = renderImage( file, size, keepAspectRatio );
119+
cachedDataSize += result.width() * result.height() * 32;
120120
if ( cachedDataSize > mMaxCacheSize / 2 )
121121
{
122122
fitsInCache = false;
@@ -214,6 +214,12 @@ QImage QgsImageCache::renderImage( const QString &path, QSize size, const bool k
214214
// render image at desired size -- null size means original size
215215
if ( !size.isValid() || size.isNull() || im.size() == size )
216216
return im;
217+
// when original aspect ratio is respected and provided height value is 0, automatically compute height
218+
else if ( keepAspectRatio && size.height() == 0 )
219+
return im.scaledToWidth( size.width(), Qt::SmoothTransformation );
220+
// when original aspect ratio is respected and provided width value is 0, automatically compute width
221+
else if ( keepAspectRatio && size.width() == 0 )
222+
return im.scaledToHeight( size.height(), Qt::SmoothTransformation );
217223
else
218224
return im.scaled( size, keepAspectRatio ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
219225
}

‎tests/src/core/testqgsimagecache.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class TestQgsImageCache : public QObject
4949
void cleanup() {} // will be called after every testfunction.
5050
void fillCache();
5151
void threadSafeImage();
52-
void changeImage(); //check that cache is updated if image source file changes
52+
void changeImage(); // check that cache is updated if image source file changes
53+
void size(); // check various size-specific handling
5354
void base64();
5455

5556
};
@@ -193,6 +194,29 @@ void TestQgsImageCache::changeImage()
193194
QVERIFY( imageCheck( "imagecache_changed_before", img, 30 ) );
194195
}
195196

197+
void TestQgsImageCache::size()
198+
{
199+
QgsImageCache cache;
200+
QImage img;
201+
bool inCache;
202+
QString originalImage = TEST_DATA_DIR + QStringLiteral( "/sample_image.png" );
203+
204+
// null size should return image using original size
205+
img = cache.pathAsImage( originalImage, QSize(), true, 1.0, inCache );
206+
QCOMPARE( img.width(), 511 );
207+
QCOMPARE( img.height(), 800 );
208+
209+
// a size with an height set to 0 while keep aspect ratio is true should return an image with automatically computed height
210+
img = cache.pathAsImage( originalImage, QSize( 100, 0 ), true, 1.0, inCache );
211+
QCOMPARE( img.width(), 100 );
212+
QCOMPARE( img.height(), 157 );
213+
214+
// a size with an width set to 0 while keep aspect ratio is true should return an image with automatically computed width
215+
img = cache.pathAsImage( originalImage, QSize( 0, 100 ), true, 1.0, inCache );
216+
QCOMPARE( img.width(), 64 );
217+
QCOMPARE( img.height(), 100 );
218+
}
219+
196220
void TestQgsImageCache::base64()
197221
{
198222
// test rendering images encoded in base 64

0 commit comments

Comments
 (0)
Please sign in to comment.