18 #ifndef QGSIMAGEOPERATION_H
19 #define QGSIMAGEOPERATION_H
23 #include <QtCore/qmath.h>
67 static void convertToGrayscale( QImage &image,
const GrayscaleMode mode = GrayscaleLuminosity );
78 static void adjustBrightnessContrast( QImage &image,
const int brightness,
const double contrast );
87 static void adjustHueSaturation( QImage &image,
const double saturation,
const QColor& colorizeColor = QColor(),
88 const double colorizeStrength = 1.0 );
94 static void multiplyOpacity( QImage &image,
const double factor );
101 static void overlayColor( QImage &image,
const QColor& color );
107 : shadeExterior( true )
108 , useMaxDistance( true )
147 static void stackBlur( QImage &image,
const int radius,
const bool alphaOnly =
false );
156 static QImage* gaussianBlur( QImage &image,
const int radius );
162 static void flipImage( QImage &image,
FlipType type );
167 enum LineOperationDirection
172 template <
class BlockOperation>
static void runBlockOperationInThreads( QImage &image, BlockOperation& operation, LineOperationDirection direction );
175 unsigned int beginLine;
176 unsigned int endLine;
177 unsigned int lineLength;
182 template <
typename RectOperation>
static void runRectOperation( QImage &image, RectOperation& operation );
183 template <
class RectOperation>
static void runRectOperationOnWholeImage( QImage &image, RectOperation& operation );
186 template <
class PixelOperation>
static void runPixelOperation( QImage &image, PixelOperation& operation );
187 template <
class PixelOperation>
static void runPixelOperationOnWholeImage( QImage &image, PixelOperation& operation );
188 template <
class PixelOperation>
189 struct ProcessBlockUsingPixelOperation
191 ProcessBlockUsingPixelOperation( PixelOperation& operation )
192 : mOperation( operation ) { }
194 typedef void result_type;
196 void operator()( ImageBlock& block )
198 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
200 QRgb* ref = ( QRgb* )block.image->scanLine( y );
201 for (
unsigned int x = 0; x < block.lineLength; ++x )
203 mOperation( ref[x], x, y );
208 PixelOperation& mOperation;
212 template <
typename LineOperation>
static void runLineOperation( QImage &image, LineOperation& operation );
213 template <
class LineOperation>
static void runLineOperationOnWholeImage( QImage &image, LineOperation& operation );
214 template <
class LineOperation>
215 struct ProcessBlockUsingLineOperation
217 ProcessBlockUsingLineOperation( LineOperation& operation )
218 : mOperation( operation ) { }
220 typedef void result_type;
222 void operator()( ImageBlock& block )
225 int bpl = block.image->bytesPerLine();
226 if ( mOperation.direction() == ByRow )
228 for (
unsigned int y = block.beginLine; y < block.endLine; ++y )
230 QRgb* ref = ( QRgb* )block.image->scanLine( y );
231 mOperation( ref, block.lineLength, bpl );
237 unsigned char* ref = block.image->scanLine( 0 ) + 4 * block.beginLine;
238 for (
unsigned int x = block.beginLine; x < block.endLine; ++x, ref += 4 )
240 mOperation(( QRgb* )ref, block.lineLength, bpl );
245 LineOperation& mOperation;
251 class GrayscalePixelOperation
254 GrayscalePixelOperation(
const GrayscaleMode mode )
258 void operator()( QRgb& rgb,
const int x,
const int y );
263 static void grayscaleLightnessOp( QRgb& rgb );
264 static void grayscaleLuminosityOp( QRgb& rgb );
265 static void grayscaleAverageOp( QRgb& rgb );
268 class BrightnessContrastPixelOperation
271 BrightnessContrastPixelOperation(
const int brightness,
const double contrast )
272 : mBrightness( brightness )
273 , mContrast( contrast )
276 void operator()( QRgb& rgb,
const int x,
const int y );
284 class HueSaturationPixelOperation
287 HueSaturationPixelOperation(
const double saturation,
const bool colorize,
288 const int colorizeHue,
const int colorizeSaturation,
289 const double colorizeStrength )
290 : mSaturation( saturation )
291 , mColorize( colorize )
292 , mColorizeHue( colorizeHue )
293 , mColorizeSaturation( colorizeSaturation )
294 , mColorizeStrength( colorizeStrength )
297 void operator()( QRgb& rgb,
const int x,
const int y );
303 int mColorizeSaturation;
304 double mColorizeStrength;
306 static int adjustColorComponent(
int colorComponent,
int brightness,
double contrastFactor );
309 class MultiplyOpacityPixelOperation
312 MultiplyOpacityPixelOperation(
const double factor )
316 void operator()( QRgb& rgb,
const int x,
const int y );
322 class ConvertToArrayPixelOperation
325 ConvertToArrayPixelOperation(
const int width,
double * array,
const bool exterior =
true,
const int alphaThreshold = 255 )
328 , mExterior( exterior )
329 , mAlphaThreshold( alphaThreshold )
333 void operator()( QRgb& rgb,
const int x,
const int y );
342 class ShadeFromArrayOperation
345 ShadeFromArrayOperation(
const int width,
double* array,
const double spread,
346 const DistanceTransformProperties& properties )
350 , mProperties( properties )
352 mSpreadSquared = qPow( mSpread, 2.0 );
355 void operator()( QRgb& rgb,
const int x,
const int y );
361 double mSpreadSquared;
362 const DistanceTransformProperties& mProperties;
364 static void distanceTransform2d(
double *im,
int width,
int height );
365 static void distanceTransform1d(
double *f,
int n,
int *v,
double *z,
double *d );
366 static double maxValueInDistanceTransformArray(
const double *array,
const unsigned int size );
369 class StackBlurLineOperation
372 StackBlurLineOperation(
int alpha, LineOperationDirection direction,
bool forwardDirection,
int i1,
int i2 )
374 , mDirection( direction )
375 , mForwardDirection( forwardDirection )
380 typedef void result_type;
382 LineOperationDirection direction() {
return mDirection; }
384 void operator()( QRgb* startRef,
const int lineLength,
const int bytesPerLine );
388 LineOperationDirection mDirection;
389 bool mForwardDirection;
394 static double *createGaussianKernel(
const int radius );
396 class GaussianBlurOperation
399 GaussianBlurOperation(
int radius, LineOperationDirection direction, QImage* destImage,
double* kernel )
401 , mDirection( direction )
402 , mDestImage( destImage )
403 , mDestImageBpl( destImage->bytesPerLine() )
407 typedef void result_type;
409 void operator()( ImageBlock& block );
413 LineOperationDirection mDirection;
418 inline QRgb gaussianBlurVertical(
const int posy,
unsigned char *sourceFirstLine,
const int sourceBpl,
const int height );
419 inline QRgb gaussianBlurHorizontal(
const int posx,
unsigned char *sourceFirstLine,
const int width );
425 class FlipLineOperation
428 FlipLineOperation( LineOperationDirection direction )
429 : mDirection( direction )
432 typedef void result_type;
434 LineOperationDirection direction() {
return mDirection; }
436 void operator()( QRgb* startRef,
const int lineLength,
const int bytesPerLine );
439 LineOperationDirection mDirection;
445 #endif // QGSIMAGEOPERATION_H