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

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:

Include dependency graph

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 34 of file contrast.cpp.

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

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

00035 {  
00036   //setup progress bar
00037   if(status)
00038   {
00039   QString statusMessage = qApp->translate( "enhanceImageContrast", "Enhancing Contrast:" );
00040   status->showProgressBar( statusMessage, 100 );
00041   qApp->processEvents();  
00042   }
00043   
00044   //update progress bar for every 1% of completion
00045   const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
00046   int newProgress = 0; 
00047 
00051   
00052   //construct intensity histograph
00053   int grayVals[256];
00054   int i=0;
00055   for(i=0; i<256; i++) { grayVals[i] = 0; }  
00056   
00057   //populate histogram by iterating over all image pixels
00058   int numPixels = editedImage->width()*editedImage->height();  
00059   QRgb* rgb;
00060   double grayValue; 
00061   uchar* scanLine;
00062   int x, y;
00063   for( y=0; y<editedImage->height(); y++)
00064   {   
00065     //iterate over each selected pixel in scanline
00066     scanLine = editedImage->scanLine(y);
00067     for( x=0; x<editedImage->width(); x++)
00068     {
00069       rgb = ((QRgb*)scanLine+x);
00070       grayValue = RGBtoL(rgb);
00071       grayVals[(int)grayValue]++;              
00072     } //for x
00073   } //for y
00074   
00075   //find 1% and 99% precenticles
00076   //we'll stretch these values so we avoid outliers from affecting the stretch
00077   int sum=0;
00078   double indexLow, indexHigh;
00079   indexLow = -1.0; indexHigh = -1.0;
00080   for(i=0; i<256; i++)
00081   {
00082     sum+=grayVals[i];
00083     
00084     //if 1% not found yet and criteria met set index
00085     if(indexLow < 0 &&
00086        sum >= 0.01*numPixels)
00087     {
00088       indexLow = ((double)i)/255.0;
00089     }
00090     
00091     //if 99% not found yet and criteria met set index
00092     if(indexHigh < 0 &&
00093        sum >= 0.99*numPixels)
00094     {
00095       indexHigh = ((double)i)/255.0;
00096     }    
00097   }
00098   
00099   //only apply scaling if indexHigh > indexLow
00100   if(indexHigh > indexLow)
00101   {   
00102     //run through all image pixels a second time, this time scaling coordinates as necessary
00103     for( y=0; y<editedImage->height(); y++)
00104     {   
00105       //iterate over each selected pixel in scanline
00106       scanLine = editedImage->scanLine(y);
00107       for( x=0; x<editedImage->width(); x++)
00108       {
00109         //get color coordinates and convert to 0-1 scale
00110         rgb = ((QRgb*)scanLine+x);
00111         double r = ((double)qRed(*rgb)   )/255.0;
00112         double g = ((double)qGreen(*rgb) )/255.0;
00113         double b = ((double)qBlue(*rgb)  )/255.0;
00114         
00115         //convert to hsv
00116         double h,s,v;
00117         RGBtoHSV(r,g,b,&h,&s,&v);
00118         
00119         //scale and clamp v
00120         v = (v-indexLow)/(indexHigh-indexLow);
00121         
00122         //convert adjusted color back to rgb colorspace and clamp
00123         HSVtoRGB( &r,&g,&b, h,s,v);         
00124         int rp = (int) QMIN( QMAX((r*255), 0), 255 );
00125         int gp = (int) QMIN( QMAX((g*255), 0), 255 );
00126         int bp = (int) QMIN( QMAX((b*255), 0), 255 );
00127         
00128         //set adjusted color value
00129         *rgb = qRgb(rp,gp,bp);
00130 
00131         //update status bar if significant progress has been made since last update
00132         if(status)
00133         {
00134           newProgress++;
00135           if(newProgress >= updateIncrement)
00136           {
00137             newProgress = 0;
00138             status->incrementProgress();
00139             qApp->processEvents();  
00140           }
00141         }
00142       
00143       } //for x
00144     } //for y
00145   } //if scaling should be preforemd
00146   
00147   //remove status bar
00148   if(status)
00149   {
00150     status->setStatus( "" );
00151     qApp->processEvents();
00152   }
00153 }

QImage* enhanceImageContrast QString  filename,
StatusWidget status
 

Definition at line 22 of file contrast.cpp.

00023 {
00024   //load original image
00025   QImage* editedImage = new QImage( filename );
00026 
00027   //enhance contrast
00028   enhanceImageContrast( editedImage, status );
00029 
00030   //return pointer to edited image
00031   return editedImage;    
00032 }


Generated on Sat Apr 2 05:44:13 2005 for AlbumShaper by  doxygen 1.3.9.1