Skip to content

Commit

Permalink
Allow passing {width,height}-only size to the image cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 5, 2018
1 parent c1df802 commit ce5636a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/core/qgsimagecache.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
26 changes: 25 additions & 1 deletion tests/src/core/testqgsimagecache.cpp
Expand Up @@ -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();

};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce5636a

Please sign in to comment.