~ubuntu-branches/ubuntu/vivid/digikam/vivid

« back to all changes in this revision

Viewing changes to core/libs/dimg/filters/auto/stretchfilter.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-02-14 19:29:29 UTC
  • mfrom: (1.2.38) (3.1.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120214192929-cx2zia3b2nt67lvz
Tags: 4:2.5.0-1ubuntu1
* Merge from debian unstable, remaining changes:
  - keep seperate binary packages:
    + libkface1, libkface-data, libkface-dev
    + libkgeomap1, libkgeomap-data, libkgeomap-dev
    + libvkontakte1, libkvkontakte-dev
    + libmediawiki1, libmediawiki-dev
  - keep patches:
    + kubuntu_mysqld_executable_name.diff
  - don't fail on missing files in dh_install
  - install oxygen icons for kipi-plugins
  - build-depend on mysql 5.5 instead of 5.1
  - update install files
* digikam breaks/replaces kipi-plugins-common << 4:2.5.0~
* digikam-doc breaks/replaces digikam-data << 4:2.5.0~
* digikam-data breaks/replaces kipi-plugins << 4:2.5.0~
* kipi-plugins-common breaks/replaces kipi-plugins << 4:2.5.0~

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
    of color values. This is a contrast enhancement technique.*/
74
74
void StretchFilter::stretchContrastImage()
75
75
{
 
76
    if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
 
77
    {
 
78
        kDebug() << "Ref. image and Org. has different bits depth";
 
79
        return;
 
80
    }
 
81
 
76
82
    struct double_packet high, low, intensity;
77
 
    struct int_packet*   normalize_map;
78
83
    long long            number_pixels;
79
84
    register long        i;
80
85
    int                  progress;
81
86
    unsigned long        threshold_intensity;
82
87
 
83
 
    if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
84
 
    {
85
 
        kDebug() << "Ref. image and Org. has different bits depth";
86
 
        return;
87
 
    }
88
 
 
89
88
    // Create an histogram of the reference image.
90
 
    ImageHistogram* histogram = new ImageHistogram(m_refImage.bits(), m_refImage.width(),
91
 
            m_refImage.height(), m_refImage.sixteenBit());
92
 
    if ( !histogram )
 
89
    QScopedPointer<ImageHistogram> histogram(new ImageHistogram(m_refImage.bits(), m_refImage.width(),
 
90
                                                                m_refImage.height(), m_refImage.sixteenBit()));
 
91
    if (histogram.isNull())
93
92
    {
94
 
      kWarning() << ("Unable to allocate memory!");
95
 
      return;
 
93
        kWarning() << ("Unable to allocate memory!");
 
94
        return;
96
95
    }
97
96
    histogram->calculate();
98
97
 
99
98
    // Memory allocation.
100
 
    normalize_map = new int_packet[histogram->getHistogramSegments()];
 
99
    QScopedArrayPointer<int_packet> normalize_map(new int_packet[histogram->getHistogramSegments()]);
101
100
 
102
 
    if ( !normalize_map )
 
101
    if (normalize_map.isNull())
103
102
    {
104
 
        if (histogram)
105
 
        {
106
 
            delete histogram;
107
 
        }
108
 
 
109
 
        if (normalize_map)
110
 
        {
111
 
            delete [] normalize_map;
112
 
        }
113
 
 
114
103
        kWarning() << ("Unable to allocate memory!");
115
104
        return;
116
105
    }
131
120
    {
132
121
        intensity.red += histogram->getValue(RedChannel, (int)high.red);
133
122
 
134
 
        if ( intensity.red > threshold_intensity )
 
123
        if (intensity.red > threshold_intensity)
135
124
        {
136
125
            break;
137
126
        }
138
127
    }
139
128
 
140
 
    if ( low.red == high.red )
 
129
    if (low.red == high.red)
141
130
    {
142
131
        threshold_intensity = 0;
143
132
        memset(&intensity, 0, sizeof(struct double_packet));
146
135
        {
147
136
            intensity.red += histogram->getValue(RedChannel, (int)low.red);
148
137
 
149
 
            if ( intensity.red > threshold_intensity )
 
138
            if (intensity.red > threshold_intensity)
150
139
            {
151
140
                break;
152
141
            }
158
147
        {
159
148
            intensity.red += histogram->getValue(RedChannel, (int)high.red);
160
149
 
161
 
            if ( intensity.red > threshold_intensity )
 
150
            if (intensity.red > threshold_intensity)
162
151
            {
163
152
                break;
164
153
            }
173
162
    {
174
163
        intensity.green += histogram->getValue(GreenChannel, (int)high.green);
175
164
 
176
 
        if ( intensity.green > threshold_intensity )
 
165
        if (intensity.green > threshold_intensity)
177
166
        {
178
167
            break;
179
168
        }
180
169
    }
181
170
 
182
 
    if ( low.green == high.green )
 
171
    if (low.green == high.green)
183
172
    {
184
173
        threshold_intensity = 0;
185
174
        memset(&intensity, 0, sizeof(struct double_packet));
188
177
        {
189
178
            intensity.green += histogram->getValue(GreenChannel, (int)low.green);
190
179
 
191
 
            if ( intensity.green > threshold_intensity )
 
180
            if (intensity.green > threshold_intensity)
192
181
            {
193
182
                break;
194
183
            }
200
189
        {
201
190
            intensity.green += histogram->getValue(GreenChannel, (int)high.green);
202
191
 
203
 
            if ( intensity.green > threshold_intensity )
 
192
            if (intensity.green > threshold_intensity)
204
193
            {
205
194
                break;
206
195
            }
215
204
    {
216
205
        intensity.blue += histogram->getValue(BlueChannel, (int)high.blue);
217
206
 
218
 
        if ( intensity.blue > threshold_intensity )
 
207
        if (intensity.blue > threshold_intensity)
219
208
        {
220
209
            break;
221
210
        }
222
211
    }
223
212
 
224
 
    if ( low.blue == high.blue )
 
213
    if (low.blue == high.blue)
225
214
    {
226
215
        threshold_intensity = 0;
227
216
        memset(&intensity, 0, sizeof(struct double_packet));
230
219
        {
231
220
            intensity.blue += histogram->getValue(BlueChannel, (int)low.blue);
232
221
 
233
 
            if ( intensity.blue > threshold_intensity )
 
222
            if (intensity.blue > threshold_intensity)
234
223
            {
235
224
                break;
236
225
            }
242
231
        {
243
232
            intensity.blue += histogram->getValue(BlueChannel, (int)high.blue);
244
233
 
245
 
            if ( intensity.blue > threshold_intensity )
 
234
            if (intensity.blue > threshold_intensity)
246
235
            {
247
236
                break;
248
237
            }
257
246
    {
258
247
        intensity.alpha += histogram->getValue(AlphaChannel, (int)high.alpha);
259
248
 
260
 
        if ( intensity.alpha > threshold_intensity )
 
249
        if (intensity.alpha > threshold_intensity)
261
250
        {
262
251
            break;
263
252
        }
264
253
    }
265
254
 
266
 
    if ( low.alpha == high.alpha )
 
255
    if (low.alpha == high.alpha)
267
256
    {
268
257
        threshold_intensity = 0;
269
258
        memset(&intensity, 0, sizeof(struct double_packet));
272
261
        {
273
262
            intensity.alpha += histogram->getValue(AlphaChannel, (int)low.alpha);
274
263
 
275
 
            if ( intensity.alpha > threshold_intensity )
 
264
            if (intensity.alpha > threshold_intensity)
276
265
            {
277
266
                break;
278
267
            }
284
273
        {
285
274
            intensity.alpha += histogram->getValue(AlphaChannel, (int)high.alpha);
286
275
 
287
 
            if ( intensity.alpha > threshold_intensity )
 
276
            if (intensity.alpha > threshold_intensity)
288
277
            {
289
278
                break;
290
279
            }
293
282
 
294
283
    // Stretch the histogram to create the normalized image mapping.
295
284
 
296
 
    memset(normalize_map, 0, histogram->getHistogramSegments()*sizeof(struct int_packet));
 
285
    memset(normalize_map.data(), 0, histogram->getHistogramSegments()*sizeof(struct int_packet));
297
286
 
298
287
    // TODO magic number 256
299
288
    for (i = 0 ; runningFlag() && (i <= (long)histogram->getMaxSegmentIndex()) ; ++i)
304
293
        }
305
294
        else if (i > (long) high.red)
306
295
        {
307
 
            normalize_map[i].red = (256*histogram->getHistogramSegments() -1);
 
296
            normalize_map[i].red = (256 * histogram->getHistogramSegments() - 1);
308
297
        }
309
298
        else if (low.red != high.red)
310
299
        {
311
 
            normalize_map[i].red = (int)(((256*histogram->getHistogramSegments() -1)*(i-low.red))/(high.red-low.red));
 
300
            normalize_map[i].red = (int)(((256 * histogram->getHistogramSegments() - 1) * (i - low.red)) / (high.red - low.red));
312
301
        }
313
302
 
314
303
        if (i < (long) low.green)
317
306
        }
318
307
        else if (i > (long) high.green)
319
308
        {
320
 
            normalize_map[i].green = (256*histogram->getHistogramSegments() -1);
 
309
            normalize_map[i].green = (256 * histogram->getHistogramSegments() - 1);
321
310
        }
322
311
        else if (low.green != high.green)
323
312
        {
324
 
            normalize_map[i].green = (int)(((256*histogram->getHistogramSegments() -1)*(i-low.green))/(high.green-low.green));
 
313
            normalize_map[i].green = (int)(((256 * histogram->getHistogramSegments() - 1) * (i - low.green)) / (high.green - low.green));
325
314
        }
326
315
 
327
316
        if (i < (long) low.blue)
330
319
        }
331
320
        else if (i > (long) high.blue)
332
321
        {
333
 
            normalize_map[i].blue = (256*histogram->getHistogramSegments() -1);
 
322
            normalize_map[i].blue = (256 * histogram->getHistogramSegments() - 1);
334
323
        }
335
324
        else if (low.blue != high.blue)
336
325
        {
337
 
            normalize_map[i].blue = (int)(((256*histogram->getHistogramSegments() -1)*(i-low.blue))/(high.blue-low.blue));
 
326
            normalize_map[i].blue = (int)(((256 * histogram->getHistogramSegments() - 1) * (i - low.blue)) / (high.blue - low.blue));
338
327
        }
339
328
 
340
329
        if (i < (long) low.alpha)
343
332
        }
344
333
        else if (i > (long) high.alpha)
345
334
        {
346
 
            normalize_map[i].alpha = (256*histogram->getHistogramSegments() -1);
 
335
            normalize_map[i].alpha = (256 * histogram->getHistogramSegments() - 1);
347
336
        }
348
337
        else if (low.alpha != high.alpha)
349
338
        {
350
 
            normalize_map[i].alpha = (int)(((256*histogram->getHistogramSegments() -1)*(i-low.alpha))/(high.alpha-low.alpha));
 
339
            normalize_map[i].alpha = (int)(((256 * histogram->getHistogramSegments() - 1) * (i - low.alpha)) / (high.alpha - low.alpha));
351
340
        }
352
341
    }
353
342
 
357
346
    int w           = m_orgImage.width();
358
347
    int h           = m_orgImage.height();
359
348
    bool sixteenBit = m_orgImage.sixteenBit();
360
 
    int size        = w*h;
 
349
    int size        = w * h;
361
350
 
362
351
    // TODO magic number 257
363
352
    if (!sixteenBit)        // 8 bits image.
374
363
 
375
364
            if (low.red != high.red)
376
365
            {
377
 
                red = (normalize_map[red].red)/257;
 
366
                red = (normalize_map[red].red) / 257;
378
367
            }
379
368
 
380
369
            if (low.green != high.green)
381
370
            {
382
 
                green = (normalize_map[green].green)/257;
 
371
                green = (normalize_map[green].green) / 257;
383
372
            }
384
373
 
385
374
            if (low.blue != high.blue)
386
375
            {
387
 
                blue = (normalize_map[blue].blue)/257;
 
376
                blue = (normalize_map[blue].blue) / 257;
388
377
            }
389
378
 
390
379
            if (low.alpha != high.alpha)
391
380
            {
392
 
                alpha = (normalize_map[alpha].alpha)/257;
 
381
                alpha = (normalize_map[alpha].alpha) / 257;
393
382
            }
394
383
 
395
384
            ptr[0] = blue;
400
389
 
401
390
            progress = (int)(((double)i * 100.0) / size);
402
391
 
403
 
            if ( progress%5 == 0 )
 
392
            if (progress % 5 == 0)
404
393
            {
405
 
                postProgress( progress );
 
394
                postProgress(progress);
406
395
            }
407
396
        }
408
397
    }
420
409
 
421
410
            if (low.red != high.red)
422
411
            {
423
 
                red = (normalize_map[red].red)/257;
 
412
                red = (normalize_map[red].red) / 257;
424
413
            }
425
414
 
426
415
            if (low.green != high.green)
427
416
            {
428
 
                green = (normalize_map[green].green)/257;
 
417
                green = (normalize_map[green].green) / 257;
429
418
            }
430
419
 
431
420
            if (low.blue != high.blue)
432
421
            {
433
 
                blue = (normalize_map[blue].blue)/257;
 
422
                blue = (normalize_map[blue].blue) / 257;
434
423
            }
435
424
 
436
425
            if (low.alpha != high.alpha)
437
426
            {
438
 
                alpha = (normalize_map[alpha].alpha)/257;
 
427
                alpha = (normalize_map[alpha].alpha) / 257;
439
428
            }
440
429
 
441
430
            ptr[0] = blue;
446
435
 
447
436
            progress = (int)(((double)i * 100.0) / size);
448
437
 
449
 
            if ( progress%5 == 0 )
 
438
            if (progress % 5 == 0)
450
439
            {
451
 
                postProgress( progress );
 
440
                postProgress(progress);
452
441
            }
453
442
        }
454
443
    }
455
 
 
456
 
    delete histogram;
457
 
    delete [] normalize_map;
458
444
}
459
445
 
460
446
FilterAction StretchFilter::filterAction()