Skip to content
Permalink
Browse files

Further optimisations to nonTransparentImageRect calculation

  • Loading branch information
nyalldawson committed Oct 19, 2016
1 parent c9251c5 commit 8c7d772f22339495f9650554cd23a99a39a8702d
Showing with 50 additions and 7 deletions.
  1. +50 −7 src/core/effects/qgsimageoperation.cpp
@@ -806,19 +806,62 @@ QRect QgsImageOperation::nonTransparentImageRect( const QImage &image, QSize min
int ymin = height;
int ymax = 0;

for ( int x = 0; x < width; ++x )
// scan down till we hit something
for ( int y = 0; y < height; ++y )
{
for ( int y = 0; y < height; ++y )
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
for ( int x = 0; x < width; ++x )
{
if ( qAlpha( imgScanline[x] ) )
{
ymin = y;
xmin = x;
xmax = x;
}
}
}

//scan up till we hit something
for ( int y = height - 1; y > ymin; --y )
{
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
for ( int x = 0; x < width; ++x )
{
if ( qAlpha( imgScanline[x] ) )
{
ymax = y;
xmin = qMin( xmin, x );
xmax = qMax( xmax, x );
}
}
}

//scan left to right till we hit something, using a refined y region
for ( int y = ymin; y <= ymax; ++y )
{
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
for ( int x = 0; x < xmin; ++x )
{
if ( qAlpha( image.pixel( x, y ) ) )
if ( qAlpha( imgScanline[x] ) )
{
xmin = qMin( x, xmin );
xmax = qMax( x, xmax );
ymin = qMin( y, ymin );
ymax = qMax( y, ymax );
xmin = x;
}
}
}

//scan right to left till we hit something, using the refined y region
for ( int y = ymin; y <= ymax; ++y )
{
const QRgb* imgScanline = reinterpret_cast< const QRgb* >( image.constScanLine( y ) );
for ( int x = width - 1; x > xmax; --x )
{
if ( qAlpha( imgScanline[x] ) )
{
xmax = x;
}
}
}

if ( minSize.isValid() )
{
if ( xmax - xmin < minSize.width() ) // centers image on x

0 comments on commit 8c7d772

Please sign in to comment.
You can’t perform that action at this time.