AlbumShaper 1.0a3
color.cpp
Go to the documentation of this file.
1//==============================================
2// copyright : (C) 2003-2005 by Will Stokes
3//==============================================
4// This program is free software; you can redistribute it
5// and/or modify it under the terms of the GNU General
6// Public License as published by the Free Software
7// Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//==============================================
10
11//Systemwide includes
12#include <qimage.h>
13#include <qstring.h>
14#include <qapplication.h>
15
16//Projectwide includes
17#include "color.h"
19
20#define MIN(x,y) ((x) < (y) ? (x) : (y))
21#define MAX(x,y) ((x) > (y) ? (x) : (y))
22
23//----------------------------------------------
24// Inputs:
25// -------
26// QString filename - location of original image on disk
27// StatusWidget* status - widget for making progress visible to user
28//
29// Outputs:
30// --------
31// QImage* returned - constructed image
32//
33// Description:
34// ------------
35// This method simply stretchs the color values to use up the entire dynamic range, in our
36// case red/green/blue values ranging from [0,255].
37//
38// The algorithm can be broken up into two steps:
39// 1.) finding the endpoints of the curretn color values (e.g. [6,120])
40// 2.) stretching the current image colors to fit the full [0,255] range
41//
42// One could simply iterate over the entire image and find the minimum and
43// maximum red, gree, and blue color values. However; often there is residual
44// noise at the endpoints since camera sensors always provide some noise,
45// while the majority of pixels fall somewhere in the middle. If we constructed
46// a histogram for a particular color channel it might look like this:
47// 5 | .
48// . | . .
49// . | . .
50// 1 | . . . . .
51// 0 |. .... ........ ............. .....
52// |-X------------*--------------*-----------X-----
53// 0 100 175 255
54//
55//
56// Here color values are plotted across the x axis, while the y axis indicates
57// how offten a particular color value was seen. Note we're plotting one of the
58// three color channels here (red, green, blue).
59//
60// In this example we notice most pixels that were observed fall in the 100-175
61// range (that is the red/green/blue color value was most often observed with a
62// value between 100-175). A few pixels with values of 5 or 10 were seen, and a
63// few with around 230 were seen. We want to stretch the common case, 100-175,
64// out to the full 0-255 range. That will make the pixels that were 175 now 255,
65// those that wer 100 now 0, thus darks will become darker, brights will become
66// brigther. We'll lineraly scale all pixels inbetween. Any pixels beyond the
67// 100-175 range will get clamped at 0 and 255 respectively.
68//
69// Now, if we simply find the smallest and largest values we'll get soemthinglike
70// 5 and 235, and stretching will have very little effect on the image. Instead,
71// we first actually populate a histogram array for each color channel (an int
72// array of 256 elements). We then determine the 1% and 99% boundaries but walking
73// from the end points of hte histogram array and adding up the elements. Once 1%
74// of the pixels have been accounted for we set the the boundary endpoint to the index,
75// thus giving us 100,175 in this case.
76//
77// Actually scaling the colors is relatively trivial. A second pass over the
78// image takes place during which a pixels color is fetched, modified, then written
79// back out according to the following equations. It should be noted that as a
80// sanity check we clamp scaled color values to the [0,255] range.
81//
82// r' = [255*(r-rLow)] / [rHigh - rLow]
83// g' = [255*(g-gLow)] / [gHigh - gLow]
84// b' = [255*(b-bLow)] / [bHigh - bLow]
85//
86// where [r/g/b][Low/High] refer to the 1% and 99% endpoints for the red,
87// green, and blue color changes respectively.
88//
89//----------------------------------------------
90
91//==============================================
92QImage* improveColorBalance( QString filename, StatusWidget* status )
93{
94 //load original image
95 QImage* editedImage = new QImage( filename );
96
97 //convert to 32-bit depth if necessary
98 if( editedImage->depth() < 32 )
99 {
100 QImage* tmp = editedImage;
101 editedImage = new QImage( tmp->convertDepth( 32, Qt::AutoColor ) );
102 delete tmp; tmp=NULL;
103 }
104
105 //setup progress bar
106 QString statusMessage = qApp->translate( "improveColorBalance", "Enhancing Color Balance:" );
107 status->showProgressBar( statusMessage, 100 );
108 qApp->processEvents();
109
110 //update progress bar for every 1% of completion
111 const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
112 int newProgress = 0;
113
114 //construct intensity histographs for each color channel
115 int redVals[256];
116 int greenVals[256];
117 int blueVals[256];
118 int i=0;
119 for(i=0; i<256; i++)
120 {
121 redVals[i] = 0;
122 greenVals[i] = 0;
123 blueVals[i] = 0;
124 }
125
126 //populate histogram by iterating over all image pixels
127 int numPixels = editedImage->width()*editedImage->height();
128 QRgb* rgb;
129 uchar* scanLine;
130 int x, y;
131 for( y=0; y<editedImage->height(); y++)
132 {
133 //iterate over each selected pixel in scanline
134 scanLine = editedImage->scanLine(y);
135 for( x=0; x<editedImage->width(); x++)
136 {
137 rgb = ((QRgb*)scanLine+x);
138 redVals[qRed(*rgb)]++;
139 greenVals[qGreen(*rgb)]++;
140 blueVals[qBlue(*rgb)]++;
141 } //for x
142 } //for y
143
144 //find 1% and 99% precenticles
145 //we'll stretch these values so we avoid outliers from affecting the stretch
146 int sumR=0;
147 int sumG=0;
148 int sumB=0;
149 int indexLowR, indexHighR;
150 int indexLowG, indexHighG;
151 int indexLowB, indexHighB;
152 indexLowR = -1; indexHighR = -1;
153 indexLowG = -1; indexHighG = -1;
154 indexLowB = -1; indexHighB = -1;
155 for(i=0; i<256; i++)
156 {
157 sumR+=redVals[i];
158 sumG+=greenVals[i];
159 sumB+=blueVals[i];
160
161 //if 1% not found yet and criteria met set index
162 if(indexLowR < 0 && sumR >= 0.01*numPixels)
163 { indexLowR = i; }
164 if(indexLowG < 0 && sumG >= 0.01*numPixels)
165 { indexLowG = i; }
166 if(indexLowB < 0 && sumB >= 0.01*numPixels)
167 { indexLowB = i; }
168
169 //if 99% not found yet and criteria met set index
170 if(indexHighR < 0 && sumR >= 0.99*numPixels)
171 { indexHighR = i; }
172 if(indexHighG < 0 && sumG >= 0.99*numPixels)
173 { indexHighG = i; }
174 if(indexHighB < 0 && sumB >= 0.99*numPixels)
175 { indexHighB = i; }
176 }
177
178 //run through all image pixels a second time, this time scaling coordinates as necessary
179 for( y=0; y<editedImage->height(); y++)
180 {
181 //iterate over each selected pixel in scanline
182 scanLine = editedImage->scanLine(y);
183 for( x=0; x<editedImage->width(); x++)
184 {
185 //get color coordinates and convert to 0-1 scale
186 rgb = ((QRgb*)scanLine+x);
187 double r = ((double)qRed(*rgb) );
188 double g = ((double)qGreen(*rgb) );
189 double b = ((double)qBlue(*rgb) );
190
191 if(indexHighR != indexLowR) { r = (255*(r-indexLowR))/(indexHighR-indexLowR); }
192 if(indexHighG != indexLowG) { g = (255*(g-indexLowG))/(indexHighG-indexLowG); }
193 if(indexHighB != indexLowB) { b = (255*(b-indexLowB))/(indexHighB-indexLowB); }
194
195 int rp = (int) MIN( MAX(r, 0), 255 );
196 int gp = (int) MIN( MAX(g, 0), 255 );
197 int bp = (int) MIN( MAX(b, 0), 255 );
198
199 //set adjusted color value
200 *rgb = qRgb(rp,gp,bp);
201
202 //update status bar if significant progress has been made since last update
203 newProgress++;
205 {
206 newProgress = 0;
208 qApp->processEvents();
209 }
210
211 } //for x
212 } //for y
213
214 //remove status bar
215 status->setStatus( "" );
216 qApp->processEvents();
217
218 //return pointer to edited image
219 return editedImage;
220}
221//==============================================
void setStatus(QString message)
Update message.
void showProgressBar(QString message, int numSteps)
Initializes the progress bar.
void incrementProgress()
Updates the progress bar by one step.
#define MIN(x, y)
Definition color.cpp:20
#define MAX(x, y)
Definition color.cpp:21
QImage * improveColorBalance(QString filename, StatusWidget *status)
Definition color.cpp:92
long b
int updateIncrement
QImage * editedImage
StatusWidget * status
int newProgress