]> icculus.org git repositories - taylor/freespace2.git/blob - src/ac/convert.cpp
silence various MSVC warnings
[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, 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;
281
282         SDL_assert( !(AVI_stream.flags & AVI_STREAM_F_USED) );
283
284         pfile = fopen(filename, "rb");
285
286         if (pfile == NULL) {
287                 printf("AVI ==> Unable to open %s", filename);
288                 return -1;
289         }
290
291         // get file size
292         fseek(pfile, 0, SEEK_END);
293         file_size = ftell(pfile);
294         fseek(pfile, 0, SEEK_SET);
295
296         // check for valid file type
297         fread(&id, 1, 4, pfile);
298         id = INTEL_INT(id);
299
300         // 'RIFF'
301         if (id != 0x46464952) {
302                 printf("Not a RIFF file '%s'\n", filename);
303                 fclose(pfile);
304                 return -1;
305         }
306
307         // skip RIFF size
308         fread(&id, 1, 4, pfile);
309
310         // check for valid RIFF type
311         fread(&id, 1, 4, pfile);
312         id = INTEL_INT(id);
313
314         // 'AVI '
315         if (id != 0x20495641) {
316                 printf("Not an AVI file '%s'\n", filename);
317                 fclose(pfile);
318                 return -1;
319         }
320
321         // used for main 'LIST' chunks
322         unsigned int offset_tmp = 0;
323
324         // parse WAVE tags
325         while ( ftell(pfile) < file_size ) {
326                 fread(&tag, 1, 4, pfile);
327                 fread(&size, 1, 4, pfile);
328
329                 tag = INTEL_INT(tag);
330                 size = INTEL_INT(size);
331
332                 next_chunk = ftell(pfile) + size;
333
334                 switch (tag) {
335                         // 'LIST'
336                         case 0x5453494c: {
337                                 // sub tag
338                                 fread(&s_tag, 1, 4, pfile);
339                                 s_tag = INTEL_INT(s_tag);
340
341                                 switch (s_tag) {
342                                         // 'hdrl'
343                                         case 0x6c726468: {
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);
357
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);
370
371                                                 // check for 'avih'
372                                                 SDL_assert(avi_header.fcc == 0x68697661);
373
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);
377                                                         fclose(pfile);
378                                                         return -1;
379                                                 }
380
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);
384                                                         fclose(pfile);
385                                                         return -1;
386                                                 }
387
388                                                 // update next_chunk offset for sub-chunk
389                                                 offset_tmp = next_chunk;
390                                                 next_chunk = ftell(pfile);
391
392                                                 break;
393                                         }
394
395                                         // 'strl' - subchunk of 'hdrl'
396                                         case 0x6c727473: {
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);
416
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);
436
437                                                 // check for 'strh'
438                                                 SDL_assert(stream_header.fcc == 0x68727473);
439
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);
443                                                         fclose(pfile);
444                                                         return -1;
445                                                 }
446                                                 SDL_assert(stream_header.fccType == 0x73646976);
447
448                                                 // only handle 'MRLE' encoding
449                                                 if (stream_header.fccHandler != 0x454c524d) {
450                                                         printf("AVI is not MRLE encoded '%s'\n", filename);
451                                                         fclose(pfile);
452                                                         return -1;
453                                                 }
454
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);
458                                                         fclose(pfile);
459                                                         return -1;
460                                                 }
461
462                                                 // next stream sub-chunk -------------------------------
463
464                                                 // check for 'strf'
465                                                 fread(&tmp, 1, 4, pfile);
466                                                 tmp = INTEL_INT(tmp);
467                                                 SDL_assert(tmp == 0x66727473);
468
469                                                 // size of 'strf'
470                                                 fread(&tmp, 1, 4, pfile);
471
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);
483
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);
495
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);
499                                                         fclose(pfile);
500                                                         return -1;
501                                                 }
502
503                                                 // palette
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);
509                                                 }
510
511                                                 // reset next_chunk back to main 'LIST'
512                                                 next_chunk = offset_tmp;
513
514                                                 break;
515                                         }
516
517                                         // 'movi'
518                                         case 0x69766f6d: {
519                                                 // need this for later
520                                                 movi_offset = ftell(pfile);
521
522                                                 // requiring an index, so don't mess with this anymore
523
524                                                 break;
525                                         }
526
527                                         default:
528                                                 printf("sub-something else: 0x%x\n", tag);
529                                                 break;
530                                 }
531
532                                 break;
533                         }
534
535                         // 'idx1'
536                         case 0x31786469: {
537                                 SDL_assert(AVI_stream.frame_index == NULL);
538
539                                 AVI_stream.frame_index = (FRAMEINDEX*) malloc(sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
540                                 SDL_assert(AVI_stream.frame_index != NULL);
541
542                                 memset(AVI_stream.frame_index, 0, sizeof(FRAMEINDEX) * avi_header.dwTotalFrames);
543
544                                 unsigned int c_id, c_flags, c_offset, c_size, i = 0;
545
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);
551
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);
556
557                                         // only interested in stream 0, compressed data: '00dc'
558                                         if (c_id == 0x63643030) {
559                                                 SDL_assert(i < avi_header.dwTotalFrames);
560
561                                                 AVI_stream.frame_index[i].offset = c_offset;
562                                                 AVI_stream.frame_index[i].size = c_size;
563                                                 i++;
564                                         }
565                                 }
566
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;
571                                 }
572
573                                 break;
574                         }
575
576                         // drop everything else
577                         default:
578                                 break;
579                 }
580
581                 fseek(pfile, next_chunk, SEEK_SET);
582         }
583
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);
587                 fclose(pfile);
588                 return -1;
589         }
590
591         // fix up frame_index (assumes frame_index[0] is first frame in stream)
592         unsigned int base_offset = 0;
593
594         if (AVI_stream.frame_index[0].offset > movi_offset) {
595                 base_offset = 0;
596         } else if (AVI_stream.frame_index[0].offset == movi_offset) {
597                 base_offset = 4;
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;
602         } else {
603                 SDL_assert(0);
604         }
605
606         for (unsigned int i = 0; i < avi_header.dwTotalFrames; i++) {
607                 AVI_stream.frame_index[i].offset += base_offset;
608
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;
612                 }
613         }
614
615         // now set up AVI_stream
616
617         strcpy(AVI_stream.filename, filename);
618         AVI_stream.pfile = pfile;
619
620         AVI_stream.min_compressed_buffer_size = max(avi_header.dwSuggestedBufferSize, stream_header.dwSuggestedBufferSize);
621         SDL_assert(AVI_stream.min_compressed_buffer_size > 0);
622
623         AVI_stream.w = bitmap_header.bmiHeader.biWidth;
624         AVI_stream.h = bitmap_header.bmiHeader.biHeight;
625         AVI_stream.bpp = bitmap_header.bmiHeader.biBitCount;
626
627         // store the number of frames in the AVI_info[] structure
628         AVI_stream.num_frames = avi_header.dwTotalFrames;
629
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;
635         }
636
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;  
639
640         return 0;
641 }
642
643
644 // AVI_stream_close() should be called when you are finished reading all the frames of an AVI
645 //
646 void AVI_stream_close()
647 {       
648 //      SDL_assert( AVI_stream.flags & AVI_STREAM_F_USED);
649
650         if (AVI_stream.pfile) {
651                 fclose(AVI_stream.pfile);
652                 AVI_stream.pfile = NULL;
653         }
654
655         if (AVI_stream.frame_index) {
656                 free(AVI_stream.frame_index);
657                 AVI_stream.frame_index = NULL;
658         }
659
660         AVI_stream.flags &= ~AVI_STREAM_F_USED;                 // clear the used flag
661
662         AVI_stream_inited = 0;
663 }
664
665
666
667
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).
672 // 
673 // returns:    0 ==> success
674 //            !0 ==> error
675 //
676 int AVI_stream_get_frame(ubyte* buffer, int frame_number)
677 {
678         if ( frame_number > AVI_stream.num_frames ) {
679                 buffer = NULL;
680                 return -1;
681         }
682
683         SDL_assert( (frame_number - 1) >= 0 );
684
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);
688
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);
691
692         AVI_decompress_RLE8(compressed_frame, buffer, AVI_stream.w, AVI_stream.h);
693
694         free( compressed_frame );
695
696         return 0;
697 }
698
699
700
701 // -------------------------------------------------------------------------------------------------
702 // AVI_decompress_RLE8() will decompress the data pointed to by src, and store in dest.
703 //
704 //      NOTE:  1. memory for dest must be already allocated before calling function
705 //
706
707 void AVI_decompress_RLE8(ubyte* src, ubyte* dest, int w, int h)
708 {
709         int src_index = 0;
710         int dest_index = 0;
711         int i;
712
713         SDL_assert( src != NULL);
714         SDL_assert( dest != NULL);
715         SDL_assert( w > 0 );
716         SDL_assert( h > 0 );
717
718         ubyte count = 0;
719         ubyte run = 0;
720         ubyte control_code = 0;
721         ubyte x_off = 0;
722         ubyte y_off = 0;
723
724         int size_src = w * h + 1;
725
726         int scan_line = h-1;
727         int height_offset = scan_line * w;
728
729         while ( src_index < size_src ) {
730                 
731                 count = src[src_index];
732                 
733                 if ( count == 0 ) {     // control code follows
734                         src_index++;
735                         control_code = src[src_index];
736                         if ( control_code == 1 ) {
737                                 src_index++;
738 //                              nprintf(("AVI","AVI ==> Reached end of compressed image\n"));
739                                 break;
740                         }
741                         else if ( control_code == 0 ) {
742                                 src_index++;
743                                 scan_line--;
744                                 height_offset = scan_line * w;  // only need to calc once per scanline
745                                 dest_index = 0;
746                                 if (height_offset < 0) {
747                                         break;
748                                 }
749                                 //nprintf(("AVI","AVI ==> Reached end of line in compressed image\n"));
750                         }
751                         else if ( control_code == 2 ) {
752                                 // delta - horizontal and veritcal offsets
753                                 src_index++;
754                                 x_off = src[src_index];
755
756                                 if (x_off) {
757                                         dest_index += x_off;
758
759                                         if (dest_index >= w) {
760                                                 break;
761                                         }
762                                 }
763
764                                 src_index++;
765                                 y_off = src[src_index];
766
767                                 if (y_off) {
768                                         scan_line -= y_off;
769                                         height_offset = scan_line * w;
770
771                                         if (height_offset < 0) {
772                                                 break;
773                                         }
774                                 }
775
776                                 src_index++;
777                         }
778                         else {
779                                 // in absolute mode
780                                 src_index++;
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) {
784                                                 break;
785                                         }
786                                         dest[height_offset + dest_index] = src[src_index];
787                                         dest_index++;
788                                         src_index++;
789                                 }
790                                 // run must end on a word boundry
791                                 if ( control_code & 1 )
792                                         src_index++;
793                         }
794                 }
795                 else {
796                         src_index++;
797                         run = src[src_index];
798                         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) {
805                                         break;
806                                 }
807                                 dest[height_offset + dest_index] = run;
808                                 dest_index++;
809                         }
810                 }
811
812         }       // end while
813
814 }
815
816 int save_anim_header()
817 {
818         int i, new_format_id = 0;
819
820         SDL_assert(anim_fp);
821         fclose(anim_fp);
822         anim_fp = fopen(anim_save_filename, "r+b");
823
824         if (!fwrite(&new_format_id, 2, 1, anim_fp))
825                 return -1;
826         if (!fwrite(&Anim.version, 2, 1, anim_fp))
827                 return -1;
828         if (!fwrite(&Anim.fps, 2, 1, anim_fp))
829                 return -1;
830         if (!fwrite(&Anim.xparent_r, 1, 1, anim_fp))
831                 return -1;
832         if (!fwrite(&Anim.xparent_g, 1, 1, anim_fp))
833                 return -1;
834         if (!fwrite(&Anim.xparent_b, 1, 1, anim_fp))
835                 return -1;
836         if (!fwrite(&Anim.width, 2, 1, anim_fp))
837                 return -1;
838         if (!fwrite(&Anim.height, 2, 1, anim_fp))
839                 return -1;
840         if (!fwrite(&Anim.total_frames, 2, 1, anim_fp))
841                 return -1;
842         if (!fwrite(&Anim.packer_code, 1, 1, anim_fp))
843                 return -1;
844         if (fwrite(&Anim.palette, 3, 256, anim_fp) != 256)
845                 return -1;
846         if (!fwrite(&total_key_frames, 2, 1, anim_fp))
847                 return -1;
848
849         for (i=0; i<Anim.num_keys; i++) {
850                 if (!fwrite(&Anim.keys[i].frame_num, 2, 1, anim_fp))
851                         return -1;
852
853                 if (!fwrite(&Anim.keys[i].offset, 4, 1, anim_fp))
854                         return -1;
855         }
856
857         if (!fwrite(&anim_offset, 4, 1, anim_fp))
858                 return -1;
859
860         return 0;
861 }
862
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)
867 {
868         int count = 0, frame = 1, rate = key_frame_rate, last_frame;
869
870         if (!rate)
871                 rate = total_frames;
872
873         while (frame <= total_frames) {
874                 count++;
875                 frame += rate;
876         }
877
878         if (force_key_frame >= 0)
879                 count++;
880
881         if (count)
882                 Anim.keys = (key_frame *) malloc(count * sizeof(key_frame));
883
884         count = 0;
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;
889
890                 Anim.keys[count++].frame_num = frame;
891                 frame += rate;
892         }
893
894         if (force_key_frame > last_frame)
895                 Anim.keys[count++].frame_num = force_key_frame;
896
897         Anim.num_keys = count;
898         return count;  // number of key frames
899 }
900
901 int anim_save_init(char *file, int width, int height, int frames)
902 {
903         SDL_assert(file);
904         anim_save_filename = file;
905         anim_fp = fopen(file, "wb");
906         if (!anim_fp)
907                 return -1;
908
909         Anim.version = ANIM_VERSION;
910         Anim.fps = Default_fps;
911         Anim.width = width;
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;
918         anim_offset = 0;
919         cur_frame_num = 0;
920         total_key_frames = allocate_key_frames(frames);
921         fseek(anim_fp, ANIM_HEADER_SIZE + total_key_frames * 6, SEEK_SET);
922
923         switch ( Compression_type ) {
924                 case CUSTOM_DELTA_RLE:
925                         Key_frame_compression = PACKING_METHOD_RLE_KEY;
926                         Regular_frame_compression = PACKING_METHOD_RLE;
927                         break;
928
929                 case STD_DELTA_RLE:
930                         Key_frame_compression = PACKING_METHOD_STD_RLE_KEY;
931                         Regular_frame_compression = PACKING_METHOD_STD_RLE;
932                         break;
933
934                 default:
935                         Int3();
936                         return -1;
937                         break;
938         } // end switch
939
940         return 0;
941 }
942
943 int anim_save_frame()
944 {
945         ubyte *temp;
946         int i, size;
947         key_frame *keyp = NULL;
948
949         SDL_assert(anim_fp);
950         cur_frame_num++;
951         SDL_assert(cur_frame_num <= Anim.total_frames);
952
953         for (i=0; i<Anim.num_keys; i++)
954                 if (Anim.keys[i].frame_num == cur_frame_num) {
955                         keyp = &Anim.keys[i];
956                         break;
957                 }
958
959         if (keyp) {
960                 fprintf(stdout, "*");
961                 fflush(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);
964
965         } else {
966                 fprintf(stdout, ".");
967                 fflush(stdout);
968                 size = pack_frame(cur_frame, last_frame, anim_buffer, Anim.width * Anim.height, ANIM_BUFFER_MAX, Regular_frame_compression);
969         }
970
971         if (size < 0)
972                 return -1;
973
974         if ((int) fwrite(anim_buffer, 1, size, anim_fp) != size)
975                 return -1;
976
977         anim_offset += size;
978         temp = cur_frame;
979         cur_frame = last_frame;
980         last_frame = temp;
981         return 0;
982 }
983
984
985 // converts an avi file to an anim file
986 //
987 // returns:   0 ==> success
988 //                !0 ==> failure
989 //
990 int convert_avi_to_anim(char* filename)
991 {
992         char ani_filename[255];
993         int ret = 1;
994         int rc, i, xparent_pal_index;
995         int avi_stream_opened = 0;
996
997         rc = AVI_stream_open(filename);
998         if ( rc != 0 ) {
999                 // could not open the AVI stream
1000                 goto Finish;
1001         }
1002         avi_stream_opened = 1;
1003         
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);
1008
1009         strcpy(ani_filename, AVI_stream.filename);
1010         strcpy(ani_filename + strlen(ani_filename) - 3, "ani");
1011
1012         memset(&Anim, 0, sizeof(anim));
1013
1014         memcpy(Anim.palette, AVI_stream.palette, 768);
1015
1016         if (Use_custom_xparent_color) {
1017                 // Need to look at pixel in top-left 
1018                 rc = AVI_stream_get_frame(cur_frame, 1);
1019                 if ( rc != 0 )
1020                         goto Finish;
1021
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];
1026
1027         } else {
1028                 Xparent_color.r = 0;
1029                 Xparent_color.g = 255;
1030                 Xparent_color.b = 0;
1031         }
1032         
1033         rc = anim_save_init(ani_filename, AVI_stream.w, AVI_stream.h, AVI_stream.num_frames);
1034         if (rc == -1)
1035                 goto Finish;
1036
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);
1040                 if ( rc != 0 )
1041                         goto Finish;
1042
1043                 // pass to the anim compression
1044                 rc = anim_save_frame();
1045                 if ( rc != 0 )
1046                         goto Finish;
1047         }
1048
1049         rc = save_anim_header();
1050         if ( rc != 0 )
1051                 goto Finish;
1052
1053         ret = 0;
1054
1055         Finish:
1056         // done with the AVI.. close the stream
1057         if ( avi_stream_opened )
1058                 AVI_stream_close();
1059
1060         if ( anim_fp )
1061                 fclose(anim_fp);
1062
1063         if (Anim.keys)
1064                 free(Anim.keys);
1065
1066         free(cur_frame);
1067         free(last_frame);
1068         fprintf(stdout,"\n");
1069         fflush(stdout);
1070         return ret;
1071 }
1072
1073 int convert_frames_to_anim(char *filename)
1074 {
1075         int first_frame, frame, pos, width, height, xparent_pal_index, r = -1;
1076         char ani_filename[255], name[255], temp[8];     
1077         int rc;
1078         FILE *fp;
1079
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;
1087
1088         memset(&Anim, 0, sizeof(anim));
1089
1090         // first file
1091         fp = fopen(name, "rb");
1092         if(fp != NULL){
1093                 do {
1094                         fclose(fp);
1095                         frame++;
1096                         sprintf(temp, "%04d", frame);
1097                         strncpy(&name[pos], temp, 4);   
1098
1099                         // next file
1100                         fp = fopen(name, "rb");
1101                 } while(fp != NULL);    
1102         }
1103
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);
1107                 return -1;
1108         }
1109
1110         cur_frame = (ubyte *) malloc(width * height);
1111         last_frame = (ubyte *) malloc(width * height);
1112
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);
1116                 return -1;
1117         }
1118
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];
1125
1126         } else {
1127                 Xparent_color.r = 0;
1128                 Xparent_color.g = 255;
1129                 Xparent_color.b = 0;
1130         }
1131
1132         if (anim_save_init(ani_filename, width, height, frame - first_frame))
1133                 goto done;
1134
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)
1140                         goto done;
1141
1142                 if (anim_save_frame())
1143                         goto done;
1144
1145                 first_frame++;
1146         }
1147
1148         if (save_anim_header())
1149                 goto done;
1150
1151         r = 0;
1152
1153 done:
1154         if (Anim.keys)
1155                 free(Anim.keys);
1156
1157         fclose(anim_fp);
1158         free(cur_frame);
1159         free(last_frame);
1160         fprintf(stdout, "\n");
1161         fflush(stdout);
1162         return r;
1163 }
1164