]> icculus.org git repositories - taylor/freespace2.git/blob - src/ac/convert.cpp
silence various compiler warnings from MSVC (debug)
[taylor/freespace2.git] / src / ac / convert.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/AC/convert.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
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.
17  *
18  * $Log$
19  * Revision 1.2  2002/06/09 04:41:15  relnev
20  * added copyright header
21  *
22  * Revision 1.1.1.1  2002/05/03 03:28:11  root
23  * Initial import.
24  *
25  * 
26  * 2     10/23/98 6:03p Dave
27  * 
28  * 1     10/23/98 5:34p Dave
29  * 
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
33  * 
34  * 2     10/07/98 10:52a Dave
35  * Initial checkin.
36  * 
37  * 1     10/07/98 10:48a Dave
38  * 
39  * 12    6/23/98 2:52p Hoffoss
40  * Changed code so AC compiles once again.
41  * 
42  * 11    7/20/97 6:57p Lawrance
43  * supporting new RLE format
44  * 
45  * 10    6/25/97 11:57a Lawrance
46  * forgot linefeed
47  * 
48  * 9     6/25/97 11:49a Lawrance
49  * don't assert if PCX read fails, print warning
50  * 
51  * 8     5/21/97 2:26p Lawrance
52  * bug fix: not using correct header size
53  * 
54  * 7     5/21/97 11:06a Lawrance
55  * enabling a user-defined transparent value
56  * 
57  * 6     5/19/97 3:21p Lawrance
58  * add fps parm, version num to anim header
59  * 
60  * 5     2/25/97 5:18p Lawrance
61  * force_key_frame now numbering from 1 for PCXs as well as AVIs
62  * 
63  * 4     2/20/97 1:47p Lawrance
64  * adding dots when making frames
65  * 
66  * 3     2/20/97 10:30a Hoffoss
67  * Added in support for forcing a key frame.
68  * 
69  * 2     2/19/97 9:26p Lawrance
70  * added force_key_frame global
71  * 
72  * 1     2/19/97 7:14p Lawrance
73  * 
74  * 14    2/19/97 4:21p Lawrance
75  * took out unnecessary #include BmpMan.h
76  * 
77  * 13    2/19/97 3:59p Lawrance
78  * using pcxutils to load bitmaps, not bmpman
79  * 
80  * 12    2/17/97 2:59p Lawrance
81  * integrating into game
82  * 
83  * 11    2/17/97 3:02p Hoffoss
84  * Added headers to files, and implemented key frame dialog stuff.
85  * 
86  * 10    2/14/97 10:38p Lawrance
87  * fixing bugs
88  * 
89  * 9     2/14/97 9:47p Hoffoss
90  * fixed bugs.
91  * 
92  * 8     2/14/97 8:44p Lawrance
93  * fixed bug with saving header
94  * 
95  * 7     2/14/97 8:04p Hoffoss
96  * Fixed bug.
97  * 
98  * 6     2/14/97 7:44p Lawrance
99  * fixed some bugs in loading an AVI
100  * 
101  * 5     2/14/97 7:33p Lawrance
102  * added convert_avi_to_anim() function
103  * 
104  * 4     2/14/97 5:38p Hoffoss
105  * Changes to get AnimCoverter project to compile and link.
106  * 
107  * 3     2/14/97 3:28p Hoffoss
108  * Wrote functions to save an anim file.
109  * 
110  * 2     2/13/97 5:55p Lawrance
111  * reading AVI / decompressing AVI functions in
112  *
113  * $NoKeywords: $
114  */
115
116
117 #include "pstypes.h"
118 #include "convert.h"
119 #include "pcxutils.h"
120 #include "animplay.h"
121 #include "packunpack.h"
122
123 #define AVI_STREAM_F_USED       ( 1 << 0 )
124
125 typedef struct _frame_index {
126         unsigned int offset;
127         unsigned int size;
128 } FRAMEINDEX;
129
130 typedef struct AVI_STREAM_TYPE {
131         FILE            *pfile;
132         FRAMEINDEX      *frame_index;
133         int                     num_frames;
134         int                     current_frame;
135         int                     w,h,bpp;
136         int                     min_compressed_buffer_size;
137         ubyte                   palette[768];
138         char                    filename[255];
139         //ubyte                 pal_translation[256];           // palette translation look-up table
140         int             flags;
141 }       AVI_STREAM_TYPE;
142
143 #define AVIF_HASINDEX 0x00000010
144
145 typedef struct _avimainheader {
146         unsigned int fcc;
147         unsigned int cb;
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];
159 } AVIMAINHEADER;
160
161 #define AVISF_VIDEO_PALCHANGES 0x00010000
162
163 typedef struct _avistreamheader {
164         unsigned int fcc;
165         unsigned int cb;
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;
173         unsigned int dwRate;
174         unsigned int dwStart;
175         unsigned int dwLength;
176         unsigned int dwSuggestedBufferSize;
177         unsigned int dwQuality;
178         unsigned int dwSampleSize;
179         struct {
180                 short left;
181                 short top;
182                 short right;
183                 short bottom;
184         } rcFrame;
185 } AVISTREAMHEADER;
186
187 #define BI_RLE8 0x00000001
188
189 typedef struct _bitmapinfo {
190         struct {
191                 unsigned int biSize;
192                 int biWidth;
193                 int biHeight;
194                 unsigned short biPlanes;
195                 unsigned short biBitCount;
196                 unsigned int biCompression;
197                 unsigned int biSizeImage;
198                 int biXPelsPerMeter;
199                 int biYPelsPerMeter;
200                 unsigned int biClrUsed;
201                 unsigned int biClrImportant;
202         } bmiHeader;
203
204         struct {
205                 ubyte b;
206                 ubyte g;
207                 ubyte r;
208                 ubyte p;
209         } bmiColors[256];
210 } BITMAPINFO;
211
212
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);
218
219 // Global to file
220 static          AVI_STREAM_TYPE AVI_stream;     
221 static int      AVI_stream_inited = 0;
222
223 // Globals 
224 char    *anim_save_filename;
225 ubyte *cur_frame, *last_frame;
226 ubyte   anim_buffer[ANIM_BUFFER_MAX];
227 int     key_frame_rate;
228 int     force_key_frame;
229 int     total_key_frames;
230 int     anim_offset;
231 int     cur_frame_num;
232 int     Default_fps;
233 int     Use_custom_xparent_color;
234 rgb_triple      Xparent_color;
235
236 int     Compression_type;               // what kind of RLE compression is going to be used 
237 int     Key_frame_compression, Regular_frame_compression;
238
239 key_frame *first_frame;
240 FILE *anim_fp = NULL;
241 anim Anim;
242
243 // AVI_stream_init() is called only to clear USED flag of the AVI_stream structure
244 // and reset the current frame.
245 //
246 //      This does not need to be called explicity, since it will be called by AVI_stream_open()
247 // anyways.
248 //
249 void AVI_stream_init()
250 {
251         AVI_stream.flags &= ~AVI_STREAM_F_USED;
252         AVI_stream.current_frame = 0;
253         AVI_stream_inited = 1;
254
255         AVI_stream.pfile = NULL;
256         AVI_stream.frame_index = NULL;
257 }
258
259 // AVI_stream_open() will open the AVI file and prepare it for reading, but will not 
260 // store any of the frame data. 
261 //
262 //      returns:   0 ==> success
263 //           !0 ==> could not open the AVI stream
264 //
265 // The filename is expected to be an absolute pathname (or file in the current working directory)
266 //
267 int AVI_stream_open(char* filename)
268 {
269         if ( !AVI_stream_inited )
270                 AVI_stream_init();
271
272         FILE *pfile = NULL;
273         int id = 0;
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;
280
281         SDL_assert( !(AVI_stream.flags & AVI_STREAM_F_USED) );
282
283         SDL_zero(avi_header);
284         SDL_zero(stream_header);
285         SDL_zero(bitmap_header);
286
287         pfile = fopen(filename, "rb");
288
289         if (pfile == NULL) {
290                 printf("AVI ==> Unable to open %s", filename);
291                 return -1;
292         }
293
294         // get file size
295         fseek(pfile, 0, SEEK_END);
296         file_size = ftell(pfile);
297         fseek(pfile, 0, SEEK_SET);
298
299         // check for valid file type
300         fread(&id, 1, 4, pfile);
301         id = INTEL_INT(id);
302
303         // 'RIFF'
304         if (id != 0x46464952) {
305                 printf("Not a RIFF file '%s'\n", filename);
306                 fclose(pfile);
307                 return -1;
308         }
309
310         // skip RIFF size
311         fread(&id, 1, 4, pfile);
312
313         // check for valid RIFF type
314         fread(&id, 1, 4, pfile);
315         id = INTEL_INT(id);
316
317         // 'AVI '
318         if (id != 0x20495641) {
319                 printf("Not an AVI file '%s'\n", filename);
320                 fclose(pfile);
321                 return -1;
322         }
323
324         // used for main 'LIST' chunks
325         long offset_tmp = 0;
326
327         // parse WAVE tags
328         while ( ftell(pfile) < file_size ) {
329                 fread(&tag, 1, 4, pfile);
330                 fread(&size, 1, 4, pfile);
331
332                 tag = INTEL_INT(tag);
333                 size = INTEL_INT(size);
334
335                 next_chunk = ftell(pfile) + size;
336
337                 switch (tag) {
338                         // 'LIST'
339                         case 0x5453494c: {
340                                 // sub tag
341                                 fread(&s_tag, 1, 4, pfile);
342                                 s_tag = INTEL_INT(s_tag);
343
344                                 switch (s_tag) {
345                                         // 'hdrl'
346                                         case 0x6c726468: {
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);
360
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);
373
374                                                 // check for 'avih'
375                                                 SDL_assert(avi_header.fcc == 0x68697661);
376
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);
380                                                         fclose(pfile);
381                                                         return -1;
382                                                 }
383
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);
387                                                         fclose(pfile);
388                                                         return -1;
389                                                 }
390
391                                                 // update next_chunk offset for sub-chunk
392                                                 offset_tmp = next_chunk;
393                                                 next_chunk = ftell(pfile);
394
395                                                 break;
396                                         }
397
398                                         // 'strl' - subchunk of 'hdrl'
399                                         case 0x6c727473: {
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);
419
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);
439
440                                                 // check for 'strh'
441                                                 SDL_assert(stream_header.fcc == 0x68727473);
442
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);
446                                                         fclose(pfile);
447                                                         return -1;
448                                                 }
449                                                 SDL_assert(stream_header.fccType == 0x73646976);
450
451                                                 // only handle 'MRLE' encoding
452                                                 if (stream_header.fccHandler != 0x454c524d) {
453                                                         printf("AVI is not MRLE encoded '%s'\n", filename);
454                                                         fclose(pfile);
455                                                         return -1;
456                                                 }
457
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);
461                                                         fclose(pfile);
462                                                         return -1;
463                                                 }
464
465                                                 // next stream sub-chunk -------------------------------
466
467                                                 // check for 'strf'
468                                                 fread(&tmp, 1, 4, pfile);
469                                                 tmp = INTEL_INT(tmp);
470                                                 SDL_assert(tmp == 0x66727473);
471
472                                                 // size of 'strf'
473                                                 fread(&tmp, 1, 4, pfile);
474
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);
486
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);
498
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);
502                                                         fclose(pfile);
503                                                         return -1;
504                                                 }
505
506                                                 // palette
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);
512                                                 }
513
514                                                 // reset next_chunk back to main 'LIST'
515                                                 next_chunk = offset_tmp;
516
517                                                 break;
518                                         }
519
520                                         // 'movi'
521                                         case 0x69766f6d: {
522                                                 // need this for later
523                                                 movi_offset = ftell(pfile);
524
525                                                 // requiring an index, so don't mess with this anymore
526
527                                                 break;
528                                         }
529
530                                         default:
531                                                 printf("sub-something else: 0x%x\n", tag);
532                                                 break;
533                                 }
534
535                                 break;
536                         }
537
538                         // 'idx1'
539                         case 0x31786469: {
540                                 SDL_assert(AVI_stream.frame_index == NULL);
541
542                                 AVI_stream.frame_index = (FRAMEINDEX*) malloc(sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
543                                 SDL_assert(AVI_stream.frame_index != NULL);
544
545                                 memset(AVI_stream.frame_index, 0, sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
546
547                                 unsigned int c_id, c_flags, c_offset, c_size, i = 0;
548
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);
554
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);
559
560                                         // only interested in stream 0, compressed data: '00dc'
561                                         if (c_id == 0x63643030) {
562                                                 SDL_assert(i < avi_header.dwTotalFrames);
563
564                                                 AVI_stream.frame_index[i].offset = c_offset;
565                                                 AVI_stream.frame_index[i].size = c_size;
566                                                 i++;
567                                         }
568                                 }
569
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;
574                                 }
575
576                                 break;
577                         }
578
579                         // drop everything else
580                         default:
581                                 break;
582                 }
583
584                 fseek(pfile, next_chunk, SEEK_SET);
585         }
586
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);
590                 fclose(pfile);
591                 return -1;
592         }
593
594         // fix up frame_index (assumes frame_index[0] is first frame in stream)
595         long base_offset = 0;
596
597         if (AVI_stream.frame_index[0].offset > (unsigned int)movi_offset) {
598                 base_offset = 0;
599         } else if (AVI_stream.frame_index[0].offset == (unsigned int)movi_offset) {
600                 base_offset = 4;
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;
605         } else {
606                 SDL_assert(0);
607         }
608
609         for (unsigned int i = 0; i < avi_header.dwTotalFrames; i++) {
610                 AVI_stream.frame_index[i].offset += base_offset;
611
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;
615                 }
616         }
617
618         // now set up AVI_stream
619
620         strcpy(AVI_stream.filename, filename);
621         AVI_stream.pfile = pfile;
622
623         AVI_stream.min_compressed_buffer_size = max(avi_header.dwSuggestedBufferSize, stream_header.dwSuggestedBufferSize);
624         SDL_assert(AVI_stream.min_compressed_buffer_size > 0);
625
626         AVI_stream.w = bitmap_header.bmiHeader.biWidth;
627         AVI_stream.h = bitmap_header.bmiHeader.biHeight;
628         AVI_stream.bpp = bitmap_header.bmiHeader.biBitCount;
629
630         // store the number of frames in the AVI_info[] structure
631         AVI_stream.num_frames = avi_header.dwTotalFrames;
632
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;
638         }
639
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;  
642
643         return 0;
644 }
645
646
647 // AVI_stream_close() should be called when you are finished reading all the frames of an AVI
648 //
649 void AVI_stream_close()
650 {       
651 //      SDL_assert( AVI_stream.flags & AVI_STREAM_F_USED);
652
653         if (AVI_stream.pfile) {
654                 fclose(AVI_stream.pfile);
655                 AVI_stream.pfile = NULL;
656         }
657
658         if (AVI_stream.frame_index) {
659                 free(AVI_stream.frame_index);
660                 AVI_stream.frame_index = NULL;
661         }
662
663         AVI_stream.flags &= ~AVI_STREAM_F_USED;                 // clear the used flag
664
665         AVI_stream_inited = 0;
666 }
667
668
669
670
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).
675 // 
676 // returns:    0 ==> success
677 //            !0 ==> error
678 //
679 int AVI_stream_get_frame(ubyte* buffer, int frame_number)
680 {
681         if ( frame_number > AVI_stream.num_frames ) {
682                 buffer = NULL;
683                 return -1;
684         }
685
686         SDL_assert( (frame_number - 1) >= 0 );
687
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);
691
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);
694
695         AVI_decompress_RLE8(compressed_frame, buffer, AVI_stream.w, AVI_stream.h);
696
697         free( compressed_frame );
698
699         return 0;
700 }
701
702
703
704 // -------------------------------------------------------------------------------------------------
705 // AVI_decompress_RLE8() will decompress the data pointed to by src, and store in dest.
706 //
707 //      NOTE:  1. memory for dest must be already allocated before calling function
708 //
709
710 void AVI_decompress_RLE8(ubyte* src, ubyte* dest, int w, int h)
711 {
712         int src_index = 0;
713         int dest_index = 0;
714         int i;
715
716         SDL_assert( src != NULL);
717         SDL_assert( dest != NULL);
718         SDL_assert( w > 0 );
719         SDL_assert( h > 0 );
720
721         ubyte count = 0;
722         ubyte run = 0;
723         ubyte control_code = 0;
724         ubyte x_off = 0;
725         ubyte y_off = 0;
726
727         int size_src = w * h + 1;
728
729         int scan_line = h-1;
730         int height_offset = scan_line * w;
731
732         while ( src_index < size_src ) {
733                 
734                 count = src[src_index];
735                 
736                 if ( count == 0 ) {     // control code follows
737                         src_index++;
738                         control_code = src[src_index];
739                         if ( control_code == 1 ) {
740                                 src_index++;
741 //                              nprintf(("AVI","AVI ==> Reached end of compressed image\n"));
742                                 break;
743                         }
744                         else if ( control_code == 0 ) {
745                                 src_index++;
746                                 scan_line--;
747                                 height_offset = scan_line * w;  // only need to calc once per scanline
748                                 dest_index = 0;
749                                 if (height_offset < 0) {
750                                         break;
751                                 }
752                                 //nprintf(("AVI","AVI ==> Reached end of line in compressed image\n"));
753                         }
754                         else if ( control_code == 2 ) {
755                                 // delta - horizontal and veritcal offsets
756                                 src_index++;
757                                 x_off = src[src_index];
758
759                                 if (x_off) {
760                                         dest_index += x_off;
761
762                                         if (dest_index >= w) {
763                                                 break;
764                                         }
765                                 }
766
767                                 src_index++;
768                                 y_off = src[src_index];
769
770                                 if (y_off) {
771                                         scan_line -= y_off;
772                                         height_offset = scan_line * w;
773
774                                         if (height_offset < 0) {
775                                                 break;
776                                         }
777                                 }
778
779                                 src_index++;
780                         }
781                         else {
782                                 // in absolute mode
783                                 src_index++;
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) {
787                                                 break;
788                                         }
789                                         dest[height_offset + dest_index] = src[src_index];
790                                         dest_index++;
791                                         src_index++;
792                                 }
793                                 // run must end on a word boundry
794                                 if ( control_code & 1 )
795                                         src_index++;
796                         }
797                 }
798                 else {
799                         src_index++;
800                         run = src[src_index];
801                         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) {
808                                         break;
809                                 }
810                                 dest[height_offset + dest_index] = run;
811                                 dest_index++;
812                         }
813                 }
814
815         }       // end while
816
817 }
818
819 int save_anim_header()
820 {
821         int i, new_format_id = 0;
822
823         SDL_assert(anim_fp);
824         fclose(anim_fp);
825         anim_fp = fopen(anim_save_filename, "r+b");
826
827         if (!fwrite(&new_format_id, 2, 1, anim_fp))
828                 return -1;
829         if (!fwrite(&Anim.version, 2, 1, anim_fp))
830                 return -1;
831         if (!fwrite(&Anim.fps, 2, 1, anim_fp))
832                 return -1;
833         if (!fwrite(&Anim.xparent_r, 1, 1, anim_fp))
834                 return -1;
835         if (!fwrite(&Anim.xparent_g, 1, 1, anim_fp))
836                 return -1;
837         if (!fwrite(&Anim.xparent_b, 1, 1, anim_fp))
838                 return -1;
839         if (!fwrite(&Anim.width, 2, 1, anim_fp))
840                 return -1;
841         if (!fwrite(&Anim.height, 2, 1, anim_fp))
842                 return -1;
843         if (!fwrite(&Anim.total_frames, 2, 1, anim_fp))
844                 return -1;
845         if (!fwrite(&Anim.packer_code, 1, 1, anim_fp))
846                 return -1;
847         if (fwrite(&Anim.palette, 3, 256, anim_fp) != 256)
848                 return -1;
849         if (!fwrite(&total_key_frames, 2, 1, anim_fp))
850                 return -1;
851
852         for (i=0; i<Anim.num_keys; i++) {
853                 if (!fwrite(&Anim.keys[i].frame_num, 2, 1, anim_fp))
854                         return -1;
855
856                 if (!fwrite(&Anim.keys[i].offset, 4, 1, anim_fp))
857                         return -1;
858         }
859
860         if (!fwrite(&anim_offset, 4, 1, anim_fp))
861                 return -1;
862
863         return 0;
864 }
865
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)
870 {
871         int count = 0, frame = 1, rate = key_frame_rate, last_frame;
872
873         if (!rate)
874                 rate = total_frames;
875
876         while (frame <= total_frames) {
877                 count++;
878                 frame += rate;
879         }
880
881         if (force_key_frame >= 0)
882                 count++;
883
884         if (count)
885                 Anim.keys = (key_frame *) malloc(count * sizeof(key_frame));
886
887         count = 0;
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;
892
893                 Anim.keys[count++].frame_num = frame;
894                 frame += rate;
895         }
896
897         if (force_key_frame > last_frame)
898                 Anim.keys[count++].frame_num = force_key_frame;
899
900         Anim.num_keys = count;
901         return count;  // number of key frames
902 }
903
904 int anim_save_init(char *file, int width, int height, int frames)
905 {
906         SDL_assert(file);
907         anim_save_filename = file;
908         anim_fp = fopen(file, "wb");
909         if (!anim_fp)
910                 return -1;
911
912         Anim.version = ANIM_VERSION;
913         Anim.fps = Default_fps;
914         Anim.width = width;
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;
921         anim_offset = 0;
922         cur_frame_num = 0;
923         total_key_frames = allocate_key_frames(frames);
924         fseek(anim_fp, ANIM_HEADER_SIZE + total_key_frames * 6, SEEK_SET);
925
926         switch ( Compression_type ) {
927                 case CUSTOM_DELTA_RLE:
928                         Key_frame_compression = PACKING_METHOD_RLE_KEY;
929                         Regular_frame_compression = PACKING_METHOD_RLE;
930                         break;
931
932                 case STD_DELTA_RLE:
933                         Key_frame_compression = PACKING_METHOD_STD_RLE_KEY;
934                         Regular_frame_compression = PACKING_METHOD_STD_RLE;
935                         break;
936
937                 default:
938                         Int3();
939                         return -1;
940                         break;
941         } // end switch
942
943         return 0;
944 }
945
946 int anim_save_frame()
947 {
948         ubyte *temp;
949         int i, size;
950         key_frame *keyp = NULL;
951
952         SDL_assert(anim_fp);
953         cur_frame_num++;
954         SDL_assert(cur_frame_num <= Anim.total_frames);
955
956         for (i=0; i<Anim.num_keys; i++)
957                 if (Anim.keys[i].frame_num == cur_frame_num) {
958                         keyp = &Anim.keys[i];
959                         break;
960                 }
961
962         if (keyp) {
963                 fprintf(stdout, "*");
964                 fflush(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);
967
968         } else {
969                 fprintf(stdout, ".");
970                 fflush(stdout);
971                 size = pack_frame(cur_frame, last_frame, anim_buffer, Anim.width * Anim.height, ANIM_BUFFER_MAX, Regular_frame_compression);
972         }
973
974         if (size < 0)
975                 return -1;
976
977         if ((int) fwrite(anim_buffer, 1, size, anim_fp) != size)
978                 return -1;
979
980         anim_offset += size;
981         temp = cur_frame;
982         cur_frame = last_frame;
983         last_frame = temp;
984         return 0;
985 }
986
987
988 // converts an avi file to an anim file
989 //
990 // returns:   0 ==> success
991 //                !0 ==> failure
992 //
993 int convert_avi_to_anim(char* filename)
994 {
995         char ani_filename[255];
996         int ret = 1;
997         int rc, i, xparent_pal_index;
998         int avi_stream_opened = 0;
999
1000         rc = AVI_stream_open(filename);
1001         if ( rc != 0 ) {
1002                 // could not open the AVI stream
1003                 goto Finish;
1004         }
1005         avi_stream_opened = 1;
1006         
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);
1011
1012         strcpy(ani_filename, AVI_stream.filename);
1013         strcpy(ani_filename + strlen(ani_filename) - 3, "ani");
1014
1015         memset(&Anim, 0, sizeof(anim));
1016
1017         memcpy(Anim.palette, AVI_stream.palette, 768);
1018
1019         if (Use_custom_xparent_color) {
1020                 // Need to look at pixel in top-left 
1021                 rc = AVI_stream_get_frame(cur_frame, 1);
1022                 if ( rc != 0 )
1023                         goto Finish;
1024
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];
1029
1030         } else {
1031                 Xparent_color.r = 0;
1032                 Xparent_color.g = 255;
1033                 Xparent_color.b = 0;
1034         }
1035         
1036         rc = anim_save_init(ani_filename, AVI_stream.w, AVI_stream.h, AVI_stream.num_frames);
1037         if (rc == -1)
1038                 goto Finish;
1039
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);
1043                 if ( rc != 0 )
1044                         goto Finish;
1045
1046                 // pass to the anim compression
1047                 rc = anim_save_frame();
1048                 if ( rc != 0 )
1049                         goto Finish;
1050         }
1051
1052         rc = save_anim_header();
1053         if ( rc != 0 )
1054                 goto Finish;
1055
1056         ret = 0;
1057
1058         Finish:
1059         // done with the AVI.. close the stream
1060         if ( avi_stream_opened )
1061                 AVI_stream_close();
1062
1063         if ( anim_fp )
1064                 fclose(anim_fp);
1065
1066         if (Anim.keys)
1067                 free(Anim.keys);
1068
1069         free(cur_frame);
1070         free(last_frame);
1071         fprintf(stdout,"\n");
1072         fflush(stdout);
1073         return ret;
1074 }
1075
1076 int convert_frames_to_anim(char *filename)
1077 {
1078         int first_frame, frame, pos, width, height, xparent_pal_index, r = -1;
1079         char ani_filename[255], name[255], temp[8];     
1080         int rc;
1081         FILE *fp;
1082
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;
1090
1091         memset(&Anim, 0, sizeof(anim));
1092
1093         // first file
1094         fp = fopen(name, "rb");
1095         if(fp != NULL){
1096                 do {
1097                         fclose(fp);
1098                         frame++;
1099                         sprintf(temp, "%04d", frame);
1100                         strncpy(&name[pos], temp, 4);   
1101
1102                         // next file
1103                         fp = fopen(name, "rb");
1104                 } while(fp != NULL);    
1105         }
1106
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);
1110                 return -1;
1111         }
1112
1113         cur_frame = (ubyte *) malloc(width * height);
1114         last_frame = (ubyte *) malloc(width * height);
1115
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);
1119                 return -1;
1120         }
1121
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];
1128
1129         } else {
1130                 Xparent_color.r = 0;
1131                 Xparent_color.g = 255;
1132                 Xparent_color.b = 0;
1133         }
1134
1135         if (anim_save_init(ani_filename, width, height, frame - first_frame))
1136                 goto done;
1137
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)
1143                         goto done;
1144
1145                 if (anim_save_frame())
1146                         goto done;
1147
1148                 first_frame++;
1149         }
1150
1151         if (save_anim_header())
1152                 goto done;
1153
1154         r = 0;
1155
1156 done:
1157         if (Anim.keys)
1158                 free(Anim.keys);
1159
1160         fclose(anim_fp);
1161         free(cur_frame);
1162         free(last_frame);
1163         fprintf(stdout, "\n");
1164         fflush(stdout);
1165         return r;
1166 }
1167