00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qimage.h>
00013 #include <qstring.h>
00014 #include <qapplication.h>
00015
00016
00017 #include "contrast.h"
00018 #include "../tools/imageTools.h"
00019 #include "../../gui/statusWidget.h"
00020
00021
00022 QImage* enhanceImageContrast( QString filename, StatusWidget* status )
00023 {
00024
00025 QImage* editedImage = new QImage( filename );
00026
00027
00028 enhanceImageContrast( editedImage, status );
00029
00030
00031 return editedImage;
00032 }
00033
00034 void enhanceImageContrast( QImage* editedImage, StatusWidget* status )
00035 {
00036
00037 if(status)
00038 {
00039 QString statusMessage = qApp->translate( "enhanceImageContrast", "Enhancing Contrast:" );
00040 status->showProgressBar( statusMessage, 100 );
00041 qApp->processEvents();
00042 }
00043
00044
00045 const int updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
00046 int newProgress = 0;
00047
00051
00052
00053 int grayVals[256];
00054 int i=0;
00055 for(i=0; i<256; i++) { grayVals[i] = 0; }
00056
00057
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
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 }
00073 }
00074
00075
00076
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
00085 if(indexLow < 0 &&
00086 sum >= 0.01*numPixels)
00087 {
00088 indexLow = ((double)i)/255.0;
00089 }
00090
00091
00092 if(indexHigh < 0 &&
00093 sum >= 0.99*numPixels)
00094 {
00095 indexHigh = ((double)i)/255.0;
00096 }
00097 }
00098
00099
00100 if(indexHigh > indexLow)
00101 {
00102
00103 for( y=0; y<editedImage->height(); y++)
00104 {
00105
00106 scanLine = editedImage->scanLine(y);
00107 for( x=0; x<editedImage->width(); x++)
00108 {
00109
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
00116 double h,s,v;
00117 RGBtoHSV(r,g,b,&h,&s,&v);
00118
00119
00120 v = (v-indexLow)/(indexHigh-indexLow);
00121
00122
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
00129 *rgb = qRgb(rp,gp,bp);
00130
00131
00132 if(status)
00133 {
00134 newProgress++;
00135 if(newProgress >= updateIncrement)
00136 {
00137 newProgress = 0;
00138 status->incrementProgress();
00139 qApp->processEvents();
00140 }
00141 }
00142
00143 }
00144 }
00145 }
00146
00147
00148 if(status)
00149 {
00150 status->setStatus( "" );
00151 qApp->processEvents();
00152 }
00153 }
00154