AlbumShaper 1.0a3
color.cpp File Reference
#include <qimage.h>
#include <qstring.h>
#include <qapplication.h>
#include "color.h"
#include "../../gui/statusWidget.h"
Include dependency graph for color.cpp:

Go to the source code of this file.

Macros

#define MIN(x, y)
 
#define MAX(x, y)
 

Functions

QImage * improveColorBalance (QString filename, StatusWidget *status)
 

Macro Definition Documentation

◆ MAX

◆ MIN

Function Documentation

◆ improveColorBalance()

QImage * improveColorBalance ( QString filename,
StatusWidget * status )

Definition at line 92 of file color.cpp.

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}
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
long b
int updateIncrement
QImage * editedImage
StatusWidget * status
int newProgress

References b, editedImage, StatusWidget::incrementProgress(), MAX, MIN, newProgress, StatusWidget::setStatus(), StatusWidget::showProgressBar(), status, and updateIncrement.

Referenced by EditingInterface::colorBalance().