contrast.cpp File Reference

#include <qimage.h>
#include <qstring.h>
#include <qapplication.h>
#include "contrast.h"
#include "../tools/imageTools.h"
#include "../../gui/statusWidget.h"

Include dependency graph for contrast.cpp:

Go to the source code of this file.

Functions

QImage * enhanceImageContrast (QString filename, StatusWidget *status)
void enhanceImageContrast (QImage *editedImage, StatusWidget *status)


Function Documentation

void enhanceImageContrast ( QImage *  editedImage,
StatusWidget status 
)

------ 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.

00106 {  
00107   //setup progress bar
00108   if(status)
00109   {
00110   QString statusMessage = qApp->translate( "enhanceImageContrast", "Enhancing Contrast:" );
00111   status->showProgressBar( statusMessage, 100 );
00112   qApp->processEvents();  
00113   }
00114   
00115   //update progress bar for every 1% of completion
00116   const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
00117   int newProgress = 0; 
00118 
00122   
00123   //construct intensity histograph
00124   int grayVals[256];
00125   int i=0;
00126   for(i=0; i<256; i++) { grayVals[i] = 0; }  
00127   
00128   //populate histogram by iterating over all image pixels
00129   int numPixels = editedImage->width()*editedImage->height();  
00130   QRgb* rgb;
00131   double grayValue; 
00132   uchar* scanLine;
00133   int x, y;
00134   for( y=0; y<editedImage->height(); y++)
00135   {   
00136     //iterate over each selected pixel in scanline
00137     scanLine = editedImage->scanLine(y);
00138     for( x=0; x<editedImage->width(); x++)
00139     {
00140       rgb = ((QRgb*)scanLine+x);
00141       grayValue = RGBtoL(rgb);
00142       grayVals[(int)grayValue]++;              
00143     } //for x
00144   } //for y
00145   
00146   //find 1% and 99% precenticles
00147   //we'll stretch these values so we avoid outliers from affecting the stretch
00148   int sum=0;
00149   double indexLow, indexHigh;
00150   indexLow = -1.0; indexHigh = -1.0;
00151   for(i=0; i<256; i++)
00152   {
00153     sum+=grayVals[i];
00154     
00155     //if 1% not found yet and criteria met set index
00156     if(indexLow < 0 &&
00157        sum >= 0.01*numPixels)
00158     {
00159       indexLow = ((double)i)/255.0;
00160     }
00161     
00162     //if 99% not found yet and criteria met set index
00163     if(indexHigh < 0 &&
00164        sum >= 0.99*numPixels)
00165     {
00166       indexHigh = ((double)i)/255.0;
00167     }    
00168   }
00169   
00170   //only apply scaling if indexHigh > indexLow
00171   if(indexHigh > indexLow)
00172   {   
00173     //run through all image pixels a second time, this time scaling coordinates as necessary
00174     for( y=0; y<editedImage->height(); y++)
00175     {   
00176       //iterate over each selected pixel in scanline
00177       scanLine = editedImage->scanLine(y);
00178       for( x=0; x<editedImage->width(); x++)
00179       {
00180         //get color coordinates and convert to 0-1 scale
00181         rgb = ((QRgb*)scanLine+x);
00182         double r = ((double)qRed(*rgb)   )/255.0;
00183         double g = ((double)qGreen(*rgb) )/255.0;
00184         double b = ((double)qBlue(*rgb)  )/255.0;
00185         
00186         //convert to hsv
00187         double h,s,v;
00188         RGBtoHSV(r,g,b,&h,&s,&v);
00189         
00190         //scale and clamp v
00191         v = (v-indexLow)/(indexHigh-indexLow);
00192         
00193         //convert adjusted color back to rgb colorspace and clamp
00194         HSVtoRGB( &r,&g,&b, h,s,v);         
00195         int rp = (int) QMIN( QMAX((r*255), 0), 255 );
00196         int gp = (int) QMIN( QMAX((g*255), 0), 255 );
00197         int bp = (int) QMIN( QMAX((b*255), 0), 255 );
00198         
00199         //set adjusted color value
00200         *rgb = qRgb(rp,gp,bp);
00201 
00202         //update status bar if significant progress has been made since last update
00203         if(status)
00204         {
00205           newProgress++;
00206           if(newProgress >= updateIncrement)
00207           {
00208             newProgress = 0;
00209             status->incrementProgress();
00210             qApp->processEvents();  
00211           }
00212         }
00213       
00214       } //for x
00215     } //for y
00216   } //if scaling should be preforemd
00217   
00218   //remove status bar
00219   if(status)
00220   {
00221     status->setStatus( "" );
00222     qApp->processEvents();
00223   }
00224 }

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().

00086 {
00087   //load original image
00088   QImage* editedImage = new QImage( filename );
00089 
00090   //convert to 32-bit depth if necessary
00091   if( editedImage->depth() < 32 )
00092   {
00093     QImage* tmp = editedImage;
00094     editedImage = new QImage( tmp->convertDepth( 32, Qt::AutoColor ) );
00095     delete tmp; tmp=NULL;
00096   }
00097 
00098   //enhance contrast
00099   enhanceImageContrast( editedImage, status );
00100 
00101   //return pointer to edited image
00102   return editedImage;    
00103 }


Generated on Thu Jan 3 10:54:41 2008 for AlbumShaper by  doxygen 1.5.4