summaryrefslogtreecommitdiff
path: root/noncore/graphics/opie-eye/slave/thumbnailtool.cpp
blob: 62bb1e6b2d41cec62b363e258d5c72693e039c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "thumbnailtool.h"

/* OPIE */
#include <opie2/odebug.h>
using namespace Opie::Core;

/* QT */
#include <qfileinfo.h>
#include <qdir.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qstring.h>

static bool makeThumbDir( const QFileInfo& inf,  bool make = false) {
    QDir dir( inf.dirPath()+ "/.opie-eye" );
    if ( !dir.exists() )
        if ( make )
            return dir.mkdir(QString::null);
        else
            return false;
    return true;
}


/*
 * check if the Opie opie-eye dir exists
 * check if a thumbnail exists
 * load the thumbnail
 * /foo/bar/imagefoo.gif
 * check for a png in /foo/bar/.opie-eye/%dx%d-imagefoo.gif
 */
QPixmap ThumbNailTool::getThumb( const QString& path, int width, int height ) {
    QFileInfo inf( path );
    if ( !makeThumbDir( inf ) ) {
        QPixmap pix;
        return pix;
    }
    QString str = QString( "/.opie-eye/%1x%2-%3" ).arg( width ).arg( height ).arg( inf.fileName() );
    return QPixmap( inf.dirPath()+str,"PNG" );

}

void ThumbNailTool::putThumb( const QString& path, const QPixmap& pix, int width, int height ) {
    QFileInfo inf( path );
    makeThumbDir( inf, true );
    QString str = QString( "/.opie-eye/%1x%2-%3" ).arg( width ).arg( height ).arg( inf.fileName() );
    pix.save( inf.dirPath()+str, "PNG" );
}


QPixmap ThumbNailTool::scaleImage( QImage& img, int w, int h ) {
    double hs = (double)h / (double)img.height() ;
    double ws = (double)w / (double)img.width() ;
    double scaleFactor = (hs > ws) ? ws : hs;
    int smoothW = (int)(scaleFactor * img.width());
    int smoothH = (int)(scaleFactor * img.height());
    QPixmap pixmap;
    if ( img.width() <= w && img.height() <= h )
        pixmap.convertFromImage( img );
    else
        pixmap.convertFromImage( img.smoothScale( smoothW, smoothH) );
    return pixmap;
}