18#ifndef QGSIMAGEOPERATION_H
19#define QGSIMAGEOPERATION_H
78 static void convertToGrayscale( QImage &image, GrayscaleMode mode = GrayscaleLuminosity,
QgsFeedback *feedback =
nullptr );
91 static void adjustBrightnessContrast( QImage &image,
int brightness,
double contrast,
QgsFeedback *feedback =
nullptr );
102 static void adjustHueSaturation( QImage &image,
double saturation,
const QColor &colorizeColor = QColor(),
103 double colorizeStrength = 1.0,
QgsFeedback *feedback =
nullptr );
111 static void multiplyOpacity( QImage &image,
double factor,
QgsFeedback *feedback =
nullptr );
119 static void overlayColor( QImage &image,
const QColor &color );
130 bool shadeExterior =
true;
136 bool useMaxDistance =
true;
142 double spread = 10.0;
170 static void stackBlur( QImage &image,
int radius,
bool alphaOnly =
false,
QgsFeedback *feedback =
nullptr );
188 static
void flipImage( QImage &image,
FlipType type );
200 static QRect nonTransparentImageRect( const QImage &image, QSize minSize = QSize(),
bool center = false );
211 static QImage cropTransparent( const QImage &image, QSize minSize = QSize(),
bool center = false );
216 enum LineOperationDirection
221 template <
class BlockOperation>
static void runBlockOperationInThreads( QImage &image, BlockOperation &operation, LineOperationDirection direction );
224 unsigned int beginLine;
225 unsigned int endLine;
226 unsigned int lineLength;
227 QImage *image =
nullptr;
231 template <
typename RectOperation>
static void runRectOperation( QImage &image, RectOperation &operation );
232 template <
class RectOperation>
static void runRectOperationOnWholeImage( QImage &image, RectOperation &operation );
235 template <
class PixelOperation>
static void runPixelOperation( QImage &image, PixelOperation &operation,
QgsFeedback *feedback =
nullptr );
236 template <
class PixelOperation>
static void runPixelOperationOnWholeImage( QImage &image, PixelOperation &operation,
QgsFeedback *feedback =
nullptr );
237 template <
class PixelOperation>
238 struct ProcessBlockUsingPixelOperation
240 explicit ProcessBlockUsingPixelOperation( PixelOperation &operation,
QgsFeedback *feedback )
241 : mOperation( operation )
242 , mFeedback( feedback )
245 typedef void result_type;
247 void operator()( ImageBlock &block )
249 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
251 if ( mFeedback && mFeedback->isCanceled() )
254 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
255 for (
unsigned int x = 0; x < block.lineLength; ++x )
257 mOperation( ref[x], x, y );
262 PixelOperation &mOperation;
267 template <
typename LineOperation>
static void runLineOperation( QImage &image, LineOperation &operation,
QgsFeedback *feedback =
nullptr );
268 template <
class LineOperation>
static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation,
QgsFeedback *feedback =
nullptr );
269 template <
class LineOperation>
270 struct ProcessBlockUsingLineOperation
272 explicit ProcessBlockUsingLineOperation( LineOperation &operation )
273 : mOperation( operation ) { }
275 typedef void result_type;
277 void operator()( ImageBlock &block )
280 int bpl = block.image->bytesPerLine();
281 if ( mOperation.direction() == ByRow )
283 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
285 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
286 mOperation( ref, block.lineLength, bpl );
292 unsigned char *ref = block.image->scanLine( 0 ) + 4 * block.beginLine;
293 for (
unsigned int x = block.beginLine; x < block.endLine; ++x, ref += 4 )
295 mOperation(
reinterpret_cast< QRgb *
>( ref ), block.lineLength, bpl );
300 LineOperation &mOperation;
306 class GrayscalePixelOperation
309 explicit GrayscalePixelOperation(
const GrayscaleMode mode )
313 void operator()( QRgb &rgb,
int x,
int y )
const;
318 static void grayscaleLightnessOp( QRgb &rgb );
319 static void grayscaleLuminosityOp( QRgb &rgb );
320 static void grayscaleAverageOp( QRgb &rgb );
323 class BrightnessContrastPixelOperation
326 BrightnessContrastPixelOperation(
const int brightness,
const double contrast )
327 : mBrightness( brightness )
328 , mContrast( contrast )
331 void operator()( QRgb &rgb,
int x,
int y );
339 class HueSaturationPixelOperation
342 HueSaturationPixelOperation(
const double saturation,
const bool colorize,
343 const int colorizeHue,
const int colorizeSaturation,
344 const double colorizeStrength )
345 : mSaturation( saturation )
346 , mColorize( colorize )
347 , mColorizeHue( colorizeHue )
348 , mColorizeSaturation( colorizeSaturation )
349 , mColorizeStrength( colorizeStrength )
352 void operator()( QRgb &rgb,
int x,
int y );
358 int mColorizeSaturation;
359 double mColorizeStrength;
361 static int adjustColorComponent(
int colorComponent,
int brightness,
double contrastFactor );
364 class MultiplyOpacityPixelOperation
367 explicit MultiplyOpacityPixelOperation(
const double factor )
371 void operator()( QRgb &rgb,
int x,
int y );
377 class ConvertToArrayPixelOperation
380 ConvertToArrayPixelOperation(
const int width,
double *array,
const bool exterior =
true )
383 , 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,
399 const DistanceTransformProperties &properties )
403 , mProperties( properties )
405 mSpreadSquared = std::pow( mSpread, 2.0 );
408 void operator()( QRgb &rgb,
int x,
int y );
412 double *mArray =
nullptr;
414 double mSpreadSquared;
415 const DistanceTransformProperties &mProperties;
417 static void distanceTransform2d(
double *im,
int width,
int height,
QgsFeedback *feedback =
nullptr );
418 static void distanceTransform1d(
double *f,
int n,
int *v,
double *z,
double *d );
419 static double maxValueInDistanceTransformArray(
const double *array,
unsigned int size );
422 class StackBlurLineOperation
425 StackBlurLineOperation(
int alpha, LineOperationDirection direction,
bool forwardDirection,
int i1,
int i2,
QgsFeedback *feedback )
427 , mDirection( direction )
428 , mForwardDirection( forwardDirection )
431 , mFeedback( feedback )
434 typedef void result_type;
436 LineOperationDirection direction()
const {
return mDirection; }
438 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
440 if ( mFeedback && mFeedback->isCanceled() )
443 unsigned char *p =
reinterpret_cast< unsigned char *
>( startRef );
445 int increment = ( mDirection == QgsImageOperation::ByRow ) ? 4 : bytesPerLine;
446 if ( !mForwardDirection )
448 p += ( lineLength - 1 ) * increment;
449 increment = -increment;
452 for (
int i = mi1; i <= mi2; ++i )
458 for (
int j = 1; j < lineLength; ++j, p += increment )
460 if ( mFeedback && mFeedback->isCanceled() )
463 for (
int i = mi1; i <= mi2; ++i )
465 p[i] = ( rgba[i] += ( ( p[i] << 4 ) - rgba[i] ) * mAlpha / 16 ) >> 4;
472 LineOperationDirection mDirection;
473 bool mForwardDirection;
479 static double *createGaussianKernel(
int radius );
481 class GaussianBlurOperation
484 GaussianBlurOperation(
int radius, LineOperationDirection direction, QImage *destImage,
double *kernel,
QgsFeedback *feedback )
486 , mDirection( direction )
487 , mDestImage( destImage )
488 , mDestImageBpl( destImage->bytesPerLine() )
490 , mFeedback( feedback )
493 typedef void result_type;
495 void operator()( ImageBlock &block );
499 LineOperationDirection mDirection;
500 QImage *mDestImage =
nullptr;
502 double *mKernel =
nullptr;
505 inline QRgb gaussianBlurVertical(
int posy,
unsigned char *sourceFirstLine,
int sourceBpl,
int height )
const;
506 inline QRgb gaussianBlurHorizontal(
int posx,
unsigned char *sourceFirstLine,
int width )
const;
512 class FlipLineOperation
515 explicit FlipLineOperation( LineOperationDirection direction )
516 : mDirection( direction )
519 typedef void result_type;
521 LineOperationDirection direction()
const {
return mDirection; }
523 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine )
const;
526 LineOperationDirection mDirection;
Abstract base class for color ramps.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Contains operations and filters which apply to QImages.
FlipType
Flip operation types.
@ FlipHorizontal
Flip the image horizontally.
GrayscaleMode
Modes for converting a QImage to grayscale.
@ GrayscaleLightness
Keep the lightness of the color, drops the saturation.
@ GrayscaleLuminosity
Grayscale by perceptual luminosity (weighted sum of color RGB components)
@ GrayscaleAverage
Grayscale by taking average of color RGB components.