80
80
miracles on an image or destroy it.*/
81
81
void EqualizeFilter::equalizeImage()
83
if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
85
kDebug() << "Ref. image and Org. has different bits depth";
83
89
struct double_packet high, low, intensity;
84
struct double_packet* map;
85
struct int_packet* equalize_map;
89
if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
91
kDebug() << "Ref. image and Org. has different bits depth";
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();
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()]);
105
if ( !histogram || !map || !equalize_map )
102
if (map.isNull() || equalize_map.isNull())
115
delete [] equalize_map;
118
104
kWarning() << ("Unable to allocate memory!");
137
123
// Stretch the histogram.
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));
143
129
// TODO magic number 256
144
130
for (i = 0 ; runningFlag() && (i < histogram->getHistogramSegments()) ; ++i)
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));
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));
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));
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));
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();
172
155
// Apply results to image.
173
156
// TODO magic number 257
186
169
if (low.red != high.red)
188
red = (equalize_map[red].red)/257;
171
red = (equalize_map[red].red) / 257;
191
174
if (low.green != high.green)
193
green = (equalize_map[green].green)/257;
176
green = (equalize_map[green].green) / 257;
196
179
if (low.blue != high.blue)
198
blue = (equalize_map[blue].blue)/257;
181
blue = (equalize_map[blue].blue) / 257;
201
184
if (low.alpha != high.alpha)
203
alpha = (equalize_map[alpha].alpha)/257;
186
alpha = (equalize_map[alpha].alpha) / 257;
232
215
if (low.red != high.red)
234
red = (equalize_map[red].red)/257;
217
red = (equalize_map[red].red) / 257;
237
220
if (low.green != high.green)
239
green = (equalize_map[green].green)/257;
222
green = (equalize_map[green].green) / 257;
242
225
if (low.blue != high.blue)
244
blue = (equalize_map[blue].blue)/257;
227
blue = (equalize_map[blue].blue) / 257;
247
230
if (low.alpha != high.alpha)
249
alpha = (equalize_map[alpha].alpha)/257;
232
alpha = (equalize_map[alpha].alpha) / 257;