AlbumShaper  1.0a3
Functions
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

QImage* enhanceImageContrast ( QString  filename,
StatusWidget status 
)

Definition at line 85 of file contrast.cpp.

References editedImage, and enhanceImageContrast().

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

{
  //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;
  }

  //enhance contrast
  enhanceImageContrast( editedImage, status );

  //return pointer to edited image
  return editedImage;    
}
void enhanceImageContrast ( QImage *  image,
StatusWidget status = NULL 
)

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

Definition at line 105 of file contrast.cpp.

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

{  
  //setup progress bar
  if(status)
  {
  QString statusMessage = qApp->translate( "enhanceImageContrast", "Enhancing Contrast:" );
  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 histograph
  int grayVals[256];
  int i=0;
  for(i=0; i<256; i++) { grayVals[i] = 0; }  
  
  //populate histogram by iterating over all image pixels
  int numPixels = editedImage->width()*editedImage->height();  
  QRgb* rgb;
  double grayValue; 
  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);
      grayValue = RGBtoL(rgb);
      grayVals[(int)grayValue]++;              
    } //for x
  } //for y
  
  //find 1% and 99% precenticles
  //we'll stretch these values so we avoid outliers from affecting the stretch
  int sum=0;
  double indexLow, indexHigh;
  indexLow = -1.0; indexHigh = -1.0;
  for(i=0; i<256; i++)
  {
    sum+=grayVals[i];
    
    //if 1% not found yet and criteria met set index
    if(indexLow < 0 &&
       sum >= 0.01*numPixels)
    {
      indexLow = ((double)i)/255.0;
    }
    
    //if 99% not found yet and criteria met set index
    if(indexHigh < 0 &&
       sum >= 0.99*numPixels)
    {
      indexHigh = ((double)i)/255.0;
    }    
  }
  
  //only apply scaling if indexHigh > indexLow
  if(indexHigh > indexLow)
  {   
    //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)   )/255.0;
        double g = ((double)qGreen(*rgb) )/255.0;
        double b = ((double)qBlue(*rgb)  )/255.0;
        
        //convert to hsv
        double h,s,v;
        RGBtoHSV(r,g,b,&h,&s,&v);
        
        //scale and clamp v
        v = (v-indexLow)/(indexHigh-indexLow);
        
        //convert adjusted color back to rgb colorspace and clamp
        HSVtoRGB( &r,&g,&b, h,s,v);         
        int rp = (int) QMIN( QMAX((r*255), 0), 255 );
        int gp = (int) QMIN( QMAX((g*255), 0), 255 );
        int bp = (int) QMIN( QMAX((b*255), 0), 255 );
        
        //set adjusted color value
        *rgb = qRgb(rp,gp,bp);

        //update status bar if significant progress has been made since last update
        if(status)
        {
          newProgress++;
          if(newProgress >= updateIncrement)
          {
            newProgress = 0;
            status->incrementProgress();
            qApp->processEvents();  
          }
        }
      
      } //for x
    } //for y
  } //if scaling should be preforemd
  
  //remove status bar
  if(status)
  {
    status->setStatus( "" );
    qApp->processEvents();
  }
}