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 );
216 static QImage
floodFill( const QImage &image, const QPoint &startPoint, const QColor &newColor,
int tolerance = 0,
QgsFeedback *feedback =
nullptr );
220 enum LineOperationDirection
225 template<
class BlockOperation>
static void runBlockOperationInThreads( QImage &image, BlockOperation &operation, LineOperationDirection direction );
228 unsigned int beginLine;
229 unsigned int endLine;
230 unsigned int lineLength;
231 QImage *image =
nullptr;
235 template<
typename RectOperation>
static void runRectOperation( QImage &image, RectOperation &operation );
236 template<
class RectOperation>
static void runRectOperationOnWholeImage( QImage &image, RectOperation &operation );
239 template<
class PixelOperation>
static void runPixelOperation( QImage &image, PixelOperation &operation, QgsFeedback *feedback =
nullptr );
240 template<
class PixelOperation>
static void runPixelOperationOnWholeImage( QImage &image, PixelOperation &operation, QgsFeedback *feedback =
nullptr );
241 template<
class PixelOperation>
struct ProcessBlockUsingPixelOperation
243 explicit ProcessBlockUsingPixelOperation( PixelOperation &operation, QgsFeedback *feedback )
244 : mOperation( operation )
245 , mFeedback( feedback )
248 typedef void result_type;
250 void operator()( ImageBlock &block )
252 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
254 if ( mFeedback && mFeedback->isCanceled() )
257 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
258 for (
unsigned int x = 0; x < block.lineLength; ++x )
260 mOperation( ref[x], x, y );
265 PixelOperation &mOperation;
266 QgsFeedback *mFeedback =
nullptr;
270 template<
typename LineOperation>
static void runLineOperation( QImage &image, LineOperation &operation, QgsFeedback *feedback =
nullptr );
271 template<
class LineOperation>
static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation, QgsFeedback *feedback =
nullptr );
272 template<
class LineOperation>
struct ProcessBlockUsingLineOperation
274 explicit ProcessBlockUsingLineOperation( LineOperation &operation )
275 : mOperation( operation )
278 typedef void result_type;
280 void operator()( ImageBlock &block )
283 int bpl = block.image->bytesPerLine();
284 if ( mOperation.direction() == ByRow )
286 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
288 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
289 mOperation( ref, block.lineLength, bpl );
295 unsigned char *ref = block.image->scanLine( 0 ) + 4 * block.beginLine;
296 for (
unsigned int x = block.beginLine; x < block.endLine; ++x, ref += 4 )
298 mOperation(
reinterpret_cast< QRgb *
>( ref ), block.lineLength, bpl );
303 LineOperation &mOperation;
309 class GrayscalePixelOperation
312 explicit GrayscalePixelOperation(
const GrayscaleMode mode )
316 void operator()( QRgb &rgb,
int x,
int y )
const;
321 static void grayscaleLightnessOp( QRgb &rgb );
322 static void grayscaleLuminosityOp( QRgb &rgb );
323 static void grayscaleAverageOp( QRgb &rgb );
326 class BrightnessContrastPixelOperation
329 BrightnessContrastPixelOperation(
const int brightness,
const double contrast )
330 : mBrightness( brightness )
331 , mContrast( contrast )
334 void operator()( QRgb &rgb,
int x,
int y )
const;
342 class HueSaturationPixelOperation
345 HueSaturationPixelOperation(
const double saturation,
const bool colorize,
const int colorizeHue,
const int colorizeSaturation,
const double colorizeStrength )
346 : mSaturation( saturation )
347 , mColorize( colorize )
348 , mColorizeHue( colorizeHue )
349 , mColorizeSaturation( colorizeSaturation )
350 , mColorizeStrength( colorizeStrength )
353 void operator()( QRgb &rgb,
int x,
int y )
const;
359 int mColorizeSaturation;
360 double mColorizeStrength;
362 static int adjustColorComponent(
int colorComponent,
int brightness,
double contrastFactor );
365 class MultiplyOpacityPixelOperation
368 explicit MultiplyOpacityPixelOperation(
const double factor )
372 void operator()( QRgb &rgb,
int x,
int y )
const;
378 class ConvertToArrayPixelOperation
381 ConvertToArrayPixelOperation(
const int width,
double *array,
const bool exterior =
true )
384 , mExterior( exterior )
387 void operator()( QRgb &rgb,
int x,
int y );
391 double *mArray =
nullptr;
395 class ShadeFromArrayOperation
398 ShadeFromArrayOperation(
const int width,
double *array,
const double spread,
const DistanceTransformProperties &properties )
402 , mProperties( properties )
404 mSpreadSquared = std::pow( mSpread, 2.0 );
407 void operator()( QRgb &rgb,
int x,
int y );
411 double *mArray =
nullptr;
413 double mSpreadSquared;
414 const DistanceTransformProperties &mProperties;
416 static void distanceTransform2d(
double *im,
int width,
int height, QgsFeedback *feedback =
nullptr );
417 static void distanceTransform1d(
double *f,
int n,
int *v,
double *z,
double *d );
418 static double maxValueInDistanceTransformArray(
const double *array,
unsigned int size );
421 class StackBlurLineOperation
424 StackBlurLineOperation(
int alpha, LineOperationDirection direction,
bool forwardDirection,
int i1,
int i2, QgsFeedback *feedback )
426 , mDirection( direction )
427 , mForwardDirection( forwardDirection )
430 , mFeedback( feedback )
433 typedef void result_type;
435 LineOperationDirection direction()
const {
return mDirection; }
437 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
439 if ( mFeedback && mFeedback->isCanceled() )
442 unsigned char *p =
reinterpret_cast< unsigned char *
>( startRef );
444 int increment = ( mDirection == QgsImageOperation::ByRow ) ? 4 : bytesPerLine;
445 if ( !mForwardDirection )
447 p +=
static_cast< std::size_t
>( lineLength - 1 ) * increment;
448 increment = -increment;
451 for (
int i = mi1; i <= mi2; ++i )
457 for (
int j = 1; j < lineLength; ++j, p += increment )
459 if ( mFeedback && mFeedback->isCanceled() )
462 for (
int i = mi1; i <= mi2; ++i )
464 p[i] = ( rgba[i] += ( ( p[i] << 4 ) - rgba[i] ) * mAlpha / 16 ) >> 4;
471 LineOperationDirection mDirection;
472 bool mForwardDirection;
475 QgsFeedback *mFeedback =
nullptr;
478 static double *createGaussianKernel(
int radius );
480 class GaussianBlurOperation
483 GaussianBlurOperation(
int radius, LineOperationDirection direction, QImage *destImage,
double *kernel, QgsFeedback *feedback )
485 , mDirection( direction )
486 , mDestImage( destImage )
487 , mDestImageBpl( destImage->bytesPerLine() )
489 , mFeedback( feedback )
492 typedef void result_type;
494 void operator()( ImageBlock &block );
498 LineOperationDirection mDirection;
499 QImage *mDestImage =
nullptr;
501 double *mKernel =
nullptr;
502 QgsFeedback *mFeedback =
nullptr;
504 inline QRgb gaussianBlurVertical(
int posy,
unsigned char *sourceFirstLine,
int sourceBpl,
int height )
const;
505 inline QRgb gaussianBlurHorizontal(
int posx,
unsigned char *sourceFirstLine,
int width )
const;
511 class FlipLineOperation
514 explicit FlipLineOperation( LineOperationDirection direction )
515 : mDirection( direction )
518 typedef void result_type;
520 LineOperationDirection direction()
const {
return mDirection; }
522 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
const;
525 LineOperationDirection mDirection;