18 #ifndef QGSIMAGEOPERATION_H 19 #define QGSIMAGEOPERATION_H 25 #include "qgis_core.h" 74 static void convertToGrayscale( QImage &image,
GrayscaleMode mode = GrayscaleLuminosity );
86 static void adjustBrightnessContrast( QImage &image,
int brightness,
double contrast );
96 static void adjustHueSaturation( QImage &image,
double saturation,
const QColor &colorizeColor = QColor(),
97 double colorizeStrength = 1.0 );
104 static void multiplyOpacity( QImage &image,
double factor );
112 static void overlayColor( QImage &image,
const QColor &color );
123 bool shadeExterior =
true;
129 bool useMaxDistance =
true;
135 double spread = 10.0;
161 static void stackBlur( QImage &image,
int radius,
bool alphaOnly =
false );
171 static QImage *gaussianBlur( QImage &image,
int radius )
SIP_FACTORY;
178 static void flipImage( QImage &image,
FlipType type );
190 static QRect nonTransparentImageRect(
const QImage &image, QSize minSize = QSize(),
bool center =
false );
201 static QImage cropTransparent(
const QImage &image, QSize minSize = QSize(),
bool center =
false );
206 enum LineOperationDirection
211 template <
class BlockOperation>
static void runBlockOperationInThreads( QImage &image, BlockOperation &operation, LineOperationDirection direction );
214 unsigned int beginLine;
215 unsigned int endLine;
216 unsigned int lineLength;
217 QImage *image =
nullptr;
221 template <
typename RectOperation>
static void runRectOperation( QImage &image, RectOperation &operation );
222 template <
class RectOperation>
static void runRectOperationOnWholeImage( QImage &image, RectOperation &operation );
225 template <
class PixelOperation>
static void runPixelOperation( QImage &image, PixelOperation &operation );
226 template <
class PixelOperation>
static void runPixelOperationOnWholeImage( QImage &image, PixelOperation &operation );
227 template <
class PixelOperation>
228 struct ProcessBlockUsingPixelOperation
230 explicit ProcessBlockUsingPixelOperation( PixelOperation &operation )
231 : mOperation( operation ) { }
233 typedef void result_type;
235 void operator()( ImageBlock &block )
237 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
239 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
240 for (
unsigned int x = 0; x < block.lineLength; ++x )
242 mOperation( ref[x], x, y );
247 PixelOperation &mOperation;
251 template <
typename LineOperation>
static void runLineOperation( QImage &image, LineOperation &operation );
252 template <
class LineOperation>
static void runLineOperationOnWholeImage( QImage &image, LineOperation &operation );
253 template <
class LineOperation>
254 struct ProcessBlockUsingLineOperation
256 explicit ProcessBlockUsingLineOperation( LineOperation &operation )
257 : mOperation( operation ) { }
259 typedef void result_type;
261 void operator()( ImageBlock &block )
264 int bpl = block.image->bytesPerLine();
265 if ( mOperation.direction() == ByRow )
267 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
269 QRgb *ref =
reinterpret_cast< QRgb *
>( block.image->scanLine( y ) );
270 mOperation( ref, block.lineLength, bpl );
276 unsigned char *ref = block.image->scanLine( 0 ) + 4 * block.beginLine;
277 for (
unsigned int x = block.beginLine; x < block.endLine; ++x, ref += 4 )
279 mOperation( reinterpret_cast< QRgb * >( ref ), block.lineLength, bpl );
284 LineOperation &mOperation;
290 class GrayscalePixelOperation
297 void operator()( QRgb &rgb,
int x,
int y );
302 static void grayscaleLightnessOp( QRgb &rgb );
303 static void grayscaleLuminosityOp( QRgb &rgb );
304 static void grayscaleAverageOp( QRgb &rgb );
307 class BrightnessContrastPixelOperation
310 BrightnessContrastPixelOperation(
const int brightness,
const double contrast )
311 : mBrightness( brightness )
312 , mContrast( contrast )
315 void operator()( QRgb &rgb,
int x,
int y );
323 class HueSaturationPixelOperation
326 HueSaturationPixelOperation(
const double saturation,
const bool colorize,
327 const int colorizeHue,
const int colorizeSaturation,
328 const double colorizeStrength )
329 : mSaturation( saturation )
330 , mColorize( colorize )
331 , mColorizeHue( colorizeHue )
332 , mColorizeSaturation( colorizeSaturation )
333 , mColorizeStrength( colorizeStrength )
336 void operator()( QRgb &rgb,
int x,
int y );
342 int mColorizeSaturation;
343 double mColorizeStrength;
345 static int adjustColorComponent(
int colorComponent,
int brightness,
double contrastFactor );
348 class MultiplyOpacityPixelOperation
351 explicit MultiplyOpacityPixelOperation(
const double factor )
355 void operator()( QRgb &rgb,
int x,
int y );
361 class ConvertToArrayPixelOperation
364 ConvertToArrayPixelOperation(
const int width,
double *array,
const bool exterior =
true )
367 , mExterior( exterior )
371 void operator()( QRgb &rgb,
int x,
int y );
375 double *mArray =
nullptr;
379 class ShadeFromArrayOperation
382 ShadeFromArrayOperation(
const int width,
double *array,
const double spread,
387 , mProperties( properties )
389 mSpreadSquared = std::pow( mSpread, 2.0 );
392 void operator()( QRgb &rgb,
int x,
int y );
396 double *mArray =
nullptr;
398 double mSpreadSquared;
401 static void distanceTransform2d(
double *im,
int width,
int height );
402 static void distanceTransform1d(
double *f,
int n,
int *v,
double *z,
double *d );
403 static double maxValueInDistanceTransformArray(
const double *array,
unsigned int size );
406 class StackBlurLineOperation
409 StackBlurLineOperation(
int alpha, LineOperationDirection direction,
bool forwardDirection,
int i1,
int i2 )
411 , mDirection( direction )
412 , mForwardDirection( forwardDirection )
417 typedef void result_type;
419 LineOperationDirection direction() {
return mDirection; }
421 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine );
425 LineOperationDirection mDirection;
426 bool mForwardDirection;
431 static double *createGaussianKernel(
int radius );
433 class GaussianBlurOperation
436 GaussianBlurOperation(
int radius, LineOperationDirection direction, QImage *destImage,
double *kernel )
438 , mDirection( direction )
439 , mDestImage( destImage )
440 , mDestImageBpl( destImage->bytesPerLine() )
444 typedef void result_type;
446 void operator()( ImageBlock &block );
450 LineOperationDirection mDirection;
451 QImage *mDestImage =
nullptr;
453 double *mKernel =
nullptr;
455 inline QRgb gaussianBlurVertical(
int posy,
unsigned char *sourceFirstLine,
int sourceBpl,
int height );
456 inline QRgb gaussianBlurHorizontal(
int posx,
unsigned char *sourceFirstLine,
int width );
462 class FlipLineOperation
465 explicit FlipLineOperation( LineOperationDirection direction )
466 : mDirection( direction )
469 typedef void result_type;
471 LineOperationDirection direction() {
return mDirection; }
473 void operator()( QRgb *startRef,
int lineLength,
int bytesPerLine );
476 LineOperationDirection mDirection;
482 #endif // QGSIMAGEOPERATION_H Flip the image horizontally.
Contains operations and filters which apply to QImages.
Keep the lightness of the color, drops the saturation.
Abstract base class for color ramps.
FlipType
Flip operation types.
Grayscale by perceptual luminosity (weighted sum of color RGB components)
Grayscale by taking average of color RGB components.
GrayscaleMode
Modes for converting a QImage to grayscale.