1
/* ============================================================
3
* This file is a part of digiKam project
4
* http://www.digikam.org
7
* Description : normalize image filter.
9
* Copyright (C) 2005-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
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)
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.
22
* ============================================================ */
24
#include "normalizefilter.h"
42
NormalizeFilter::NormalizeFilter(DImg* orgImage, const DImg* refImage, QObject* parent)
43
: DImgThreadedFilter(orgImage, parent, "NormalizeFilter"),
49
NormalizeFilter::~NormalizeFilter()
54
void NormalizeFilter::filterImage()
57
m_destImage = m_orgImage;
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()
73
if (m_orgImage.sixteenBit() != m_refImage.sixteenBit())
75
kDebug() << "Ref. image and Org. has different bits depth";
79
bool sixteenBit = m_orgImage.sixteenBit();
80
int segments = sixteenBit ? NUM_SEGMENTS_16BIT : NUM_SEGMENTS_8BIT;
84
param.lut = new unsigned short[segments];
86
// Find min. and max. values.
88
param.min = segments-1;
90
uint refSize = m_refImage.width()*m_refImage.height();
92
if (!sixteenBit) // 8 bits image.
94
uchar red, green, blue;
95
uchar* ptr = m_refImage.bits();
97
for (i = 0 ; runningFlag() && (i < refSize) ; ++i)
113
if (green < param.min)
118
if (green > param.max)
123
if (blue < param.min)
128
if (blue > param.max)
136
else // 16 bits image.
138
unsigned short red, green, blue;
139
unsigned short* ptr = (unsigned short*)m_refImage.bits();
141
for (i = 0 ; runningFlag() && (i < refSize) ; ++i)
157
if (green < param.min)
162
if (green > param.max)
167
if (blue < param.min)
172
if (blue > param.max)
185
range = (unsigned short)(param.max - param.min);
189
for (x = (int)param.min ; x <= (int)param.max ; ++x)
191
param.lut[x] = (unsigned short)((segments-1) * (x - param.min) / range);
196
param.lut[(int)param.min] = (unsigned short)param.min;
200
uchar* data = m_orgImage.bits();
201
int w = m_orgImage.width();
202
int h = m_orgImage.height();
205
// Apply LUT to image.
207
if (!sixteenBit) // 8 bits image.
209
uchar red, green, blue;
212
for (i = 0 ; runningFlag() && (i < size) ; ++i)
218
ptr[0] = param.lut[blue];
219
ptr[1] = param.lut[green];
220
ptr[2] = param.lut[red];
224
progress = (int)(((double)i * 100.0) / size);
226
if ( progress%5 == 0 )
228
postProgress( progress );
232
else // 16 bits image.
234
unsigned short red, green, blue;
235
unsigned short* ptr = (unsigned short*)data;
237
for (i = 0 ; runningFlag() && (i < size) ; ++i)
243
ptr[0] = param.lut[blue];
244
ptr[1] = param.lut[green];
245
ptr[2] = param.lut[red];
249
progress = (int)(((double)i * 100.0) / size);
251
if ( progress%5 == 0 )
253
postProgress( progress );
261
} // namespace Digikam