File: mng.c

package info (click to toggle)
mgp 1.13a%2Bupstream20090219-11
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,128 kB
  • sloc: ansic: 32,387; sh: 4,295; lisp: 1,405; yacc: 945; lex: 264; makefile: 158; perl: 117; awk: 9
file content (399 lines) | stat: -rw-r--r-- 9,197 bytes parent folder | download | duplicates (7)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 *	modified by Yoshifumi Nishida <nishida@csl.sony.co.jp> for magicpoint
 */
/*
	mngplay

	$Date: 2004/09/01 03:50:32 $

	Ralph Giles <giles@ashlu.bc.ca>

	This program my be redistributed under the terms of the
	GNU General Public Licence, version 2, or at your preference,
	any later version.

	(this assuming there's no problem with libmng not being GPL...)


	this is an SDL based mng player. the code is very rough;
	patches welcome.
*/
/*
 * $Id: mng.c,v 1.2 2004/09/01 03:50:32 nishida Exp $
 */

#ifdef MNG

#include <sys/time.h>
#include <libmng.h>
#include "mgp.h"

static GC gcmng;
static Display *mngdisplay;
static int mngscreen;
static Visual  *mngvisual;
static mng_handle mng;

/* structure for keeping track of our mng stream inside the callbacks */
typedef struct {
	FILE		*file;	   /* pointer to the file we're decoding */
	char		*filename; /* pointer to the file's path/name */
	Image 		*image;	
	Window 		window;
	mng_uint32	delay;     /* ticks to wait before resuming decode */
} mngstuff;

void mng_window_setup __P((mngstuff *mymng, int x, int y, int width, int height));

/* callbacks for the mng decoder */

/* memory allocation; data must be zeroed */
mng_ptr mymngalloc(mng_uint32 size)
{
	return (mng_ptr)calloc(1, size);
}

/* memory deallocation */
void mymngfree(mng_ptr p, mng_uint32 size)
{
	free(p);
	return;
}

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, "rb");
	if (mymng->file == NULL) {
		fprintf(stderr, "unable to open '%s'\n", mymng->filename);
		return MNG_FALSE;
	}

	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 */

	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;
	Image 		*image;
	Visual		*get_visual();

	image = (Image *)malloc(sizeof(Image));
	image->type= ITRUE;
	image->title= strdup("");
	image->rgb.used= image->rgb.size= 0;
	image->width= width;
	image->height= height;
	image->depth= 24;
	image->pixlen= 3;
	image->data = (char *)malloc(width * height * 24);

	if (image->data == NULL){
		fprintf(stderr, "unable to allocate %dx%d video memory \n", 
			width, height);
		return MNG_FALSE;
	}

	/* save the image pointer */
 	mymng = (mngstuff*)mng_get_userdata(mng);
	mymng->image = image;

	/* tell the mng decoder about our bit-depth choice */
	/* FIXME: this works on intel. is it correct in general? */
	mng_set_canvasstyle(mng, MNG_CANVAS_RGB8);

	return 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;

	/* dereference our structure */
	mymng = (mngstuff*)mng_get_userdata(mng);

	/* we assume any necessary locking has happened 
	   outside, in the frame level code */
	row = mymng->image->data + 
				mymng->image->pixlen * mymng->image->width * line;

	return (row);
}

/* timer */
mng_uint32 mymnggetticks(mng_handle mng)
{
	struct timeval now;
	mng_uint32 ticks;

    gettimeofday(&now, NULL);
    ticks=(now.tv_sec)*1000+(now.tv_usec)/1000;

	return(ticks);
}

mng_bool mymngrefresh(mng_handle mng, mng_uint32 x, mng_uint32 y,
			mng_uint32 w, mng_uint32 h)
{
	XImageInfo *ximageinfo;
	mngstuff	*mymng;

	/* dereference our structure */
	mymng = (mngstuff*)mng_get_userdata(mng);

	ximageinfo= imageToXImage(mngdisplay, mngscreen, mngvisual, depth,
			mymng->image, 0, 0, 0, 0);
	if (ximageinfo == NULL) {
		fprintf(stderr, "Cannot convert Image to XImage\n");
		exit(-1);
	}
	XPutImage(mngdisplay, mymng->window, gcmng, ximageinfo->ximage, 
		0, 0, 0, 0, mymng->image->width, mymng->image->height);

	freeXImage(ximageinfo->ximage, ximageinfo);

	return MNG_TRUE;
}

/* interframe delay callback */
mng_bool mymngsettimer(mng_handle mng, mng_uint32 msecs)
{
	mngstuff	*mymng;

	/* look up our stream struct */
	mymng = (mngstuff*)mng_get_userdata(mng);

	/* set the timer for when the decoder wants to be woken */
	mymng->delay = msecs;

	return MNG_TRUE;
}

