Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

color.h File Reference

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Functions

QImage * improveColorBalance (QString filename, StatusWidget *status)


Function Documentation

QImage* improveColorBalance QString  filename,
StatusWidget status
 

Definition at line 89 of file color.cpp.

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

Referenced by EditingInterface::colorBalance().

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 }


Generated on Mon Apr 11 18:27:52 2005 for AlbumShaper by  doxygen 1.3.9.1