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
366
367
368
369
370
371
372
|
/* This file is part of ksquirrel-libs (http://ksquirrel.sf.net)
Copyright (c) 2005 Dmitry Baryshev <[email protected]>
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.
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ksquirrel-libs/fmt_types.h"
#include "ksquirrel-libs/fileio.h"
#include "ksquirrel-libs/fmt_utils.h"
#include <libmng.h>
#include "fmt_codec_mng_defs.h"
#include "fmt_codec_mng.h"
#include "ksquirrel-libs/error.h"
#include "../xpm/codec_mng.xpm"
/* structure for keeping track of our mng stream inside the callbacks */
struct mngstuff
{
FILE *file; /* pointer to the file we're decoding */
std::string filename; /* pointer to the file's path/name */
fmt_codec *codec;
};
/*
*
* MNG (Multiple-image Network Graphics) is the animation extension of the popular PNG image-format.
* PNG (Portable Network Graphics) is an extensible file format for the lossless, portable,
* well-compressed storage of raster images.
*
* MNG has advanced animation features which make it very useful as a full replacement
* for GIF animations. These features allow animations that are impossible with GIF or
* result in much smaller files as GIF.
*
* As MNG builds on the same structure as PNG, it is robust, extensible and free of
* patents. It retains the same clever file integrity checks as in PNG.
*
* MNG also embraces the lossy JPEG image-format in a sub-format named JNG, which
* allows for alpha-transparency and color-correction on highly compressed (photographic) images.
*
*/
/* ******************************************************************** */
/* callbacks from mngplay.c (C) Ralph Giles <[email protected]> */
/* ******************************************************************** */
/* memory allocation; data must be zeroed */
mng_ptr mymngalloc(mng_size_t size)
{
// libmng requires calloc...
return (mng_ptr)calloc(1, size);
}
/* memory deallocation */
void mymngfree(mng_ptr p, mng_size_t /*size*/)
{
free(p);
}
mng_bool mymngopenstream(mng_handle mng)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff*)mng_get_userdata(mng);
/* open the file */
mymng->file = fopen(mymng->filename.c_str(), "rb");
if(mymng->file == NULL)
return MNG_FALSE;
return MNG_TRUE;
}
mng_bool mymngprocesstext(mng_handle mng, mng_uint8 /*iType*/, mng_pchar zKeyword, mng_pchar zText,
mng_pchar /*zLanguage*/, mng_pchar /*zTranslation*/)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *)mng_get_userdata(mng);
if(zKeyword && zText)
{
fmt_metaentry mt;
mt.group = zKeyword;
mt.data = zText;
mymng->codec->addmeta(mt);
}
return MNG_TRUE;
}
mng_bool mymngclosestream(mng_handle mng)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *)mng_get_userdata(mng);
/* close the file */
fclose(mymng->file);
mymng->file = NULL;/* for safety */
mymng->filename.clear();
return MNG_TRUE;
}
/* feed data to the decoder */
mng_bool mymngreadstream(mng_handle mng, mng_ptr buffer, mng_uint32 size, mng_uint32 *bytesread)
{
mngstuff *mymng;
/* look up our stream struct */
mymng = (mngstuff *)mng_get_userdata(mng);
/* read the requested amount of data from the file */
*bytesread = fread(buffer, 1, size, mymng->file);
return MNG_TRUE;
}
/* the header's been read. set up the display stuff */
mng_bool mymngprocessheader(mng_handle mng, mng_uint32 width, mng_uint32 height)
{
mngstuff *mymng;
mymng = (mngstuff *)mng_get_userdata(mng);
mymng->codec->priv.w = width;
mymng->codec->priv.frame = new RGBA [width * height];
return (!mymng->codec->priv.frame) ? MNG_FALSE : MNG_TRUE;
}
/* return a row pointer for the decoder to fill */
mng_ptr mymnggetcanvasline(mng_handle mng, mng_uint32 line)
{
mngstuff *mymng;
mng_ptr row;
mymng = (mngstuff *)mng_get_userdata(mng);
row = mymng->codec->priv.frame + line * mymng->codec->priv.w;
return row;
}
/* timer */
mng_uint32 mymnggetticks(mng_handle /*mng*/)
{
return 0;
}
mng_bool mymngrefresh(mng_handle /*mng*/, mng_uint32 /*x*/, mng_uint32 /*y*/, mng_uint32 /*w*/, mng_uint32 /*h*/)
{
return MNG_TRUE;
}
/* interframe delay callback */
mng_bool mymngsettimer(mng_handle mng, mng_uint32 msecs)
{
mngstuff *mymng;
mymng = (mngstuff *)mng_get_userdata(mng);
mymng->codec->priv.ms = (msecs == 1) ? 100 : msecs;
return MNG_TRUE;
}
/* ******************************************************************** */
fmt_codec::fmt_codec() : fmt_codec_base()
{}
fmt_codec::~fmt_codec()
{}
void fmt_codec::options(codec_options *o)
{
o->version = "0.3.4";
o->name = "Multiple Network Graphics";
#ifdef MNG_INCLUDE_JNG
o->filter = "*.mng *.jng ";
o->mimetype = "video/x-mng;image/x-jng";
#else
o->filter = "*.mng ";
o->mimetype = "video/x-mng";
#endif
o->config = "";
o->mime = "";
o->pixmap = codec_mng;
o->readable = true;
o->canbemultiple = true;
o->writestatic = false;
o->writeanimated = false;
o->needtempfile = false;
}
s32 fmt_codec::read_init(const std::string &file)
{
frs.open(file.c_str(), ios::binary | ios::in);
if(!frs.good())
return SQE_R_NOFILE;
frs.close();
currentImage = -1;
read_error = false;
finfo.animated = false;
// allocate our stream data structure
mymng = new mngstuff;
if(!mymng)
return SQE_R_NOMEMORY;
mymng->filename = file;
mymng->codec = this;
priv.frame = 0;
priv.ms = 10;
/* set up the mng decoder for our stream */
mng = mng_initialize(mymng, mymngalloc, mymngfree, MNG_NULL);
if (mng == MNG_NULL)
return SQE_R_NOMEMORY;
/* set the callbacks */
mng_setcb_openstream(mng, ::mymngopenstream);
mng_setcb_closestream(mng, ::mymngclosestream);
mng_setcb_readdata(mng, ::mymngreadstream);
mng_setcb_gettickcount(mng, ::mymnggetticks);
mng_setcb_settimer(mng, ::mymngsettimer);
mng_setcb_processheader(mng, ::mymngprocessheader);
mng_setcb_getcanvasline(mng, ::mymnggetcanvasline);
mng_setcb_refresh(mng, ::mymngrefresh);
mng_setcb_processtext(mng, ::mymngprocesstext);
mng_set_suspensionmode(mng, MNG_TRUE);
mng_set_canvasstyle(mng, MNG_CANVAS_RGBA8);
total = 0;
return SQE_OK;
}
s32 fmt_codec::read_next()
{
currentImage++;
if((total && currentImage == total) || (!total && currentImage))
return SQE_NOTOK;
int myretcode;
if(!currentImage)
{
myretcode = mng_read(mng);
if(myretcode != MNG_NOERROR)
return SQE_R_BADFILE;
total = mng_get_totallayers(mng);
if(total > 1) total--;
myretcode = mng_display(mng);
if(myretcode != MNG_NOERROR && myretcode != MNG_NEEDTIMERWAIT)
return SQE_R_BADFILE;
}
else
{
myretcode = mng_display_resume(mng);
if(myretcode != MNG_NOERROR && myretcode != MNG_NEEDTIMERWAIT)
return SQE_R_BADFILE;
finfo.animated = true;
}
fmt_image image;
image.w = mng_get_imagewidth(mng);
image.h = mng_get_imageheight(mng);
image.bpp = 32;
image.compression = (mng_get_imagetype(mng) == MNG_IMAGETYPE_PNG) ? "Deflate method 8, 32K window" : "JPEG";
image.hasalpha = true;
int cc = mng_get_colortype(mng);
switch(cc)
{
case MNG_COLORTYPE_GRAY: image.colorspace = "Grayscale"; break;
case MNG_COLORTYPE_RGB: image.colorspace = "RGB"; break;
case MNG_COLORTYPE_INDEXED: image.colorspace = "Indexed"; break;
case MNG_COLORTYPE_GRAYA: image.colorspace = "Grayscale with alpha"; break;
case MNG_COLORTYPE_RGBA: image.colorspace = "RGBA"; break;
default: image.colorspace = "Unknown";
}
image.delay = priv.ms;
finfo.image.push_back(image);
line = -1;
return SQE_OK;
}
s32 fmt_codec::read_next_pass()
{
return SQE_OK;
}
s32 fmt_codec::read_scanline(RGBA *scan)
{
line++;
fmt_image *im = image(currentImage);
fmt_utils::fillAlpha(scan, im->w);
memcpy(scan, priv.frame+im->w*line, im->w*sizeof(RGBA));
return SQE_OK;
}
void fmt_codec::read_close()
{
finfo.meta.clear();
finfo.image.clear();
mng_cleanup(&mng);
delete [] priv.frame;
priv.frame = 0;
}
#include "fmt_codec_cd_func.h"
|