Skip to content
Permalink
Browse files

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

  • Loading branch information
nirvn committed Dec 4, 2018
1 parent c1df802 commit ce5636afedcf5ea73ca095a96dcd23c9d8e17018
Showing with 32 additions and 2 deletions.
  1. +7 −1 src/core/qgsimagecache.cpp
  2. +25 −1 tests/src/core/testqgsimagecache.cpp
@@ -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 );
}
@@ -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

0 comments on commit ce5636a

Please sign in to comment.
You can’t perform that action at this time.