AlbumShaper  1.0a3
contrast.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 "contrast.h"
18 #include "../tools/imageTools.h"
19 #include "../../gui/statusWidget.h"
20 
21 #define MIN(x,y) ((x) < (y) ? (x) : (y))
22 #define MAX(x,y) ((x) < (y) ? (x) : (y))
23 
24 //----------------------------------------------
25 // Inputs:
26 // -------
27 // QString filename - location of original image on disk
28 // StatusWidget* status - widget for making progress visible to user
29 //
30 // Outputs:
31 // --------
32 // QImage* returned - constructed image
33 //
34 // Description:
35 // ------------
36 // This method simply stretchs the luminance rnage to use up the entire dynamic range, in our
37 // case from [0,255]. This algorithm is very similar to the color enhancement algorithm, except
38 // in this case we convert color values to HSL space and work with the L values (luminance).
39 //
40 // The algorithm can be broken up into two steps:
41 // 1.) finding the current luminance endpoints (e.g. [6,120])
42 // 2.) stretching the luminance range to fit the full [0,255] range
43 //
44 // One could simply iterate over the entire image and find the minimum and
45 // maximum luminance values. However; often there is residual
46 // noise at the endpoints since camera sensors always provide some noise,
47 // while the majority of pixels fall somewhere in the middle. If we constructed
48 // a histogram for of pixel luminance values it might look like this:
49 // 5 | .
50 // . | . .
51 // . | . .
52 // 1 | . . . . .
53 // 0 |. .... ........ ............. .....
54 // |-X------------*--------------*-----------X-----
55 // 0 100 175 255
56 //
57 // Here luminance values are plotted across the x axis, while the y axis indicates
58 // how offten a particular luminance value was seen.
59 //
60 // In this example we notice most pixel luminance values were observed fall in
61 // the 100-175 range. A few pixels with values of 5 or 10 were seen, and a
62 // few with around 230 were seen. We want to stretch the common case, 100-175,
63 // out to the full 0-255 range. That will make the pixels that were 175 now 255,
64 // those that wer 100 now 0, thus darks will become darker, brights will become
65 // brigther. We'll lineraly scale all pixels inbetween. Any pixels beyond the
66 // 100-175 range will get clamped at 0 and 255 respectively.
67 //
68 // Now, if we simply find the smallest and largest values we'll get soemthinglike
69 // 5 and 235, and stretching will have very little effect on the image. Instead,
70 // we first actually populate a histogram array, then determine the 1% and
71 // 99% boundaries but walking from the end points of hte histogram array and
72 // adding up the elements. Once 1% of the pixels have been accounted for we set
73 // the boundary endpoint to the current index, thus giving us 100,175 in this case.
74 //
75 // Scaling the pixels is only marginally more complicated when in
76 // the color enhancement algorithm. A second pass over the
77 // image takes place during which a pixels color is fetched, converted to HSL,
78 // the luminance is stretched according to the following equation, the color is
79 // converted back to RGB, and the value is written back to the image.
80 //
81 // l' = [255*(l-lLow)] / [lHigh - lLow]
82 //
83 // where l[Low/High] refer to the 1% and 99% endpoints for the luminance histogram.
84 //
85 //----------------------------------------------
86 
87 //==============================================
88 QImage* enhanceImageContrast( QString filename, StatusWidget* status )
89 {
90  //load original image
91  QImage* editedImage = new QImage( filename );
92 
93  //convert to 32-bit depth if necessary
94  if( editedImage->depth() < 32 )
95  {
96  QImage* tmp = editedImage;
97  editedImage = new QImage( tmp->convertDepth( 32, Qt::AutoColor ) );
98  delete tmp; tmp=NULL;
99  }
100 
101  //enhance contrast
102  enhanceImageContrast( editedImage, status );
103 
104  //return pointer to edited image
105  return editedImage;
106 }
107 //==============================================
109 {
110  //setup progress bar
111  if(status)
112  {
113  QString statusMessage = qApp->translate( "enhanceImageContrast", "Enhancing Contrast:" );
114  status->showProgressBar( statusMessage, 100 );
115  qApp->processEvents();
116  }
117 
118  //update progress bar for every 1% of completion
119  const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
120  int newProgress = 0;
121 
125 
126  //construct intensity histograph
127  int grayVals[256];
128  int i=0;
129  for(i=0; i<256; i++) { grayVals[i] = 0; }
130 
131  //populate histogram by iterating over all image pixels
132  int numPixels = editedImage->width()*editedImage->height();
133  QRgb* rgb;
134  double grayValue;
135  uchar* scanLine;
136  int x, y;
137  for( y=0; y<editedImage->height(); y++)
138  {
139  //iterate over each selected pixel in scanline
140  scanLine = editedImage->scanLine(y);
141  for( x=0; x<editedImage->width(); x++)
142  {
143  rgb = ((QRgb*)scanLine+x);
144  grayValue = RGBtoL(rgb);
145  grayVals[(int)grayValue]++;
146  } //for x
147  } //for y
148 
149  //find 1% and 99% precenticles
150  //we'll stretch these values so we avoid outliers from affecting the stretch
151  int sum=0;
152  double indexLow, indexHigh;
153  indexLow = -1.0; indexHigh = -1.0;
154  for(i=0; i<256; i++)
155  {
156  sum+=grayVals[i];
157 
158  //if 1% not found yet and criteria met set index
159  if(indexLow < 0 &&
160  sum >= 0.01*numPixels)
161  {
162  indexLow = ((double)i)/255.0;
163  }
164 
165  //if 99% not found yet and criteria met set index
166  if(indexHigh < 0 &&
167  sum >= 0.99*numPixels)
168  {
169  indexHigh = ((double)i)/255.0;
170  }
171  }
172 
173  //only apply scaling if indexHigh > indexLow
174  if(indexHigh > indexLow)
175  {
176  //run through all image pixels a second time, this time scaling coordinates as necessary
177  for( y=0; y<editedImage->height(); y++)
178  {
179  //iterate over each selected pixel in scanline
180  scanLine = editedImage->scanLine(y);
181  for( x=0; x<editedImage->width(); x++)
182  {
183  //get color coordinates and convert to 0-1 scale
184  rgb = ((QRgb*)scanLine+x);
185  double r = ((double)qRed(*rgb) )/255.0;
186  double g = ((double)qGreen(*rgb) )/255.0;
187  double b = ((double)qBlue(*rgb) )/255.0;
188 
189  //convert to hsv
190  double h,s,v;
191  RGBtoHSV(r,g,b,&h,&s,&v);
192 
193  //scale and clamp v
194  v = (v-indexLow)/(indexHigh-indexLow);
195 
196  //convert adjusted color back to rgb colorspace and clamp
197  HSVtoRGB( &r,&g,&b, h,s,v);
198  int rp = (int) MIN( MAX((r*255), 0), 255 );
199  int gp = (int) MIN( MAX((g*255), 0), 255 );
200  int bp = (int) MIN( MAX((b*255), 0), 255 );
201 
202  //set adjusted color value
203  *rgb = qRgb(rp,gp,bp);
204 
205  //update status bar if significant progress has been made since last update
206  if(status)
207  {
208  newProgress++;
209  if(newProgress >= updateIncrement)
210  {
211  newProgress = 0;
212  status->incrementProgress();
213  qApp->processEvents();
214  }
215  }
216 
217  } //for x
218  } //for y
219  } //if scaling should be preforemd
220 
221  //remove status bar
222  if(status)
223  {
224  status->setStatus( "" );
225  qApp->processEvents();
226  }
227 }
228 //==============================================
int updateIncrement
double RGBtoL(QRgb *rgb)
find luminance of a rgb color triplet
Definition: imageTools.cpp:217
void HSVtoRGB(double *r, double *g, double *b, double h, double s, double v)
Convert a HSV color triplet to RGB.
Definition: imageTools.cpp:264
void incrementProgress()
Updates the progress bar by one step.
void showProgressBar(QString message, int numSteps)
Initializes the progress bar.
long b
Definition: jpegInternal.h:125
QImage * enhanceImageContrast(QString filename, StatusWidget *status)
Definition: contrast.cpp:88
void setStatus(QString message)
Update message.
StatusWidget * status
#define MIN(x, y)
Definition: contrast.cpp:21
void RGBtoHSV(double r, double g, double b, double *h, double *s, double *v)
Convert a RGB color triplet to HSV.
Definition: imageTools.cpp:231
#define MAX(x, y)
Definition: contrast.cpp:22
QImage * editedImage
int newProgress