00001 #include "thumbnailtool.h"
00002
00003
00004 #include <opie2/odebug.h>
00005 using namespace Opie::Core;
00006
00007
00008 #include <qfileinfo.h>
00009 #include <qdir.h>
00010 #include <qimage.h>
00011 #include <qpixmap.h>
00012 #include <qstring.h>
00013
00014 static bool makeThumbDir( const QFileInfo& inf, bool make = false) {
00015 QDir dir( inf.dirPath()+ "/.opie-eye" );
00016 if ( !dir.exists() )
00017 if ( make )
00018 return dir.mkdir(QString::null);
00019 else
00020 return false;
00021 return true;
00022 }
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 QPixmap ThumbNailTool::getThumb( const QString& path, int width, int height ) {
00033 QFileInfo inf( path );
00034 if ( !makeThumbDir( inf ) ) {
00035 QPixmap pix;
00036 return pix;
00037 }
00038 QString str = QString( "/.opie-eye/%1x%2-%3" ).arg( width ).arg( height ).arg( inf.fileName() );
00039 return QPixmap( inf.dirPath()+str,"PNG" );
00040
00041 }
00042
00043 void ThumbNailTool::putThumb( const QString& path, const QPixmap& pix, int width, int height ) {
00044 QFileInfo inf( path );
00045 makeThumbDir( inf, true );
00046 QString str = QString( "/.opie-eye/%1x%2-%3" ).arg( width ).arg( height ).arg( inf.fileName() );
00047 pix.save( inf.dirPath()+str, "PNG" );
00048 }
00049
00050
00051 QPixmap ThumbNailTool::scaleImage( QImage& img, int w, int h ) {
00052 double hs = (double)h / (double)img.height() ;
00053 double ws = (double)w / (double)img.width() ;
00054 double scaleFactor = (hs > ws) ? ws : hs;
00055 int smoothW = (int)(scaleFactor * img.width());
00056 int smoothH = (int)(scaleFactor * img.height());
00057 QPixmap pixmap;
00058 if ( img.width() <= w && img.height() <= h )
00059 pixmap.convertFromImage( img );
00060 else
00061 pixmap.convertFromImage( img.smoothScale( smoothW, smoothH) );
00062 return pixmap;
00063 }