~ubuntu-branches/ubuntu/maverick/digikam/maverick

« back to all changes in this revision

Viewing changes to imageplugins/localcontrast/ToneMappingFloat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Luka Renko
  • Date: 2009-12-22 21:38:14 UTC
  • mfrom: (1.2.26 upstream)
  • Revision ID: james.westby@ubuntu.com-20091222213814-dtgz0u1068y5rql7
Tags: 2:1.0.0-1ubuntu1
* Merge with Debian, remaining changes:
  - Export .pot name and copy to plugins in debian/rules
  - Build-depend on libkipi7-dev, libkexiv2-8-dev and 
    libkdcraw8-dev (KDE 4.3 -> 4.4)
* Build-depend on liblqr-1-0-dev: now in main (LP: #493508)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ============================================================
2
 
 *
3
 
 * This file is a part of digiKam project
4
 
 * http://www.digikam.org
5
 
 *
6
 
 * Date        : 2009-08-09
7
 
 * Description : LDR ToneMapper <http://zynaddsubfx.sourceforge.net/other/tonemapping>.
8
 
 *
9
 
 * Copyright (C) 2009 by Nasca Octavian Paul <zynaddsubfx at yahoo dot com>
10
 
 * Copyright (C) 2009 by Gilles Caulier <caulier dot gilles at gmail dot com>
11
 
 *
12
 
 * This program is free software; you can redistribute it
13
 
 * and/or modify it under the terms of the GNU General
14
 
 * Public License as published by the Free Software Foundation;
15
 
 * either version 2, or (at your option)
16
 
 * any later version.
17
 
 *
18
 
 * This program is distributed in the hope that it will be useful,
19
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
 * GNU General Public License for more details.
22
 
 *
23
 
 * ============================================================ */
24
 
 
25
 
// C++ includes.
26
 
 
27
 
#include <cstdio>
28
 
#include <cstdlib>
29
 
#include <cmath>
30
 
 
31
 
// Local includes.
32
 
 
33
 
#include "ToneMappingFloat.h"
34
 
 
35
 
namespace DigikamLocalContrastImagesPlugin
36
 
{
37
 
 
38
 
ToneMappingFloat::ToneMappingFloat()
39
 
                : ToneMappingBase()
40
 
{
41
 
    par.info_fast_mode = false;
42
 
}
43
 
 
44
 
ToneMappingFloat::~ToneMappingFloat()
45
 
{
46
 
}
47
 
 
48
 
void ToneMappingFloat::process_rgb_image(REALTYPE *img, int sizex, int sizey)
49
 
{
50
 
    update_preprocessed_values();
51
 
 
52
 
    int size            = sizex*sizey;
53
 
    REALTYPE *blurimage = new REALTYPE[size];
54
 
    REALTYPE *srcimg    = new REALTYPE[size*3];
55
 
 
56
 
    for (int i=0 ; i < (size*3) ; i++)
57
 
        srcimg[i] = img[i];
58
 
 
59
 
    if (par.stretch_contrast)
60
 
    {
61
 
        stretch_contrast(img,size*3);
62
 
    }
63
 
 
64
 
    int pos = 0;
65
 
 
66
 
    for (int nstage=0 ; !par.cancel() && (nstage < TONEMAPPING_MAX_STAGES) ; nstage++)
67
 
    {
68
 
        if (par.stage[nstage].enabled)
69
 
        {
70
 
            // compute the desatured image
71
 
 
72
 
            pos = 0;
73
 
 
74
 
            for (int i=0 ; !par.cancel() && (i < size) ; i++)
75
 
            {
76
 
                blurimage[i] = (REALTYPE)((img[pos]+img[pos+1]+img[pos+2])/3.0);
77
 
                pos += 3;
78
 
            }
79
 
 
80
 
            current_process_power_value = par.get_power(nstage);
81
 
 
82
 
            // blur
83
 
 
84
 
            inplace_blur(blurimage, sizex,sizey, par.get_blur(nstage));
85
 
 
86
 
            pos = 0;
87
 
 
88
 
            for (int i=0 ; !par.cancel() && (i<size) ; i++)
89
 
            {
90
 
                REALTYPE src_r  = img[pos];
91
 
                REALTYPE src_g  = img[pos+1];
92
 
                REALTYPE src_b  = img[pos+2];
93
 
 
94
 
                REALTYPE blur   = blurimage[i];
95
 
 
96
 
                REALTYPE dest_r = func(src_r,blur);
97
 
                REALTYPE dest_g = func(src_g,blur);
98
 
                REALTYPE dest_b = func(src_b,blur);
99
 
 
100
 
                img[pos]        = dest_r;
101
 
                img[pos+1]      = dest_g;
102
 
                img[pos+2]      = dest_b;
103
 
 
104
 
                pos += 3;
105
 
            }
106
 
        }
107
 
        par.postProgress(30 + nstage*10);
108
 
    }
109
 
 
110
 
    int high_saturation_value = 100-par.high_saturation;
111
 
    int low_saturation_value  = 100-par.low_saturation;
112
 
 
113
 
    if ((par.high_saturation != 100) || (par.low_saturation != 100))
114
 
    {
115
 
        int pos = 0;
116
 
 
117
 
        for (int i=0 ; !par.cancel() && (i < size) ; i++)
118
 
        {
119
 
            REALTYPE src_h, src_s, src_v;
120
 
            REALTYPE dest_h, dest_s, dest_v;
121
 
            rgb2hsv(srcimg[pos], srcimg[pos+1], srcimg[pos+2], src_h, src_s, src_v);
122
 
            rgb2hsv(img[pos], img[pos+1], img[pos+2], dest_h, dest_s, dest_v);
123
 
 
124
 
            REALTYPE dest_saturation = (REALTYPE)((src_s*high_saturation_value+dest_s*(100.0-high_saturation_value))*0.01);
125
 
            if (dest_v>src_v)
126
 
            {
127
 
                REALTYPE s1     = (REALTYPE)(dest_saturation*src_v/(dest_v+1.0/255.0));
128
 
                dest_saturation = (REALTYPE)((low_saturation_value*s1+par.low_saturation*dest_saturation)*0.01);
129
 
            }
130
 
 
131
 
            hsv2rgb(dest_h, dest_saturation, dest_v, img[pos], img[pos+1], img[pos+2]);
132
 
 
133
 
            pos += 3;
134
 
        }
135
 
    }
136
 
 
137
 
    par.postProgress(70);
138
 
 
139
 
    // Unsharp Mask filter
140
 
 
141
 
    if (par.unsharp_mask.enabled)
142
 
    {
143
 
        REALTYPE *val = new REALTYPE[size];
144
 
 
145
 
        // compute the desatured image
146
 
 
147
 
        int pos = 0;
148
 
 
149
 
        for (int i=0 ; !par.cancel() && (i < size) ; i++)
150
 
        {
151
 
            val[i] = blurimage[i] = (REALTYPE)((img[pos]+img[pos+1]+img[pos+2])/3.0);
152
 
            //val[i] = blurimage[i] = (REALTYPE)(max3(img[pos],img[pos+1],img[pos+2]));
153
 
            pos += 3;
154
 
        }
155
 
 
156
 
        REALTYPE blur_value = par.get_unsharp_mask_blur();
157
 
        inplace_blur(blurimage, sizex, sizey, blur_value);
158
 
 
159
 
        pos                 = 0;
160
 
        REALTYPE pow        = (REALTYPE)(2.5*par.get_unsharp_mask_power());
161
 
        REALTYPE threshold  = (REALTYPE)(par.unsharp_mask.threshold*pow/250.0);
162
 
        REALTYPE threshold2 = threshold/2;
163
 
 
164
 
        for (int i=0 ; !par.cancel() && (i < size) ; i++)
165
 
        {
166
 
            REALTYPE dval     = (val[i]-blurimage[i])*pow;
167
 
            REALTYPE abs_dval = fabs(dval);
168
 
            if (abs_dval < threshold)
169
 
            {
170
 
                if (abs_dval > threshold2)
171
 
                {
172
 
                    bool sign = (dval < 0.0);
173
 
                    dval      = (REALTYPE)((abs_dval-threshold2)*2.0);
174
 
                    if (sign) dval =- dval;
175
 
                }
176
 
                else
177
 
                {
178
 
                    dval = 0;
179
 
                }
180
 
            }
181
 
 
182
 
            REALTYPE r   = img[pos]  +dval;
183
 
            REALTYPE g   = img[pos+1]+dval;
184
 
            REALTYPE b   = img[pos+2]+dval;
185
 
 
186
 
            if (r<0.0) r = 0.0;
187
 
            if (r>1.0) r = 1.0;
188
 
            if (g<0.0) g = 0.0;
189
 
            if (g>1.0) g = 1.0;
190
 
            if (b<0.0) b = 0.0;
191
 
            if (b>1.0) b = 1.0;
192
 
 
193
 
            img[pos]     = r;
194
 
            img[pos+1]   = g;
195
 
            img[pos+2]   = b;
196
 
 
197
 
            pos += 3;
198
 
        }
199
 
 
200
 
        delete [] val;
201
 
    }
202
 
 
203
 
    delete [] srcimg;
204
 
    delete [] blurimage;
205
 
 
206
 
    par.postProgress(80);
207
 
}
208
 
 
209
 
void ToneMappingFloat::update_preprocessed_values()
210
 
{
211
 
    par.postProgress(20);
212
 
}
213
 
 
214
 
void ToneMappingFloat::process_16bit_rgb_image(unsigned short int *img, int sizex, int sizey)
215
 
{
216
 
    int size              = sizex*sizey;
217
 
    REALTYPE *tmpimage    = new REALTYPE[size*3];
218
 
    const float inv_65536 = 1.0/65536.0;
219
 
 
220
 
    for (int i=0 ; !par.cancel() && (i < size*3) ; i++)
221
 
    {
222
 
        //convert to floating point
223
 
        tmpimage[i] = (REALTYPE)(img[i]/65535.0);
224
 
    }
225
 
 
226
 
    process_rgb_image(tmpimage, sizex, sizey);
227
 
 
228
 
    //convert back to 8 bits (with dithering)
229
 
    int pos = 0;
230
 
 
231
 
    for (int i=0 ; !par.cancel() && (i < size) ; i++)
232
 
    {
233
 
        REALTYPE dither = ((rand()/65536)%65536)*inv_65536;
234
 
        img[pos]        = (int)(tmpimage[pos]  *65535.0+dither);
235
 
        img[pos+1]      = (int)(tmpimage[pos+1]*65535.0+dither);
236
 
        img[pos+2]      = (int)(tmpimage[pos+2]*65535.0+dither);
237
 
        pos+=3;
238
 
    }
239
 
 
240
 
    delete [] tmpimage;
241
 
 
242
 
    par.postProgress(90);
243
 
}
244
 
 
245
 
void ToneMappingFloat::process_8bit_rgb_image(unsigned char *img, int sizex, int sizey)
246
 
{
247
 
    int size            = sizex*sizey;
248
 
    REALTYPE *tmpimage  = new REALTYPE[size*3];
249
 
    const float inv_256 = 1.0/256.0;
250
 
 
251
 
    for (int i=0 ; !par.cancel() && (i < size*3) ; i++)
252
 
    {
253
 
        //convert to floating point
254
 
        tmpimage[i] = (REALTYPE)(img[i]/255.0);
255
 
    }
256
 
 
257
 
    process_rgb_image(tmpimage, sizex, sizey);
258
 
 
259
 
    //convert back to 8 bits (with dithering)
260
 
    int pos=0;
261
 
 
262
 
    for (int i=0 ; !par.cancel() && (i < size) ; i++)
263
 
    {
264
 
        REALTYPE dither = ((rand()/256)%256)*inv_256;
265
 
        img[pos]        = (int)(tmpimage[pos]  *255.0+dither);
266
 
        img[pos+1]      = (int)(tmpimage[pos+1]*255.0+dither);
267
 
        img[pos+2]      = (int)(tmpimage[pos+2]*255.0+dither);
268
 
        pos += 3;
269
 
    }
270
 
 
271
 
    delete [] tmpimage;
272
 
    par.postProgress(90);
273
 
}
274
 
 
275
 
void ToneMappingFloat::inplace_blur(REALTYPE *data, int sizex, int sizey, REALTYPE blur)
276
 
{
277
 
    blur /= preview_zoom;
278
 
 
279
 
    if (blur < 0.3) return;
280
 
 
281
 
    REALTYPE a = (REALTYPE)(exp(log(0.25)/blur));
282
 
 
283
 
    if ((a <= 0.0) || (a >= 1.0)) return;
284
 
 
285
 
    a *= a;
286
 
    REALTYPE denormal_remove = (REALTYPE)(1e-15);
287
 
 
288
 
    for (int stage=0 ; !par.cancel() && (stage < 2) ; stage++)
289
 
    {
290
 
        for (int y=0 ; !par.cancel() && (y < sizey) ; y++)
291
 
        {
292
 
            int pos      = y*sizex;
293
 
            REALTYPE old = data[pos];
294
 
            pos++;
295
 
 
296
 
            for (int x=1 ; !par.cancel() && (x < sizex) ; x++)
297
 
            {
298
 
                old       = (data[pos]*(1-a)+old*a)+denormal_remove;
299
 
                data[pos] = old;
300
 
                pos++;
301
 
            }
302
 
 
303
 
            pos = y*sizex+sizex-1;
304
 
 
305
 
            for (int x=1 ; !par.cancel() && (x < sizex) ; x++)
306
 
            {
307
 
                old       = (data[pos]*(1-a)+old*a)+denormal_remove;
308
 
                data[pos] = old;
309
 
                pos--;
310
 
            }
311
 
        }
312
 
 
313
 
        for (int x=0 ; !par.cancel() && (x < sizex) ; x++)
314
 
        {
315
 
            int pos      = x;
316
 
            REALTYPE old = data[pos];
317
 
 
318
 
            for (int y=1 ; !par.cancel() && (y < sizey) ; y++)
319
 
            {
320
 
                old       = (data[pos]*(1-a)+old*a)+denormal_remove;
321
 
                data[pos] = old;
322
 
                pos += sizex;
323
 
            }
324
 
 
325
 
            pos = x+sizex*(sizey-1);
326
 
 
327
 
            for (int y=1 ; !par.cancel() && (y < sizey) ; y++)
328
 
            {
329
 
                old       = (data[pos]*(1-a)+old*a)+denormal_remove;
330
 
                data[pos] = old;
331
 
                pos -= sizex;
332
 
            }
333
 
        }
334
 
    }
335
 
}
336
 
 
337
 
void ToneMappingFloat::stretch_contrast(REALTYPE *data, int datasize)
338
 
{
339
 
    //stretch the contrast
340
 
    const unsigned int histogram_size=256;
341
 
    //first, we compute the histogram
342
 
    unsigned int histogram[histogram_size];
343
 
 
344
 
    for (unsigned int i=0 ; i < histogram_size ; i++) histogram[i] = 0;
345
 
 
346
 
    for (unsigned int i=0 ; !par.cancel() && (i < (unsigned int)datasize) ; i++)
347
 
    {
348
 
        int m = (int)(data[i]*(histogram_size-1));
349
 
        if (m < 0) m = 0;
350
 
        if (m > (int)(histogram_size-1)) m = histogram_size-1;
351
 
        histogram[m]++;
352
 
    }
353
 
 
354
 
    //I want to strip the lowest and upper 0.1 procents (in the histogram) of the pixels
355
 
    int          min = 0,max = 255;
356
 
    unsigned int desired_sum = datasize/1000;
357
 
    unsigned int sum_min     = 0;
358
 
    unsigned int sum_max     = 0;
359
 
 
360
 
    for (unsigned int i=0 ; !par.cancel() && (i < histogram_size) ; i++)
361
 
    {
362
 
        sum_min += histogram[i];
363
 
        if (sum_min > desired_sum)
364
 
        {
365
 
            min = i;
366
 
            break;
367
 
        }
368
 
    }
369
 
 
370
 
    for (int i = histogram_size-1 ; !par.cancel() && (i >= 0) ; i--)
371
 
    {
372
 
        sum_max += histogram[i];
373
 
        if (sum_max > desired_sum)
374
 
        {
375
 
            max = i;
376
 
            break;
377
 
        }
378
 
    }
379
 
 
380
 
    if (min >= max)
381
 
    {
382
 
        min = 0;
383
 
        max = 255;
384
 
    }
385
 
 
386
 
    REALTYPE min_src_val = (REALTYPE)(min/255.0);
387
 
    REALTYPE max_src_val = (REALTYPE)(max/255.0);
388
 
 
389
 
    for (int i=0 ; !par.cancel() && (i < datasize) ; i++)
390
 
    {
391
 
        //stretch the contrast
392
 
        REALTYPE x = data[i];
393
 
        x          = (x-min_src_val)/(max_src_val-min_src_val);
394
 
        if (x < 0.0) x = 0.0;
395
 
        if (x > 1.0) x = 1.0;
396
 
        data[i]    = x;
397
 
    }
398
 
}
399
 
 
400
 
} // namespace DigikamNoiseReductionImagesPlugin