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

« back to all changes in this revision

Viewing changes to core/libs/dimg/filters/auto/equalizefilter.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:
80
80
    miracles on an image or destroy it.*/
81
81
void EqualizeFilter::equalizeImage()
82
82
{
 
83
    if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
 
84
    {
 
85
        kDebug() << "Ref. image and Org. has different bits depth";
 
86
        return;
 
87
    }
 
88
 
83
89
    struct double_packet  high, low, intensity;
84
 
    struct double_packet* map;
85
 
    struct int_packet*    equalize_map;
86
90
    register int          i;
87
91
    int                   progress;
88
92
 
89
 
    if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
90
 
    {
91
 
        kDebug() << "Ref. image and Org. has different bits depth";
92
 
        return;
93
 
    }
94
 
 
95
93
    // Create an histogram of the reference image.
96
 
    ImageHistogram* histogram = new ImageHistogram(m_refImage.bits(), m_refImage.width(),
97
 
            m_refImage.height(), m_refImage.sixteenBit());
 
94
    QScopedPointer<ImageHistogram> histogram(new ImageHistogram(m_refImage.bits(), m_refImage.width(),
 
95
                                                                m_refImage.height(), m_refImage.sixteenBit()));
98
96
    histogram->calculate();
99
97
 
100
98
    // Memory allocation.
101
 
    // FIXME: Possible null pointer dereference "histogram", otherwise it is redundant to check if "histgram" is null below.
102
 
    map          = new double_packet[histogram->getHistogramSegments()];
103
 
    equalize_map = new int_packet[histogram->getHistogramSegments()];
 
99
    QScopedArrayPointer<double_packet> map(new double_packet[histogram->getHistogramSegments()]);
 
100
    QScopedArrayPointer<int_packet> equalize_map(new int_packet[histogram->getHistogramSegments()]);
104
101
 
105
 
    if ( !histogram || !map || !equalize_map )
 
102
    if (map.isNull() || equalize_map.isNull())
106
103
    {
107
 
        delete histogram;
108
 
        if (map)
109
 
        {
110
 
            delete [] map;
111
 
        }
112
 
 
113
 
        if (equalize_map)
114
 
        {
115
 
            delete [] equalize_map;
116
 
        }
117
 
 
118
104
        kWarning() << ("Unable to allocate memory!");
119
105
        return;
120
106
    }
137
123
    // Stretch the histogram.
138
124
 
139
125
    low  = map[0];
140
 
    high = map[histogram->getHistogramSegments()-1];
141
 
    memset(equalize_map, 0, histogram->getHistogramSegments()*sizeof(int_packet));
 
126
    high = map[histogram->getHistogramSegments() - 1];
 
127
    memset(equalize_map.data(), 0, histogram->getHistogramSegments()*sizeof(int_packet));
142
128
 
143
129
    // TODO magic number 256
144
130
    for (i = 0 ; runningFlag() && (i < histogram->getHistogramSegments()) ; ++i)
145
131
    {
146
132
        if (high.red != low.red)
147
 
            equalize_map[i].red = (uint)(((256*histogram->getHistogramSegments() -1) *
148
 
                                          (map[i].red-low.red))/(high.red-low.red));
 
133
            equalize_map[i].red = (uint)(((256 * histogram->getHistogramSegments() - 1) *
 
134
                                          (map[i].red - low.red)) / (high.red - low.red));
149
135
 
150
136
        if (high.green != low.green)
151
 
            equalize_map[i].green = (uint)(((256*histogram->getHistogramSegments() -1) *
152
 
                                            (map[i].green-low.green))/(high.green-low.green));
 
137
            equalize_map[i].green = (uint)(((256 * histogram->getHistogramSegments() - 1) *
 
138
                                            (map[i].green - low.green)) / (high.green - low.green));
153
139
 
154
140
        if (high.blue != low.blue)
155
 
            equalize_map[i].blue = (uint)(((256*histogram->getHistogramSegments() -1) *
156
 
                                           (map[i].blue-low.blue))/(high.blue-low.blue));
 
141
            equalize_map[i].blue = (uint)(((256 * histogram->getHistogramSegments() - 1) *
 
142
                                           (map[i].blue - low.blue)) / (high.blue - low.blue));
157
143
 
158
144
        if (high.alpha != low.alpha)
159
 
            equalize_map[i].alpha = (uint)(((256*histogram->getHistogramSegments() -1) *
160
 
                                            (map[i].alpha-low.alpha))/(high.alpha-low.alpha));
 
145
            equalize_map[i].alpha = (uint)(((256 * histogram->getHistogramSegments() - 1) *
 
146
                                            (map[i].alpha - low.alpha)) / (high.alpha - low.alpha));
161
147
    }
162
148
 
163
 
    delete histogram;
164
 
    delete [] map;
165
 
 
166
149
    uchar* data     = m_orgImage.bits();
167
150
    int w           = m_orgImage.width();
168
151
    int h           = m_orgImage.height();
169
152
    bool sixteenBit = m_orgImage.sixteenBit();
170
 
    int size        = w*h;
 
153
    int size        = w * h;
171
154
 
172
155
    // Apply results to image.
173
156
    // TODO magic number 257
185
168
 
186
169
            if (low.red != high.red)
187
170
            {
188
 
                red = (equalize_map[red].red)/257;
 
171
                red = (equalize_map[red].red) / 257;
189
172
            }
190
173
 
191
174
            if (low.green != high.green)
192
175
            {
193
 
                green = (equalize_map[green].green)/257;
 
176
                green = (equalize_map[green].green) / 257;
194
177
            }
195
178
 
196
179
            if (low.blue != high.blue)
197
180
            {
198
 
                blue = (equalize_map[blue].blue)/257;
 
181
                blue = (equalize_map[blue].blue) / 257;
199
182
            }
200
183
 
201
184
            if (low.alpha != high.alpha)
202
185
            {
203
 
                alpha = (equalize_map[alpha].alpha)/257;
 
186
                alpha = (equalize_map[alpha].alpha) / 257;
204
187
            }
205
188
 
206
189
            ptr[0] = blue;
211
194
 
212
195
            progress = (int)(((double)i * 100.0) / size);
213
196
 
214
 
            if ( progress%5 == 0 )
 
197
            if (progress % 5 == 0)
215
198
            {
216
 
                postProgress( progress );
 
199
                postProgress(progress);
217
200
            }
218
201
        }
