@@ -806,19 +806,62 @@ QRect QgsImageOperation::nonTransparentImageRect( const QImage &image, QSize min
806
806
int ymin = height;
807
807
int ymax = 0 ;
808
808
809
- for ( int x = 0 ; x < width; ++x )
809
+ // scan down till we hit something
810
+ for ( int y = 0 ; y < height; ++y )
810
811
{
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 )
812
844
{
813
- if ( qAlpha ( image. pixel ( x, y ) ) )
845
+ if ( qAlpha ( imgScanline[x] ) )
814
846
{
815
- xmin = qMin ( x, xmin );
816
- xmax = qMax ( x, xmax );
817
- ymin = qMin ( y, ymin );
818
- ymax = qMax ( y, ymax );
847
+ xmin = x;
819
848
}
820
849
}
821
850
}
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
+
822
865
if ( minSize.isValid () )
823
866
{
824
867
if ( xmax - xmin < minSize.width () ) // centers image on x
0 commit comments