mng_bool mymngerror(mng_handle mng, mng_int32 code, mng_int8 severity,
	mng_chunkid chunktype, mng_uint32 chunkseq,
	mng_int32 extra1, mng_int32 extra2, mng_pchar text)
{
	mngstuff	*mymng;
	char		chunk[5];
	
	/* dereference our data so we can get the filename */
	mymng = (mngstuff*)mng_get_userdata(mng);

	/* pull out the chuck type as a string */
	// FIXME: does this assume unsigned char?
	chunk[0] = (char)((chunktype >> 24) & 0xFF);
	chunk[1] = (char)((chunktype >> 16) & 0xFF);
	chunk[2] = (char)((chunktype >>  8) & 0xFF);
	chunk[3] = (char)((chunktype      ) & 0xFF);
	chunk[4] = '\0';

	/* output the error */

	return (0);
}

void mymngquit()
{
	mngstuff	*mymng;

	/* dereference our data so we can free it */
	mymng = (mngstuff*)mng_get_userdata(mng);

	XDestroyWindow(display, mymng->window);	

	/* cleanup. this will call mymngclosestream */
	mng_cleanup(&mng);

	/* free our data */
	free(mymng);

	/* quit */
	exit(0);
}

int checkevents(mng_handle mng)
{
	/* not implemented yet */
}

void
mngload(mngfile, x, y, width, height)
	char *mngfile;
	int x, y;
	int width, height;
{
	mngstuff	*mymng;
	u_int		myretcode;

	signal(SIGTERM, mymngquit);

	/* allocate our stream data structure */
	mymng = (mngstuff*)calloc(1, sizeof(*mymng));
	if (mymng == NULL) {
		fprintf(stderr, "could not allocate stream structure.\n");
		exit(0);
	}

	/* pass the name of the file we want to play */
	mymng->filename = mngfile;

	/* set up the mng decoder for our stream */
	mng = mng_initialize(mymng, mymngalloc, mymngfree, MNG_NULL);
	if (mng == MNG_NULL) {
		fprintf(stderr, "could not initialize libmng.\n");
		exit(1);
	}

	/* set the callbacks */
	mng_setcb_errorproc(mng, mymngerror);
	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);
	/* FIXME: should check for errors here */

	myretcode = mng_set_suspensionmode(mng, MNG_TRUE);
	if (myretcode != MNG_NOERROR){
		fprintf(stderr, "something happen.\n");
		exit(-1);
	}

	mng_window_setup(mymng, x, y, width, height);
	mng_readdisplay(mng);

	/* loop though the frames */
	while (mymng->delay) {

		usleep(mymng->delay * 1000);

		/* reset the delay in case the decoder
		   doesn't update it again */
		mymng->delay = 0;

		mng_display_resume(mng);

		/* check for user input (just quit at this point) */
		checkevents(mng);
	}
}

void 
mng_window_setup(mymng, x, y, width, height)
	mngstuff	*mymng;
	int x, y;
	int width, height;
{
	Visual		*get_visual();

	if (!mngdisplay){
	    if ((mngdisplay = XOpenDisplay(NULL)) == NULL) {
			fprintf(stderr, "Can't open display\n");
			exit(-1);
		}
		mngscreen = DefaultScreen(mngdisplay);
		mngvisual = get_visual(mngdisplay, mngscreen, &depth);
	}

	mymng->window = XCreateSimpleWindow(mngdisplay, 
		window,
		0, 0,
		width, height, 0, 
		WhitePixel(display, 0), WhitePixel(display, 0));

	XMoveWindow(mngdisplay, mymng->window, x, y);
	XMapRaised(mngdisplay, mymng->window);
	XFlush(mngdisplay);

	if (!gcmng)
		gcmng = XCreateGC(mngdisplay, mymng->window, 0, 0);
}

void
mngpreload(state, mngfile, width, height)
	struct render_state *state;
	char *mngfile;
	int *width, *height;
{
	mngstuff	*mymng;
	mng_handle	mng;
	u_int		myretcode;

	/* allocate our stream data structure */
	mymng = (mngstuff*)calloc(1, sizeof(*mymng));
	if (mymng == NULL) {
		fprintf(stderr, "could not allocate stream structure.\n");
		exit(0);
	}

	/* pass the name of the file we want to play */
	mymng->filename = mngfile;

	/* set up the mng decoder for our stream */
	mng = mng_initialize(mymng, mymngalloc, mymngfree, MNG_NULL);
	if (mng == MNG_NULL) {
		fprintf(stderr, "could not initialize libmng.\n");
		exit(1);
	}

	/* set the callbacks */
	mng_setcb_errorproc(mng, mymngerror);
	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);
	/* FIXME: should check for errors here */

	mng_read(mng);
	*width = mymng->image->width;
	*height = mymng->image->height;

	mng_cleanup(&mng);
	free(mymng);
}
#endif