00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <fstream>
00013 #include <cstdlib>
00014 #include <iostream>
00015 #include <cmath>
00016
00017 #include <qstring.h>
00018 #include <qimage.h>
00019 #include <qdir.h>
00020 #include <qfile.h>
00021 #include <qcolor.h>
00022 #include <fstream>
00023 #include <qpoint.h>
00024 #include <qpainter.h>
00025
00026
00027 #include "imageTools.h"
00028 #include "jpeg/jpegTools.h"
00029 #include "jpeg/jpegSize.h"
00030 #include "../../config.h"
00031
00032
00033 bool isJpeg(const char* filename)
00034 {
00035 int w,h;
00036 return getJPEGSize( QFile::encodeName(filename), w, h );
00037 }
00038
00039 void calcScaledImageDimensions(int origWidth, int origHeight,
00040 int idealWidth, int idealHeight,
00041 int& width, int& height)
00042 {
00043
00044
00045 if(origWidth <= idealWidth &&
00046 origHeight <= idealHeight)
00047 {
00048 width = origWidth;
00049 height = origHeight;
00050 return;
00051 }
00052
00053
00054 float widthRatio = ((float)idealWidth) / ((float)origWidth);
00055 float heightRatio = ((float)idealHeight) / ((float)origHeight);
00056
00057 if(widthRatio < heightRatio)
00058 {
00059 width = idealWidth;
00060 height = (int)((((float)idealWidth) / ((float)origWidth)) * ((float)origHeight));
00061 }
00062 else
00063 {
00064 height = idealHeight;
00065 width = (int)((((float)idealHeight) / ((float)origHeight)) * ((float)origWidth));
00066 }
00067 }
00068
00069 void constructImages(QString imageName,
00070 QImage& slideshowImage, QImage& thumbnailImage)
00071 {
00072
00073
00074 int origWidth, origHeight;
00075 getImageSize( imageName, origWidth, origHeight );
00076
00077
00078 int thumbWidth, thumbHeight;
00079 calcScaledImageDimensions( origWidth, origHeight,
00080 THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
00081 thumbWidth, thumbHeight);
00082
00083 int slideWidth, slideHeight;
00084 calcScaledImageDimensions( origWidth, origHeight,
00085 SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT,
00086 slideWidth, slideHeight);
00087
00088
00089
00090
00091 QImage temp;
00092 scaleImage( imageName, temp, slideWidth, slideHeight );
00093 slideWidth = temp.width();
00094 slideHeight = temp.height();
00095
00096
00097 slideshowImage.create( SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT, 32 );
00098 slideshowImage.fill( Qt::white.rgb() );
00099
00100
00101 int xDiff = SLIDESHOW_WIDTH - slideWidth;
00102 int yDiff = SLIDESHOW_HEIGHT - slideHeight;
00103 bitBlt( &slideshowImage, xDiff/2, yDiff/2,
00104 &temp, 0, 0, slideWidth, slideHeight );
00105
00106
00107
00108 scaleImage( imageName, thumbnailImage, thumbWidth, thumbHeight );
00109
00110 }
00111
00112 bool transformImage( QString fileIn, QString fileOut, TRANSFORM_CODE transformation )
00113 {
00114
00115
00116
00117 if( isJpeg(fileIn) )
00118 return transformJPEG( fileIn, fileOut, transformation );
00119
00120
00121 QImage origImage(fileIn);
00122 QImage transformedImage;
00123
00124
00125 if(transformation == ROTATE_90)
00126 {
00127 if(!transformedImage.create( origImage.height(), origImage.width(), origImage.depth() ) )
00128 return false;
00129
00130 int x,y;
00131 for(x=0; x < origImage.height(); x++)
00132 {
00133 for(y=0; y < origImage.width(); y++)
00134 transformedImage.setPixel(origImage.height() - 1 - x, y, origImage.pixel(y, x) );
00135 }
00136 }
00137 else if(transformation == ROTATE_270)
00138 {
00139 if(!transformedImage.create( origImage.height(), origImage.width(), origImage.depth() ) )
00140 return false;
00141
00142 int x,y;
00143 for(x=0; x < origImage.height(); x++)
00144 {
00145 for(y=0; y < origImage.width(); y++)
00146 transformedImage.setPixel(x, origImage.width() - 1 - y, origImage.pixel(y, x) );
00147 }
00148 }
00149 else if(transformation == FLIP_H)
00150 { transformedImage = origImage.mirror(false,true); }
00151 else
00152 { transformedImage = origImage.mirror(true,false); }
00153
00154
00155 transformedImage.save( fileOut, "JPEG", 95 );
00156 return true;
00157 }
00158
00159 bool scaleImage( QString fileIn, QString fileOut,
00160 int newWidth, int newHeight)
00161 {
00162
00163 QImage scaledImage;
00164 if( scaleImage(fileIn, scaledImage, newWidth, newHeight ) )
00165 {
00166 scaledImage.save( fileOut, "JPEG", 95 );
00167 return true;
00168 }
00169 else
00170 return false;
00171 }
00172
00173 bool scaleImage(QString fileIn, QImage& scaledImage, int targetWidth, int targetHeight)
00174 {
00175
00176 QString extension = QFileInfo(fileIn).extension(false).lower();
00177 if( extension.compare("jpeg") == 0 ||
00178 extension.compare("jpg") == 0 )
00179 return scaleJPEG( QFile::encodeName(fileIn), scaledImage, targetWidth, targetHeight );
00180
00181
00182
00183 QImage orig(fileIn);
00184 if(QMIN( ((float)targetWidth)/orig.width(), ((float)targetHeight)/orig.height() ) > 2)
00185 {
00186 targetWidth = 2*orig.width();
00187 targetHeight = 2*orig.height();
00188 }
00189
00190 scaledImage = orig.smoothScale( targetWidth, targetHeight, QImage::ScaleMin );
00191 return true;
00192 }
00193
00194 bool getImageSize( const char* filename,QSize& size )
00195 {
00196 int w,h;
00197 bool result = getImageSize( filename, w, h );
00198 size.setWidth( w );
00199 size.setHeight( h );
00200 return result;
00201 }
00202
00203 bool getImageSize( const char* filename, int& width, int& height )
00204 {
00205
00206 QString extension = QFileInfo(filename).extension(false).lower();
00207 if( extension.compare("jpeg") == 0 ||
00208 extension.compare("jpg") == 0 )
00209 return getJPEGSize( QFile::encodeName(filename),
00210 width, height );
00211
00212
00213 QImage image(filename);
00214 width = image.width();
00215 height = image.height();
00216 return true;
00217 }
00218
00219 double RGBtoL(QRgb* rgb)
00220 {
00221 double r = ((double)qRed(*rgb) )/255.0;
00222 double g = ((double)qGreen(*rgb) )/255.0;
00223 double b = ((double)qBlue(*rgb) )/255.0;
00224
00225 double h,s,v;
00226 RGBtoHSV(r,g,b,&h,&s,&v);
00227 return 255.0*v;
00228 }
00229
00230
00231
00232
00233 void RGBtoHSV( double r, double g, double b,
00234 double *h, double *s, double *v )
00235 {
00236 double min, max, delta;
00237
00238 min = QMIN(QMIN( r, g), b );
00239 max = QMAX(QMAX( r, g), b );
00240 *v = max;
00241
00242 delta = max - min;
00243
00244 if( max != 0 )
00245 *s = delta / max;
00246 else {
00247
00248 *s = 0;
00249 *h = -1;
00250 return;
00251 }
00252
00253 if( r == max )
00254 *h = ( g - b ) / delta;
00255 else if( g == max )
00256 *h = 2 + ( b - r ) / delta;
00257 else
00258 *h = 4 + ( r - g ) / delta;
00259
00260 *h *= 60;
00261 if( *h < 0 )
00262 *h += 360;
00263
00264 }
00265
00266 void HSVtoRGB( double *r, double *g, double *b,
00267 double h, double s, double v )
00268 {
00269 int i;
00270 double f, p, q, t;
00271
00272 if( s == 0 ) {
00273
00274 *r = *g = *b = v;
00275 return;
00276 }
00277
00278 h /= 60;
00279 i = (int)floor( h );
00280 f = h - i;
00281 p = v * ( 1 - s );
00282 q = v * ( 1 - s * f );
00283 t = v * ( 1 - s * ( 1 - f ) );
00284
00285 switch( i ) {
00286 case 0:
00287 *r = v;
00288 *g = t;
00289 *b = p;
00290 break;
00291 case 1:
00292 *r = q;
00293 *g = v;
00294 *b = p;
00295 break;
00296 case 2:
00297 *r = p;
00298 *g = v;
00299 *b = t;
00300 break;
00301 case 3:
00302 *r = p;
00303 *g = q;
00304 *b = v;
00305 break;
00306 case 4:
00307 *r = t;
00308 *g = p;
00309 *b = v;
00310 break;
00311 default:
00312 *r = v;
00313 *g = p;
00314 *b = q;
00315 break;
00316 }
00317 }
00318