1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2005-05-25
* Description : Raindrop threaded image filter.
*
* Copyright (C) 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
* Copyright (C) 2006-2007 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* Original RainDrop algorithm copyrighted 2004-2005 by
* Pieter Z. Voloshyn <pieter dot voloshyn at gmail dot com>.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
// C++ includes.
#include <cmath>
#include <cstdlib>
// TQt includes.
#include <tqdeepcopy.h>
#include <tqdatetime.h>
#include <tqrect.h>
// Local includes.
#include "dimg.h"
#include "dimgimagefilters.h"
#include "raindrop.h"
namespace DigikamRainDropImagesPlugin
{
RainDrop::RainDrop(Digikam::DImg *orgImage, TQObject *tqparent, int drop,
int amount, int coeff, TQRect *selection)
: Digikam::DImgThreadedFilter(orgImage, tqparent, "RainDrop")
{
m_drop = drop;
m_amount = amount;
m_coeff = coeff;
m_selectedX = m_selectedY = m_selectedW = m_selectedH = 0;
if ( selection )
{
m_selectedX = selection->left();
m_selectedY = selection->top();
m_selectedW = selection->width();
m_selectedH = selection->height();
}
initFilter();
}
void RainDrop::filterImage(void)
{
int w = m_orgImage.width();
int h = m_orgImage.height();
// If we have a region selection in image, use it to apply the filter modification around,
// else, applied the filter on the full image.
if (m_selectedW && m_selectedH)
{
Digikam::DImg zone1, zone2, zone3, zone4,
zone1Dest, zone2Dest, zone3Dest, zone4Dest,
selectedImg;
selectedImg = m_orgImage.copy(m_selectedX, m_selectedY, m_selectedW, m_selectedH);
// Cut the original image in 4 areas without clipping region.
zone1 = m_orgImage.copy(0, 0, m_selectedX, w);
zone2 = m_orgImage.copy(m_selectedX, 0, m_selectedX + m_selectedW, m_selectedY);
zone3 = m_orgImage.copy(m_selectedX, m_selectedY + m_selectedH, m_selectedX + m_selectedW, h);
zone4 = m_orgImage.copy(m_selectedX + m_selectedW, 0, w, h);
zone1Dest = Digikam::DImg(zone1.width(), zone1.height(), zone1.sixteenBit(), zone1.hasAlpha());
zone2Dest = Digikam::DImg(zone2.width(), zone2.height(), zone2.sixteenBit(), zone2.hasAlpha());
zone3Dest = Digikam::DImg(zone3.width(), zone3.height(), zone3.sixteenBit(), zone3.hasAlpha());
zone4Dest = Digikam::DImg(zone4.width(), zone4.height(), zone4.sixteenBit(), zone4.hasAlpha());
// Apply effect on each area.
rainDropsImage(&zone1, &zone1Dest, 0, m_drop, m_amount, m_coeff, true, 0, 25);
rainDropsImage(&zone2, &zone2Dest, 0, m_drop, m_amount, m_coeff, true, 25, 50);
rainDropsImage(&zone3, &zone3Dest, 0, m_drop, m_amount, m_coeff, true, 50, 75);
rainDropsImage(&zone4, &zone4Dest, 0, m_drop, m_amount, m_coeff, true, 75, 100);
// Build the target image.
m_destImage.bitBltImage(&zone1Dest, 0, 0);
m_destImage.bitBltImage(&zone2Dest, m_selectedX, 0);
m_destImage.bitBltImage(&zone3Dest, m_selectedX, m_selectedY + m_selectedH);
m_destImage.bitBltImage(&zone4Dest, m_selectedX + m_selectedW, 0);
m_destImage.bitBltImage(&selectedImg, m_selectedX, m_selectedY);
}
else
{
rainDropsImage(&m_orgImage, &m_destImage, 0, m_drop, m_amount, m_coeff, true, 0, 100);
}
}
/* Function to apply the RainDrops effect backported from ImageProcessing version 2
*
* orgImage => The image
* MinDropSize => It's the minimum random size for rain drop.
* MaxDropSize => It's the minimum random size for rain drop.
* Amount => It's the maximum number for rain drops inside the image.
* Coeff => It's the fisheye's coefficient.
* bLimitRange => If true, the drop will not be cut.
* progressMin => Min. value for progress bar (can be different if using clipping area).
* progressMax => Max. value for progress bar (can be different if using clipping area).
*
* Theory => This functions does several math's functions and the engine
* is simple to undestand, but a little hard to implement. A
* control will indicate if there is or not a raindrop in that
* area, if not, a fisheye effect with a random size (max=MaxDropSize)
* will be applied, after this, a shadow will be applied too.
* and after this, a blur function will finish the effect.
*/
void RainDrop::rainDropsImage(Digikam::DImg *orgImage, Digikam::DImg *destImage, int MinDropSize, int MaxDropSize,
int Amount, int Coeff, bool bLimitRange, int progressMin, int progressMax)
{
bool bResp;
int nRandSize, i;
int nRandX, nRandY;
int nCounter = 0;
int nWidth = orgImage->width();
int nHeight = orgImage->height();
bool sixteenBit = orgImage->sixteenBit();
int bytesDepth = orgImage->bytesDepth();
uchar *data = orgImage->bits();
uchar *pResBits = destImage->bits();
if (Amount <= 0)
return;
if (MinDropSize >= MaxDropSize)
MaxDropSize = MinDropSize + 1;
if (MaxDropSize <= 0)
return;
uchar *pStatusBits = new uchar[nHeight * nWidth];
memset(pStatusBits, 0, sizeof(nHeight * nWidth));
// Initially, copy all pixels to destination
destImage->bitBltImage(orgImage, 0, 0);
// Randomize.
TQDateTime dt = TQDateTime::tqcurrentDateTime();
TQDateTime Y2000( TQDate(2000, 1, 1), TQTime(0, 0, 0) );
uint seed = dt.secsTo(Y2000);
for (i = 0; !m_cancel && (i < Amount); i++)
{
nCounter = 0;
do
{
nRandX = (int)(rand_r(&seed) * ((double)( nWidth - 1) / RAND_MAX));
nRandY = (int)(rand_r(&seed) * ((double)(nHeight - 1) / RAND_MAX));
nRandSize = (rand() % (MaxDropSize - MinDropSize)) + MinDropSize;
bResp = CreateRainDrop (data, nWidth, nHeight, sixteenBit, bytesDepth,
pResBits, pStatusBits,
nRandX, nRandY, nRandSize, Coeff, bLimitRange);
nCounter++;
}
while ((bResp == false) && (nCounter < 10000) && !m_cancel);
// Update the progress bar in dialog.
if (nCounter >= 10000)
{
i = Amount;
postProgress(progressMax);
break;
}
postProgress( (int)(progressMin + ((double)(i) *
(double)(progressMax-progressMin)) / (double)Amount) );
}
delete [] pStatusBits;
}
bool RainDrop::CreateRainDrop(uchar *pBits, int Width, int Height, bool sixteenBit, int bytesDepth,
uchar *pResBits, uchar* pStatusBits,
int X, int Y, int DropSize, double Coeff, bool bLimitRange)
{
register int w, h, nw1, nh1, nw2, nh2;
int nHalfSize = DropSize / 2;
int nBright;
double lfRadius, lfOldRadius, lfAngle, lfDiv;
Digikam::DColor imageData;
uint nTotalR, nTotalG, nTotalB, offset;
int nBlurPixels, nBlurRadius;
if (CanBeDropped(Width, Height, pStatusBits, X, Y, DropSize, bLimitRange))
{
Coeff *= 0.01;
lfDiv = (double)nHalfSize / log (Coeff * (double)nHalfSize + 1.0);
for (h = -nHalfSize; !m_cancel && (h <= nHalfSize); h++)
{
for (w = -nHalfSize; !m_cancel && (w <= nHalfSize); w++)
{
lfRadius = sqrt (h * h + w * w);
lfAngle = atan2 ((double)h, (double)w);
if (lfRadius <= (double)nHalfSize)
{
lfOldRadius = lfRadius;
lfRadius = (exp (lfRadius / lfDiv) - 1.0) / Coeff;
nw1 = (int)((double)X + lfRadius * cos (lfAngle));
nh1 = (int)((double)Y + lfRadius * sin (lfAngle));
nw2 = X + w;
nh2 = Y + h;
if (IsInside(Width, Height, nw1, nh1))
{
if (IsInside(Width, Height, nw2, nh2))
{
nBright = 0;
if (lfOldRadius >= 0.9 * (double)nHalfSize)
{
if ((lfAngle >= 0.0) && (lfAngle < 2.25))
nBright = -80;
else if ((lfAngle >= 2.25) && (lfAngle < 2.5))
nBright = -40;
else if ((lfAngle >= -0.25) && (lfAngle < 0.0))
nBright = -40;
}
else if (lfOldRadius >= 0.8 * (double)nHalfSize)
{
if ((lfAngle >= 0.75) && (lfAngle < 1.50))
nBright = -40;
else if ((lfAngle >= -0.10) && (lfAngle < 0.75))
nBright = -30;
else if ((lfAngle >= 1.50) && (lfAngle < 2.35))
nBright = -30;
}
else if (lfOldRadius >= 0.7 * (double)nHalfSize)
{
if ((lfAngle >= 0.10) && (lfAngle < 2.0))
nBright = -20;
else if ((lfAngle >= -2.50) && (lfAngle < -1.90))
nBright = 60;
}
else if (lfOldRadius >= 0.6 * (double)nHalfSize)
{
if ((lfAngle >= 0.50) && (lfAngle < 1.75))
nBright = -20;
else if ((lfAngle >= 0.0) && (lfAngle < 0.25))
nBright = 20;
else if ((lfAngle >= 2.0) && (lfAngle < 2.25))
nBright = 20;
}
else if (lfOldRadius >= 0.5 * (double)nHalfSize)
{
if ((lfAngle >= 0.25) && (lfAngle < 0.50))
nBright = 30;
else if ((lfAngle >= 1.75 ) && (lfAngle < 2.0))
nBright = 30;
}
else if (lfOldRadius >= 0.4 * (double)nHalfSize)
{
if ((lfAngle >= 0.5) && (lfAngle < 1.75))
nBright = 40;
}
else if (lfOldRadius >= 0.3 * (double)nHalfSize)
{
if ((lfAngle >= 0.0) && (lfAngle < 2.25))
nBright = 30;
}
else if (lfOldRadius >= 0.2 * (double)nHalfSize)
{
if ((lfAngle >= 0.5) && (lfAngle < 1.75))
nBright = 20;
}
imageData.setColor(pBits + Offset(Width, nw1, nh1, bytesDepth), sixteenBit);
if (sixteenBit)
{
// convert difference to 16-bit range
if (nBright > 0)
nBright = (nBright + 1) * 256 - 1;
else
nBright = (nBright - 1) * 256 + 1;
imageData.setRed (LimitValues16(imageData.red() + nBright));
imageData.setGreen(LimitValues16(imageData.green() + nBright));
imageData.setBlue (LimitValues16(imageData.blue() + nBright));
}
else
{
imageData.setRed (LimitValues8(imageData.red() + nBright));
imageData.setGreen(LimitValues8(imageData.green() + nBright));
imageData.setBlue (LimitValues8(imageData.blue() + nBright));
}
imageData.setPixel(pResBits + Offset(Width, nw2, nh2, bytesDepth));
}
}
}
}
}
nBlurRadius = DropSize / 25 + 1;
for (h = -nHalfSize - nBlurRadius; !m_cancel && (h <= nHalfSize + nBlurRadius); h++)
{
for (w = -nHalfSize - nBlurRadius; !m_cancel && (w <= nHalfSize + nBlurRadius); w++)
{
lfRadius = sqrt (h * h + w * w);
if (lfRadius <= (double)nHalfSize * 1.1)
{
nTotalR = nTotalG = nTotalB = 0;
nBlurPixels = 0;
for (nh1 = -nBlurRadius; !m_cancel && (nh1 <= nBlurRadius); nh1++)
{
for (nw1 = -nBlurRadius; !m_cancel && (nw1 <= nBlurRadius); nw1++)
{
nw2 = X + w + nw1;
nh2 = Y + h + nh1;
if (IsInside (Width, Height, nw2, nh2))
{
imageData.setColor(pResBits + Offset(Width, nw2, nh2, bytesDepth), sixteenBit);
nTotalR += imageData.red();
nTotalG += imageData.green();
nTotalB += imageData.blue();
nBlurPixels++;
}
}
}
nw1 = X + w;
nh1 = Y + h;
if (IsInside (Width, Height, nw1, nh1))
{
offset = Offset(Width, nw1, nh1, bytesDepth);
// to preserve alpha channel
imageData.setColor(pResBits + offset, sixteenBit);
imageData.setRed (nTotalR / nBlurPixels);
imageData.setGreen(nTotalG / nBlurPixels);
imageData.setBlue (nTotalB / nBlurPixels);
imageData.setPixel(pResBits + offset);
}
}
}
}
SetDropStatusBits (Width, Height, pStatusBits, X, Y, DropSize);
}
else
return (false);
return (true);
}
bool RainDrop::CanBeDropped(int Width, int Height, uchar *pStatusBits, int X, int Y,
int DropSize, bool bLimitRange)
{
register int w, h, i = 0;
int nHalfSize = DropSize / 2;
if (pStatusBits == NULL)
return (true);
for (h = Y - nHalfSize; h <= Y + nHalfSize; h++)
{
for (w = X - nHalfSize; w <= X + nHalfSize; w++)
{
if (IsInside (Width, Height, w, h))
{
i = h * Width + w;
if (pStatusBits[i])
return (false);
}
else
{
if (bLimitRange)
return (false);
}
}
}
return (true);
}
bool RainDrop::SetDropStatusBits (int Width, int Height, uchar *pStatusBits,
int X, int Y, int DropSize)
{
register int w, h, i = 0;
int nHalfSize = DropSize / 2;
if (pStatusBits == NULL)
return (false);
for (h = Y - nHalfSize; h <= Y + nHalfSize; h++)
{
for (w = X - nHalfSize; w <= X + nHalfSize; w++)
{
if (IsInside (Width, Height, w, h))
{
i = h * Width + w;
pStatusBits[i] = 255;
}
}
}
return (true);
}
} // NameSpace DigikamRainDropImagesPlugin
|