summaryrefslogtreecommitdiffstats
path: root/doc/html/ksquirrel-libs-olibs3.html
blob: 46150ecc4af846e8319656c541db571ff23986f4 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>KSquirrel: development</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name='Author' content='Baryshev Dmitry/Krasu'>
		    
    <link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>

<center><h3><b>How to write a new library for KSquirrel (for example, it will decode abstract 'ttx' format) ?</b></h3></center>

<ul>
<li>Create a directory with necessary sources ('generate' is distributed with ksquirrel-libs):
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
# ./generate ttx -build
  </pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>Typical fmt_codec_ttx_defs.h
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
#ifndef KSQUIRREL_READ_IMAGE_ttx
#define KSQUIRREL_READ_IMAGE_ttx

// Define constants you need here

#endif
  </pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>Typical fmt_codec_ttx.h
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
#ifndef KSQUIRREL_LIBS_CLASS_DEFINITION_ttx_H
#define KSQUIRREL_LIBS_CLASS_DEFINITION_ttx_H

#include "fmt_codec_base.h"

class fmt_codec : public fmt_codec_base
{
    public:

        fmt_codec();
        ~fmt_codec();

        virtual std::string     fmt_version();
        virtual std::string     fmt_quickinfo();
        virtual std::string     fmt_filter();
        virtual std::string     fmt_mime();
        virtual std::string     fmt_pixmap();
        virtual std::string     fmt_extension(const s32 bpp);

        virtual bool    fmt_readable() const;
        virtual s32     fmt_read_init(const std::string &amp;file);
        virtual s32     fmt_read_next();
        virtual s32     fmt_read_next_pass();
        virtual s32     fmt_read_scanline(RGBA *scan);
        virtual void    fmt_read_close();


        virtual bool    fmt_writable() const;
        virtual void    fmt_getwriteoptions(fmt_writeoptionsabs *);
        virtual s32     fmt_write_init(const std::string &amp;file, const fmt_image &amp;image,
					const fmt_writeoptions &amp;opt);
        virtual s32     fmt_write_next();
        virtual s32     fmt_write_next_pass();
        virtual s32     fmt_write_scanline(RGBA *scan);
        virtual void    fmt_write_close();

    private:
        // define variables you need here
};

extern "C" fmt_codec_base* fmt_codec_create()
{
    return (new fmt_codec);
}

extern "C" void fmt_codec_destroy(fmt_codec_base *p)
{
    delete p;
}
</pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>Typical fmt_codec_ttx.cpp
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>

#include &#60;iostream&#62;

#include "fmt_types.h"
#include "fileio.h"
#include "fmt_codec_ttx_defs.h"
#include "fmt_codec_ttx.h"

#include "error.h"

fmt_codec::fmt_codec() : fmt_codec_base()
{}

fmt_codec::~fmt_codec()
{}

<b>Returns library's version</b>
std::string fmt_codec::fmt_version()
{
    return std::string("0.0.1");
}

<b>Returns common information on ttx format</b>
std::string fmt_codec::fmt_quickinfo()
{
    return std::string("TTX is a very cool format!");
}

<b>Filter for a file manager (with a blank after
each extension)</b>
std::string fmt_codec::fmt_filter()
{
    return std::string("*.ttx1 *.ttx2 *.ttx3 ");
}

<b>The regular expression defining the file type
(usually first 2-5 characters in a file).
This example shows, that format TTX is defined by header
TTX7, TTX8 or TTX9. If the file has no header
 (like .ico and .pix) this function should return empty string.</b>
std::string fmt_codec::fmt_mime()
{
    return std::string("TTX[789]");
}
    
<b>Compile <a href='bits.html'>this program</a>,
create PNG icon 16x16 and run 
#./bits icon.png > icon.bits.
Then copy contents of icon.bits here</b>
std::string fmt_codec::fmt_pixmap()
{
    return std::string("");
}

<b>Open file, initialize variables, etc.</b>
s32 fmt_codec::fmt_read_init(const std::string &amp;file)
{
    frs.open(file.c_str(), ios::binary | ios::in);
    
    if(!frs.good())
        return SQERR_NOFILE;

    currentImage = -1;
    read_error = false;

    finfo.animated = false;

    return SQERR_OK;
}

