00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <fstream>
00019 #include <qstring.h>
00020 #include <qimage.h>
00021 #include <qfile.h>
00022 #include <qdatastream.h>
00023
00024
00025 #include "imageTools.h"
00026 #include "../config.h"
00027
00028
00030 void resizeImage(int originalWidth,
00031 int originalHeight,
00032 int maxWidth,
00033 int maxHeight,
00034 int& newWidth,
00035 int& newHeight)
00036 {
00037
00038
00039 if(originalWidth <= maxWidth &&
00040 originalHeight <= maxHeight)
00041 {
00042 newWidth = originalWidth;
00043 newHeight = originalHeight;
00044 return;
00045 }
00046
00047
00048 float ratWidth = ((float)maxWidth) / ((float)originalWidth);
00049 float ratHeight = ((float)maxHeight) / ((float)originalHeight);
00050
00051 if(ratWidth < ratHeight)
00052 {
00053 newWidth = maxWidth;
00054 newHeight = (int)((((float)maxWidth) / ((float)originalWidth)) * ((float)originalHeight));
00055 }
00056 else
00057 {
00058 newHeight = maxHeight;
00059 newWidth = (int)((((float)maxHeight) / ((float)originalHeight)) * ((float)originalWidth));
00060 }
00061 }
00062
00063 void copyFile(QString oldFilePath,
00064 QString newFilePath)
00065 {
00066
00067
00068 if(oldFilePath == newFilePath)
00069 return;
00070
00071 const int BUFFER_SIZE = 1024;
00072 Q_INT8 buffer[BUFFER_SIZE];
00073
00074 QFile oldFile(oldFilePath);
00075 QFile newFile(newFilePath);
00076 oldFile.open( IO_ReadOnly );
00077 newFile.open( IO_WriteOnly );
00078
00079 QDataStream srcStream(&oldFile);
00080 QDataStream destStream(&newFile);
00081
00082 while(!srcStream.atEnd())
00083 {
00084 int i = 0;
00085 while(!srcStream.atEnd() && i < BUFFER_SIZE)
00086 {
00087 srcStream >> buffer[i];
00088 i++;
00089 }
00090 int k = 0;
00091 for(k = 0; k < i; k++)
00092 {
00093 destStream << buffer[k];
00094 }
00095 }
00096 }
00097
00098 void createImages(QImage* fullImage,
00099 QImage** slideshowImage,
00100 QImage** thumbnailImage,
00101 QImage** paddedThumbnailImage,
00102 int &slideshowWidth,
00103 int &slideshowHeight)
00104 {
00105
00106 if((*paddedThumbnailImage) != (*thumbnailImage))
00107 delete (*paddedThumbnailImage);
00108 delete (*thumbnailImage);
00109 delete (*slideshowImage);
00110
00111 int thumbnailW = 0;
00112 int thumbnailH = 0;
00113
00114 resizeImage( fullImage->width(), fullImage->height(),
00115 THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
00116 thumbnailW, thumbnailH);
00117 resizeImage( fullImage->width(), fullImage->height(),
00118 SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT,
00119 slideshowWidth, slideshowHeight);
00120
00121
00122 QImage temp = fullImage->smoothScale( slideshowWidth, slideshowHeight );
00123 temp.setAlphaBuffer(true);
00124
00125
00126 (*slideshowImage) = new QImage(SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT, 32);
00127 (*slideshowImage)->setAlphaBuffer(true);
00128
00129 int xDiff = SLIDESHOW_WIDTH - slideshowWidth;
00130 int yDiff = SLIDESHOW_HEIGHT - slideshowHeight;
00131
00132
00133 int x,y;
00134 for(x=0; x< SLIDESHOW_WIDTH; x++)
00135 {
00136 for(y=0; y<SLIDESHOW_HEIGHT; y++)
00137 {
00138 (*slideshowImage)->setPixel(x, y, QColor(255, 255, 255).rgb());
00139 }
00140 }
00141
00142 int x2 = 0;
00143 int y2;
00144 for(x = xDiff/2; x< (xDiff/2) + slideshowWidth; x++)
00145 {
00146 y2 = 0;
00147 for(y = yDiff/2; y < (yDiff/2) + slideshowHeight; y++)
00148 {
00149 (*slideshowImage)->setPixel(x, y, temp.pixel(x2, y2));
00150 y2++;
00151 }
00152 x2++;
00153 }
00154
00155
00156 (*thumbnailImage) = new QImage(fullImage->smoothScale( thumbnailW, thumbnailH ));
00157 (*thumbnailImage)->setAlphaBuffer(true);
00158
00159
00160 if(thumbnailW == THUMBNAIL_WIDTH &&
00161 thumbnailH == THUMBNAIL_HEIGHT)
00162 {
00163 (*paddedThumbnailImage) = (*thumbnailImage);
00164 }
00165
00166 else
00167 {
00168
00169 (*paddedThumbnailImage) = new QImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, 32);
00170 (*paddedThumbnailImage)->setAlphaBuffer(true);
00171
00172
00173 xDiff = THUMBNAIL_WIDTH - thumbnailW;
00174 yDiff = THUMBNAIL_HEIGHT - thumbnailH;
00175
00176
00177 for(x=0; x< THUMBNAIL_WIDTH; x++)
00178 {
00179 for(y=0; y<THUMBNAIL_HEIGHT; y++)
00180 {
00181 (*paddedThumbnailImage)->setPixel(x, y, QColor(255, 255, 255).rgb());
00182 }
00183 }
00184
00185 x2 = 0;
00186 for(x = xDiff/2; x< (xDiff/2) + thumbnailW; x++)
00187 {
00188 y2 = 0;
00189 for(y = yDiff/2; y < (yDiff/2) + thumbnailH; y++)
00190 {
00191 (*paddedThumbnailImage)->setPixel(x, y, (*thumbnailImage)->pixel(x2, y2));
00192 y2++;
00193 }
00194 x2++;
00195 }
00196 }
00197
00198 }
00199
00200