Allow passing {width,height}-only size to the image cache

This commit is contained in:
nirvn 2018-12-04 13:09:00 +07:00 committed by Mathieu Pellerin
parent c1df802769
commit ce5636afed
2 changed files with 32 additions and 2 deletions

View File

@ -115,8 +115,8 @@ QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const
if ( currentEntry->image.isNull() )
{
long cachedDataSize = 0;
cachedDataSize += currentEntry->size.width() * currentEntry->size.height() * 32;
result = renderImage( file, size, keepAspectRatio );
cachedDataSize += result.width() * result.height() * 32;
if ( cachedDataSize > mMaxCacheSize / 2 )
{
fitsInCache = false;
@ -214,6 +214,12 @@ QImage QgsImageCache::renderImage( const QString &path, QSize size, const bool k
// render image at desired size -- null size means original size
if ( !size.isValid() || size.isNull() || im.size() == size )
return im;
// when original aspect ratio is respected and provided height value is 0, automatically compute height
else if ( keepAspectRatio && size.height() == 0 )
return im.scaledToWidth( size.width(), Qt::SmoothTransformation );
// when original aspect ratio is respected and provided width value is 0, automatically compute width
else if ( keepAspectRatio && size.width() == 0 )
return im.scaledToHeight( size.height(), Qt::SmoothTransformation );
else
return im.scaled( size, keepAspectRatio ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
}

View File

@ -49,7 +49,8 @@ class TestQgsImageCache : public QObject
void cleanup() {} // will be called after every testfunction.
void fillCache();
void threadSafeImage();
void changeImage(); //check that cache is updated if image source file changes
void changeImage(); // check that cache is updated if image source file changes
void size(); // check various size-specific handling
void base64();
};
@ -193,6 +194,29 @@ void TestQgsImageCache::changeImage()
QVERIFY( imageCheck( "imagecache_changed_before", img, 30 ) );
}
void TestQgsImageCache::size()
{
QgsImageCache cache;
QImage img;
bool inCache;
QString originalImage = TEST_DATA_DIR + QStringLiteral( "/sample_image.png" );
// null size should return image using original size
img = cache.pathAsImage( originalImage, QSize(), true, 1.0, inCache );
QCOMPARE( img.width(), 511 );
QCOMPARE( img.height(), 800 );
// a size with an height set to 0 while keep aspect ratio is true should return an image with automatically computed height
img = cache.pathAsImage( originalImage, QSize( 100, 0 ), true, 1.0, inCache );
QCOMPARE( img.width(), 100 );
QCOMPARE( img.height(), 157 );
// a size with an width set to 0 while keep aspect ratio is true should return an image with automatically computed width
img = cache.pathAsImage( originalImage, QSize( 0, 100 ), true, 1.0, inCache );
QCOMPARE( img.width(), 64 );
QCOMPARE( img.height(), 100 );
}
void TestQgsImageCache::base64()
{
// test rendering images encoded in base 64