Skip to content

Commit 0f35192

Browse files
vmoranyalldawson
authored andcommitted
Add cropTransparent to QgsImageOperation, for cropping transparent
borders from around an image
1 parent 6a8526f commit 0f35192

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

python/core/effects/qgsimageoperation.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,13 @@ class QgsImageOperation
131131
*/
132132
static void flipImage( QImage &image, FlipType type );
133133

134+
/** Crop any transparent border from around an image.
135+
* @param image source image
136+
* @param minSize minimum size for cropped image, if desired. If the
137+
* cropped image is smaller than the minimum size, it will be centered
138+
* in the returned image.
139+
* @note added in QGIS 2.9
140+
*/
141+
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize() );
142+
134143
};

src/core/effects/qgsimageoperation.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,44 @@ void QgsImageOperation::flipImage( QImage &image, QgsImageOperation::FlipType ty
793793
runLineOperation( image, flipOperation );
794794
}
795795

796+
QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize )
797+
{
798+
int width = image.width();
799+
int height = image.height();
800+
int xmin = width;
801+
int xmax = 0;
802+
int ymin = height;
803+
int ymax = 0;
804+
805+
for ( int x = 0; x < width; ++x )
806+
{
807+
for ( int y = 0; y < height; ++y )
808+
{
809+
if ( qAlpha( image.pixel( x, y ) ) )
810+
{
811+
xmin = qMin( x, xmin );
812+
xmax = qMax( x, xmax );
813+
ymin = qMin( y, ymin );
814+
ymax = qMax( y, ymax );
815+
}
816+
}
817+
}
818+
if ( minSize.isValid() )
819+
{
820+
if ( xmax - xmin < minSize.width() ) // centers image on x
821+
{
822+
xmin = qMax(( xmax + xmin ) / 2 - minSize.width() / 2, 0 );
823+
xmax = xmin + minSize.width();
824+
}
825+
if ( ymax - ymin < minSize.height() ) // centers image on y
826+
{
827+
ymin = qMax(( ymax + ymin ) / 2 - minSize.height() / 2, 0 );
828+
ymax = ymin + minSize.height();
829+
}
830+
}
831+
return image.copy( xmin, ymin, xmax - xmin, ymax - ymin );
832+
}
833+
796834
void QgsImageOperation::FlipLineOperation::operator()( QRgb *startRef, const int lineLength, const int bytesPerLine )
797835
{
798836
int increment = ( mDirection == QgsImageOperation::ByRow ) ? 4 : bytesPerLine;

src/core/effects/qgsimageoperation.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ class CORE_EXPORT QgsImageOperation
162162
*/
163163
static void flipImage( QImage &image, FlipType type );
164164

165+
/** Crop any transparent border from around an image.
166+
* @param image source image
167+
* @param minSize minimum size for cropped image, if desired. If the
168+
* cropped image is smaller than the minimum size, it will be centered
169+
* in the returned image.
170+
* @note added in QGIS 2.9
171+
*/
172+
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize() );
173+
165174
private:
166175

167176
//for blocked operations

0 commit comments

Comments
 (0)