Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Further optimisations to nonTransparentImageRect calculation
  • Loading branch information
nyalldawson committed Oct 19, 2016
1 parent c9251c5 commit 8c7d772
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions src/core/effects/qgsimageoperation.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 8c7d772

Please sign in to comment.