AlbumShaper 1.0a3
contrast.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

QImage * enhanceImageContrast (QString filename, StatusWidget *status)
 
void enhanceImageContrast (QImage *image, StatusWidget *status=NULL)
 

Function Documentation

◆ enhanceImageContrast() [1/2]

void enhanceImageContrast ( QImage * image,
StatusWidget * status = NULL )

Contrast stretching - http://www.ph.tn.tudelft.nl/Courses/FIP/frames/fip-istogram.html

Definition at line 108 of file contrast.cpp.

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++;
210 {
211 newProgress = 0;
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}
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 contrast.cpp:21
#define MAX(x, y)
Definition contrast.cpp:22
void RGBtoHSV(double r, double g, double b, double *h, double *s, double *v)
Convert a RGB color triplet to HSV.
double RGBtoL(QRgb *rgb)
find luminance of a rgb color triplet
void HSVtoRGB(double *r, double *g, double *b, double h, double s, double v)
Convert a HSV color triplet to RGB.
long b
int updateIncrement
QImage * editedImage
StatusWidget * status
int newProgress

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

◆ enhanceImageContrast() [2/2]

QImage * enhanceImageContrast ( QString filename,
StatusWidget * status )

Definition at line 88 of file contrast.cpp.

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
103
104 //return pointer to edited image
105 return editedImage;
106}
QImage * enhanceImageContrast(QString filename, StatusWidget *status)
Definition contrast.cpp:88

References editedImage, enhanceImageContrast(), and status.

Referenced by EdgeDetect::constructEdgeImage(), EditingInterface::enhanceContrast(), and enhanceImageContrast().