Skip to content

Commit c38ff51

Browse files
committed
Followup b8bc181, don't create unnecessary temporary image
1 parent 09192ee commit c38ff51

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

python/core/effects/qgsimageoperation.sip

+12-1
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,23 @@ class QgsImageOperation
131131
*/
132132
static void flipImage( QImage &image, FlipType type );
133133

134+
/** Calculates the non-transparent region of an image.
135+
* @param image source image
136+
* @param minSize minimum size for returned region, if desired. If the
137+
* non-transparent region of the image is smaller than this minimum size,
138+
* it will be centered in the returned rectangle.
139+
* @param center return rectangle will be centered on the center of the original image if set to true
140+
* @note added in QGIS 2.9
141+
* @see cropTransparent
142+
*/
143+
static QRect nonTransparentImageRect( const QImage & image, const QSize& minSize = QSize(), bool center = false );
144+
134145
/** Crop any transparent border from around an image.
135146
* @param image source image
136147
* @param minSize minimum size for cropped image, if desired. If the
137148
* cropped image is smaller than the minimum size, it will be centered
138149
* in the returned image.
139-
* @param center croped image will be centered on the center of the original image
150+
* @param center cropped image will be centered on the center of the original image if set to true
140151
* @note added in QGIS 2.9
141152
*/
142153
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );

src/core/effects/qgsimageoperation.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ void QgsImageOperation::flipImage( QImage &image, QgsImageOperation::FlipType ty
793793
runLineOperation( image, flipOperation );
794794
}
795795

796-
QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center )
796+
QRect QgsImageOperation::nonTransparentImageRect( const QImage &image, const QSize &minSize, bool center )
797797
{
798798
int width = image.width();
799799
int height = image.height();
@@ -838,7 +838,13 @@ QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &min
838838
ymin = qMax( 0, height / 2 - dy );
839839
ymax = qMin( height, height / 2 + dy );
840840
}
841-
return image.copy( xmin, ymin, xmax - xmin, ymax - ymin );
841+
842+
return QRect( xmin, ymin, xmax - xmin, ymax - ymin );
843+
}
844+
845+
QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center )
846+
{
847+
return image.copy( QgsImageOperation::nonTransparentImageRect( image, minSize, center ) );
842848
}
843849

844850
void QgsImageOperation::FlipLineOperation::operator()( QRgb *startRef, const int lineLength, const int bytesPerLine )

src/core/effects/qgsimageoperation.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,23 @@ class CORE_EXPORT QgsImageOperation
162162
*/
163163
static void flipImage( QImage &image, FlipType type );
164164

165+
/** Calculates the non-transparent region of an image.
166+
* @param image source image
167+
* @param minSize minimum size for returned region, if desired. If the
168+
* non-transparent region of the image is smaller than this minimum size,
169+
* it will be centered in the returned rectangle.
170+
* @param center return rectangle will be centered on the center of the original image if set to true
171+
* @note added in QGIS 2.9
172+
* @see cropTransparent
173+
*/
174+
static QRect nonTransparentImageRect( const QImage & image, const QSize& minSize = QSize(), bool center = false );
175+
165176
/** Crop any transparent border from around an image.
166177
* @param image source image
167178
* @param minSize minimum size for cropped image, if desired. If the
168179
* cropped image is smaller than the minimum size, it will be centered
169180
* in the returned image.
170-
* @param center croped image will be centered on the center of the original image
181+
* @param center cropped image will be centered on the center of the original image if set to true
171182
* @note added in QGIS 2.9
172183
*/
173184
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );

src/core/layertree/qgslayertreemodellegendnode.cpp

+9-18
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,19 @@ QSize QgsSymbolV2LegendNode::minimumIconSize() const
163163
if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker )
164164
{
165165
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
166-
QPixmap pix = QPixmap::fromImage(
167-
QgsImageOperation::cropTransparent(
168-
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
169-
mItem.symbol(),
170-
QSize( 512, 512 ),
171-
context.data() ).toImage(),
172-
mIconSize,
173-
true ) );
174-
minSz = pix.size();
166+
minSz = QgsImageOperation::nonTransparentImageRect(
167+
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( 512, 512 ), context.data() ).toImage(),
168+
mIconSize,
169+
true ).size();
175170
}
176171
else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line )
177172
{
178173
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
179-
QPixmap pix = QPixmap::fromImage(
180-
QgsImageOperation::cropTransparent(
181-
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
182-
mItem.symbol(),
183-
QSize( mIconSize.width(), 512 ),
184-
context.data() ).toImage(),
185-
mIconSize,
186-
true ) );
187-
minSz = pix.size();
174+
minSz = QgsImageOperation::nonTransparentImageRect(
175+
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( mIconSize.width(), 512 ),
176+
context.data() ).toImage(),
177+
mIconSize,
178+
true ).size();
188179
}
189180
else
190181
{

0 commit comments

Comments
 (0)