219
202
    }
231
214
 
232
215
            if (low.red != high.red)
233
216
            {
234
 
                red = (equalize_map[red].red)/257;
 
217
                red = (equalize_map[red].red) / 257;
235
218
            }
236
219
 
237
220
            if (low.green != high.green)
238
221
            {
239
 
                green = (equalize_map[green].green)/257;
 
222
                green = (equalize_map[green].green) / 257;
240
223
            }
241
224
 
242
225
            if (low.blue != high.blue)
243
226
            {
244
 
                blue = (equalize_map[blue].blue)/257;
 
227
                blue = (equalize_map[blue].blue) / 257;
245
228
            }
246
229
 
247
230
            if (low.alpha != high.alpha)
248
231
            {
249
 
                alpha = (equalize_map[alpha].alpha)/257;
 
232
                alpha = (equalize_map[alpha].alpha) / 257;
250
233
            }
251
234
 
252
235
            ptr[0] = blue;
257
240
 
258
241
            progress = (int)(((double)i * 100.0) / size);
259
242
 
260
 
            if ( progress%5 == 0 )
 
243
            if (progress % 5 == 0)
261
244
            {
262
 
                postProgress( progress );
 
245
                postProgress(progress);
263
246
            }
264
247
        }
265
248
    }
266
 
 
267
 
    delete [] equalize_map;
268
249
}
269
250
 
270
251
FilterAction EqualizeFilter::filterAction()