Main Page   Compound List   File List   Compound Members   File Members  

photo.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 <qpixmap.h>
00019 #include <qimage.h>
00020 #include <qstring.h>
00021 #include <qtextstream.h>
00022 #include <qdom.h>
00023 #include <qdir.h>
00024 
00025 //Projectwide includes
00026 #include "photo.h"
00027 #include "subalbum.h"
00028 #include "imageTools.h"
00029 #include "xmlTools.h"
00030 #include "../config.h"
00031 
00032 #include <iostream.h>
00033 
00034 //==============================================
00036 Photo::Photo(Subalbum* subalbum, int number)
00037 {
00038   //set subalbum pointer
00039   this->subalbum = subalbum;
00040   
00041   //set initial photo number and subalbum number
00042   initialNumber = number;
00043   initialSubalbumNumber = subalbum->getSubalbumNumber();
00044   
00045   //set strings to default values
00046   description ="";
00047 
00048   //set next pointer to NULL
00049   next = NULL;
00050   
00051   //set thumbnail image
00052   thumbnailImage = NULL;
00053   paddedThumbnailImage = NULL; 
00054   slideshowImage = NULL;
00055   fullImage = NULL;
00056   
00057   //set checksums to the empty string by default
00058   imageChecksum = "";
00059   slideshowChecksum = "";
00060   thumbnailChecksum = "";
00061   
00062   //image does not need to be saved by default
00063   needsSaving = false;
00064   
00065   //filenames not set by default
00066   imageLocation = "";
00067   slideshowLocation = "";
00068   
00069   //no dimension of slideshow image known yet
00070   slideshowWidth = -1;
00071   slideshowHeight = -1;
00072 }
00073 //==============================================
00075 Photo::~Photo()
00076 {
00077   if(paddedThumbnailImage != thumbnailImage)
00078     delete paddedThumbnailImage;
00079   delete thumbnailImage;
00080   delete slideshowImage;
00081   delete fullImage;
00082 } 
00083 //==============================================
00084 bool Photo::setImage(QString filename)
00085 {
00086   //reset image object
00087   delete fullImage;
00088   fullImage = new QImage(filename);
00089   if(fullImage->isNull()) return false;
00090 
00091   //create slideshow and thumbnail formats
00092   createImages(fullImage,
00093                      &slideshowImage,
00094                      &thumbnailImage,
00095                      &paddedThumbnailImage,
00096                      slideshowWidth,
00097                      slideshowHeight); 
00098   
00099   //store image and slideshow filenames
00100   imageLocation = filename;  
00101 
00102   //image is being stored in temp location, needs saving!
00103   needsSaving = true;
00104   return true;
00105 }
00106 //==============================================
00107 bool Photo::setImage(QString imageName,
00108                               QString slideshowName,
00109                               QString thumbnailName)
00110 {
00111   //---------------------------------------------------------    
00112    //free old images
00113   delete fullImage;
00114   if(paddedThumbnailImage != thumbnailImage)
00115     delete paddedThumbnailImage;
00116   delete thumbnailImage;
00117   delete slideshowImage;
00118   
00119   fullImage = NULL;
00120   paddedThumbnailImage = NULL;
00121   thumbnailImage = NULL;
00122   slideshowImage = NULL;
00123   //---------------------------------------------------------    
00124   //construct thumbnail image
00125   thumbnailImage = new QImage(thumbnailName);
00126   if(thumbnailImage->isNull()) return false;
00127   //---------------------------------------------------------    
00128   //construct padded thumbnail image
00129   //if scaled thumbnail image is same size as padded thumbnail should be reuse object
00130   if(thumbnailImage->width() == THUMBNAIL_WIDTH &&
00131      thumbnailImage->height() == THUMBNAIL_HEIGHT)
00132   {  
00133     paddedThumbnailImage = thumbnailImage;  
00134   }
00135   //else pad thumbnail
00136   else
00137   {
00138     //created padded thumbnail image
00139     paddedThumbnailImage = new QImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, thumbnailImage->depth());
00140     paddedThumbnailImage->setAlphaBuffer(true);
00141   
00142     //copy scaled image into centered portion of padded image
00143     int xDiff = THUMBNAIL_WIDTH - thumbnailImage->width();
00144     int yDiff = THUMBNAIL_HEIGHT - thumbnailImage->height();
00145 
00146     //set all pixels to white
00147     int x,y;
00148     for(x=0; x< THUMBNAIL_WIDTH; x++)
00149     {
00150       for(y=0; y<THUMBNAIL_HEIGHT; y++)
00151       {
00152         paddedThumbnailImage->setPixel(x, y, QColor(255, 255, 255).rgb()); 
00153       }
00154     }
00155   
00156     int x2 = 0;
00157     int y2;
00158     for(x = xDiff/2; x< (xDiff/2) + thumbnailImage->width(); x++)
00159     {
00160       y2 = 0;
00161       for(y = yDiff/2; y < (yDiff/2) + thumbnailImage->height(); y++)
00162       {
00163         paddedThumbnailImage->setPixel(x, y, thumbnailImage->pixel(x2, y2));
00164         y2++;
00165       }
00166       x2++;  
00167     }
00168   }  
00169   //---------------------------------------------------------    
00170   //save new image filenames
00171   imageLocation = imageName;  
00172   slideshowLocation = slideshowName;
00173   //---------------------------------------------------------    
00174   //image loaded from file, no need to save it again just yet
00175   needsSaving = false;  
00176   return true;
00177   //---------------------------------------------------------    
00178 }
00179 //==============================================
00180 bool Photo::setImage(QImage* newFullImage)
00181 {
00182   //reset image object
00183   delete fullImage;
00184   fullImage = newFullImage;
00185   if(fullImage->isNull()) return false;
00186 
00187   //create slideshow and thumbnail formats
00188   createImages(fullImage,
00189                      &slideshowImage,
00190                      &thumbnailImage,
00191                      &paddedThumbnailImage,
00192                      slideshowWidth,
00193                      slideshowHeight); 
00194 
00195   //save out updated full and slideshow images to temp folder and deallocate memory
00196   imageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2.jpg")
00197                                                             .arg(initialSubalbumNumber)
00198                                                             .arg(initialNumber);
00199   slideshowLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2_slideshow.jpg")
00200                                                             .arg(initialSubalbumNumber)
00201                                                             .arg(initialNumber);
00202          
00203   fullImage->save( imageLocation, "JPEG", 100);
00204   slideshowImage->save( slideshowLocation, "JPEG", 100);
00205  
00206   //mark image as having been modified and hence in the tmp folder  
00207   needsSaving = true;  
00208 
00209   //deallocate qimage objects
00210   deallocateLargeImages();
00211   
00212   return true;
00213 }
00214 //==============================================
00215 void Photo::setDescription(QString val)
00216 {
00217   description = val;
00218 }
00219 //==============================================
00221 QString Photo::getDescription()
00222 {
00223   return QString(description);
00224 }
00225 //==============================================
00227 Photo* Photo::getNext()
00228 {
00229   return next;
00230 }
00231 //==============================================
00233 void Photo::setNext(Photo* val)
00234 {
00235   next = val;
00236 }
00237 //==============================================
00238 QImage* Photo::getImage(int size)
00239 {
00240   if(size == THUMBNAIL)
00241     return thumbnailImage;
00242   else if(size == PADDED_THUMBNAIL)
00243     return paddedThumbnailImage;
00244   else if(size == SLIDESHOW)
00245   {
00246     if( slideshowImage != NULL)
00247       return slideshowImage;
00248     else
00249     {
00250       if(!needsSaving)
00251       {
00252         return new QImage(slideshowLocation);
00253       }
00254       else
00255       {
00256         QString tmpImageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2_slideshow.jpg")
00257                                                                 .arg(initialSubalbumNumber)
00258                                                                 .arg(initialNumber);
00259          return new QImage(tmpImageLocation);
00260       }
00261     }
00262   }
00263   else if(size == IMAGE)
00264   {
00265     if( fullImage != NULL)
00266       return fullImage;
00267     else 
00268     {
00269       if(!needsSaving)
00270       {
00271         return new QImage(imageLocation);
00272       }
00273       else
00274       {
00275         QString tmpImageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2.jpg")
00276                                                                 .arg(initialSubalbumNumber)
00277                                                                 .arg(initialNumber);
00278          return new QImage(tmpImageLocation);
00279       }
00280     }
00281   }
00282   else
00283     return NULL;
00284 }
00285 //==============================================
00286 void Photo::exportToXML(QTextStream& stream)
00287 {
00288   //write photo information
00289   stream << "    <photo>\n";
00290   stream << "      <description>" << fixXMLString(description) << "</description>\n";
00291   stream << "      <imageMD5>" << fixXMLString(imageChecksum) << "</imageMD5>\n";
00292   stream << "      <slideMD5>" << fixXMLString(slideshowChecksum) << "</slideMD5>\n";
00293   stream << "      <thumbMD5>" << fixXMLString(thumbnailChecksum) << "</thumbMD5>\n";
00294   stream << "    </photo>\n";
00295 }
00296 //==============================================
00297 void Photo::exportThumbnailHTML(QTextStream& stream)
00298 {
00299   //write photo information
00300   stream << "?";
00301 }
00302 //==============================================
00303 void Photo::exportSlideshowHTML(QTextStream& stream)
00304 {
00305   //write photo information
00306   stream << "?";
00307 }
00308 //==============================================
00309 void Photo::importFromDisk(QDomNode* root)
00310 {
00311   QDomNode node = root->firstChild();
00312   QDomText val;  
00313   while( !node.isNull() )
00314   {
00315     //------------------------------------------------------------
00316     //photo description
00317     if( node.isElement() && node.nodeName() == "description" )
00318     { 
00319       val = node.firstChild().toText();
00320       if(!val.isNull())
00321         description = val.nodeValue();
00322     }
00323     //------------------------------------------------------------
00324     //image md5
00325     if( node.isElement() && node.nodeName() == "imageMD5" )
00326     { 
00327       val = node.firstChild().toText();
00328       if(!val.isNull())
00329         imageChecksum = val.nodeValue();
00330     }
00331     //------------------------------------------------------------
00332     //slideshow md5
00333     if( node.isElement() && node.nodeName() == "slideMD5" )
00334     { 
00335       val = node.firstChild().toText();
00336       if(!val.isNull())
00337         slideshowChecksum = val.nodeValue();
00338     }
00339     //------------------------------------------------------------
00340     //thumbnail md5
00341     if( node.isElement() && node.nodeName() == "thumbMD5" )
00342     { 
00343       val = node.firstChild().toText();
00344       if(!val.isNull())
00345         thumbnailChecksum = val.nodeValue();
00346     }
00347     //------------------------------------------------------------
00348     //advance to next node   
00349     node = node.nextSibling();
00350     //------------------------------------------------------------
00351   }
00352 }
00353 //==============================================
00354 void Photo::rotate90()
00355 {
00356   //load up image
00357   //if not modified load from permanent location
00358   fullImage = new QImage(imageLocation);
00359   
00360   //create new image
00361   QImage* rotatedImage = new QImage(fullImage->height(),
00362                                     fullImage->width(),
00363                                     fullImage->depth());
00364   rotatedImage->setAlphaBuffer(true);
00365   //copy data                                    
00366   int x,y;
00367   for(x=0; x < fullImage->height(); x++)
00368   {
00369     for(y=0; y < fullImage->width(); y++)
00370     {
00371       rotatedImage->setPixel(fullImage->height() - 1 - x, y, fullImage->pixel(y, x) );
00372     }
00373   }                                    
00374 
00375   //delete old image and set new pointer
00376   delete fullImage;
00377   fullImage = rotatedImage;
00378   
00379   //create thumbnail
00380   createImages(fullImage,
00381                      &slideshowImage,
00382                      &thumbnailImage,
00383                      &paddedThumbnailImage,
00384                      slideshowWidth,
00385                      slideshowHeight); 
00386   
00387   //save out updated full and slideshow images to temp folder and deallocate memory
00388   imageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2.jpg")
00389                                                             .arg(initialSubalbumNumber)
00390                                                             .arg(initialNumber);
00391   slideshowLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2_slideshow.jpg")
00392                                                             .arg(initialSubalbumNumber)
00393                                                             .arg(initialNumber);
00394          
00395   fullImage->save( imageLocation, "JPEG", 100);
00396   slideshowImage->save( slideshowLocation, "JPEG", 100);
00397  
00398   //mark image as having been modified and hence in the tmp folder  
00399   needsSaving = true;  
00400 
00401   //deallocate qimage objects
00402   deallocateLargeImages();
00403   
00404   //update slideshow width/height
00405   int temp = slideshowWidth;
00406   slideshowWidth = slideshowHeight;
00407   slideshowHeight = temp;
00408 }
00409 //==============================================
00410 void Photo::rotate270()
00411 {
00412   //load up image
00413   //if not modified load from permanent location
00414   fullImage = new QImage(imageLocation);
00415   
00416   //create new image
00417   QImage* rotatedImage = new QImage(fullImage->height(),
00418                                     fullImage->width(),
00419                                     fullImage->depth());
00420   rotatedImage->setAlphaBuffer(true);
00421   //copy data                                    
00422   int x,y;
00423   for(x=0; x < fullImage->height(); x++)
00424   {
00425     for(y=0; y < fullImage->width(); y++)
00426     {
00427       rotatedImage->setPixel(x, fullImage->width() - 1 - y, fullImage->pixel(y, x) );
00428     }
00429   }                                    
00430 
00431   //delete old image and set new pointer
00432   delete fullImage;
00433   fullImage = rotatedImage;
00434   
00435   //create thumbnail
00436   createImages(fullImage,
00437                      &slideshowImage,
00438                      &thumbnailImage,
00439                      &paddedThumbnailImage,
00440                      slideshowWidth,
00441                      slideshowHeight); 
00442   
00443   //save out updated full and slideshow images to temp folder and deallocate memory
00444   imageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2.jpg")
00445                                                             .arg(initialSubalbumNumber)
00446                                                             .arg(initialNumber);
00447   slideshowLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2_slideshow.jpg")
00448                                                             .arg(initialSubalbumNumber)
00449                                                             .arg(initialNumber);
00450          
00451   fullImage->save( imageLocation, "JPEG", 100);
00452   slideshowImage->save( slideshowLocation, "JPEG", 100);
00453  
00454 
00455  //mark image as having been modified and hence in the tmp folder  
00456  needsSaving = true;  
00457 
00458  //deallocate qimage objects
00459  deallocateLargeImages();
00460   
00461   //update slideshow width/height
00462   int temp = slideshowWidth;
00463   slideshowWidth = slideshowHeight;
00464   slideshowHeight = temp;
00465 }
00466 //==============================================
00467 void Photo::flipHorizontally()
00468 {
00469   //load up image
00470   //if not modified load from permanent location
00471   fullImage = new QImage(imageLocation);
00472   
00473   //create new image
00474   QImage* rotatedImage = new QImage(fullImage->width(),
00475                                     fullImage->height(),
00476                                     fullImage->depth());
00477   rotatedImage->setAlphaBuffer(true);
00478   //copy data                                   
00479   int x,y; 
00480   for(x=0; x < fullImage->width(); x++)
00481   {
00482     for(y=0; y < fullImage->height(); y++)
00483     {
00484       rotatedImage->setPixel(x, fullImage->height() - 1 - y, fullImage->pixel(x, y) );
00485     }
00486   }                                    
00487 
00488   //delete old image and set new pointer
00489   delete fullImage;
00490   fullImage = rotatedImage;
00491   
00492   //create thumbnail
00493   createImages(fullImage,
00494                      &slideshowImage,
00495                      &thumbnailImage,
00496                      &paddedThumbnailImage,
00497                      slideshowWidth,
00498                      slideshowHeight); 
00499   
00500   //save out updated full and slideshow images to temp folder and deallocate memory
00501   imageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2.jpg")
00502                                                             .arg(initialSubalbumNumber)
00503                                                             .arg(initialNumber);
00504   slideshowLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2_slideshow.jpg")
00505                                                             .arg(initialSubalbumNumber)
00506                                                             .arg(initialNumber);
00507          
00508   fullImage->save( imageLocation, "JPEG", 100);
00509   slideshowImage->save( slideshowLocation, "JPEG", 100); 
00510 
00511  //mark image as having been modified and hence in the tmp folder  
00512  needsSaving = true;  
00513 
00514  //deallocate qimage objects
00515  deallocateLargeImages();
00516 }
00517 //==============================================
00518 void Photo::flipVertically()
00519 {
00520   //load up image
00521   //if not modified load from permanent location
00522   fullImage = new QImage(imageLocation);
00523   
00524   //create new image
00525   QImage* rotatedImage = new QImage(fullImage->width(),
00526                                     fullImage->height(),
00527                                     fullImage->depth());
00528   rotatedImage->setAlphaBuffer(true);
00529   //copy data                                    
00530   int x,y;
00531   for(x=0; x < fullImage->width(); x++)
00532   {
00533     for(y=0; y < fullImage->height(); y++)
00534     {
00535       rotatedImage->setPixel(fullImage->width() - 1 - x, y, fullImage->pixel(x, y) );
00536     }
00537   }                                    
00538 
00539   //delete old image and set new pointer
00540   delete fullImage;
00541   fullImage = rotatedImage;
00542   
00543   //create thumbnail
00544   createImages(fullImage,
00545                      &slideshowImage,
00546                      &thumbnailImage,
00547                      &paddedThumbnailImage,
00548                      slideshowWidth,
00549                      slideshowHeight); 
00550   
00551   //save out updated full and slideshow images to temp folder and deallocate memory
00552   imageLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2.jpg")
00553                                                             .arg(initialSubalbumNumber)
00554                                                             .arg(initialNumber);
00555   slideshowLocation = QDir::homeDirPath() + QString("/.albumShaper/tmp/%1_%2_slideshow.jpg")
00556                                                             .arg(initialSubalbumNumber)
00557                                                             .arg(initialNumber);
00558          
00559   fullImage->save( imageLocation, "JPEG", 100);
00560   slideshowImage->save( slideshowLocation, "JPEG", 100);
00561  
00562 
00563  //mark image as having been modified and hence in the tmp folder  
00564  needsSaving = true;  
00565 
00566  //deallocate qimage objects
00567  deallocateLargeImages();
00568 }
00569 //==============================================
00570 void Photo::setThumbnailChecksum(QString val)
00571 {
00572   thumbnailChecksum = val;
00573 }  
00574 //==============================================
00575 void Photo::setSlideshowChecksum(QString val)
00576 {
00577   slideshowChecksum = val;
00578 }  
00579 //==============================================
00580 void Photo::setImageChecksum(QString val)
00581 {
00582   imageChecksum = val;
00583 }  
00584 //==============================================
00585 QString Photo::getThumbnailChecksum()
00586 {
00587   return thumbnailChecksum;
00588 }  
00589 //==============================================  
00590 QString Photo::getSlideshowChecksum()
00591 {
00592   return slideshowChecksum;
00593 }  
00594 //==============================================  
00595 QString Photo::getImageChecksum()
00596 {
00597   return imageChecksum;
00598 }  
00599 //==============================================
00600 void Photo::deallocateLargeImages()
00601 {
00602   delete fullImage;
00603   fullImage = NULL;
00604   
00605   delete slideshowImage;
00606   slideshowImage = NULL;
00607 }
00608 //==============================================
00609 void Photo::setImageFilename(QString val)
00610 {
00611   imageLocation = val;
00612 }
00613 //==============================================
00614 void Photo::setSlideshowFilename(QString val)
00615 {
00616   slideshowLocation = val;
00617 }
00618 //==============================================
00619 QString Photo::getImageFilename()
00620 {
00621   return imageLocation;
00622 }
00623 //==============================================
00624 QString Photo::getSlideshowFilename()
00625 {
00626   return slideshowLocation;
00627 }
00628 //==============================================
00629 void Photo::setNeedsSavingVal(bool val)
00630 {
00631   needsSaving = val;
00632 }
00633 //==============================================
00634 bool Photo::getNeedsSavingVal()
00635 {
00636   return needsSaving;
00637 }
00638 //==============================================
00639 int Photo::getInitialPhotoNumber()
00640 {
00641   return initialNumber;
00642 }
00643 //==============================================
00644 int Photo::getInitialSubalbumNumber()
00645 {
00646   return initialSubalbumNumber;
00647 }
00648 //==============================================
00649 void Photo::setInitialPhotoNumber(int val)
00650 {
00651  initialNumber = val; 
00652 }
00653 //==============================================
00654 void Photo::setInitialSubalbumNumber(int val)
00655 {
00656  initialSubalbumNumber = val; 
00657 }
00658 //==============================================
00659 int Photo::actualSlideshowWidth()
00660 {
00661   return slideshowWidth;;
00662 }
00663 //==============================================
00664 int Photo::actualSlideshowHeight()
00665 {
00666   return slideshowHeight;
00667 }
00668 //==============================================
00669 void Photo::setActualSlideshowDimensions(int w, int h)
00670 {
00671   slideshowWidth = w;
00672   slideshowHeight = h;
00673 }
00674 //==============================================

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