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;
275 unsigned int s_tag, tmp;
276 AVIMAINHEADER avi_header;
277 AVISTREAMHEADER stream_header;
278 BITMAPINFO bitmap_header;
279 long file_size = 0, movi_offset = 0, next_chunk;
281 SDL_assert( !(AVI_stream.flags & AVI_STREAM_F_USED) );
283 SDL_zero(avi_header);
284 SDL_zero(stream_header);
285 SDL_zero(bitmap_header);
287 pfile = fopen(filename, "rb");
290 printf("AVI ==> Unable to open %s", filename);
295 fseek(pfile, 0, SEEK_END);
296 file_size = ftell(pfile);
297 fseek(pfile, 0, SEEK_SET);
299 // check for valid file type
300 fread(&id, 1, 4, pfile);
304 if (id != 0x46464952) {
305 printf("Not a RIFF file '%s'\n", filename);
311 fread(&id, 1, 4, pfile);
313 // check for valid RIFF type
314 fread(&id, 1, 4, pfile);
318 if (id != 0x20495641) {
319 printf("Not an AVI file '%s'\n", filename);
324 // used for main 'LIST' chunks
328 while ( ftell(pfile) < file_size ) {
329 fread(&tag, 1, 4, pfile);
330 fread(&size, 1, 4, pfile);
332 tag = INTEL_INT(tag);
333 size = INTEL_INT(size);
335 next_chunk = ftell(pfile) + size;
341 fread(&s_tag, 1, 4, pfile);
342 s_tag = INTEL_INT(s_tag);
347 fread(&avi_header.fcc, 1, sizeof(int), pfile);
348 fread(&avi_header.cb, 1, sizeof(int), pfile);
349 fread(&avi_header.dwMicroSecPerFrame, 1, sizeof(int), pfile);
350 fread(&avi_header.dwMaxBytesPerSec, 1, sizeof(int), pfile);
351 fread(&avi_header.dwPaddingGranularity, 1, sizeof(int), pfile);
352 fread(&avi_header.dwFlags, 1, sizeof(int), pfile);
353 fread(&avi_header.dwTotalFrames, 1, sizeof(int), pfile);
354 fread(&avi_header.dwInitialFrames, 1, sizeof(int), pfile);
355 fread(&avi_header.dwStreams, 1, sizeof(int), pfile);
356 fread(&avi_header.dwSuggestedBufferSize, 1, sizeof(int), pfile);
357 fread(&avi_header.dwWidth, 1, sizeof(int), pfile);
358 fread(&avi_header.dwHeight, 1, sizeof(int), pfile);
359 fread(&avi_header.dwReserved, 1, sizeof(avi_header.dwReserved), pfile);
361 avi_header.fcc = INTEL_INT(avi_header.fcc);
362 avi_header.cb = INTEL_INT(avi_header.cb);
363 avi_header.dwMicroSecPerFrame = INTEL_INT(avi_header.dwMicroSecPerFrame);
364 avi_header.dwMaxBytesPerSec = INTEL_INT(avi_header.dwMaxBytesPerSec);
365 avi_header.dwPaddingGranularity = INTEL_INT(avi_header.dwPaddingGranularity);
366 avi_header.dwFlags = INTEL_INT(avi_header.dwFlags);
367 avi_header.dwTotalFrames = INTEL_INT(avi_header.dwTotalFrames);
368 avi_header.dwInitialFrames = INTEL_INT(avi_header.dwInitialFrames);
369 avi_header.dwStreams = INTEL_INT(avi_header.dwStreams);
370 avi_header.dwSuggestedBufferSize = INTEL_INT(avi_header.dwSuggestedBufferSize);
371 avi_header.dwWidth = INTEL_INT(avi_header.dwWidth);
372 avi_header.dwHeight = INTEL_INT(avi_header.dwHeight);
375 SDL_assert(avi_header.fcc == 0x68697661);
377 // we're stupid, can only handle a single stream
378 if (avi_header.dwStreams != 1) {
379 printf("AVI has more than one stream '%s'\n", filename);
384 // require index chunk (should be flagged as availble)
385 if ( !(avi_header.dwFlags & AVIF_HASINDEX) ) {
386 printf("AVI does not have index '%s'\n", filename);
391 // update next_chunk offset for sub-chunk
392 offset_tmp = next_chunk;
393 next_chunk = ftell(pfile);
398 // 'strl' - subchunk of 'hdrl'
400 fread(&stream_header.fcc, 1, sizeof(int), pfile);
401 fread(&stream_header.cb, 1, sizeof(int), pfile);
402 fread(&stream_header.fccType, 1, sizeof(int), pfile);
403 fread(&stream_header.fccHandler, 1, sizeof(int), pfile);
404 fread(&stream_header.dwFlags, 1, sizeof(int), pfile);
405 fread(&stream_header.wPriority, 1, sizeof(short), pfile);
406 fread(&stream_header.wLanguage, 1, sizeof(short), pfile);
407 fread(&stream_header.dwInitialFrames, 1, sizeof(int), pfile);
408 fread(&stream_header.dwScale, 1, sizeof(int), pfile);
409 fread(&stream_header.dwRate, 1, sizeof(int), pfile);
410 fread(&stream_header.dwStart, 1, sizeof(int), pfile);
411 fread(&stream_header.dwLength, 1, sizeof(int), pfile);
412 fread(&stream_header.dwSuggestedBufferSize, 1, sizeof(int), pfile);
413 fread(&stream_header.dwQuality, 1, sizeof(int), pfile);
414 fread(&stream_header.dwSampleSize, 1, sizeof(int), pfile);
415 fread(&stream_header.rcFrame.left, 1, sizeof(short), pfile);
416 fread(&stream_header.rcFrame.top, 1, sizeof(short), pfile);
417 fread(&stream_header.rcFrame.right, 1, sizeof(short), pfile);
418 fread(&stream_header.rcFrame.bottom, 1, sizeof(short), pfile);
420 stream_header.fcc = INTEL_INT(stream_header.fcc);
421 stream_header.cb = INTEL_INT(stream_header.cb);
422 stream_header.fccType = INTEL_INT(stream_header.fccType);
423 stream_header.fccHandler = INTEL_INT(stream_header.fccHandler);
424 stream_header.dwFlags = INTEL_INT(stream_header.dwFlags);
425 stream_header.wPriority = INTEL_SHORT(stream_header.wPriority);
426 stream_header.wLanguage = INTEL_SHORT(stream_header.wLanguage);
427 stream_header.dwInitialFrames = INTEL_INT(stream_header.dwInitialFrames);
428 stream_header.dwScale = INTEL_INT(stream_header.dwScale);
429 stream_header.dwRate = INTEL_INT(stream_header.dwRate);
430 stream_header.dwStart = INTEL_INT(stream_header.dwStart);
431 stream_header.dwLength = INTEL_INT(stream_header.dwLength);
432 stream_header.dwSuggestedBufferSize = INTEL_INT(stream_header.dwSuggestedBufferSize);
433 stream_header.dwQuality = INTEL_INT(stream_header.dwQuality);
434 stream_header.dwSampleSize = INTEL_INT(stream_header.dwSampleSize);
435 stream_header.rcFrame.left = INTEL_SHORT(stream_header.rcFrame.left);
436 stream_header.rcFrame.top = INTEL_SHORT(stream_header.rcFrame.top);
437 stream_header.rcFrame.right = INTEL_SHORT(stream_header.rcFrame.right);
438 stream_header.rcFrame.bottom = INTEL_SHORT(stream_header.rcFrame.bottom);
441 SDL_assert(stream_header.fcc == 0x68727473);
443 // check stream type, can only handle 'vids'
444 if (stream_header.fccType != 0x73646976) {
445 printf("AVI => first stream must be video '%s'\n", filename);
449 SDL_assert(stream_header.fccType == 0x73646976);
451 // only handle 'MRLE' encoding
452 if (stream_header.fccHandler != 0x454c524d) {
453 printf("AVI is not MRLE encoded '%s'\n", filename);
458 // no pal changes for you cowboy
459 if (stream_header.dwFlags & AVISF_VIDEO_PALCHANGES) {
460 printf("AVI cannot have palette changes '%s'\n", filename);
465 // next stream sub-chunk -------------------------------
468 fread(&tmp, 1, 4, pfile);
469 tmp = INTEL_INT(tmp);
470 SDL_assert(tmp == 0x66727473);
473 fread(&tmp, 1, 4, pfile);
475 fread(&bitmap_header.bmiHeader.biSize, 1, sizeof(int), pfile);
476 fread(&bitmap_header.bmiHeader.biWidth, 1, sizeof(int), pfile);
477 fread(&bitmap_header.bmiHeader.biHeight, 1, sizeof(int), pfile);
478 fread(&bitmap_header.bmiHeader.biPlanes, 1, sizeof(short), pfile);
479 fread(&bitmap_header.bmiHeader.biBitCount, 1, sizeof(short), pfile);
480 fread(&bitmap_header.bmiHeader.biCompression, 1, sizeof(int), pfile);
481 fread(&bitmap_header.bmiHeader.biSizeImage, 1, sizeof(int), pfile);
482 fread(&bitmap_header.bmiHeader.biXPelsPerMeter, 1, sizeof(int), pfile);
483 fread(&bitmap_header.bmiHeader.biYPelsPerMeter, 1, sizeof(int), pfile);
484 fread(&bitmap_header.bmiHeader.biClrUsed, 1, sizeof(int), pfile);
485 fread(&bitmap_header.bmiHeader.biClrImportant, 1, sizeof(int), pfile);
487 bitmap_header.bmiHeader.biSize = INTEL_INT(bitmap_header.bmiHeader.biSize);
488 bitmap_header.bmiHeader.biWidth = INTEL_INT(bitmap_header.bmiHeader.biWidth);
489 bitmap_header.bmiHeader.biHeight = INTEL_INT(bitmap_header.bmiHeader.biHeight);
490 bitmap_header.bmiHeader.biPlanes = INTEL_SHORT(bitmap_header.bmiHeader.biPlanes);
491 bitmap_header.bmiHeader.biBitCount = INTEL_SHORT(bitmap_header.bmiHeader.biBitCount);
492 bitmap_header.bmiHeader.biCompression = INTEL_INT(bitmap_header.bmiHeader.biCompression);
493 bitmap_header.bmiHeader.biSizeImage = INTEL_INT(bitmap_header.bmiHeader.biSizeImage);
494 bitmap_header.bmiHeader.biXPelsPerMeter = INTEL_INT(bitmap_header.bmiHeader.biXPelsPerMeter);
495 bitmap_header.bmiHeader.biYPelsPerMeter = INTEL_INT(bitmap_header.bmiHeader.biYPelsPerMeter);
496 bitmap_header.bmiHeader.biClrUsed = INTEL_INT(bitmap_header.bmiHeader.biClrUsed);
497 bitmap_header.bmiHeader.biClrImportant = INTEL_INT(bitmap_header.bmiHeader.biClrImportant);
499 // verify bpp is 8 and compression is RLE8
500 if ( (bitmap_header.bmiHeader.biBitCount != 8) || (bitmap_header.bmiHeader.biCompression != BI_RLE8) ) {
501 printf("AVI has wrong bpp or compression '%s'\n", filename);
507 for (int i = 0; i < 256; i++) {
508 fread(&bitmap_header.bmiColors[i].b, 1, 1, pfile);
509 fread(&bitmap_header.bmiColors[i].g, 1, 1, pfile);
510 fread(&bitmap_header.bmiColors[i].r, 1, 1, pfile);
511 fread(&bitmap_header.bmiColors[i].p, 1, 1, pfile);
514 // reset next_chunk back to main 'LIST'
515 next_chunk = offset_tmp;
522 // need this for later
523 movi_offset = ftell(pfile);
525 // requiring an index, so don't mess with this anymore
531 printf("sub-something else: 0x%x\n", tag);
540 SDL_assert(AVI_stream.frame_index == NULL);
542 AVI_stream.frame_index = (FRAMEINDEX*) malloc(sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
543 SDL_assert(AVI_stream.frame_index != NULL);
545 memset(AVI_stream.frame_index, 0, sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
547 unsigned int c_id, c_flags, c_offset, c_size, i = 0;
549 while ( ftell(pfile) < next_chunk ) {
550 fread(&c_id, 1, sizeof(int), pfile);
551 fread(&c_flags, 1, sizeof(int), pfile);
552 fread(&c_offset, 1, sizeof(int), pfile);
553 fread(&c_size, 1, sizeof(int), pfile);
555 c_id = INTEL_INT(c_id);
556 c_flags = INTEL_INT(c_flags);
557 c_offset = INTEL_INT(c_offset);
558 c_size = INTEL_INT(c_size);
560 // only interested in stream 0, compressed data: '00dc'
561 if (c_id == 0x63643030) {
562 SDL_assert(i < avi_header.dwTotalFrames);
564 AVI_stream.frame_index[i].offset = c_offset;
565 AVI_stream.frame_index[i].size = c_size;
570 // if we didn't get good data then clear it out
571 if (i != avi_header.dwTotalFrames) {
572 free(AVI_stream.frame_index);
573 AVI_stream.frame_index = NULL;
579 // drop everything else
584 fseek(pfile, next_chunk, SEEK_SET);
587 // make sure we have a frame index
588 if (AVI_stream.frame_index == NULL) {
589 printf("AVI => No valid frame index found '%s'\n", filename);
594 // fix up frame_index (assumes frame_index[0] is first frame in stream)
595 long base_offset = 0;
597 if (AVI_stream.frame_index[0].offset > (unsigned int)movi_offset) {
599 } else if (AVI_stream.frame_index[0].offset == (unsigned int)movi_offset) {
601 } else if (AVI_stream.frame_index[0].offset == 0) {
602 base_offset = movi_offset + 4;
603 } else if (AVI_stream.frame_index[0].offset == 4) {
604 base_offset = movi_offset;
609 for (unsigned int i = 0; i < avi_header.dwTotalFrames; i++) {
610 AVI_stream.frame_index[i].offset += base_offset;
612 // maybe fix up size too
613 if (avi_header.dwSuggestedBufferSize < AVI_stream.frame_index[i].size) {
614 avi_header.dwSuggestedBufferSize = AVI_stream.frame_index[i].size;
618 // now set up AVI_stream
620 strcpy(AVI_stream.filename, filename);
621 AVI_stream.pfile = pfile;
623 AVI_stream.min_compressed_buffer_size = max(avi_header.dwSuggestedBufferSize, stream_header.dwSuggestedBufferSize);
624 SDL_assert(AVI_stream.min_compressed_buffer_size > 0);
626 AVI_stream.w = bitmap_header.bmiHeader.biWidth;
627 AVI_stream.h = bitmap_header.bmiHeader.biHeight;
628 AVI_stream.bpp = bitmap_header.bmiHeader.biBitCount;
630 // store the number of frames in the AVI_info[] structure
631 AVI_stream.num_frames = avi_header.dwTotalFrames;
633 // Store the palette in the AVI stream structure
634 for (int i = 0; i < 256; i++) {
635 AVI_stream.palette[i*3] = bitmap_header.bmiColors[i].r;
636 AVI_stream.palette[i*3+1] = bitmap_header.bmiColors[i].g;
637 AVI_stream.palette[i*3+2] = bitmap_header.bmiColors[i].b;
640 // set the flag to used, so to make sure we only process one AVI stream at a time
641 AVI_stream.flags |= AVI_STREAM_F_USED;
647 // AVI_stream_close() should be called when you are finished reading all the frames of an AVI
649 void AVI_stream_close()
651 // SDL_assert( AVI_stream.flags & AVI_STREAM_F_USED);
653 if (AVI_stream.pfile) {
654 fclose(AVI_stream.pfile);
655 AVI_stream.pfile = NULL;
658 if (AVI_stream.frame_index) {
659 free(AVI_stream.frame_index);
660 AVI_stream.frame_index = NULL;
663 AVI_stream.flags &= ~AVI_STREAM_F_USED; // clear the used flag
665 AVI_stream_inited = 0;
671 // AVI_stream_get_next_frame() will take the next RLE'd AVI frame and return the
672 // uncompressed data in the buffer pointer supplied as a parameter. The caller is
673 // responsible for allocating the memory before-hand (the memory required is easily
674 // calculated by looking at the w and h members in AVI_stream).
676 // returns: 0 ==> success
679 int AVI_stream_get_frame(ubyte* buffer, int frame_number)
681 if ( frame_number > AVI_stream.num_frames ) {
686 SDL_assert( (frame_number - 1) >= 0 );
688 ubyte* compressed_frame = (ubyte*)malloc(AVI_stream.min_compressed_buffer_size);
689 SDL_assert( compressed_frame != NULL );
690 memset(compressed_frame, 0, AVI_stream.min_compressed_buffer_size);
692 fseek(AVI_stream.pfile, AVI_stream.frame_index[frame_number-1].offset, SEEK_SET);
693 fread(compressed_frame, 1, AVI_stream.frame_index[frame_number-1].size, AVI_stream.pfile);
695 AVI_decompress_RLE8(compressed_frame, buffer, AVI_stream.w, AVI_stream.h);
697 free( compressed_frame );
704 // -------------------------------------------------------------------------------------------------
705 // AVI_decompress_RLE8() will decompress the data pointed to by src, and store in dest.
707 // NOTE: 1. memory for dest must be already allocated before calling function
710 void AVI_decompress_RLE8(ubyte* src, ubyte* dest, int w, int h)
716 SDL_assert( src != NULL);
717 SDL_assert( dest != NULL);
723 ubyte control_code = 0;
727 int size_src = w * h + 1;
730 int height_offset = scan_line * w;
732 while ( src_index < size_src ) {
734 count = src[src_index];
736 if ( count == 0 ) { // control code follows
738 control_code = src[src_index];
739 if ( control_code == 1 ) {
741 // nprintf(("AVI","AVI ==> Reached end of compressed image\n"));
744 else if ( control_code == 0 ) {
747 height_offset = scan_line * w; // only need to calc once per scanline
749 if (height_offset < 0) {
752 //nprintf(("AVI","AVI ==> Reached end of line in compressed image\n"));
754 else if ( control_code == 2 ) {
755 // delta - horizontal and veritcal offsets
757 x_off = src[src_index];
762 if (dest_index >= w) {
768 y_off = src[src_index];
772 height_offset = scan_line * w;
774 if (height_offset < 0) {
784 //SDL_assert( (height_offset + dest_index) < (AVI_stream.w * AVI_stream.h) );
785 for ( i = 0; i < control_code; i++ ) {
786 if (dest_index >= w) {
789 dest[height_offset + dest_index] = src[src_index];
793 // run must end on a word boundry
794 if ( control_code & 1 )
800 run = src[src_index];
802 // nprintf(("AVI","AVI ==> Got %d pixel run of %d\n", src[src_index], count));
803 //SDL_assert( (height_offset + dest_index + count) <= (w * h) );
804 //memset(&dest[height_offset+dest_index], run, count);
805 //dest_index += count;
806 for ( i = 0; i < count; i++ ) {
807 if (dest_index >= w) {
810 dest[height_offset + dest_index] = run;
819 int save_anim_header()
821 int i, new_format_id = 0;
825 anim_fp = fopen(anim_save_filename, "r+b");
827 if (!fwrite(&new_format_id, 2, 1, anim_fp))
829 if (!fwrite(&Anim.version, 2, 1, anim_fp))
831 if (!fwrite(&Anim.fps, 2, 1, anim_fp))
833 if (!fwrite(&Anim.xparent_r, 1, 1, anim_fp))
835 if (!fwrite(&Anim.xparent_g, 1, 1, anim_fp))
837 if (!fwrite(&Anim.xparent_b, 1, 1, anim_fp))
839 if (!fwrite(&Anim.width, 2, 1, anim_fp))
841 if (!fwrite(&Anim.height, 2, 1, anim_fp))
843 if (!fwrite(&Anim.total_frames, 2, 1, anim_fp))
845 if (!fwrite(&Anim.packer_code, 1, 1, anim_fp))
847 if (fwrite(&Anim.palette, 3, 256, anim_fp) != 256)
849 if (!fwrite(&total_key_frames, 2, 1, anim_fp))
852 for (i=0; i<Anim.num_keys; i++) {
853 if (!fwrite(&Anim.keys[i].frame_num, 2, 1, anim_fp))
856 if (!fwrite(&Anim.keys[i].offset, 4, 1, anim_fp))
860 if (!fwrite(&anim_offset, 4, 1, anim_fp))
866 // This function allocates a linked list of key frame headers.
867 // It is responsible for determining which frames in an anim
868 // should be key frames.
869 int allocate_key_frames(int total_frames)
871 int count = 0, frame = 1, rate = key_frame_rate, last_frame;
876 while (frame <= total_frames) {
881 if (force_key_frame >= 0)
885 Anim.keys = (key_frame *) malloc(count * sizeof(key_frame));
888 frame = last_frame = 1;
889 while (frame <= total_frames) {
890 if ((force_key_frame > last_frame) && (force_key_frame < frame))
891 Anim.keys[count++].frame_num = force_key_frame;
893 Anim.keys[count++].frame_num = frame;
897 if (force_key_frame > last_frame)
898 Anim.keys[count++].frame_num = force_key_frame;
900 Anim.num_keys = count;
901 return count; // number of key frames
904 int anim_save_init(char *file, int width, int height, int frames)
907 anim_save_filename = file;
908 anim_fp = fopen(file, "wb");
912 Anim.version = ANIM_VERSION;
913 Anim.fps = Default_fps;
915 Anim.height = height;
916 Anim.packer_code = PACKER_CODE;
917 Anim.xparent_r = Xparent_color.r;
918 Anim.xparent_g = Xparent_color.g;
919 Anim.xparent_b = Xparent_color.b;
920 Anim.total_frames = frames;
923 total_key_frames = allocate_key_frames(frames);
924 fseek(anim_fp, ANIM_HEADER_SIZE + total_key_frames * 6, SEEK_SET);
926 switch ( Compression_type ) {
927 case CUSTOM_DELTA_RLE:
928 Key_frame_compression = PACKING_METHOD_RLE_KEY;
929 Regular_frame_compression = PACKING_METHOD_RLE;
933 Key_frame_compression = PACKING_METHOD_STD_RLE_KEY;
934 Regular_frame_compression = PACKING_METHOD_STD_RLE;
946 int anim_save_frame()
950 key_frame *keyp = NULL;
954 SDL_assert(cur_frame_num <= Anim.total_frames);
956 for (i=0; i<Anim.num_keys; i++)
957 if (Anim.keys[i].frame_num == cur_frame_num) {
958 keyp = &Anim.keys[i];
963 fprintf(stdout, "*");
965 keyp->offset = anim_offset;
966 size = pack_key_frame(cur_frame, anim_buffer, Anim.width * Anim.height, ANIM_BUFFER_MAX, Key_frame_compression);
969 fprintf(stdout, ".");
971 size = pack_frame(cur_frame, last_frame, anim_buffer, Anim.width * Anim.height, ANIM_BUFFER_MAX, Regular_frame_compression);
977 if ((int) fwrite(anim_buffer, 1, size, anim_fp) != size)
982 cur_frame = last_frame;
988 // converts an avi file to an anim file
990 // returns: 0 ==> success
993 int convert_avi_to_anim(char* filename)
995 char ani_filename[255];
997 int rc, i, xparent_pal_index;
998 int avi_stream_opened = 0;
1000 rc = AVI_stream_open(filename);
1002 // could not open the AVI stream
1005 avi_stream_opened = 1;
1007 SDL_assert(AVI_stream.bpp == 8);
1008 cur_frame = (ubyte*) malloc(AVI_stream.w * AVI_stream.h);
1009 last_frame = (ubyte*) malloc(AVI_stream.w * AVI_stream.h);
1010 SDL_assert(cur_frame && last_frame);
1012 strcpy(ani_filename, AVI_stream.filename);
1013 strcpy(ani_filename + strlen(ani_filename) - 3, "ani");
1015 memset(&Anim, 0, sizeof(anim));
1017 memcpy(Anim.palette, AVI_stream.palette, 768);
1019 if (Use_custom_xparent_color) {
1020 // Need to look at pixel in top-left
1021 rc = AVI_stream_get_frame(cur_frame, 1);
1025 xparent_pal_index = cur_frame[0];
1026 Xparent_color.r = Anim.palette[xparent_pal_index * 3];
1027 Xparent_color.g = Anim.palette[xparent_pal_index * 3 + 1];
1028 Xparent_color.b = Anim.palette[xparent_pal_index * 3 + 2];
1031 Xparent_color.r = 0;
1032 Xparent_color.g = 255;
1033 Xparent_color.b = 0;
1036 rc = anim_save_init(ani_filename, AVI_stream.w, AVI_stream.h, AVI_stream.num_frames);
1040 for ( i=1; i <= AVI_stream.num_frames; i++ ) {
1041 // get uncompressed frame from the AVI
1042 rc = AVI_stream_get_frame(cur_frame, i);
1046 // pass to the anim compression
1047 rc = anim_save_frame();
1052 rc = save_anim_header();
1059 // done with the AVI.. close the stream
1060 if ( avi_stream_opened )
1071 fprintf(stdout,"\n");
1076 int convert_frames_to_anim(char *filename)
1078 int first_frame, frame, pos, width, height, xparent_pal_index, r = -1;
1079 char ani_filename[255], name[255], temp[8];
1083 SDL_assert(strlen(filename) < 254);
1084 strcpy(name, filename);
1085 strcpy(ani_filename, filename);
1086 strcpy(ani_filename + strlen(ani_filename) - 8, ".ani");
1087 pos = strlen(name) - 8;
1088 frame = first_frame = atoi(&name[pos]);
1089 force_key_frame -= frame;
1091 memset(&Anim, 0, sizeof(anim));
1094 fp = fopen(name, "rb");
1099 sprintf(temp, "%04d", frame);
1100 strncpy(&name[pos], temp, 4);
1103 fp = fopen(name, "rb");
1104 } while(fp != NULL);
1107 rc = pcx_read_header(filename, &width, &height, NULL);
1108 if (rc != PCX_ERROR_NONE) {
1109 fprintf(stdout, "An error reading the PCX file %s. It may not exist.\n", filename);
1113 cur_frame = (ubyte *) malloc(width * height);
1114 last_frame = (ubyte *) malloc(width * height);
1116 rc = pcx_read_bitmap_8bpp(filename, cur_frame, Anim.palette);
1117 if (rc != PCX_ERROR_NONE) {
1118 fprintf(stdout, "An error reading the PCX file %s. It may not exist.\n", filename);
1122 if (Use_custom_xparent_color) {
1123 // Need to look at pixel in top-left
1124 xparent_pal_index = ((ubyte *) cur_frame)[0];
1125 Xparent_color.r = Anim.palette[xparent_pal_index * 3];
1126 Xparent_color.g = Anim.palette[xparent_pal_index * 3 + 1];
1127 Xparent_color.b = Anim.palette[xparent_pal_index * 3 + 2];
1130 Xparent_color.r = 0;
1131 Xparent_color.g = 255;
1132 Xparent_color.b = 0;
1135 if (anim_save_init(ani_filename, width, height, frame - first_frame))
1138 while (first_frame < frame) {
1139 sprintf(temp, "%04d", first_frame);
1140 strncpy(&name[pos], temp, 4);
1141 rc = pcx_read_bitmap_8bpp(name, cur_frame, Anim.palette);
1142 if (rc != PCX_ERROR_NONE)
1145 if (anim_save_frame())
1151 if (save_anim_header())
1163 fprintf(stdout, "\n");