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

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(), and updateIncrement.

Referenced by EditingInterface::colorBalance().

{
  //load original image
  QImage* editedImage = new QImage( filename );
  
  //convert to 32-bit depth if necessary
  if( editedImage->depth() < 32 )
  {
    QImage* tmp = editedImage;
    editedImage = new QImage( tmp->convertDepth( 32, Qt::AutoColor ) );
    delete tmp; tmp=NULL;
  }

  //setup progress bar
  QString statusMessage = qApp->translate( "improveColorBalance", "Enhancing Color Balance:" );
  status->showProgressBar( statusMessage, 100 );
  qApp->processEvents();  
  
  //update progress bar for every 1% of completion
  const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
  int newProgress = 0; 

  //construct intensity histographs for each color channel
  int redVals[256];
  int greenVals[256];
  int blueVals[256];
  int i=0;
  for(i=0; i<256; i++) 
  {
    redVals[i] = 0;
    greenVals[i] = 0;
    blueVals[i] = 0;
  }  
  
  //populate histogram by iterating over all image pixels
  int numPixels = editedImage->width()*editedImage->height();  
  QRgb* rgb;
  uchar* scanLine;
  int x, y;
  for( y=0; y<editedImage->height(); y++)
  {   
    //iterate over each selected pixel in scanline
    scanLine = editedImage->scanLine(y);
    for( x=0; x<editedImage->width(); x++)
    {
      rgb = ((QRgb*)scanLine+x);
      redVals[qRed(*rgb)]++;
      greenVals[qGreen(*rgb)]++;
      blueVals[qBlue(*rgb)]++;
    } //for x
  } //for y
  
  //find 1% and 99% precenticles
  //we'll stretch these values so we avoid outliers from affecting the stretch
  int sumR=0;
  int sumG=0;
  int sumB=0;
  int indexLowR, indexHighR;
  int indexLowG, indexHighG;
  int indexLowB, indexHighB;
  indexLowR = -1; indexHighR = -1;
  indexLowG = -1; indexHighG = -1;
  indexLowB = -1; indexHighB = -1;
  for(i=0; i<256; i++)
  {
    sumR+=redVals[i];
    sumG+=greenVals[i];
    sumB+=blueVals[i];
    
    //if 1% not found yet and criteria met set index
    if(indexLowR < 0 && sumR >= 0.01*numPixels)
    { indexLowR = i; }
    if(indexLowG < 0 && sumG >= 0.01*numPixels)
    { indexLowG = i; }
    if(indexLowB < 0 && sumB >= 0.01*numPixels)
    { indexLowB = i; }
   
    //if 99% not found yet and criteria met set index
    if(indexHighR < 0 && sumR >= 0.99*numPixels)
    { indexHighR = i; }
    if(indexHighG < 0 && sumG >= 0.99*numPixels)
    { indexHighG = i; }
    if(indexHighB < 0 && sumB >= 0.99*numPixels)
    { indexHighB = i; }
  }
  
  //run through all image pixels a second time, this time scaling coordinates as necessary
  for( y=0; y<editedImage->height(); y++)
  {   
    //iterate over each selected pixel in scanline
    scanLine = editedImage->scanLine(y);
    for( x=0; x<editedImage->width(); x++)
    {
      //get color coordinates and convert to 0-1 scale
      rgb = ((QRgb*)scanLine+x);
      double r = ((double)qRed(*rgb)   );
      double g = ((double)qGreen(*rgb) );
      double b = ((double)qBlue(*rgb)  );

      if(indexHighR != indexLowR) { r = (255*(r-indexLowR))/(indexHighR-indexLowR); }
      if(indexHighG != indexLowG) { g = (255*(g-indexLowG))/(indexHighG-indexLowG); }
      if(indexHighB != indexLowB) { b = (255*(b-indexLowB))/(indexHighB-indexLowB); }
        
      int rp = (int) QMIN( QMAX(r, 0), 255 );
      int gp = (int) QMIN( QMAX(g, 0), 255 );
      int bp = (int) QMIN( QMAX(b, 0), 255 );
      
      //set adjusted color value
      *rgb = qRgb(rp,gp,bp);
      
      //update status bar if significant progress has been made since last update
      newProgress++;
      if(newProgress >= updateIncrement)
      {
        newProgress = 0;
        status->incrementProgress();
        qApp->processEvents();  
      }
      
    } //for x
  } //for y
  
  //remove status bar
  status->setStatus( "" );
  qApp->processEvents();

  //return pointer to edited image
  return editedImage;    
}