digiKam
blurfxfilter.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2005-05-25
7  * Description : Blur FX threaded image filter.
8  *
9  * Copyright 2005-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  * Copyright 2006-2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
11  * Copyright 2010 by Martin Klapetek <martin dot klapetek at gmail dot com>
12  *
13  * Original Blur algorithms copyrighted 2004 by
14  * Pieter Z. Voloshyn <pieter dot voloshyn at gmail dot com>.
15  *
16  * This program is free software; you can redistribute it
17  * and/or modify it under the terms of the GNU General
18  * Public License as published by the Free Software Foundation;
19  * either version 2, or (at your option)
20  * any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * ============================================================ */
28 
29 #ifndef DIGIKAM_BLUR_FX_FILTER_H
30 #define DIGIKAM_BLUR_FX_FILTER_H
31 
32 // Local includes
33 
34 #include "digikam_export.h"
35 #include "dimgthreadedfilter.h"
36 #include "digikam_globals.h"
37 
38 namespace Digikam
39 {
40 
41 class RandomNumberGenerator;
42 
43 class DIGIKAM_EXPORT BlurFXFilter : public DImgThreadedFilter
44 {
45  Q_OBJECT
46 
47 public:
48 
50  {
51  ZoomBlur = 0,
60  Mosaic
61  };
62 
63 private:
64 
65  struct Q_DECL_HIDDEN Args
66  {
67  explicit Args()
68  : start (0),
69  stop (0),
70  h (0),
71  w (0),
72  orgImage (nullptr),
73  destImage (nullptr),
74  X (0),
75  Y (0),
76  Distance (0),
77  nCount (0),
78  lpXArray (nullptr),
79  lpYArray (nullptr),
80  BlendRadius (0),
81  bInversed (0),
82  layer1 (nullptr),
83  layer2 (nullptr),
84  layer3 (nullptr),
85  layer4 (nullptr),
86  SizeW (0),
87  SizeH (0),
88  StrengthRange(0),
89  Radius (0),
90  Kernel (nullptr),
91  arrMult (nullptr),
92  pBlur (nullptr)
93  {
94  }
95 
96  uint start;
97  uint stop;
98  uint h;
99  uint w;
100  DImg* orgImage;
101  DImg* destImage;
102  int X;
103  int Y;
104  int Distance;
105 
106  int nCount;
107  int* lpXArray;
108  int* lpYArray;
109 
110  int BlendRadius;
111  bool bInversed;
112 
113  uchar* layer1;
114  uchar* layer2;
115  uchar* layer3;
116  uchar* layer4;
117 
118  int SizeW;
119  int SizeH;
120 
121  int StrengthRange;
122  int Radius;
123  int* Kernel;
124  int** arrMult;
125  uchar* pBlur;
126  };
127 
128 public:
129 
130  explicit BlurFXFilter(QObject* const parent = nullptr);
131  explicit BlurFXFilter(DImg* const orgImage,
132  QObject* const parent = nullptr,
133  int blurFXType = ZoomBlur,
134  int distance = 100,
135  int level = 45);
136  ~BlurFXFilter() override;
137 
138  static QString FilterIdentifier()
139  {
140  return QLatin1String("digikam:BlurFXFilter");
141  }
142 
143  static QString DisplayableName();
144 
146  {
147  return QList<int>() << 1;
148  }
149 
150  static int CurrentVersion()
151  {
152  return 1;
153  }
154 
155  QString filterIdentifier() const override
156  {
157  return FilterIdentifier();
158  }
159 
160  FilterAction filterAction() override;
161 
162  void readParameters(const FilterAction& action) override;
163 
164 private:
165 
166  void filterImage() override;
167 
168  // Backported from ImageProcessing version 1
169  void softenerBlur(DImg* const orgImage, DImg* const destImage);
170  void softenerBlurMultithreaded(const Args& prm);
171 
172  void shakeBlur(DImg* const orgImage, DImg* const destImage, int Distance);
173  void shakeBlurStage1Multithreaded(const Args& prm);
174  void shakeBlurStage2Multithreaded(const Args& prm);
175 
176  void frostGlass(DImg* const orgImage, DImg* const destImage, int Frost);
177 
178  // Backported from ImageProcessing version 2
179  void zoomBlur(DImg* const orgImage, DImg* const destImage, int X, int Y, int Distance, const QRect& pArea=QRect());
180  void zoomBlurMultithreaded(const Args& prm);
181 
182  void radialBlur(DImg* const orgImage, DImg* const destImage, int X, int Y, int Distance, const QRect& pArea=QRect());
183  void radialBlurMultithreaded(const Args& prm);
184 
185  void focusBlur(DImg* const orgImage, DImg* const destImage, int X, int Y, int BlurRadius, int BlendRadius,
186  bool bInversed=false, const QRect& pArea=QRect());
187  void focusBlurMultithreaded(const Args& prm);
188 
189  void farBlur(DImg* const orgImage, DImg* const destImage, int Distance);
190 
191  void motionBlur(DImg* const orgImage, DImg* const destImage, int Distance, double Angle=0.0);
192  void motionBlurMultithreaded(const Args& prm);
193 
194  void smartBlur(DImg* const orgImage, DImg* const destImage, int Radius, int Strength);
195  void smartBlurStage1Multithreaded(const Args& prm);
196  void smartBlurStage2Multithreaded(const Args& prm);
197 
198  void mosaic(DImg* const orgImage, DImg* const destImage, int SizeW, int SizeH);
199  void mosaicMultithreaded(const Args& prm);
200 
201 private:
202 
203  void MakeConvolution(DImg* const orgImage, DImg* const destImage, int Radius, int Kernel[]);
204  void MakeConvolutionStage1Multithreaded(const Args& prm);
205  void MakeConvolutionStage2Multithreaded(const Args& prm);
206 
207  DColor RandomColor(uchar* const Bits, int Width, int Height, bool sixteenBit, int bytesDepth,
208  int X, int Y, int Radius,
209  int alpha, RandomNumberGenerator& generator, int range, uchar* const IntensityCount,
210  uint* const AverageColorR, uint* const AverageColorG, uint* const AverageColorB);
211 
212  // Return the limit defined the max and min values.
213  inline int Lim_Max(int Now, int Up, int Max)
214  {
215  --Max;
216 
217  while (Now > Max - Up)
218  {
219  --Up;
220  }
221 
222  return (Up);
223  };
224 
225  // Return the luminance (Y) component of YIQ color model.
226  inline int GetIntensity (int R, int G, int B)
227  {
228  return (int)(R * 0.3 + G * 0.59 + B * 0.11);
229  };
230 
231  // function to allocate a 2d array
232  inline int** Alloc2DArray (int Columns, int Rows)
233  {
234  // We declare our future 2d array to be returned
235  // and we alloc the main pointer with Columns
236  int** lpcArray = new int*[Columns];
237 
238  for (int i = 0 ; i < Columns ; ++i)
239  {
240  lpcArray[i] = new int[Rows];
241  }
242 
243  return (lpcArray);
244  }
245 
246  // Function to deallocates the 2d array previously created
247  inline void Free2DArray (int** lpcArray, int Columns)
248  {
249  // loop to deallocate the columns
250  for (int i = 0 ; i < Columns ; ++i)
251  {
252  delete [] lpcArray[i];
253  }
254 
255  // now, we delete the main pointer
256  delete [] lpcArray;
257  }
258 
259  inline bool IsInside (int Width, int Height, int X, int Y)
260  {
261  bool bIsWOk = ((X < 0) ? false : (X >= Width ) ? false : true);
262  bool bIsHOk = ((Y < 0) ? false : (Y >= Height) ? false : true);
263 
264  return (bIsWOk && bIsHOk);
265  };
266 
267  inline int GetOffset(int Width, int X, int Y, int bytesDepth)
268  {
269  return (Y * Width * bytesDepth) + (X * bytesDepth);
270  };
271 
272  inline int GetOffsetAdjusted(int Width, int Height, int X, int Y, int bytesDepth)
273  {
274  X = (X < 0) ? 0 : ((X >= Width ) ? (Width - 1) : X);
275  Y = (Y < 0) ? 0 : ((Y >= Height) ? (Height - 1) : Y);
276 
277  return GetOffset(Width, X, Y, bytesDepth);
278  };
279 
280  inline bool IsColorInsideTheRange (int cR, int cG, int cB,
281  int nR, int nG, int nB,
282  int Range)
283  {
284  if ((nR >= (cR - Range)) && (nR <= (cR + Range)))
285  {
286  if ((nG >= (cG - Range)) && (nG <= (cG + Range)))
287  {
288  if ((nB >= (cB - Range)) && (nB <= (cB + Range)))
289  {
290  return true;
291  }
292  }
293  }
294 
295  return false;
296  };
297 
298 private:
299 
300  class Private;
301  Private* const d;
302 };
303 
304 } // namespace Digikam
305 
306 #endif // DIGIKAM_BLUR_FX_FILTER_H
Definition: blurfxfilter.h:44
QString filterIdentifier() const override
Definition: blurfxfilter.h:155
static int CurrentVersion()
Definition: blurfxfilter.h:150
BlurFXFilterTypes
Definition: blurfxfilter.h:50
@ ShakeBlur
Definition: blurfxfilter.h:56
@ SoftenerBlur
Definition: blurfxfilter.h:55
@ MotionBlur
Definition: blurfxfilter.h:54
@ SmartBlur
Definition: blurfxfilter.h:58
@ FarBlur
Definition: blurfxfilter.h:53
@ RadialBlur
Definition: blurfxfilter.h:52
@ FocusBlur
Definition: blurfxfilter.h:57
@ FrostGlass
Definition: blurfxfilter.h:59
static QList< int > SupportedVersions()
Definition: blurfxfilter.h:145
static QString FilterIdentifier()
Definition: blurfxfilter.h:138
Definition: dcolor.h:43
Definition: dimgthreadedfilter.h:41
Definition: dimg.h:62
Definition: filteraction.h:43
Definition: randomnumbergenerator.h:64
#define X
@ Width
Definition: coredbfields.h:87
@ Height
Definition: coredbfields.h:88
Definition: datefolderview.cpp:43