Main Page   Compound List   File List   Compound 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 <iostream.h>
00019 #include <fstream.h>
00020 #include <stdio.h>
00021 #include <qstring.h>
00022 #include <qimage.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 oldFile,
00064                     QString newFile)
00065 {
00066   ifstream inputstream(oldFile.ascii(), ios::binary|ios::in);
00067   ofstream outputstream(newFile.ascii(), ios::binary|ios::out);
00068 
00069   char buffer[500];
00070   while(inputstream.is_open() && inputstream)
00071   {
00072     inputstream.read(buffer, 500);
00073     outputstream.write(buffer, 500);
00074   }
00075 
00076   inputstream.close();
00077   outputstream.close();
00078 }
00079 //==============================================
00080 void createImages(QImage* fullImage,
00081                           QImage** slideshowImage,
00082                           QImage** thumbnailImage,
00083                           QImage** paddedThumbnailImage,
00084                           int &slideshowWidth,
00085                           int &slideshowHeight)
00086 {
00087   //---------------------------------------------------------    
00088   if((*paddedThumbnailImage) != (*thumbnailImage))
00089     delete (*paddedThumbnailImage);
00090   delete (*thumbnailImage);
00091   delete (*slideshowImage);
00092   //---------------------------------------------------------    
00093   int thumbnailW = 0;
00094   int thumbnailH = 0;
00095   //---------------------------------------------------------    
00096   resizeImage( fullImage->width(), fullImage->height(),
00097                THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
00098                thumbnailW, thumbnailH);
00099   resizeImage( fullImage->width(), fullImage->height(),
00100                SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT,
00101                slideshowWidth, slideshowHeight);
00102   //---------------------------------------------------------    
00103   //scale slide show image
00104   QImage temp = fullImage->smoothScale( slideshowWidth, slideshowHeight );
00105   temp.setAlphaBuffer(true);
00106   //create full size slide show image
00107   (*slideshowImage) = new QImage(SLIDESHOW_WIDTH, SLIDESHOW_HEIGHT, fullImage->depth());
00108   (*slideshowImage)->setAlphaBuffer(true);
00109   //copy scaled image into centered portion of real slideshow image
00110   int xDiff = SLIDESHOW_WIDTH - slideshowWidth;
00111   int yDiff = SLIDESHOW_HEIGHT - slideshowHeight;
00112 
00113   //set all pixels to white
00114   int x,y;
00115   for(x=0; x< SLIDESHOW_WIDTH; x++)
00116   {
00117     for(y=0; y<SLIDESHOW_HEIGHT; y++)
00118     {
00119       (*slideshowImage)->setPixel(x, y, QColor(255, 255, 255).rgb()); 
00120     }
00121   }
00122   
00123   int x2 = 0;
00124   int y2;
00125   for(x = xDiff/2; x< (xDiff/2) + slideshowWidth; x++)
00126   {
00127     y2 = 0;
00128     for(y = yDiff/2; y < (yDiff/2) + slideshowHeight; y++)
00129     {
00130        (*slideshowImage)->setPixel(x, y, temp.pixel(x2, y2));
00131        y2++;
00132     }
00133     x2++;  
00134   }
00135   //---------------------------------------------------------    
00136   //scale down thumbnail image
00137   (*thumbnailImage) = new QImage(fullImage->smoothScale( thumbnailW, thumbnailH ));
00138   (*thumbnailImage)->setAlphaBuffer(true); 
00139   //---------------------------------------------------------    
00140   //if scaled thumbnail image is same size as padded thumbnail should be reuse object
00141   if(thumbnailW == THUMBNAIL_WIDTH &&
00142      thumbnailH == THUMBNAIL_HEIGHT)
00143   {
00144      (*paddedThumbnailImage) = (*thumbnailImage);
00145   }
00146   //else pad thumbnail
00147   else
00148   {
00149     //created padded thumbnail image
00150     (*paddedThumbnailImage) = new QImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, fullImage->depth());
00151     (*paddedThumbnailImage)->setAlphaBuffer(true);
00152   
00153     //copy scaled image into centered portion of padded image
00154     xDiff = THUMBNAIL_WIDTH - thumbnailW;
00155     yDiff = THUMBNAIL_HEIGHT - thumbnailH;
00156 
00157     //set all pixels to white
00158     for(x=0; x< THUMBNAIL_WIDTH; x++)
00159     {
00160       for(y=0; y<THUMBNAIL_HEIGHT; y++)
00161       {
00162         (*paddedThumbnailImage)->setPixel(x, y, QColor(255, 255, 255).rgb()); 
00163       }
00164     }
00165   
00166     x2 = 0;
00167     for(x = xDiff/2; x< (xDiff/2) + thumbnailW; x++)
00168     {
00169       y2 = 0;
00170       for(y = yDiff/2; y < (yDiff/2) + thumbnailH; y++)
00171       {
00172         (*paddedThumbnailImage)->setPixel(x, y, (*thumbnailImage)->pixel(x2, y2));
00173         y2++;
00174       }
00175       x2++;  
00176     }
00177   }  
00178   //---------------------------------------------------------    
00179 }
00180 //==============================================
00181 

Generated on Tue Jun 10 23:41:20 2003 for AlbumShaper by doxygen 1.3.1