2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/AC/convert.cpp $
15 * C module for the conversion of standard animation files to our own ANIM format.
16 * This is where all the real code for this application is located really.
19 * Revision 1.2 2002/06/09 04:41:15 relnev
20 * added copyright header
22 * Revision 1.1.1.1 2002/05/03 03:28:11 root
26 * 2 10/23/98 6:03p Dave
28 * 1 10/23/98 5:34p Dave
30 * 3 10/22/98 6:14p Dave
31 * Optimized some #includes in Anim folder. Put in the beginnings of
32 * parse/localization support for externalized strings and tstrings.tbl
34 * 2 10/07/98 10:52a Dave
37 * 1 10/07/98 10:48a Dave
39 * 12 6/23/98 2:52p Hoffoss
40 * Changed code so AC compiles once again.
42 * 11 7/20/97 6:57p Lawrance
43 * supporting new RLE format
45 * 10 6/25/97 11:57a Lawrance
48 * 9 6/25/97 11:49a Lawrance
49 * don't assert if PCX read fails, print warning
51 * 8 5/21/97 2:26p Lawrance
52 * bug fix: not using correct header size
54 * 7 5/21/97 11:06a Lawrance
55 * enabling a user-defined transparent value
57 * 6 5/19/97 3:21p Lawrance
58 * add fps parm, version num to anim header
60 * 5 2/25/97 5:18p Lawrance
61 * force_key_frame now numbering from 1 for PCXs as well as AVIs
63 * 4 2/20/97 1:47p Lawrance
64 * adding dots when making frames
66 * 3 2/20/97 10:30a Hoffoss
67 * Added in support for forcing a key frame.
69 * 2 2/19/97 9:26p Lawrance
70 * added force_key_frame global
72 * 1 2/19/97 7:14p Lawrance
74 * 14 2/19/97 4:21p Lawrance
75 * took out unnecessary #include BmpMan.h
77 * 13 2/19/97 3:59p Lawrance
78 * using pcxutils to load bitmaps, not bmpman
80 * 12 2/17/97 2:59p Lawrance
81 * integrating into game
83 * 11 2/17/97 3:02p Hoffoss
84 * Added headers to files, and implemented key frame dialog stuff.
86 * 10 2/14/97 10:38p Lawrance
89 * 9 2/14/97 9:47p Hoffoss
92 * 8 2/14/97 8:44p Lawrance
93 * fixed bug with saving header
95 * 7 2/14/97 8:04p Hoffoss
98 * 6 2/14/97 7:44p Lawrance
99 * fixed some bugs in loading an AVI
101 * 5 2/14/97 7:33p Lawrance
102 * added convert_avi_to_anim() function
104 * 4 2/14/97 5:38p Hoffoss
105 * Changes to get AnimCoverter project to compile and link.
107 * 3 2/14/97 3:28p Hoffoss
108 * Wrote functions to save an anim file.
110 * 2 2/13/97 5:55p Lawrance
111 * reading AVI / decompressing AVI functions in
119 #include "pcxutils.h"
120 #include "animplay.h"
121 #include "packunpack.h"
123 #define AVI_STREAM_F_USED ( 1 << 0 )
125 typedef struct _frame_index {
130 typedef struct AVI_STREAM_TYPE {
132 FRAMEINDEX *frame_index;
136 int min_compressed_buffer_size;
139 //ubyte pal_translation[256]; // palette translation look-up table
143 #define AVIF_HASINDEX 0x00000010
145 typedef struct _avimainheader {
148 unsigned int dwMicroSecPerFrame;
149 unsigned int dwMaxBytesPerSec;
150 unsigned int dwPaddingGranularity;
151 unsigned int dwFlags;
152 unsigned int dwTotalFrames;
153 unsigned int dwInitialFrames;
154 unsigned int dwStreams;
155 unsigned int dwSuggestedBufferSize;
156 unsigned int dwWidth;
157 unsigned int dwHeight;
158 unsigned int dwReserved[4];
161 #define AVISF_VIDEO_PALCHANGES 0x00010000
163 typedef struct _avistreamheader {
166 unsigned int fccType;
167 unsigned int fccHandler;
168 unsigned int dwFlags;
169 unsigned short wPriority;
170 unsigned short wLanguage;
171 unsigned int dwInitialFrames;
172 unsigned int dwScale;
174 unsigned int dwStart;
175 unsigned int dwLength;
176 unsigned int dwSuggestedBufferSize;
177 unsigned int dwQuality;
178 unsigned int dwSampleSize;
187 #define BI_RLE8 0x00000001
189 typedef struct _bitmapinfo {
194 unsigned short biPlanes;
195 unsigned short biBitCount;
196 unsigned int biCompression;
197 unsigned int biSizeImage;
200 unsigned int biClrUsed;
201 unsigned int biClrImportant;
213 // Internal function prototypes
214 int AVI_stream_open(char* filename);
215 void AVI_stream_close();
216 int AVI_stream_get_frame(ubyte* buffer, int frame_number);
217 void AVI_decompress_RLE8(ubyte* src, ubyte* dest, int w, int h);
220 static AVI_STREAM_TYPE AVI_stream;
221 static int AVI_stream_inited = 0;
224 char *anim_save_filename;
225 ubyte *cur_frame, *last_frame;
226 ubyte anim_buffer[ANIM_BUFFER_MAX];
229 int total_key_frames;
233 int Use_custom_xparent_color;
234 rgb_triple Xparent_color;
236 int Compression_type; // what kind of RLE compression is going to be used
237 int Key_frame_compression, Regular_frame_compression;
239 key_frame *first_frame;
240 FILE *anim_fp = NULL;
243 // AVI_stream_init() is called only to clear USED flag of the AVI_stream structure
244 // and reset the current frame.
246 // This does not need to be called explicity, since it will be called by AVI_stream_open()
249 void AVI_stream_init()
251 AVI_stream.flags &= ~AVI_STREAM_F_USED;
252 AVI_stream.current_frame = 0;
253 AVI_stream_inited = 1;
255 AVI_stream.pfile = NULL;
256 AVI_stream.frame_index = NULL;
259 // AVI_stream_open() will open the AVI file and prepare it for reading, but will not
260 // store any of the frame data.
262 // returns: 0 ==> success
263 // !0 ==> could not open the AVI stream
265 // The filename is expected to be an absolute pathname (or file in the current working directory)
267 int AVI_stream_open(char* filename)
269 if ( !AVI_stream_inited )
274 unsigned int tag = 0, size = 0, next_chunk;
275 unsigned int s_tag, tmp;
276 AVIMAINHEADER avi_header;
277 AVISTREAMHEADER stream_header;
278 BITMAPINFO bitmap_header;
279 unsigned int file_size = 0;
280 unsigned int movi_offset = 0;
282 SDL_assert( !(AVI_stream.flags & AVI_STREAM_F_USED) );
284 pfile = fopen(filename, "rb");
287 printf("AVI ==> Unable to open %s", filename);
292 fseek(pfile, 0, SEEK_END);
293 file_size = ftell(pfile);
294 fseek(pfile, 0, SEEK_SET);
296 // check for valid file type
297 fread(&id, 1, 4, pfile);
301 if (id != 0x46464952) {
302 printf("Not a RIFF file '%s'\n", filename);
308 fread(&id, 1, 4, pfile);
310 // check for valid RIFF type
311 fread(&id, 1, 4, pfile);
315 if (id != 0x20495641) {
316 printf("Not an AVI file '%s'\n", filename);
321 // used for main 'LIST' chunks
322 unsigned int offset_tmp = 0;
325 while ( ftell(pfile) < file_size ) {
326 fread(&tag, 1, 4, pfile);
327 fread(&size, 1, 4, pfile);
329 tag = INTEL_INT(tag);
330 size = INTEL_INT(size);
332 next_chunk = ftell(pfile) + size;
338 fread(&s_tag, 1, 4, pfile);
339 s_tag = INTEL_INT(s_tag);
344 fread(&avi_header.fcc, 1, sizeof(int), pfile);
345 fread(&avi_header.cb, 1, sizeof(int), pfile);
346 fread(&avi_header.dwMicroSecPerFrame, 1, sizeof(int), pfile);
347 fread(&avi_header.dwMaxBytesPerSec, 1, sizeof(int), pfile);
348 fread(&avi_header.dwPaddingGranularity, 1, sizeof(int), pfile);
349 fread(&avi_header.dwFlags, 1, sizeof(int), pfile);
350 fread(&avi_header.dwTotalFrames, 1, sizeof(int), pfile);
351 fread(&avi_header.dwInitialFrames, 1, sizeof(int), pfile);
352 fread(&avi_header.dwStreams, 1, sizeof(int), pfile);
353 fread(&avi_header.dwSuggestedBufferSize, 1, sizeof(int), pfile);
354 fread(&avi_header.dwWidth, 1, sizeof(int), pfile);
355 fread(&avi_header.dwHeight, 1, sizeof(int), pfile);
356 fread(&avi_header.dwReserved, 1, sizeof(avi_header.dwReserved), pfile);
358 avi_header.fcc = INTEL_INT(avi_header.fcc);
359 avi_header.cb = INTEL_INT(avi_header.cb);
360 avi_header.dwMicroSecPerFrame = INTEL_INT(avi_header.dwMicroSecPerFrame);
361 avi_header.dwMaxBytesPerSec = INTEL_INT(avi_header.dwMaxBytesPerSec);
362 avi_header.dwPaddingGranularity = INTEL_INT(avi_header.dwPaddingGranularity);
363 avi_header.dwFlags = INTEL_INT(avi_header.dwFlags);
364 avi_header.dwTotalFrames = INTEL_INT(avi_header.dwTotalFrames);
365 avi_header.dwInitialFrames = INTEL_INT(avi_header.dwInitialFrames);
366 avi_header.dwStreams = INTEL_INT(avi_header.dwStreams);
367 avi_header.dwSuggestedBufferSize = INTEL_INT(avi_header.dwSuggestedBufferSize);
368 avi_header.dwWidth = INTEL_INT(avi_header.dwWidth);
369 avi_header.dwHeight = INTEL_INT(avi_header.dwHeight);
372 SDL_assert(avi_header.fcc == 0x68697661);
374 // we're stupid, can only handle a single stream
375 if (avi_header.dwStreams != 1) {
376 printf("AVI has more than one stream '%s'\n", filename);
381 // require index chunk (should be flagged as availble)
382 if ( !(avi_header.dwFlags & AVIF_HASINDEX) ) {
383 printf("AVI does not have index '%s'\n", filename);
388 // update next_chunk offset for sub-chunk
389 offset_tmp = next_chunk;
390 next_chunk = ftell(pfile);
395 // 'strl' - subchunk of 'hdrl'
397 fread(&stream_header.fcc, 1, sizeof(int), pfile);
398 fread(&stream_header.cb, 1, sizeof(int), pfile);
399 fread(&stream_header.fccType, 1, sizeof(int), pfile);
400 fread(&stream_header.fccHandler, 1, sizeof(int), pfile);
401 fread(&stream_header.dwFlags, 1, sizeof(int), pfile);
402 fread(&stream_header.wPriority, 1, sizeof(short), pfile);
403 fread(&stream_header.wLanguage, 1, sizeof(short), pfile);
404 fread(&stream_header.dwInitialFrames, 1, sizeof(int), pfile);
405 fread(&stream_header.dwScale, 1, sizeof(int), pfile);
406 fread(&stream_header.dwRate, 1, sizeof(int), pfile);
407 fread(&stream_header.dwStart, 1, sizeof(int), pfile);
408 fread(&stream_header.dwLength, 1, sizeof(int), pfile);
409 fread(&stream_header.dwSuggestedBufferSize, 1, sizeof(int), pfile);
410 fread(&stream_header.dwQuality, 1, sizeof(int), pfile);
411 fread(&stream_header.dwSampleSize, 1, sizeof(int), pfile);
412 fread(&stream_header.rcFrame.left, 1, sizeof(short), pfile);
413 fread(&stream_header.rcFrame.top, 1, sizeof(short), pfile);
414 fread(&stream_header.rcFrame.right, 1, sizeof(short), pfile);
415 fread(&stream_header.rcFrame.bottom, 1, sizeof(short), pfile);
417 stream_header.fcc = INTEL_INT(stream_header.fcc);
418 stream_header.cb = INTEL_INT(stream_header.cb);
419 stream_header.fccType = INTEL_INT(stream_header.fccType);
420 stream_header.fccHandler = INTEL_INT(stream_header.fccHandler);
421 stream_header.dwFlags = INTEL_INT(stream_header.dwFlags);
422 stream_header.wPriority = INTEL_SHORT(stream_header.wPriority);
423 stream_header.wLanguage = INTEL_SHORT(stream_header.wLanguage);
424 stream_header.dwInitialFrames = INTEL_INT(stream_header.dwInitialFrames);
425 stream_header.dwScale = INTEL_INT(stream_header.dwScale);
426 stream_header.dwRate = INTEL_INT(stream_header.dwRate);
427 stream_header.dwStart = INTEL_INT(stream_header.dwStart);
428 stream_header.dwLength = INTEL_INT(stream_header.dwLength);
429 stream_header.dwSuggestedBufferSize = INTEL_INT(stream_header.dwSuggestedBufferSize);
430 stream_header.dwQuality = INTEL_INT(stream_header.dwQuality);
431 stream_header.dwSampleSize = INTEL_INT(stream_header.dwSampleSize);
432 stream_header.rcFrame.left = INTEL_SHORT(stream_header.rcFrame.left);
433 stream_header.rcFrame.top = INTEL_SHORT(stream_header.rcFrame.top);
434 stream_header.rcFrame.right = INTEL_SHORT(stream_header.rcFrame.right);
435 stream_header.rcFrame.bottom = INTEL_SHORT(stream_header.rcFrame.bottom);
438 SDL_assert(stream_header.fcc == 0x68727473);
440 // check stream type, can only handle 'vids'
441 if (stream_header.fccType != 0x73646976) {
442 printf("AVI => first stream must be video '%s'\n", filename);
446 SDL_assert(stream_header.fccType == 0x73646976);
448 // only handle 'MRLE' encoding
449 if (stream_header.fccHandler != 0x454c524d) {
450 printf("AVI is not MRLE encoded '%s'\n", filename);
455 // no pal changes for you cowboy
456 if (stream_header.dwFlags & AVISF_VIDEO_PALCHANGES) {
457 printf("AVI cannot have palette changes '%s'\n", filename);
462 // next stream sub-chunk -------------------------------
465 fread(&tmp, 1, 4, pfile);
466 tmp = INTEL_INT(tmp);
467 SDL_assert(tmp == 0x66727473);
470 fread(&tmp, 1, 4, pfile);
472 fread(&bitmap_header.bmiHeader.biSize, 1, sizeof(int), pfile);
473 fread(&bitmap_header.bmiHeader.biWidth, 1, sizeof(int), pfile);
474 fread(&bitmap_header.bmiHeader.biHeight, 1, sizeof(int), pfile);
475 fread(&bitmap_header.bmiHeader.biPlanes, 1, sizeof(short), pfile);
476 fread(&bitmap_header.bmiHeader.biBitCount, 1, sizeof(short), pfile);
477 fread(&bitmap_header.bmiHeader.biCompression, 1, sizeof(int), pfile);
478 fread(&bitmap_header.bmiHeader.biSizeImage, 1, sizeof(int), pfile);
479 fread(&bitmap_header.bmiHeader.biXPelsPerMeter, 1, sizeof(int), pfile);
480 fread(&bitmap_header.bmiHeader.biYPelsPerMeter, 1, sizeof(int), pfile);
481 fread(&bitmap_header.bmiHeader.biClrUsed, 1, sizeof(int), pfile);
482 fread(&bitmap_header.bmiHeader.biClrImportant, 1, sizeof(int), pfile);
484 bitmap_header.bmiHeader.biSize = INTEL_INT(bitmap_header.bmiHeader.biSize);
485 bitmap_header.bmiHeader.biWidth = INTEL_INT(bitmap_header.bmiHeader.biWidth);
486 bitmap_header.bmiHeader.biHeight = INTEL_INT(bitmap_header.bmiHeader.biHeight);
487 bitmap_header.bmiHeader.biPlanes = INTEL_SHORT(bitmap_header.bmiHeader.biPlanes);
488 bitmap_header.bmiHeader.biBitCount = INTEL_SHORT(bitmap_header.bmiHeader.biBitCount);
489 bitmap_header.bmiHeader.biCompression = INTEL_INT(bitmap_header.bmiHeader.biCompression);
490 bitmap_header.bmiHeader.biSizeImage = INTEL_INT(bitmap_header.bmiHeader.biSizeImage);
491 bitmap_header.bmiHeader.biXPelsPerMeter = INTEL_INT(bitmap_header.bmiHeader.biXPelsPerMeter);
492 bitmap_header.bmiHeader.biYPelsPerMeter = INTEL_INT(bitmap_header.bmiHeader.biYPelsPerMeter);
493 bitmap_header.bmiHeader.biClrUsed = INTEL_INT(bitmap_header.bmiHeader.biClrUsed);
494 bitmap_header.bmiHeader.biClrImportant = INTEL_INT(bitmap_header.bmiHeader.biClrImportant);
496 // verify bpp is 8 and compression is RLE8
497 if ( (bitmap_header.bmiHeader.biBitCount != 8) || (bitmap_header.bmiHeader.biCompression != BI_RLE8) ) {
498 printf("AVI has wrong bpp or compression '%s'\n", filename);
504 for (int i = 0; i < 256; i++) {
505 fread(&bitmap_header.bmiColors[i].b, 1, 1, pfile);
506 fread(&bitmap_header.bmiColors[i].g, 1, 1, pfile);
507 fread(&bitmap_header.bmiColors[i].r, 1, 1, pfile);
508 fread(&bitmap_header.bmiColors[i].p, 1, 1, pfile);
511 // reset next_chunk back to main 'LIST'
512 next_chunk = offset_tmp;
519 // need this for later
520 movi_offset = ftell(pfile);
522 // requiring an index, so don't mess with this anymore
528 printf("sub-something else: 0x%x\n", tag);
537 SDL_assert(AVI_stream.frame_index == NULL);
539 AVI_stream.frame_index = (FRAMEINDEX*) malloc(sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
540 SDL_assert(AVI_stream.frame_index != NULL);
542 memset(AVI_stream.frame_index, 0, sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
544 unsigned int c_id, c_flags, c_offset, c_size, i = 0;
546 while ( ftell(pfile) < next_chunk ) {
547 fread(&c_id, 1, sizeof(int), pfile);
548 fread(&c_flags, 1, sizeof(int), pfile);
549 fread(&c_offset, 1, sizeof(int), pfile);
550 fread(&c_size, 1, sizeof(int), pfile);
552 c_id = INTEL_INT(c_id);
553 c_flags = INTEL_INT(c_flags);
554 c_offset = INTEL_INT(c_offset);
555 c_size = INTEL_INT(c_size);
557 // only interested in stream 0, compressed data: '00dc'
558 if (c_id == 0x63643030) {
559 SDL_assert(i < avi_header.dwTotalFrames);
561 AVI_stream.frame_index[i].offset = c_offset;
562 AVI_stream.frame_index[i].size = c_size;
567 // if we didn't get good data then clear it out
568 if (i != avi_header.dwTotalFrames) {
569 free(AVI_stream.frame_index);
570 AVI_stream.frame_index = NULL;
576 // drop everything else
581 fseek(pfile, next_chunk, SEEK_SET);
584 // make sure we have a frame index
585 if (AVI_stream.frame_index == NULL) {
586 printf("AVI => No valid frame index found '%s'\n", filename);
591 // fix up frame_index (assumes frame_index[0] is first frame in stream)
592 unsigned int base_offset = 0;
594 if (AVI_stream.frame_index[0].offset > movi_offset) {
596 } else if (AVI_stream.frame_index[0].offset == movi_offset) {
598 } else if (AVI_stream.frame_index[0].offset == 0) {
599 base_offset = movi_offset + 4;
600 } else if (AVI_stream.frame_index[0].offset == 4) {
601 base_offset = movi_offset;
606 for (unsigned int i = 0; i < avi_header.dwTotalFrames; i++) {
607 AVI_stream.frame_index[i].offset += base_offset;
609 // maybe fix up size too
610 if (avi_header.dwSuggestedBufferSize < AVI_stream.frame_index[i].size) {
611 avi_header.dwSuggestedBufferSize = AVI_stream.frame_index[i].size;
615 // now set up AVI_stream
617 strcpy(AVI_stream.filename, filename);
618 AVI_stream.pfile = pfile;
620 AVI_stream.min_compressed_buffer_size = max(avi_header.dwSuggestedBufferSize, stream_header.dwSuggestedBufferSize);
621 SDL_assert(AVI_stream.min_compressed_buffer_size > 0);
623 AVI_stream.w = bitmap_header.bmiHeader.biWidth;
624 AVI_stream.h = bitmap_header.bmiHeader.biHeight;
625 AVI_stream.bpp = bitmap_header.bmiHeader.biBitCount;
627 // store the number of frames in the AVI_info[] structure
628 AVI_stream.num_frames = avi_header.dwTotalFrames;
630 // Store the palette in the AVI stream structure
631 for (int i = 0; i < 256; i++) {
632 AVI_stream.palette[i*3] = bitmap_header.bmiColors[i].r;
633 AVI_stream.palette[i*3+1] = bitmap_header.bmiColors[i].g;
634 AVI_stream.palette[i*3+2] = bitmap_header.bmiColors[i].b;
637 // set the flag to used, so to make sure we only process one AVI stream at a time
638 AVI_stream.flags |= AVI_STREAM_F_USED;
644 // AVI_stream_close() should be called when you are finished reading all the frames of an AVI
646 void AVI_stream_close()
648 // SDL_assert( AVI_stream.flags & AVI_STREAM_F_USED);
650 if (AVI_stream.pfile) {
651 fclose(AVI_stream.pfile);
652 AVI_stream.pfile = NULL;
655 if (AVI_stream.frame_index) {
656 free(AVI_stream.frame_index);
657 AVI_stream.frame_index = NULL;
660 AVI_stream.flags &= ~AVI_STREAM_F_USED; // clear the used flag
662 AVI_stream_inited = 0;
668 // AVI_stream_get_next_frame() will take the next RLE'd AVI frame and return the
669 // uncompressed data in the buffer pointer supplied as a parameter. The caller is
670 // responsible for allocating the memory before-hand (the memory required is easily
671 // calculated by looking at the w and h members in AVI_stream).
673 // returns: 0 ==> success
676 int AVI_stream_get_frame(ubyte* buffer, int frame_number)
678 if ( frame_number > AVI_stream.num_frames ) {
683 SDL_assert( (frame_number - 1) >= 0 );
685 ubyte* compressed_frame = (ubyte*)malloc(AVI_stream.min_compressed_buffer_size);
686 SDL_assert( compressed_frame != NULL );
687 memset(compressed_frame, 0, AVI_stream.min_compressed_buffer_size);
689 fseek(AVI_stream.pfile, AVI_stream.frame_index[frame_number-1].offset, SEEK_SET);
690 fread(compressed_frame, 1, AVI_stream.frame_index[frame_number-1].size, AVI_stream.pfile);
692 AVI_decompress_RLE8(compressed_frame, buffer, AVI_stream.w, AVI_stream.h);
694 free( compressed_frame );
701 // -------------------------------------------------------------------------------------------------
702 // AVI_decompress_RLE8() will decompress the data pointed to by src, and store in dest.
704 // NOTE: 1. memory for dest must be already allocated before calling function
707 void AVI_decompress_RLE8(ubyte* src, ubyte* dest, int w, int h)
713 SDL_assert( src != NULL);
714 SDL_assert( dest != NULL);
720 ubyte control_code = 0;
724 int size_src = w * h + 1;
727 int height_offset = scan_line * w;
729 while ( src_index < size_src ) {
731 count = src[src_index];
733 if ( count == 0 ) { // control code follows
735 control_code = src[src_index];
736 if ( control_code == 1 ) {
738 // nprintf(("AVI","AVI ==> Reached end of compressed image\n"));
741 else if ( control_code == 0 ) {
744 height_offset = scan_line * w; // only need to calc once per scanline
746 if (height_offset < 0) {
749 //nprintf(("AVI","AVI ==> Reached end of line in compressed image\n"));
751 else if ( control_code == 2 ) {
752 // delta - horizontal and veritcal offsets
754 x_off = src[src_index];
759 if (dest_index >= w) {
765 y_off = src[src_index];
769 height_offset = scan_line * w;
771 if (height_offset < 0) {
781 //SDL_assert( (height_offset + dest_index) < (AVI_stream.w * AVI_stream.h) );
782 for ( i = 0; i < control_code; i++ ) {
783 if (dest_index >= w) {
786 dest[height_offset + dest_index] = src[src_index];
790 // run must end on a word boundry
791 if ( control_code & 1 )
797 run = src[src_index];
799 // nprintf(("AVI","AVI ==> Got %d pixel run of %d\n", src[src_index], count));
800 //SDL_assert( (height_offset + dest_index + count) <= (w * h) );
801 //memset(&dest[height_offset+dest_index], run, count);
802 //dest_index += count;
803 for ( i = 0; i < count; i++ ) {
804 if (dest_index >= w) {
807 dest[height_offset + dest_index] = run;
816 int save_anim_header()
818 int i, new_format_id = 0;
822 anim_fp = fopen(anim_save_filename, "r+b");
824 if (!fwrite(&new_format_id, 2, 1, anim_fp))
826 if (!fwrite(&Anim.version, 2, 1, anim_fp))
828 if (!fwrite(&Anim.fps, 2, 1, anim_fp))
830 if (!fwrite(&Anim.xparent_r, 1, 1, anim_fp))
832 if (!fwrite(&Anim.xparent_g, 1, 1, anim_fp))
834 if (!fwrite(&Anim.xparent_b, 1, 1, anim_fp))
836 if (!fwrite(&Anim.width, 2, 1, anim_fp))
838 if (!fwrite(&Anim.height, 2, 1, anim_fp))
840 if (!fwrite(&Anim.total_frames, 2, 1, anim_fp))
842 if (!fwrite(&Anim.packer_code, 1, 1, anim_fp))
844 if (fwrite(&Anim.palette, 3, 256, anim_fp) != 256)
846 if (!fwrite(&total_key_frames, 2, 1, anim_fp))
849 for (i=0; i<Anim.num_keys; i++) {
850 if (!fwrite(&Anim.keys[i].frame_num, 2, 1, anim_fp))
853 if (!fwrite(&Anim.keys[i].offset, 4, 1, anim_fp))
857 if (!fwrite(&anim_offset, 4, 1, anim_fp))
863 // This function allocates a linked list of key frame headers.
864 // It is responsible for determining which frames in an anim
865 // should be key frames.
866 int allocate_key_frames(int total_frames)
868 int count = 0, frame = 1, rate = key_frame_rate, last_frame;
873 while (frame <= total_frames) {
878 if (force_key_frame >= 0)
882 Anim.keys = (key_frame *) malloc(count * sizeof(key_frame));
885 frame = last_frame = 1;
886 while (frame <= total_frames) {
887 if ((force_key_frame > last_frame) && (force_key_frame < frame))
888 Anim.keys[count++].frame_num = force_key_frame;
890 Anim.keys[count++].frame_num = frame;
894 if (force_key_frame > last_frame)
895 Anim.keys[count++].frame_num = force_key_frame;
897 Anim.num_keys = count;
898 return count; // number of key frames
901 int anim_save_init(char *file, int width, int height, int frames)
904 anim_save_filename = file;
905 anim_fp = fopen(file, "wb");
909 Anim.version = ANIM_VERSION;
910 Anim.fps = Default_fps;
912 Anim.height = height;
913 Anim.packer_code = PACKER_CODE;
914 Anim.xparent_r = Xparent_color.r;
915 Anim.xparent_g = Xparent_color.g;
916 Anim.xparent_b = Xparent_color.b;
917 Anim.total_frames = frames;
920 total_key_frames = allocate_key_frames(frames);
921 fseek(anim_fp, ANIM_HEADER_SIZE + total_key_frames * 6, SEEK_SET);
923 switch ( Compression_type ) {
924 case CUSTOM_DELTA_RLE:
925 Key_frame_compression = PACKING_METHOD_RLE_KEY;
926 Regular_frame_compression = PACKING_METHOD_RLE;
930 Key_frame_compression = PACKING_METHOD_STD_RLE_KEY;
931 Regular_frame_compression = PACKING_METHOD_STD_RLE;
943 int anim_save_frame()
947 key_frame *keyp = NULL;
951 SDL_assert(cur_frame_num <= Anim.total_frames);
953 for (i=0; i<Anim.num_keys; i++)
954 if (Anim.keys[i].frame_num == cur_frame_num) {
955 keyp = &Anim.keys[i];
960 fprintf(stdout, "*");
962 keyp->offset = anim_offset;
963 size = pack_key_frame(cur_frame, anim_buffer, Anim.width * Anim.height, ANIM_BUFFER_MAX, Key_frame_compression);
966 fprintf(stdout, ".");
968 size = pack_frame(cur_frame, last_frame, anim_buffer, Anim.width * Anim.height, ANIM_BUFFER_MAX, Regular_frame_compression);
974 if ((int) fwrite(anim_buffer, 1, size, anim_fp) != size)
979 cur_frame = last_frame;
985 // converts an avi file to an anim file
987 // returns: 0 ==> success
990 int convert_avi_to_anim(char* filename)
992 char ani_filename[255];
994 int rc, i, xparent_pal_index;
995 int avi_stream_opened = 0;
997 rc = AVI_stream_open(filename);
999 // could not open the AVI stream
1002 avi_stream_opened = 1;
1004 SDL_assert(AVI_stream.bpp == 8);
1005 cur_frame = (ubyte*) malloc(AVI_stream.w * AVI_stream.h);
1006 last_frame = (ubyte*) malloc(AVI_stream.w * AVI_stream.h);
1007 SDL_assert(cur_frame && last_frame);
1009 strcpy(ani_filename, AVI_stream.filename);
1010 strcpy(ani_filename + strlen(ani_filename) - 3, "ani");
1012 memset(&Anim, 0, sizeof(anim));
1014 memcpy(Anim.palette, AVI_stream.palette, 768);
1016 if (Use_custom_xparent_color) {
1017 // Need to look at pixel in top-left
1018 rc = AVI_stream_get_frame(cur_frame, 1);
1022 xparent_pal_index = cur_frame[0];
1023 Xparent_color.r = Anim.palette[xparent_pal_index * 3];
1024 Xparent_color.g = Anim.palette[xparent_pal_index * 3 + 1];
1025 Xparent_color.b = Anim.palette[xparent_pal_index * 3 + 2];
1028 Xparent_color.r = 0;
1029 Xparent_color.g = 255;
1030 Xparent_color.b = 0;
1033 rc = anim_save_init(ani_filename, AVI_stream.w, AVI_stream.h, AVI_stream.num_frames);
1037 for ( i=1; i <= AVI_stream.num_frames; i++ ) {
1038 // get uncompressed frame from the AVI
1039 rc = AVI_stream_get_frame(cur_frame, i);
1043 // pass to the anim compression
1044 rc = anim_save_frame();
1049 rc = save_anim_header();
1056 // done with the AVI.. close the stream
1057 if ( avi_stream_opened )
1068 fprintf(stdout,"\n");
1073 int convert_frames_to_anim(char *filename)
1075 int first_frame, frame, pos, width, height, xparent_pal_index, r = -1;
1076 char ani_filename[255], name[255], temp[8];
1080 SDL_assert(strlen(filename) < 254);
1081 strcpy(name, filename);
1082 strcpy(ani_filename, filename);
1083 strcpy(ani_filename + strlen(ani_filename) - 8, ".ani");
1084 pos = strlen(name) - 8;
1085 frame = first_frame = atoi(&name[pos]);
1086 force_key_frame -= frame;
1088 memset(&Anim, 0, sizeof(anim));
1091 fp = fopen(name, "rb");
1096 sprintf(temp, "%04d", frame);
1097 strncpy(&name[pos], temp, 4);
1100 fp = fopen(name, "rb");
1101 } while(fp != NULL);
1104 rc = pcx_read_header(filename, &width, &height, NULL);
1105 if (rc != PCX_ERROR_NONE) {
1106 fprintf(stdout, "An error reading the PCX file %s. It may not exist.\n", filename);
1110 cur_frame = (ubyte *) malloc(width * height);
1111 last_frame = (ubyte *) malloc(width * height);
1113 rc = pcx_read_bitmap_8bpp(filename, cur_frame, Anim.palette);
1114 if (rc != PCX_ERROR_NONE) {
1115 fprintf(stdout, "An error reading the PCX file %s. It may not exist.\n", filename);
1119 if (Use_custom_xparent_color) {
1120 // Need to look at pixel in top-left
1121 xparent_pal_index = ((ubyte *) cur_frame)[0];
1122 Xparent_color.r = Anim.palette[xparent_pal_index * 3];
1123 Xparent_color.g = Anim.palette[xparent_pal_index * 3 + 1];
1124 Xparent_color.b = Anim.palette[xparent_pal_index * 3 + 2];
1127 Xparent_color.r = 0;
1128 Xparent_color.g = 255;
1129 Xparent_color.b = 0;
1132 if (anim_save_init(ani_filename, width, height, frame - first_frame))
1135 while (first_frame < frame) {
1136 sprintf(temp, "%04d", first_frame);
1137 strncpy(&name[pos], temp, 4);
1138 rc = pcx_read_bitmap_8bpp(name, cur_frame, Anim.palette);
1139 if (rc != PCX_ERROR_NONE)
1142 if (anim_save_frame())
1148 if (save_anim_header())
1160 fprintf(stdout, "\n");