<b>This method will be called BEFORE decoding of each image in a file,
so we should seek to correct file offset or do something important now.
Return SQERR_NOTOK, if the next image can't be decoded (no such image),
and any other error code on error.</b>
s32 fmt_codec::fmt_read_next()
{
    currentImage++;

    if(currentImage)
	return SQERR_NOTOK; <b>return, if TTX can
			     have only one image.</b>

    fmt_image image;

    <b>Non-interlaced image has 1 pass.
    If you need to define another value, do it here</b>
    image.passes = 1;

<b>Here you should read necessary data from file,
and initialize finfo.image[currentImage].w,h,bpp. Return error
code you need on error.</b>
<b>...</b>

    image.compression = "-";
    image.colorspace = "RGB";

    finfo.image.push_back(image);

    return SQERR_OK;
}

<b>This method will be called BEFORE decoding of each pass
in the interlaced image.</b>
s32 fmt_codec::fmt_read_next_pass()
{
    <b>Our TTX fromat can't be interlaced, so we
    won't do anything, just return SQERR_OK</b>
    return SQERR_OK;
}

<b>Reads scanline in 'scan'. This example just fills
'scan' with white color (RGBA(255,255,255,255))</b>
s32 fmt_codec::fmt_read_scanline(RGBA *scan)
{
    memset(scan, 255, finfo.image[currentImage].w * sizeof(RGBA));

    return SQERR_OK;
}

<b>Closes everything, frees allocated memory, etc.</b>
void fmt_codec::fmt_read_close()
{
    frs.close();

    // you should free information on close
    finfo.meta.clear();
    finfo.image.clear();
}

<b>Returns write options for TTX format. This method won't be
called, if fmt_writable() returns 'false'</b>
void fmt_codec::fmt_getwriteoptions(fmt_writeoptionsabs *opt)
{
    <b>Can TTX format be interlaced ? No.</b>
    opt->interlaced = false;

    <b>With which compression it can be compressed ? 
    With no compression (like not-RLE BMP).</b>
    opt->compression_scheme = CompressionNo;
    
    <b>minimum, maximum, and default compression level.
    Ignored, if the compression is CompressionNo.</b>
    opt->compression_min = 0;
    opt->compression_max = 0;
    opt->compression_def = 0;

    <b>TTX can't be interlaced, passes = 1</b>
    opt->passes = 1;

    <b>KSquirrel shouldn't flip the image
    before writing</b>
    opt->needflip = false;
}

<b>Same to fmt_read_init()</b>
s32 fmt_codec::fmt_write_init(const std::string &amp;file, const fmt_image &image, 
				const fmt_writeoptions &opt)
{
    if(!image.w || !image.h || file.empty())
        return SQE_W_WRONGPARAMS;

    writeimage = image;
    writeopt = opt;

    fws.open(file.c_str(), ios::binary | ios::out);

    if(!fws.good())
        return SQE_W_NOFILE;

    return SQE_OK;
}

<b>Same to fmt_read_next()</b>
s32 fmt_codec::fmt_write_next()
{
    return SQE_OK;
}

<b>Same to fmt_read_next_pass()</b>
s32 fmt_codec::fmt_write_next_pass()
{
    return SQE_OK;
}

<b>Write scanline. Same to fmt_read_scanline()</b>
s32 fmt_codec::fmt_write_scanline(RGBA *scan)
{
    ...
    return SQE_OK;
}

<b>Same to fmt_read_init()</b>
void fmt_codec::fmt_write_close()
{
    fws.close();
}

<b>Can this library write TTX ? No.</b>
bool fmt_codec::fmt_writable() const
{
    return false;
}

<b>Can this library read TTX ? Yes.</b>
bool fmt_codec::fmt_readable() const
{
    return true;
}

<b>Some libraries support several image types (like PNM library).
This method should be used by writing function to determine file extension by image's bpp.
For example, 1 bpp means .pbm, 8 bpp - pgm</b>"
std::string fmt_codec::fmt_extension(const s32 /*bpp*/)
{
    return std::string("");
}

  </pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>Compile it
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">
  <pre>
# ./compile-c++
# 
  </pre>
  </td>
  </tr>
  </tbody>
</table>

<br><br>
<li>That's all. You've just created a new decoder for KSquirrel. Copy libSQ_codec_ttx.so in /usr/lib/ksquirrel-libs.
</ul>

</body>
</html>