summaryrefslogtreecommitdiffstats
path: root/kernel/include/ksquirrel-libs/fmt_codec_base.h
blob: cae04ea5b968b653ce26fd28c1be0dd27450d24f (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
/*  This file is part of ksquirrel-libs (http://ksquirrel.sf.net)

    Copyright (c) 2005 Dmitry Baryshev <ksquirrel@tut.by>

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

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef KSQUIRREL_LIBS_CLASS_DEFINITION_H
#define KSQUIRREL_LIBS_CLASS_DEFINITION_H

#include <ksquirrel-libs/fmt_defs.h>
#include <ksquirrel-libs/fileio.h>
#include <ksquirrel-libs/settings.h>

//////////////////////////////////
//                              //
//  Base class for all          //
//  codecs                      //
//                              //
//////////////////////////////////


class fmt_codec_base
{
    public:
	fmt_codec_base()
	{}

	virtual ~fmt_codec_base()
	{}

	// version of the library, e.g. "1.2.2", etc.
        //
        // tell clients that fmt_codec_base couldn't be
        // instantianted
	virtual void	        options(codec_options *) = 0;

	// return file extension for a given bpp
	virtual std::string     extension(const s32 bpp);

	/*
	 *             read methods
	 */

	// fmt_read_init: do what you need before decoding
	virtual s32	read_init(const std::string &file);

	// fmt_read_next: seek to correct file offset, do other initialization stuff.
	// this method should be (and will be) called before image is about to
	// be decoded.
	virtual s32	read_next();

	// fmt_read_next_pass: do somethimg important before the next pass
	// will be decoded (usually do nothing, if the image has only 1 pass (like BMP, PCX ...),
	// or adjust variables if the image is interlaced, with passes > 1 (like GIF, PNG))
	virtual s32	read_next_pass();

	// fmt_readscanline: read one scanline from file
	virtual s32	read_scanline(RGBA *scan);

	// fmt_read_close: close all handles, free memory, etc.
	virtual void	read_close();

	/*
	 *             write methods
	 */

	// fmt_getwriteoptions: return write options for this image format
	virtual void    getwriteoptions(fmt_writeoptionsabs *);

	// fmt_write_init: init writing
	virtual s32     write_init(const std::string &file, const fmt_image &image, const fmt_writeoptions &opt);

	virtual s32	write_next();

	virtual s32	write_next_pass();

	// fmt_write_scanline: write scanline
	virtual s32	write_scanline(RGBA *scan);

	// fmt_write_close: close writing(close descriptors, free memory, etc.)
	virtual void	write_close();

	fmt_info information() const;

	fmt_image* image(const int index);

        void addmeta(const fmt_metaentry &m);

        void settempfile(const std::string &t);

        // fill fmt_settings with values
        void set_settings(const fmt_settings &sett);

        fmt_settings settings() const;

        virtual void fill_default_settings()
        {}

    protected:
	// image index in finfo.image
	s32               currentImage;

	// fmt_info structure
	fmt_info          finfo;

	// input stream
	ifstreamK         frs;

	// output stream
	ofstreamK         fws;

	// some additional error checkers
	bool              read_error, write_error;

	// line and layer indexes - needed by some
	// interlaced or layered images
	s32               line, layer;

	// error code
	s32               write_error_code;

	// write options
	fmt_writeoptions  writeopt;

	// saved fmt_image
	fmt_image         writeimage;

        // path to temporary file
        // should be set by the highlevel application
        // with settempfile()
        std::string       tmp;

        fmt_settings      m_settings;
};

inline
fmt_settings fmt_codec_base::settings() const
{
    return m_settings;
}

inline
void fmt_codec_base::set_settings(const fmt_settings &sett)
{
    m_settings = sett;
}

inline
fmt_info fmt_codec_base::information() const
{
    return finfo;
}

inline
fmt_image* fmt_codec_base::image(const int index)
{
    return &finfo.image[index];
}

inline
void fmt_codec_base::addmeta(const fmt_metaentry &m)
{
    finfo.meta.push_back(m);
}

inline
void fmt_codec_base::settempfile(const std::string &t)
{
    tmp = t;
}

inline
std::string fmt_codec_base::extension(const s32)
{
    return std::string("");
}

inline
s32 fmt_codec_base::read_init(const std::string &)
{ return 1; }

inline
s32 fmt_codec_base::read_next()
{ return 1; }

inline
s32 fmt_codec_base::read_next_pass()
{ return 1; }

inline
s32 fmt_codec_base::read_scanline(RGBA *)
{ return 1; }

inline
void fmt_codec_base::read_close()
{}

inline
void fmt_codec_base::getwriteoptions(fmt_writeoptionsabs *)
{}

inline
s32 fmt_codec_base::write_init(const std::string &, const fmt_image &, const fmt_writeoptions &)
{ return 1; }

inline
s32 fmt_codec_base::write_next()
{ return 1; }

inline
s32 fmt_codec_base::write_next_pass()
{ return 1; }

inline
s32 fmt_codec_base::write_scanline(RGBA *)
{ return 1; }

inline
void fmt_codec_base::write_close()
{}

#define BASE_CODEC_DECLARATIONS \
	fmt_codec();            \
	~fmt_codec();           \
                                \
	virtual void    options(codec_options *o); \
	virtual s32	read_init(const std::string &file);\
	virtual s32	read_next();                       \
	virtual s32	read_next_pass();                  \
	virtual s32	read_scanline(RGBA *scan);         \
	virtual void	read_close();

#define WRITE_CODEC_DECLARATIONS  \
	virtual std::string extension(const s32 bpp);  \
	virtual void	getwriteoptions(fmt_writeoptionsabs *);  \
        virtual s32     write_init(const std::string &file, const fmt_image &image, const fmt_writeoptions &opt); \
        virtual s32     write_next();                \
        virtual s32     write_next_pass();           \
        virtual s32     write_scanline(RGBA *scan);  \
        virtual void    write_close();

#endif