summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_imageloader.cpp
blob: 48b11cd29a8890a35dd0feb05823fd7b49f64250 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************
                          sq_imageloader.cpp  -  description
                             -------------------
    begin                : Tue Sep 20 2005
    copyright            : (C) 2005 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 <tqfile.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sq_imageloader.h"
#include "sq_libraryhandler.h"

#include <ksquirrel-libs/fileio.h>
#include <ksquirrel-libs/fmt_utils.h>
#include <ksquirrel-libs/fmt_codec_base.h>
#include <ksquirrel-libs/error.h>

SQ_ImageLoader * SQ_ImageLoader::m_instance = 0;

SQ_ImageLoader::SQ_ImageLoader(TQObject *tqparent) : TQObject(tqparent), m_errors(0)
{
    m_instance = this;

    m_image = 0;
    dumbscan = 0;
    finfo = new fmt_info;
}

SQ_ImageLoader::~SQ_ImageLoader()
{
    delete finfo;

    if(dumbscan) free(dumbscan);
    if(m_image) free(m_image);
}

/*
 *  Try to determine image dimensions.
 */
bool SQ_ImageLoader::tasteImage(const TQString &path, int *w, int *h, SQ_LIBRARY *_lib)
{
    SQ_LIBRARY      *lib;
    int             res;
    fmt_codec_base  *codeK;

    // determine library for file
    lib = _lib ? _lib : SQ_LibraryHandler::instance()->libraryForFile(path);

    if(!lib)
        return false;

    // determine codec
    codeK = lib->codec_il;

    if(!codeK)
        return false;

    // init...
    res = codeK->read_init(TQString(TQFile::encodeName(path)).ascii());

    // error in init()!
    if(res != SQE_OK)
    {
        codeK->read_close();
        return false;
    }

    // advance to next page
    res = codeK->read_next();

    if(res != SQE_OK)
    {
        codeK->read_close();
        return false;
    }
    else
    {
        fmt_image *image;

        image = codeK->image(0);
        *w = image->w;
        *h = image->h;
        codeK->read_close();

        return true;
    }
}

/*
 *  Try to load image and store a pointer to decoded image data in m_image. Aslo
 *  store information about the image in finfo.
 */
bool SQ_ImageLoader::loadImage(const TQString &pixPath, const SQ_CodecSettings::settings &sett, bool multi, int nomorethan)
{
    SQ_LIBRARY      *lib;
    int    res, current = 0, i, j;
    RGBA     *scan;
    fmt_codec_base  *codeK;
    fmt_image       *image;

    finfo->image.clear();
    finfo->meta.clear();

#define SQ_SAVE_INF \
    *finfo = codeK->information(); \
    codeK->read_close();

    // reset error count
    m_errors = 0;

    // determine library for file
    lib = SQ_LibraryHandler::instance()->libraryForFile(pixPath);

    if(!lib)
        return false;

    // determine codec
    codeK = lib->codec_il;

    if(!codeK)
        return false;

    if(sett != SQ_CodecSettings::None)
        SQ_CodecSettings::applySettings(lib, sett);

    // init...
    res = codeK->read_init(TQString(TQFile::encodeName(pixPath)).ascii());

    // error in init()!
    if(res != SQE_OK)
    {
        codeK->read_close();
        return false;
    }

    // read all pages in image...
    while(true)
    {
        if(m_errors || (!multi && current) || (multi && current == nomorethan))
            break;

        // advance to next page
        i = codeK->read_next();

        // error occured while decoding first page
        if(i != SQE_OK && i != SQE_NOTOK && !current)
        {
            codeK->read_close();
            return false;
        }
        else if((i != SQE_OK && i != SQE_NOTOK && current) || (i == SQE_NOTOK && current))
            break;

        image = codeK->image(current);

        // realloc memory
        if(!current)
        {
            const int S = image->w * image->h * sizeof(RGBA);

            m_image = (RGBA *)realloc(m_image, S);

            if(!m_image)
            {
                codeK->read_close();
                return false;
            }

            memset(m_image, 255, S);
        }
        else
        {
            dumbscan = (RGBA *)realloc(dumbscan, image->w * image->h * sizeof(RGBA));

            if(!dumbscan)
            {
                SQ_SAVE_INF
                return false;
            }
        }

        // read all passes
        for(int pass = 0;pass < image->passes;pass++)
        {
            codeK->read_next_pass();

            if(!current)
            {
                for(j = 0;j < image->h;j++)
                {
                    scan = m_image + j * image->w;
                    i = codeK->read_scanline(scan);
                    m_errors += (i != SQE_OK);
                }
            }
            else
            {
                for(j = 0;j < image->h;j++)
                {
                    i = codeK->read_scanline(dumbscan);

                    SQ_SAVE_INF

                    return (i == SQE_OK);
                }
            }
        }

        // flip decoded image, if needed
        if(image->needflip && !current)
            fmt_utils::flipv((char *)m_image, image->w * sizeof(RGBA), image->h);

        current++;
    }

    SQ_SAVE_INF

    // all done!
    return true;
}

// Delete buffers...
void SQ_ImageLoader::cleanup(bool del)
{
    if(dumbscan) free(dumbscan);
    if(del && m_image) free(m_image);

    dumbscan = 0;
    m_image = 0;
}

/*
 *  Return decoded image as TQImage.
 */
TQImage SQ_ImageLoader::image() const
{
    TQImage image((unsigned char*)bits(), finfo->image[0].w, finfo->image[0].h, 32, 0, 0, TQImage::LittleEndian);

    // if the image has alpha channel, let TQImage know it
    if(finfo->image[0].hasalpha)
        image.setAlphaBuffer(true);

    return image.swapRGB();
}