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

imageTools.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it 
00005 //  and/or modify it under the terms of the GNU General 
00006 //  Public License as published by the Free Software 
00007 //  Foundation; either version 2 of the License, or  
00008 //  (at your option) any later version.         
00009 //
00010 //  As a special exception, Will Stokes gives permission to 
00011 //  link this program with Qt non-commercial edition, and 
00012 //  distribute the resulting executable, without including the 
00013 //  source code for the Qt non-commercial edition in the 
00014 //  source distribution. 
00015 //==============================================
00016 
00017 //Systemwide includes
00018 #include <fstream>
00019 #include <qstring.h>
00020 #include <qimage.h>
00021 #include <qfile.h>
00022 #include <qdatastream.h>
00023 
00024 //Projectwide includes
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   //if original dimensions are within max new size then use
00038   //original dimensions
00039   if(originalWidth <= maxWidth &&
00040      originalHeight <= maxHeight)
00041   {
00042     newWidth = originalWidth;
00043     newHeight = originalHeight;
00044     return;
00045   }
00046   
00047   //else find dimension which is way over bounds
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   //if copying file from old location to new just skip, this
00067   //occurs when resaving albums with new themes but to same location
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   //scale slide show image
00122   QImage temp = fullImage->smoothScale( slideshowWidth, slideshowHeight );
00123   temp.setAlphaBuffer(true);
00124   //create full size slide show image
00125     
00126   (*slideshowImage) = new QImage(SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT, 32);
00127   (*slideshowImage)->setAlphaBuffer(true);
00128   //copy scaled image into centered portion of real slideshow image
00129   int xDiff = SLIDESHOW_WIDTH - slideshowWidth;
00130   int yDiff = SLIDESHOW_HEIGHT - slideshowHeight;
00131   
00132   //set all pixels to white
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   //scale down thumbnail image
00156   (*thumbnailImage) = new QImage(fullImage->smoothScale( thumbnailW, thumbnailH ));
00157   (*thumbnailImage)->setAlphaBuffer(true); 
00158   //---------------------------------------------------------    
00159   //if scaled thumbnail image is same size as padded thumbnail should be reuse object
00160   if(thumbnailW == THUMBNAIL_WIDTH &&
00161      thumbnailH == THUMBNAIL_HEIGHT)
00162   {
00163      (*paddedThumbnailImage) = (*thumbnailImage);
00164   }
00165   //else pad thumbnail
00166   else
00167   {
00168     //created padded thumbnail image
00169     (*paddedThumbnailImage) = new QImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, 32);
00170     (*paddedThumbnailImage)->setAlphaBuffer(true);
00171   
00172     //copy scaled image into centered portion of padded image
00173     xDiff = THUMBNAIL_WIDTH - thumbnailW;
00174     yDiff = THUMBNAIL_HEIGHT - thumbnailH;
00175 
00176     //set all pixels to white
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 

Generated on Thu Nov 13 00:10:53 2003 for AlbumShaper by doxygen 1.3.4