Skip to content

Commit 8c7d772

Browse files
committed
Further optimisations to nonTransparentImageRect calculation
1 parent c9251c5 commit 8c7d772

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

src/core/effects/qgsimageoperation.cpp

+50-7
Original file line numberDiff line numberDiff line change
@@ -806,19 +806,62 @@ QRect QgsImageOperation::nonTransparentImageRect( const QImage &image, QSize min
806806
int ymin = height;
807807
int ymax = 0;
808808

809-
for ( int x = 0; x < width; ++x )
809+
// scan down till we hit something
810+
for ( int y = 0; y < height; ++y )
810811
{
811-
for ( int y = 0; y < height; ++y )
812+
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
813+
for ( int x = 0; x < width; ++x )
814+
{
815+
if ( qAlpha( imgScanline[x] ) )
816+
{
817+
ymin = y;
818+
xmin = x;
819+
xmax = x;
820+
}
821+
}
822+
}
823+
824+
//scan up till we hit something
825+
for ( int y = height - 1; y > ymin; --y )
826+
{
827+
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
828+
for ( int x = 0; x < width; ++x )
829+
{
830+
if ( qAlpha( imgScanline[x] ) )
831+
{
832+
ymax = y;
833+
xmin = qMin( xmin, x );
834+
xmax = qMax( xmax, x );
835+
}
836+
}
837+
}
838+
839+
//scan left to right till we hit something, using a refined y region
840+
for ( int y = ymin; y <= ymax; ++y )
841+
{
842+
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
843+
for ( int x = 0; x < xmin; ++x )
812844
{
813-
if ( qAlpha( image.pixel( x, y ) ) )
845+
if ( qAlpha( imgScanline[x] ) )
814846
{
815-
xmin = qMin( x, xmin );
816-
xmax = qMax( x, xmax );
817-
ymin = qMin( y, ymin );
818-
ymax = qMax( y, ymax );
847+
xmin = x;
819848
}
820849
}
821850
}
851+
852+
//scan right to left till we hit something, using the refined y region
853+
for ( int y = ymin; y <= ymax; ++y )
854+
{
855+
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
856+
for ( int x = width - 1; x > xmax; --x )
857+
{
858+
if ( qAlpha( imgScanline[x] ) )
859+
{
860+
xmax = x;
861+
}
862+
}
863+
}
864+
822865
if ( minSize.isValid() )
823866
{
824867
if ( xmax - xmin < minSize.width() ) // centers image on x

0 commit comments

Comments
 (0)