00001 //============================================== 00002 // copyright : (C) 2003-2005 by Will Stokes 00003 //============================================== 00004 // This program is free software; you can redistribute it 00005 // and/or modify it under the terms of the GNU General 00006 // Public License as published by the Free Software 00007 // Foundation; either version 2 of the License, or 00008 // (at your option) any later version. 00009 //============================================== 00010 00011 //Systemwide includes 00012 #include <qimage.h> 00013 #include <qstring.h> 00014 #include <qapplication.h> 00015 00016 //Projectwide includes 00017 #include "color.h" 00018 #include "../../gui/statusWidget.h" 00019 00020 //---------------------------------------------- 00021 // Inputs: 00022 // ------- 00023 // QString filename - location of original image on disk 00024 // StatusWidget* status - widget for making progress visible to user 00025 // 00026 // Outputs: 00027 // -------- 00028 // QImage* returned - constructed image 00029 // 00030 // Description: 00031 // ------------ 00032 // This method simply stretchs the color values to use up the entire dynamic range, in our 00033 // case red/green/blue values ranging from [0,255]. 00034 // 00035 // The algorithm can be broken up into two steps: 00036 // 1.) finding the endpoints of the curretn color values (e.g. [6,120]) 00037 // 2.) stretching the current image colors to fit the full [0,255] range 00038 // 00039 // One could simply iterate over the entire image and find the minimum and 00040 // maximum red, gree, and blue color values. However; often there is residual 00041 // noise at the endpoints since camera sensors always provide some noise, 00042 // while the majority of pixels fall somewhere in the middle. If we constructed 00043 // a histogram for a particular color channel it might look like this: 00044 // 5 | . 00045 // . | . . 00046 // . | . . 00047 // 1 | . . . . . 00048 // 0 |. .... ........ ............. ..... 00049 // |-X------------*--------------*-----------X----- 00050 // 0 100 175 255 00051 // 00052 // 00053 // Here color values are plotted across the x axis, while the y axis indicates 00054 // how offten a particular color value was seen. Note we're plotting one of the 00055 // three color channels here (red, green, blue). 00056 // 00057 // In this example we notice most pixels that were observed fall in the 100-175 00058 // range (that is the red/green/blue color value was most often observed with a 00059 // value between 100-175). A few pixels with values of 5 or 10 were seen, and a 00060 // few with around 230 were seen. We want to stretch the common case, 100-175, 00061 // out to the full 0-255 range. That will make the pixels that were 175 now 255, 00062 // those that wer 100 now 0, thus darks will become darker, brights will become 00063 // brigther. We'll lineraly scale all pixels inbetween. Any pixels beyond the 00064 // 100-175 range will get clamped at 0 and 255 respectively. 00065 // 00066 // Now, if we simply find the smallest and largest values we'll get soemthinglike 00067 // 5 and 235, and stretching will have very little effect on the image. Instead, 00068 // we first actually populate a histogram array for each color channel (an int 00069 // array of 256 elements). We then determine the 1% and 99% boundaries but walking 00070 // from the end points of hte histogram array and adding up the elements. Once 1% 00071 // of the pixels have been accounted for we set the the boundary endpoint to the index, 00072 // thus giving us 100,175 in this case. 00073 // 00074 // Actually scaling the colors is relatively trivial. A second pass over the 00075 // image takes place during which a pixels color is fetched, modified, then written 00076 // back out according to the following equations. It should be noted that as a 00077 // sanity check we clamp scaled color values to the [0,255] range. 00078 // 00079 // r' = [255*(r-rLow)] / [rHigh - rLow] 00080 // g' = [255*(g-gLow)] / [gHigh - gLow] 00081 // b' = [255*(b-bLow)] / [bHigh - bLow] 00082 // 00083 // where [r/g/b][Low/High] refer to the 1% and 99% endpoints for the red, 00084 // green, and blue color changes respectively. 00085 // 00086 //---------------------------------------------- 00087 00088 //============================================== 00089 QImage* improveColorBalance( QString filename, StatusWidget* status ) 00090 { 00091 //load original image 00092 QImage* editedImage = new QImage( filename ); 00093 00094 //convert to 32-bit depth if necessary 00095 if( editedImage->depth() < 32 ) 00096 { 00097 QImage* tmp = editedImage; 00098 editedImage = new QImage( tmp->convertDepth( 32, Qt::AutoColor ) ); 00099 delete tmp; tmp=NULL; 00100 } 00101 00102 //setup progress bar 00103 QString statusMessage = qApp->translate( "improveColorBalance", "Enhancing Color Balance:" ); 00104 status->showProgressBar( statusMessage, 100 ); 00105 qApp->processEvents(); 00106 00107 //update progress bar for every 1% of completion 00108 const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() ); 00109 int newProgress = 0; 00110 00111 //construct intensity histographs for each color channel 00112 int redVals[256]; 00113 int greenVals[256]; 00114 int blueVals[256]; 00115 int i=0; 00116 for(i=0; i<256; i++) 00117 { 00118 redVals[i] = 0; 00119 greenVals[i] = 0; 00120 blueVals[i] = 0; 00121 } 00122 00123 //populate histogram by iterating over all image pixels 00124 int numPixels = editedImage->width()*editedImage->height(); 00125 QRgb* rgb; 00126 uchar* scanLine; 00127 int x, y; 00128 for( y=0; y<editedImage->height(); y++) 00129 { 00130 //iterate over each selected pixel in scanline 00131 scanLine = editedImage->scanLine(y); 00132 for( x=0; x<editedImage->width(); x++) 00133 { 00134 rgb = ((QRgb*)scanLine+x); 00135 redVals[qRed(*rgb)]++; 00136 greenVals[qGreen(*rgb)]++; 00137 blueVals[qBlue(*rgb)]++; 00138 } //for x 00139 } //for y 00140 00141 //find 1% and 99% precenticles 00142 //we'll stretch these values so we avoid outliers from affecting the stretch 00143 int sumR=0; 00144 int sumG=0; 00145 int sumB=0; 00146 int indexLowR, indexHighR; 00147 int indexLowG, indexHighG; 00148 int indexLowB, indexHighB; 00149 indexLowR = -1; indexHighR = -1; 00150 indexLowG = -1; indexHighG = -1; 00151 indexLowB = -1; indexHighB = -1; 00152 for(i=0; i<256; i++) 00153 { 00154 sumR+=redVals[i]; 00155 sumG+=greenVals[i]; 00156 sumB+=blueVals[i]; 00157 00158 //if 1% not found yet and criteria met set index 00159 if(indexLowR < 0 && sumR >= 0.01*numPixels) 00160 { indexLowR = i; } 00161 if(indexLowG < 0 && sumG >= 0.01*numPixels) 00162 { indexLowG = i; } 00163 if(indexLowB < 0 && sumB >= 0.01*numPixels) 00164 { indexLowB = i; } 00165 00166 //if 99% not found yet and criteria met set index 00167 if(indexHighR < 0 && sumR >= 0.99*numPixels) 00168 { indexHighR = i; } 00169 if(indexHighG < 0 && sumG >= 0.99*numPixels) 00170 { indexHighG = i; } 00171 if(indexHighB < 0 && sumB >= 0.99*numPixels) 00172 { indexHighB = i; } 00173 } 00174 00175 //run through all image pixels a second time, this time scaling coordinates as necessary 00176 for( y=0; y<editedImage->height(); y++) 00177 { 00178 //iterate over each selected pixel in scanline 00179 scanLine = editedImage->scanLine(y); 00180 for( x=0; x<editedImage->width(); x++) 00181 { 00182 //get color coordinates and convert to 0-1 scale 00183 rgb = ((QRgb*)scanLine+x); 00184 double r = ((double)qRed(*rgb) ); 00185 double g = ((double)qGreen(*rgb) ); 00186 double b = ((double)qBlue(*rgb) ); 00187 00188 if(indexHighR != indexLowR) { r = (255*(r-indexLowR))/(indexHighR-indexLowR); } 00189 if(indexHighG != indexLowG) { g = (255*(g-indexLowG))/(indexHighG-indexLowG); } 00190 if(indexHighB != indexLowB) { b = (255*(b-indexLowB))/(indexHighB-indexLowB); } 00191 00192 int rp = (int) QMIN( QMAX(r, 0), 255 ); 00193 int gp = (int) QMIN( QMAX(g, 0), 255 ); 00194 int bp = (int) QMIN( QMAX(b, 0), 255 ); 00195 00196 //set adjusted color value 00197 *rgb = qRgb(rp,gp,bp); 00198 00199 //update status bar if significant progress has been made since last update 00200 newProgress++; 00201 if(newProgress >= updateIncrement) 00202 { 00203 newProgress = 0; 00204 status->incrementProgress(); 00205 qApp->processEvents(); 00206 } 00207 00208 } //for x 00209 } //for y 00210 00211 //remove status bar 00212 status->setStatus( "" ); 00213 qApp->processEvents(); 00214 00215 //return pointer to edited image 00216 return editedImage; 00217 } 00218 //==============================================