Hydra 0.20
hydra.src/hydra/Thumb.h
00001 
00002 /*
00003  * Copyright (c) 2009 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_HYDRA_THUMB_H__
00009 #define __INCLUDED_HYDRA_THUMB_H__
00010 
00011 #include <QString>
00012 #include <QImage>
00013 
00014 namespace hydra
00015 {
00016   /**
00017    * Retains the aspect ratio on resized dimensions.
00018    *
00019    * C, R current image width, heigth
00020    * WC WR the designed (wanted) width, heigth
00021    * c, r the resulting width, height
00022    * @param growtofit if true, images smaller than desired will be scaled UP to fit
00023    * @author Aleksander Demko
00024    */ 
00025   void calcAspect(unsigned long C, unsigned long R, unsigned long WC, unsigned long WR, unsigned long &c, unsigned long &r, 
00026       bool growtofit);
00027 
00028   class Thumb;
00029 }
00030 
00031 /**
00032  * Generator and maintainer of image thumbnails (and other scaled versions
00033  * of an image).
00034  *
00035  * Thumbnails are stores in ~/.hydradb/thumbs/imghash.wantedsize.rotatecode.jpg and generated when needed.
00036  *
00037  * Where size is the thumbnail-desired size (but maybe not the final thumb size, as the
00038  * aspect ratio will be conserved and therefore the image size will often be smaller
00039  * than the thumbnail size.
00040  * The rotatecode is the rotate code that was applied to the thumbnail.
00041  *
00042  * @author Aleksander Demko
00043  */
00044 class hydra::Thumb
00045 {
00046   public:
00047     enum {
00048       Generate_Ok = 0,
00049       Generate_FileExists,
00050       Generate_LoadError,
00051     };
00052 
00053     static const int DEFAULT_VIEW_W = 800;
00054     static const int DEFAULT_VIEW_H = 600;
00055     static const int DEFAULT_THUMB_W = 266;
00056     static const int DEFAULT_THUMB_H = 200;
00057 
00058   public:
00059     /**
00060      * Constructor.
00061      * The source image file.
00062      * This doesnt load the file until (and if) Generate is called.
00063      *
00064      * @author Aleksander Demko
00065      */
00066     Thumb(const QString &filename);
00067     /**
00068      * Constructor.
00069      * Uses the existing in-memory QImage.
00070      *
00071      * @param note that no additional rotating will be done on this image
00072      *
00073      * @author Aleksander Demko
00074      */ 
00075     Thumb(QImage &srcimg);
00076     /// dtor
00077     ~Thumb();
00078 
00079     /**
00080      * Returns the thumb dir, which is usually in a subdir
00081      * of Engine::dbDir()
00082      *
00083      * @author Aleksander Demko
00084      */ 
00085     static QString thumbDir(void);
00086 
00087     /**
00088      * Creates the thumb nail directory.
00089      *
00090      * @author Aleksander Demko
00091      */
00092     static void mkThumbDir(void);
00093 
00094     /**
00095      * Computes the standard thumbnail filename (suitable to be given to generate())
00096      * for an image with the given hash and desired dimensions.
00097      *
00098      * @author Aleksander Demko
00099      */ 
00100     static QString fileName(const QString &hash, int rotateCode, unsigned long desiredW, unsigned long desiredH);
00101 
00102     /**
00103      * Makes the thumbnail and saves it to the given filename.
00104      *
00105      * If destimage is non-null, then the given image object will be loaded with
00106      * the genered thumbnail. If no thumbnail needed to be generated, then the existing one
00107      * will be read into the destimage.
00108      *
00109      * A Generate_* error code is returned.
00110      *
00111      * Returns a Generate_* code.
00112      *
00113      * @param rotateCode is how to rotate the image. it must be >=0
00114      * @author Aleksander Demko
00115      */
00116     int generate(const QString &destfilename, QImage *destimage,
00117         int rotateCode,
00118         unsigned long desiredW, unsigned long desiredH,
00119         unsigned long *actualW = 0, unsigned long *actualH = 0);
00120 
00121   private:
00122     QString dm_filename;
00123 
00124     QImage dm_img;
00125 
00126   private:
00127     int verifyLoadImage(void);
00128 };
00129 
00130 #endif
00131 
 All Classes Namespaces Functions Variables