~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/dimg/filters/auto/normalizefilter.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer, Rohan Garg, Philip Muškovac, Felix Geyer
  • Date: 2011-09-23 18:18:55 UTC
  • mfrom: (1.2.36 upstream)
  • Revision ID: package-import@ubuntu.com-20110923181855-ifs67wxkugshev9k
Tags: 2:2.1.1-0ubuntu1
[ Rohan Garg ]
* New upstream release (LP: #834190)
  - debian/control
    + Build with libqtwebkit-dev
 - debian/kipi-plugins-common
    + Install libkvkontakte required by kipi-plugins
 - debian/digikam
    + Install panoramagui

[ Philip Muškovac ]
* New upstream release
  - debian/control:
    + Add libcv-dev, libcvaux-dev, libhighgui-dev, libboost-graph1.46-dev,
      libksane-dev, libxml2-dev, libxslt-dev, libqt4-opengl-dev, libqjson-dev,
      libgpod-dev and libqca2-dev to build-deps
    + Add packages for kipi-plugins, libmediawiki, libkface, libkgeomap and
      libkvkontakte
  - debian/rules:
    + Don't build with gphoto2 since it doesn't build with it.
  - Add kubuntu_fix_test_linking.diff to fix linking of the dngconverter test
  - update install files
  - update kubuntu_01_mysqld_executable_name.diff for new cmake layout
    and rename to kubuntu_mysqld_executable_name.diff
* Fix typo in digikam-data description (LP: #804894)
* Fix Vcs links

[ Felix Geyer ]
* Move library data files to the new packages libkface-data, libkgeomap-data
  and libkvkontakte-data.
* Override version of the embedded library packages to 1.0~digikam<version>.
* Exclude the library packages from digikam-dbg to prevent file conflicts in
  the future.
* Call dh_install with --list-missing.

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        : 2005-24-01
7
 
 * Description : normalize image filter.
8
 
 *
9
 
 * Copyright (C) 2005-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
10
 
 *
11
 
 * This program is free software; you can redistribute it
12
 
 * and/or modify it under the terms of the GNU General
13
 
 * Public License as published by the Free Software Foundation;
14
 
 * either version 2, or (at your option)
15
 
 * any later version.
16
 
 *
17
 
 * This program is distributed in the hope that it will be useful,
18
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
 * GNU General Public License for more details.
21
 
 *
22
 
 * ============================================================ */
23
 
 
24
 
#include "normalizefilter.h"
25
 
 
26
 
// C++ includes
27
 
 
28
 
#include <cstdio>
29
 
#include <cmath>
30
 
 
31
 
// KDE includes
32
 
 
33
 
#include <kdebug.h>
34
 
 
35
 
// Local includes
36
 
 
37
 
#include "dimg.h"
38
 
 
39
 
namespace Digikam
40
 
{
41
 
 
42
 
NormalizeFilter::NormalizeFilter(DImg* orgImage, const DImg* refImage, QObject* parent)
43
 
    : DImgThreadedFilter(orgImage, parent, "NormalizeFilter"),
44
 
      m_refImage(*refImage)
45
 
{
46
 
    initFilter();
47
 
}
48
 
 
49
 
NormalizeFilter::~NormalizeFilter()
50
 
{
51
 
    cancelFilter();
52
 
}
53
 
 
54
 
void NormalizeFilter::filterImage()
55
 
{
56
 
    normalizeImage();
57
 
    m_destImage = m_orgImage;
58
 
}
59
 
 
60
 
/** This method scales brightness values across the active
61
 
    image so that the darkest point becomes black, and the
62
 
    brightest point becomes as bright as possible without
63
 
    altering its hue. This is often a magic fix for
64
 
    images that are dim or washed out.*/
65
 
void NormalizeFilter::normalizeImage()
66
 
{
67
 
    NormalizeParam param;
68
 
    int            x;
69
 
    uint           i;
70
 
    unsigned short range;
71
 
    int            progress;
72
 
 
73
 
    if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
74
 
    {
75
 
        kDebug() << "Ref. image and Org. has different bits depth";
76
 
        return;
77
 
    }
78
 
 
79
 
    bool sixteenBit = m_orgImage.sixteenBit();
80
 
    int segments    = sixteenBit ? NUM_SEGMENTS_16BIT : NUM_SEGMENTS_8BIT;
81
 
 
82
 
    // Memory allocation.
83
 
 
84
 
    param.lut = new unsigned short[segments];
85
 
 
86
 
    // Find min. and max. values.
87
 
 
88
 
    param.min    = segments-1;
89
 
    param.max    = 0;
90
 
    uint refSize = m_refImage.width()*m_refImage.height();
91
 
 
92
 
    if (!sixteenBit)        // 8 bits image.
93
 
    {
94
 
        uchar  red, green, blue;
95
 
        uchar* ptr = m_refImage.bits();
96
 
 
97
 
        for (i = 0 ; runningFlag() && (i < refSize) ; ++i)
98
 
        {
99
 
            blue  = ptr[0];
100
 
            green = ptr[1];
101
 
            red   = ptr[2];
102
 
 
103
 
            if (red < param.min)
104
 
            {
105
 
                param.min = red;
106
 
            }
107
 
 
108
 
            if (red > param.max)
109
 
            {
110
 
                param.max = red;
111
 
            }
112
 
 
113
 
            if (green < param.min)
114
 
            {
115
 
                param.min = green;
116
 
            }
117
 
 
118
 
            if (green > param.max)
119
 
            {
120
 
                param.max = green;
121
 
            }
122
 
 
123
 
            if (blue < param.min)
124
 
            {
125
 
                param.min = blue;
126
 
            }
127
 
 
128
 
            if (blue > param.max)
129
 
            {
130
 
                param.max = blue;
131
 
            }
132
 
 
133
 
            ptr += 4;
134
 
        }
135
 
    }
136
 
    else                    // 16 bits image.
137
 
    {
138
 
        unsigned short  red, green, blue;
139
 
        unsigned short* ptr = (unsigned short*)m_refImage.bits();
140
 
 
141
 
        for (i = 0 ; runningFlag() && (i < refSize) ; ++i)
142
 
        {
143
 
            blue  = ptr[0];
144
 
            green = ptr[1];
145
 
            red   = ptr[2];
146
 
 
147
 
            if (red < param.min)
148
 
            {
149
 
                param.min = red;
150
 
            }
151
 
 
152
 
            if (red > param.max)
153
 
            {
154
 
                param.max = red;
155
 
            }
156
 
 
157
 
            if (green < param.min)
158
 
            {
159
 
                param.min = green;
160
 
            }
161
 
 
162
 
            if (green > param.max)
163
 
            {
164
 
                param.max = green;
165
 
            }
166
 
 
167
 
            if (blue < param.min)
168
 
            {
169
 
                param.min = blue;
170
 
            }
171
 
 
172
 
            if (blue > param.max)
173
 
            {
174
 
                param.max = blue;
175
 
            }
176
 
 
177
 
            ptr += 4;
178
 
        }
179
 
    }
180
 
 
181
 
    // Calculate LUT.
182
 
 
183
 
    if (runningFlag())
184
 
    {
185
 
        range = (unsigned short)(param.max - param.min);
186
 
 
187
 
        if (range != 0)
188
 
        {
189
 
            for (x = (int)param.min ; x <= (int)param.max ; ++x)
190
 
            {
191
 
                param.lut[x] = (unsigned short)((segments-1) * (x - param.min) / range);
192
 
            }
193
 
        }
194
 
        else
195
 
        {
196
 
            param.lut[(int)param.min] = (unsigned short)param.min;
197
 
        }
198
 
    }
199
 
 
200
 
    uchar* data = m_orgImage.bits();
201
 
    int w       = m_orgImage.width();
202
 
    int h       = m_orgImage.height();
203
 
    uint size   = w*h;
204
 
 
205
 
    // Apply LUT to image.
206
 
 
207
 
    if (!sixteenBit)        // 8 bits image.
208
 
    {
209
 
        uchar  red, green, blue;
210
 
        uchar* ptr = data;
211
 
 
212
 
        for (i = 0 ; runningFlag() && (i < size) ; ++i)
213
 
        {
214
 
            blue   = ptr[0];
215
 
            green  = ptr[1];
216
 
            red    = ptr[2];
217
 
 
218
 
            ptr[0] = param.lut[blue];
219
 
            ptr[1] = param.lut[green];
220
 
            ptr[2] = param.lut[red];
221
 
 
222
 
            ptr += 4;
223
 
 
224
 
            progress = (int)(((double)i * 100.0) / size);
225
 
 
226
 
            if ( progress%5 == 0 )
227
 
            {
228
 
                postProgress( progress );
229
 
            }
230
 
        }
231
 
    }
232
 
    else                    // 16 bits image.
233
 
    {
234
 
        unsigned short  red, green, blue;
235
 
        unsigned short* ptr = (unsigned short*)data;
236
 
 
237
 
        for (i = 0 ; runningFlag() && (i < size) ; ++i)
238
 
        {
239
 
            blue   = ptr[0];
240
 
            green  = ptr[1];
241
 
            red    = ptr[2];
242
 
 
243
 
            ptr[0] = param.lut[blue];
244
 
            ptr[1] = param.lut[green];
245
 
            ptr[2] = param.lut[red];
246
 
 
247
 
            ptr += 4;
248
 
 
249
 
            progress = (int)(((double)i * 100.0) / size);
250
 
 
251
 
            if ( progress%5 == 0 )
252
 
            {
253
 
                postProgress( progress );
254
 
            }
255
 
        }
256
 
    }
257
 
 
258
 
    delete [] param.lut;
259
 
}
260
 
 
261
 
}  // namespace Digikam