Skip to content

Commit

Permalink
Followup b8bc181, don't create unnecessary temporary image
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 20, 2015
1 parent 09192ee commit c38ff51
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
13 changes: 12 additions & 1 deletion python/core/effects/qgsimageoperation.sip
Expand Up @@ -131,12 +131,23 @@ class QgsImageOperation
*/
static void flipImage( QImage &image, FlipType type );

/** Calculates the non-transparent region of an image.
* @param image source image
* @param minSize minimum size for returned region, if desired. If the
* non-transparent region of the image is smaller than this minimum size,
* it will be centered in the returned rectangle.
* @param center return rectangle will be centered on the center of the original image if set to true
* @note added in QGIS 2.9
* @see cropTransparent
*/
static QRect nonTransparentImageRect( const QImage & image, const QSize& minSize = QSize(), bool center = false );

/** Crop any transparent border from around an image.
* @param image source image
* @param minSize minimum size for cropped image, if desired. If the
* cropped image is smaller than the minimum size, it will be centered
* in the returned image.
* @param center croped image will be centered on the center of the original image
* @param center cropped image will be centered on the center of the original image if set to true
* @note added in QGIS 2.9
*/
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );
Expand Down
10 changes: 8 additions & 2 deletions src/core/effects/qgsimageoperation.cpp
Expand Up @@ -793,7 +793,7 @@ void QgsImageOperation::flipImage( QImage &image, QgsImageOperation::FlipType ty
runLineOperation( image, flipOperation );
}

QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center )
QRect QgsImageOperation::nonTransparentImageRect( const QImage &image, const QSize &minSize, bool center )
{
int width = image.width();
int height = image.height();
Expand Down Expand Up @@ -838,7 +838,13 @@ QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &min
ymin = qMax( 0, height / 2 - dy );
ymax = qMin( height, height / 2 + dy );
}
return image.copy( xmin, ymin, xmax - xmin, ymax - ymin );

return QRect( xmin, ymin, xmax - xmin, ymax - ymin );
}

QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center )
{
return image.copy( QgsImageOperation::nonTransparentImageRect( image, minSize, center ) );
}

void QgsImageOperation::FlipLineOperation::operator()( QRgb *startRef, const int lineLength, const int bytesPerLine )
Expand Down
13 changes: 12 additions & 1 deletion src/core/effects/qgsimageoperation.h
Expand Up @@ -162,12 +162,23 @@ class CORE_EXPORT QgsImageOperation
*/
static void flipImage( QImage &image, FlipType type );

/** Calculates the non-transparent region of an image.
* @param image source image
* @param minSize minimum size for returned region, if desired. If the
* non-transparent region of the image is smaller than this minimum size,
* it will be centered in the returned rectangle.
* @param center return rectangle will be centered on the center of the original image if set to true
* @note added in QGIS 2.9
* @see cropTransparent
*/
static QRect nonTransparentImageRect( const QImage & image, const QSize& minSize = QSize(), bool center = false );

/** Crop any transparent border from around an image.
* @param image source image
* @param minSize minimum size for cropped image, if desired. If the
* cropped image is smaller than the minimum size, it will be centered
* in the returned image.
* @param center croped image will be centered on the center of the original image
* @param center cropped image will be centered on the center of the original image if set to true
* @note added in QGIS 2.9
*/
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );
Expand Down
27 changes: 9 additions & 18 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -163,28 +163,19 @@ QSize QgsSymbolV2LegendNode::minimumIconSize() const
if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker )
{
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
QPixmap pix = QPixmap::fromImage(
QgsImageOperation::cropTransparent(
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
mItem.symbol(),
QSize( 512, 512 ),
context.data() ).toImage(),
mIconSize,
true ) );
minSz = pix.size();
minSz = QgsImageOperation::nonTransparentImageRect(
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( 512, 512 ), context.data() ).toImage(),
mIconSize,
true ).size();
}
else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line )
{
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
QPixmap pix = QPixmap::fromImage(
QgsImageOperation::cropTransparent(
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
mItem.symbol(),
QSize( mIconSize.width(), 512 ),
context.data() ).toImage(),
mIconSize,
true ) );
minSz = pix.size();
minSz = QgsImageOperation::nonTransparentImageRect(
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( mIconSize.width(), 512 ),
context.data() ).toImage(),
mIconSize,
true ).size();
}
else
{
Expand Down

0 comments on commit c38ff51

Please sign in to comment.