76 static void convertToGrayscale( QImage &image, GrayscaleMode mode = GrayscaleLuminosity,
QgsFeedback *feedback =
nullptr );
89 static void adjustBrightnessContrast( QImage &image,
int brightness,
double contrast,
QgsFeedback *feedback =
nullptr );
100 static void adjustHueSaturation( QImage &image,
double saturation,
const QColor &colorizeColor = QColor(),
double colorizeStrength = 1.0,
QgsFeedback *feedback =
nullptr );
108 static void multiplyOpacity( QImage &image,
double factor,
QgsFeedback *feedback =
nullptr );
116 static void overlayColor( QImage &image,
const QColor &color );
166 static void stackBlur( QImage &image,
int radius,
bool alphaOnly =
false,
QgsFeedback *feedback =
nullptr );
205 static QImage
cropTransparent( const QImage &image, QSize minSize = QSize(),
bool center = false );
224 static
bool isSingleColor( const QImage &image, const QColor &color );
235 static QImage
floodFill( const QImage &image, const QPoint &startPoint, const QColor &newColor,
int tolerance = 0,
QgsFeedback *feedback =
nullptr );
239 enum LineOperationDirection
244 template<
class BlockOperation>
static void runBlockOperationInThreads( QImage &image, BlockOperation &operation, LineOperationDirection direction );
247 unsigned int beginLine;
248 unsigned int endLine;
249 unsigned int lineLength;
250 QImage *image =
nullptr;
254 template<
typename RectOperation>
static void runRectOperation( QImage &image, RectOperation &operation );
255 template<
class RectOperation>
static void runRectOperationOnWholeImage( QImage &image, RectOperation &operation );
258 template<
class PixelOperation>
static void runPixelOperation( QImage &image, PixelOperation &operation, QgsFeedback *feedback =
nullptr );
259 template<
class PixelOperation>
static void runPixelOperationOnWholeImage( QImage &image, PixelOperation &operation, QgsFeedback *feedback =
nullptr );
260 template<
class PixelOperation>
struct ProcessBlockUsingPixelOperation
262 explicit ProcessBlockUsingPixelOperation( PixelOperation &operation, QgsFeedback *feedback )
263 : mOperation( operation )
264 , mFeedback( feedback )
267 typedef void result_type;
269 void operator()( ImageBlock &block )
271 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
273 if ( mFeedback && mFeedback->isCanceled() )
276 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
277 for (
unsigned int x = 0; x < block.lineLength; ++x )
279 mOperation( ref[x], x, y );
284 PixelOperation &mOperation;
285 QgsFeedback *mFeedback =
nullptr;
289 template<
typename LineOperation>
static void runLineOperation( QImage &image, LineOperation &operation, QgsFeedback *feedback =
nullptr );
290 template<
class LineOperation>
static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation, QgsFeedback *feedback =
nullptr );
291 template<
class LineOperation>
struct ProcessBlockUsingLineOperation
293 explicit ProcessBlockUsingLineOperation( LineOperation &operation )
294 : mOperation( operation )
297 typedef void result_type;
299 void operator()( ImageBlock &block )
302 int bpl = block.image->bytesPerLine();
303 if ( mOperation.direction() == ByRow )
305 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
307 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
308 mOperation( ref, block.lineLength, bpl );
314 unsigned char *ref = block.image->scanLine( 0 ) + 4 * block.beginLine;
315 for (
unsigned int x = block.beginLine; x < block.endLine; ++x, ref += 4 )
317 mOperation(
reinterpret_cast< QRgb *
>( ref ), block.lineLength, bpl );
322 LineOperation &mOperation;
328 class GrayscalePixelOperation
331 explicit GrayscalePixelOperation(
const GrayscaleMode mode )
335 void operator()( QRgb &rgb,
int x,
int y )
const;
340 static void grayscaleLightnessOp( QRgb &rgb );
341 static void grayscaleLuminosityOp( QRgb &rgb );
342 static void grayscaleAverageOp( QRgb &rgb );
345 class BrightnessContrastPixelOperation
348 BrightnessContrastPixelOperation(
const int brightness,
const double contrast )
349 : mBrightness( brightness )
350 , mContrast( contrast )
353 void operator()( QRgb &rgb,
int x,
int y )
const;
361 class HueSaturationPixelOperation
364 HueSaturationPixelOperation(
const double saturation,
const bool colorize,
const int colorizeHue,
const int colorizeSaturation,
const double colorizeStrength )
365 : mSaturation( saturation )
366 , mColorize( colorize )
367 , mColorizeHue( colorizeHue )
368 , mColorizeSaturation( colorizeSaturation )
369 , mColorizeStrength( colorizeStrength )
372 void operator()( QRgb &rgb,
int x,
int y )
const;
378 int mColorizeSaturation;
379 double mColorizeStrength;
381 static int adjustColorComponent(
int colorComponent,
int brightness,
double contrastFactor );
384 class MultiplyOpacityPixelOperation
387 explicit MultiplyOpacityPixelOperation(
const double factor )
391 void operator()( QRgb &rgb,
int x,
int y )
const;
397 class ConvertToArrayPixelOperation
400 ConvertToArrayPixelOperation(
const int width,
double *array,
const bool exterior =
true )
403 , mExterior( exterior )
406 void operator()( QRgb &rgb,
int x,
int y );
410 double *mArray =
nullptr;
414 class ShadeFromArrayOperation
417 ShadeFromArrayOperation(
const int width,
double *array,
const double spread,
const DistanceTransformProperties &properties )
421 , mProperties( properties )
423 mSpreadSquared = std::pow( mSpread, 2.0 );
426 void operator()( QRgb &rgb,
int x,
int y );
430 double *mArray =
nullptr;
432 double mSpreadSquared;
433 const DistanceTransformProperties &mProperties;
435 static void distanceTransform2d(
double *im,
int width,
int height, QgsFeedback *feedback =
nullptr );
436 static void distanceTransform1d(
double *f,
int n,
int *v,
double *z,
double *d );
437 static double maxValueInDistanceTransformArray(
const double *array,
unsigned int size );
440 class StackBlurLineOperation
443 StackBlurLineOperation(
int alpha, LineOperationDirection direction,
bool forwardDirection,
int i1,
int i2, QgsFeedback *feedback )
445 , mDirection( direction )
446 , mForwardDirection( forwardDirection )
449 , mFeedback( feedback )
452 typedef void result_type;
454 LineOperationDirection direction()
const {
return mDirection; }
456 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
458 if ( mFeedback && mFeedback->isCanceled() )
461 unsigned char *p =
reinterpret_cast< unsigned char *
>( startRef );
463 int increment = ( mDirection == QgsImageOperation::ByRow ) ? 4 : bytesPerLine;
464 if ( !mForwardDirection )
466 p +=
static_cast< std::size_t
>( lineLength - 1 ) * increment;
467 increment = -increment;
470 for (
int i = mi1; i <= mi2; ++i )
476 for (
int j = 1; j < lineLength; ++j, p += increment )
478 if ( mFeedback && mFeedback->isCanceled() )
481 for (
int i = mi1; i <= mi2; ++i )
483 p[i] = ( rgba[i] += ( ( p[i] << 4 ) - rgba[i] ) * mAlpha / 16 ) >> 4;
490 LineOperationDirection mDirection;
491 bool mForwardDirection;
494 QgsFeedback *mFeedback =
nullptr;
497 static double *createGaussianKernel(
int radius );
499 class GaussianBlurOperation
502 GaussianBlurOperation(
int radius, LineOperationDirection direction, QImage *destImage,
double *kernel, QgsFeedback *feedback )
504 , mDirection( direction )
505 , mDestImage( destImage )
506 , mDestImageBpl( destImage->bytesPerLine() )
508 , mFeedback( feedback )
511 typedef void result_type;
513 void operator()( ImageBlock &block );
517 LineOperationDirection mDirection;
518 QImage *mDestImage =
nullptr;
520 double *mKernel =
nullptr;
521 QgsFeedback *mFeedback =
nullptr;
523 inline QRgb gaussianBlurVertical(
int posy,
unsigned char *sourceFirstLine,
int sourceBpl,
int height )
const;
524 inline QRgb gaussianBlurHorizontal(
int posx,
unsigned char *sourceFirstLine,
int width )
const;
530 class FlipLineOperation
533 explicit FlipLineOperation( LineOperationDirection direction )
534 : mDirection( direction )
537 typedef void result_type;
539 LineOperationDirection direction()
const {
return mDirection; }
541 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
const;
544 LineOperationDirection mDirection;