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 );
209 enum LineOperationDirection
214 template<
class BlockOperation>
static void runBlockOperationInThreads( QImage &image, BlockOperation &operation, LineOperationDirection direction );
217 unsigned int beginLine;
218 unsigned int endLine;
219 unsigned int lineLength;
220 QImage *image =
nullptr;
224 template<
typename RectOperation>
static void runRectOperation( QImage &image, RectOperation &operation );
225 template<
class RectOperation>
static void runRectOperationOnWholeImage( QImage &image, RectOperation &operation );
228 template<
class PixelOperation>
static void runPixelOperation( QImage &image, PixelOperation &operation, QgsFeedback *feedback =
nullptr );
229 template<
class PixelOperation>
static void runPixelOperationOnWholeImage( QImage &image, PixelOperation &operation, QgsFeedback *feedback =
nullptr );
230 template<
class PixelOperation>
struct ProcessBlockUsingPixelOperation
232 explicit ProcessBlockUsingPixelOperation( PixelOperation &operation, QgsFeedback *feedback )
233 : mOperation( operation )
234 , mFeedback( feedback )
237 typedef void result_type;
239 void operator()( ImageBlock &block )
241 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
243 if ( mFeedback && mFeedback->isCanceled() )
246 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
247 for (
unsigned int x = 0; x < block.lineLength; ++x )
249 mOperation( ref[x], x, y );
254 PixelOperation &mOperation;
255 QgsFeedback *mFeedback =
nullptr;
259 template<
typename LineOperation>
static void runLineOperation( QImage &image, LineOperation &operation, QgsFeedback *feedback =
nullptr );
260 template<
class LineOperation>
static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation, QgsFeedback *feedback =
nullptr );
261 template<
class LineOperation>
struct ProcessBlockUsingLineOperation
263 explicit ProcessBlockUsingLineOperation( LineOperation &operation )
264 : mOperation( operation )
267 typedef void result_type;
269 void operator()( ImageBlock &block )
272 int bpl = block.image->bytesPerLine();
273 if ( mOperation.direction() == ByRow )
275 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
277 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
278 mOperation( ref, block.lineLength, bpl );
284 unsigned char *ref = block.image->scanLine( 0 ) + 4 * block.beginLine;
285 for (
unsigned int x = block.beginLine; x < block.endLine; ++x, ref += 4 )
287 mOperation(
reinterpret_cast< QRgb *
>( ref ), block.lineLength, bpl );
292 LineOperation &mOperation;
298 class GrayscalePixelOperation
301 explicit GrayscalePixelOperation(
const GrayscaleMode mode )
305 void operator()( QRgb &rgb,
int x,
int y )
const;
310 static void grayscaleLightnessOp( QRgb &rgb );
311 static void grayscaleLuminosityOp( QRgb &rgb );
312 static void grayscaleAverageOp( QRgb &rgb );
315 class BrightnessContrastPixelOperation
318 BrightnessContrastPixelOperation(
const int brightness,
const double contrast )
319 : mBrightness( brightness )
320 , mContrast( contrast )
323 void operator()( QRgb &rgb,
int x,
int y )
const;
331 class HueSaturationPixelOperation
334 HueSaturationPixelOperation(
const double saturation,
const bool colorize,
const int colorizeHue,
const int colorizeSaturation,
const double colorizeStrength )
335 : mSaturation( saturation )
336 , mColorize( colorize )
337 , mColorizeHue( colorizeHue )
338 , mColorizeSaturation( colorizeSaturation )
339 , mColorizeStrength( colorizeStrength )
342 void operator()( QRgb &rgb,
int x,
int y )
const;
348 int mColorizeSaturation;
349 double mColorizeStrength;
351 static int adjustColorComponent(
int colorComponent,
int brightness,
double contrastFactor );
354 class MultiplyOpacityPixelOperation
357 explicit MultiplyOpacityPixelOperation(
const double factor )
361 void operator()( QRgb &rgb,
int x,
int y )
const;
367 class ConvertToArrayPixelOperation
370 ConvertToArrayPixelOperation(
const int width,
double *array,
const bool exterior =
true )
373 , mExterior( exterior )
376 void operator()( QRgb &rgb,
int x,
int y );
380 double *mArray =
nullptr;
384 class ShadeFromArrayOperation
387 ShadeFromArrayOperation(
const int width,
double *array,
const double spread,
const DistanceTransformProperties &properties )
391 , mProperties( properties )
393 mSpreadSquared = std::pow( mSpread, 2.0 );
396 void operator()( QRgb &rgb,
int x,
int y );
400 double *mArray =
nullptr;
402 double mSpreadSquared;
403 const DistanceTransformProperties &mProperties;
405 static void distanceTransform2d(
double *im,
int width,
int height, QgsFeedback *feedback =
nullptr );
406 static void distanceTransform1d(
double *f,
int n,
int *v,
double *z,
double *d );
407 static double maxValueInDistanceTransformArray(
const double *array,
unsigned int size );
410 class StackBlurLineOperation
413 StackBlurLineOperation(
int alpha, LineOperationDirection direction,
bool forwardDirection,
int i1,
int i2, QgsFeedback *feedback )
415 , mDirection( direction )
416 , mForwardDirection( forwardDirection )
419 , mFeedback( feedback )
422 typedef void result_type;
424 LineOperationDirection direction()
const {
return mDirection; }
426 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
428 if ( mFeedback && mFeedback->isCanceled() )
431 unsigned char *p =
reinterpret_cast< unsigned char *
>( startRef );
433 int increment = ( mDirection == QgsImageOperation::ByRow ) ? 4 : bytesPerLine;
434 if ( !mForwardDirection )
436 p +=
static_cast< std::size_t
>( lineLength - 1 ) * increment;
437 increment = -increment;
440 for (
int i = mi1; i <= mi2; ++i )
446 for (
int j = 1; j < lineLength; ++j, p += increment )
448 if ( mFeedback && mFeedback->isCanceled() )
451 for (
int i = mi1; i <= mi2; ++i )
453 p[i] = ( rgba[i] += ( ( p[i] << 4 ) - rgba[i] ) * mAlpha / 16 ) >> 4;
460 LineOperationDirection mDirection;
461 bool mForwardDirection;
464 QgsFeedback *mFeedback =
nullptr;
467 static double *createGaussianKernel(
int radius );
469 class GaussianBlurOperation
472 GaussianBlurOperation(
int radius, LineOperationDirection direction, QImage *destImage,
double *kernel, QgsFeedback *feedback )
474 , mDirection( direction )
475 , mDestImage( destImage )
476 , mDestImageBpl( destImage->bytesPerLine() )
478 , mFeedback( feedback )
481 typedef void result_type;
483 void operator()( ImageBlock &block );
487 LineOperationDirection mDirection;
488 QImage *mDestImage =
nullptr;
490 double *mKernel =
nullptr;
491 QgsFeedback *mFeedback =
nullptr;
493 inline QRgb gaussianBlurVertical(
int posy,
unsigned char *sourceFirstLine,
int sourceBpl,
int height )
const;
494 inline QRgb gaussianBlurHorizontal(
int posx,
unsigned char *sourceFirstLine,
int width )
const;
500 class FlipLineOperation
503 explicit FlipLineOperation( LineOperationDirection direction )
504 : mDirection( direction )
507 typedef void result_type;
509 LineOperationDirection direction()
const {
return mDirection; }
511 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
const;
514 LineOperationDirection mDirection;