summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_utils.cpp
blob: 2cfadaff8976736efac6bb08dd63bd98c7f5f17c (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***************************************************************************
                          sq_utils.cpp  -  description
                             -------------------
    begin                : Thu Aug 2 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqstring.h>

#include <ksquirrel-libs/fmt_defs.h>

#include "sq_libraryhandler.h"
#include "sq_utils.h"

#ifndef KSQUIRREL_PART
#include <kurl.h>
#include "sq_imageloader.h"
#include "sq_thumbnailinfo.h"
#include "sq_thumbnailsize.h"
#endif

#ifdef SQ_HAVE_KEXIF
#include <libkexif/kexifdata.h>
#endif

void SQ_Utils::exifRotate(const TQString &file, TQImage &im, int o)
{
#ifdef SQ_HAVE_KEXIF
    im = im.xForm(SQ_Utils::exifGetMatrix(file, o));
#endif
}

TQWMatrix SQ_Utils::exifGetMatrix(const TQString &file, int o)
{
    TQWMatrix matrix;

#ifdef SQ_HAVE_KEXIF
    int O;

    if(o == -1)
    {
        KExifData data;
        data.readFromFile(file);
        O = data.getImageQt::Orientation();
    }
    else
        O = o;

    switch(O)
    {
        case KExifData::HFLIP:        matrix.scale(-1,1); break;
        case KExifData::ROT_180:      matrix.rotate(180); break;
        case KExifData::VFLIP:        matrix.scale(1,-1); break;
        case KExifData::ROT_90_HFLIP: matrix.scale(-1,1); matrix.rotate(90); break;
        case KExifData::ROT_90:       matrix.rotate(90);  break;
        case KExifData::ROT_90_VFLIP: matrix.scale(1,-1); matrix.rotate(90); break;
        case KExifData::ROT_270:      matrix.rotate(270); break;

        // normal rotation or unspecified
        default: ;
    }

#endif

    return matrix;
}

TQImage SQ_Utils::scaleImage(unsigned char *im, int w, int h, int fitwithin)
{
    if(w <= fitwithin && h <= fitwithin)
    {
        TQImage scaled(im, w, h, 32, 0, 0, TQImage::LittleEndian);
        scaled.setAlphaBuffer(true);
        return scaled.copy();
    }

    TQImage orig(im, w, h, 32, 0, 0, TQImage::LittleEndian);
    orig.setAlphaBuffer(true);

    // return scaled image
    return SQ_Utils::scale(orig, fitwithin, fitwithin, SQ_Utils::SMOOTH_FAST, TQ_ScaleMin);
}

#ifndef KSQUIRREL_PART
bool SQ_Utils::loadThumbnail(const KURL &pixPath, SQ_Thumbnail &t)
{
    SQ_LIBRARY *lib = 0;

#ifdef SQ_HAVE_KEXIF
    lib = SQ_LibraryHandler::instance()->libraryForFile(pixPath.path());
    bool th = false;

    if(lib)
    {
        KExifData data;
        data.readFromFile(pixPath.path());
        TQImage im = data.getThumbnail();

        if(!im.isNull())
        {
            SQ_Utils::exifRotate(TQString(), im, data.getImageQt::Orientation());

            th = true;
            t.w = 0;
            t.h = 0;
            t.mime = lib->mime;

            TQString w, h;
            w = im.text("Thumb::Image::Width");
            h = im.text("Thumb::Image::Height");
            t.w = w.toInt();
            t.h = h.toInt();

            if(!t.w || !t.h)
                SQ_ImageLoader::instance()->tasteImage(pixPath.path(), &t.w, &t.h, lib);

            t.thumbnail = SQ_Utils::scaleImage((unsigned char *)im.bits(), im.width(),
                im.height(), SQ_ThumbnailSize::biggest());
            t.thumbnail = t.thumbnail.swapRGB();
        }
    }
    else
        return false;

    // thumbnail loaded - nothing to do,
    // or load thumbnail by hands otherwise.
    if(th)
        return true;
#endif

    fmt_info *finfo;

    RGBA *all;

    bool b = SQ_ImageLoader::instance()->loadImage(pixPath.path(), SQ_CodecSettings::ThumbnailLoader);

    finfo = SQ_ImageLoader::instance()->info();
    all = SQ_ImageLoader::instance()->bits();

    // memory allocation failed in SQ_ImageLoader::loadImage()
    if(!all)
        return false;

    // another error occured...
    if(!b)
    {
        // if our image is partially corrupted - show it. The image
        // is partially corrupted, if number of errors < number of scanlines
        // and at least one page was loaded.
        if(!finfo->image.size()
            || (SQ_ImageLoader::instance()->errors() == finfo->image[0].h && finfo->image.size() == 1))
        {
            SQ_ImageLoader::instance()->cleanup();
            return false;
        }
    }

    if(!lib) lib = SQ_LibraryHandler::instance()->libraryForFile(pixPath.path());

    t.w = finfo->image[0].w;
    t.h = finfo->image[0].h;
    t.mime = lib->mime;

    t.thumbnail = SQ_Utils::scaleImage((unsigned char *)all, finfo->image[0].w,
            finfo->image[0].h, SQ_ThumbnailSize::biggest());

    SQ_ImageLoader::instance()->cleanup();

    // finally, rotate thumbnail using EXIF
    SQ_Utils::exifRotate(pixPath.path(), t.thumbnail);

    return true;
}
#endif