Skip to content

Commit

Permalink
A few more early exit paths for image operations...
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 24, 2021
1 parent 806735c commit 81dcc34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
27 changes: 18 additions & 9 deletions src/core/effects/qgsimageoperation.cpp
Expand Up @@ -73,7 +73,7 @@ template <typename RectOperation>
void QgsImageOperation::runRectOperation( QImage &image, RectOperation &operation )
{
//possibly could be tweaked for rect operations
if ( image.height() * image.width() < 100000 )
if ( static_cast< qgssize >( image.height() ) * image.width() < 100000 )
{
//small image, don't multithread
//this threshold was determined via testing various images
Expand Down Expand Up @@ -101,14 +101,14 @@ void QgsImageOperation::runRectOperationOnWholeImage( QImage &image, RectOperati
//linear operations

template <typename LineOperation>
void QgsImageOperation::runLineOperation( QImage &image, LineOperation &operation )
void QgsImageOperation::runLineOperation( QImage &image, LineOperation &operation, QgsFeedback *feedback )
{
//possibly could be tweaked for rect operations
if ( image.height() * image.width() < 100000 )
if ( static_cast< qgssize >( image.height() ) * image.width() < 100000 )
{
//small image, don't multithread
//this threshold was determined via testing various images
runLineOperationOnWholeImage( image, operation );
runLineOperationOnWholeImage( image, operation, feedback );
}
else
{
Expand All @@ -119,7 +119,7 @@ void QgsImageOperation::runLineOperation( QImage &image, LineOperation &operatio
}

template <class LineOperation>
void QgsImageOperation::runLineOperationOnWholeImage( QImage &image, LineOperation &operation )
void QgsImageOperation::runLineOperationOnWholeImage( QImage &image, LineOperation &operation, QgsFeedback *feedback )
{
int height = image.height();
int width = image.width();
Expand All @@ -130,6 +130,9 @@ void QgsImageOperation::runLineOperationOnWholeImage( QImage &image, LineOperati
{
for ( int y = 0; y < height; ++y )
{
if ( feedback && feedback->isCanceled() )
break;

QRgb *ref = reinterpret_cast< QRgb * >( image.scanLine( y ) );
operation( ref, width, bpl );
}
Expand All @@ -140,6 +143,9 @@ void QgsImageOperation::runLineOperationOnWholeImage( QImage &image, LineOperati
unsigned char *ref = image.scanLine( 0 );
for ( int x = 0; x < width; ++x, ref += 4 )
{
if ( feedback && feedback->isCanceled() )
break;

operation( reinterpret_cast< QRgb * >( ref ), height, bpl );
}
}
Expand Down Expand Up @@ -600,25 +606,25 @@ void QgsImageOperation::stackBlur( QImage &image, const int radius, const bool a
i1 = i2 = ( QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3 );

StackBlurLineOperation topToBottomBlur( alpha, QgsImageOperation::ByColumn, true, i1, i2, feedback );
runLineOperation( *pImage, topToBottomBlur );
runLineOperation( *pImage, topToBottomBlur, feedback );

if ( feedback && feedback->isCanceled() )
return;

StackBlurLineOperation leftToRightBlur( alpha, QgsImageOperation::ByRow, true, i1, i2, feedback );
runLineOperation( *pImage, leftToRightBlur );
runLineOperation( *pImage, leftToRightBlur, feedback );

if ( feedback && feedback->isCanceled() )
return;

StackBlurLineOperation bottomToTopBlur( alpha, QgsImageOperation::ByColumn, false, i1, i2, feedback );
runLineOperation( *pImage, bottomToTopBlur );
runLineOperation( *pImage, bottomToTopBlur, feedback );

if ( feedback && feedback->isCanceled() )
return;

StackBlurLineOperation rightToLeftBlur( alpha, QgsImageOperation::ByRow, false, i1, i2, feedback );
runLineOperation( *pImage, rightToLeftBlur );
runLineOperation( *pImage, rightToLeftBlur, feedback );

if ( feedback && feedback->isCanceled() )
return;
Expand Down Expand Up @@ -687,6 +693,9 @@ QImage *QgsImageOperation::gaussianBlur( QImage &image, const int radius, QgsFee

void QgsImageOperation::GaussianBlurOperation::operator()( QgsImageOperation::ImageBlock &block )
{
if ( mFeedback && mFeedback->isCanceled() )
return;

int width = block.image->width();
int height = block.image->height();
int sourceBpl = block.image->bytesPerLine();
Expand Down
7 changes: 5 additions & 2 deletions src/core/effects/qgsimageoperation.h
Expand Up @@ -264,8 +264,8 @@ class CORE_EXPORT QgsImageOperation
};

//for linear operations
template <typename LineOperation> static void runLineOperation( QImage &image, LineOperation &operation );
template <class LineOperation> static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation );
template <typename LineOperation> static void runLineOperation( QImage &image, LineOperation &operation, QgsFeedback *feedback = nullptr );
template <class LineOperation> static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation, QgsFeedback *feedback = nullptr );
template <class LineOperation>
struct ProcessBlockUsingLineOperation
{
Expand Down Expand Up @@ -437,6 +437,9 @@ class CORE_EXPORT QgsImageOperation

void operator()( QRgb *startRef, int lineLength, int bytesPerLine )
{
if ( mFeedback && mFeedback->isCanceled() )
return;

unsigned char *p = reinterpret_cast< unsigned char * >( startRef );
int rgba[4];
int increment = ( mDirection == QgsImageOperation::ByRow ) ? 4 : bytesPerLine;
Expand Down

0 comments on commit 81dcc34

Please sign in to comment.