2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Anim/PackUnpack.cpp $
15 * Code for handling packing and unpacking in Hoffoss's RLE format, used for
16 * Anim files. Also handles Anim loading, creating Anim instances (for
17 * utilizing an Anim), and getting getting frames of the Anim.
20 * Revision 1.2 2002/06/09 04:41:15 relnev
21 * added copyright header
23 * Revision 1.1.1.1 2002/05/03 03:28:08 root
27 * 12 7/30/99 10:10p Dave
28 * Fixed loading bar in 32 bit mode.
30 * 11 7/18/99 1:59p Johnson
31 * Fixed potential anim locking problem.
33 * 10 7/16/99 1:49p Dave
34 * 8 bit aabitmaps. yay.
36 * 9 7/13/99 1:15p Dave
37 * 32 bit support. Whee!
39 * 8 4/09/99 2:21p Dave
40 * Multiplayer beta stuff. CD checking.
42 * 7 1/14/99 12:48a Dave
43 * Todo list bug fixes. Made a pass at putting briefing icons back into
44 * FRED. Sort of works :(
46 * 6 12/01/98 5:53p Dave
47 * Simplified the way pixel data is swizzled. Fixed tga bitmaps to work
48 * properly in D3D and Glide.
50 * 5 12/01/98 8:06a Dave
51 * Temporary checkin to fix some texture transparency problems in d3d.
53 * 4 11/30/98 1:07p Dave
54 * 16 bit conversion, first run.
56 * 3 10/22/98 6:14p Dave
57 * Optimized some #includes in Anim folder. Put in the beginnings of
58 * parse/localization support for externalized strings and tstrings.tbl
60 * 2 10/07/98 10:52a Dave
63 * 1 10/07/98 10:48a Dave
65 * 30 5/14/98 3:38p John
66 * Added in more non-darkening colors for Adam. Had to fix some bugs in
67 * BmpMan and Ani stuff to get this to work.
69 * 29 5/07/98 3:11a Lawrance
70 * Implement custom streaming code
72 * 28 11/19/97 8:28p Dave
73 * Hooked in Main Hall screen. Put in Anim support for ping ponging
74 * animations as well as general reversal of anim direction.
76 * 27 9/09/97 3:39p Sandeep
77 * warning level 4 bugs
79 * 26 8/22/97 8:21a Lawrance
80 * short circuit key frame check if keyframe matches frame we are
83 * 25 8/21/97 5:11p Lawrance
84 * frame numbering for ANI's now is from 0 -> total_frames-1.
86 * 24 8/19/97 10:59a Lawrance
87 * fix problem with accessing key frames
89 * 23 7/28/97 10:42p Lawrance
90 * re-did interface to unpack_frame() to make more general
92 * 22 7/28/97 10:52a Lawrance
93 * correctly set bitmap flags in anim_get_frame()
95 * 21 7/21/97 5:10p Lawrance
96 * fix problem that was causing infinite recursion
98 * 20 7/20/97 6:57p Lawrance
99 * supporting new RLE format
101 * 19 6/27/97 4:36p Lawrance
102 * update pal translation table when gr_screen.signature changes
104 * 18 6/26/97 12:12a Lawrance
105 * supporting anti-aliased bitmap animations
107 * 17 6/25/97 3:03p Lawrance
108 * fix palette translation problem with anti-alised bitmaps
110 * 16 5/19/97 2:28p Lawrance
111 * changes some variables to flags
113 * 15 5/15/97 4:42p Lawrance
114 * supporting animations in-game
116 * 14 2/25/97 11:06a Lawrance
117 * moved some higher level functions to from PackUnpack to AnimPlay
119 * 13 2/19/97 9:51p Lawrance
120 * made keyframe decompression more effecient, moved
121 * default anim FPS to header file
123 * 12 2/19/97 4:00p Lawrance
124 * don't assert when cannot find anim filename, return a NULL instead
126 * 11 2/17/97 4:17p Hoffoss
127 * modified packing internal format and added random access function to an
130 * 10 2/17/97 2:59p Lawrance
131 * integrating into game
133 * 9 2/14/97 11:27p Lawrance
134 * optimized unpacking some more (Jason)
136 * 8 2/14/97 11:09p Hoffoss
137 * Made optimizations.
139 * 7 2/14/97 10:48p Hoffoss
142 * 6 2/14/97 10:38p Lawrance
145 * 5 2/14/97 5:38p Hoffoss
146 * Changes to get AnimCoverter project to compile and link.
148 * 4 2/14/97 3:29p Hoffoss
149 * Added header for MSDEV to fill in.
154 #include "grinternal.h"
158 #include "animplay.h"
159 #include "packunpack.h"
161 int packer_code = PACKER_CODE;
162 int transparent_code = 254;
164 void anim_check_for_palette_change(anim_instance *instance) {
165 if ( instance->parent->screen_sig != gr_screen.signature ) {
166 instance->parent->screen_sig = gr_screen.signature;
167 anim_set_palette(instance->parent);
171 anim_instance *init_anim_instance(anim *ptr, int bpp)
180 if ( ptr->flags & ANF_STREAMED ) {
181 if ( ptr->file_offset < 0 ) {
192 ptr->instance_count++;
193 inst = (anim_instance *) malloc(sizeof(anim_instance));
195 inst->frame_num = -1;
196 inst->last_frame_num = -1;
198 inst->data = ptr->data;
199 inst->file_offset = ptr->file_offset;
200 inst->stop_now = FALSE;
201 inst->aa_color = NULL;
204 inst->frame = (ubyte *) malloc(inst->parent->width * inst->parent->height * 2);
206 inst->frame = (ubyte *) malloc(inst->parent->width * inst->parent->height);
211 void free_anim_instance(anim_instance *inst)
213 SDL_assert(inst->frame);
216 inst->parent->instance_count--;
219 inst->file_offset = -1;
224 int anim_get_next_frame(anim_instance *inst)
226 int bm, bitmap_flags;
230 if ( anim_instance_is_streamed(inst) ) {
231 if ( inst->file_offset <= 0 ) {
240 if (inst->frame_num >= inst->parent->total_frames) {
242 inst->file_offset = inst->parent->file_offset;
246 if (inst->parent->flags & ANF_XPARENT) {
247 // bitmap_flags = BMP_XPARENT;
254 if(inst->aa_color != NULL){
255 bitmap_flags |= BMP_AABITMAP;
260 anim_check_for_palette_change(inst);
262 // if we're using bitmap polys
263 BM_SELECT_TEX_FORMAT();
265 if ( anim_instance_is_streamed(inst) ) {
266 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation, aabitmap, bpp);
268 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation, aabitmap, bpp);
271 bm = bm_create(bpp, inst->parent->width, inst->parent->height, inst->frame, bitmap_flags);
276 ubyte *anim_get_next_raw_buffer(anim_instance *inst, int xlate_pal, int aabitmap, int bpp)
278 if ( anim_instance_is_streamed(inst) ) {
279 if ( inst->file_offset < 0 ) {
289 if (inst->frame_num >= inst->parent->total_frames) {
291 inst->file_offset = inst->parent->file_offset;
295 anim_check_for_palette_change(inst);
297 if ( anim_instance_is_streamed(inst) ) {
299 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation, aabitmap, bpp);
301 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, NULL, aabitmap, bpp);
305 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation, aabitmap, bpp);
307 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, NULL, aabitmap, bpp);
314 // --------------------------------------------------------------------
317 // Get a bitmap id from the anim_instance for the specified frame_num
319 // input: *inst => pointer to anim instance
320 // frame_num => frame number to get (first frame is 0)
321 // xlate_pal => DEFAULT PARM (value 1): whether to translate the palette
322 // to the current game palette
324 int anim_get_frame(anim_instance *inst, int frame_num, int xlate_pal)
327 int bm, bitmap_flags, key = 0, offset = 0;
330 if ((frame_num < 0) || (frame_num >= inst->parent->total_frames)) // illegal frame number
334 if ( anim_instance_is_streamed(inst) ) {
335 if ( inst->file_offset < 0 ) {
344 if (need_reset || (inst->frame_num >= inst->parent->total_frames)) { // reset to valid info
345 inst->data = inst->parent->data;
346 inst->file_offset = inst->parent->file_offset;
351 if (inst->parent->flags & ANF_XPARENT) {
352 // bitmap_flags = BMP_XPARENT;
356 if ( inst->frame_num == frame_num ) {
357 bm = bm_create(16, inst->parent->width, inst->parent->height, inst->frame, bitmap_flags);
362 if (inst->parent->flags & ANF_XPARENT){
363 // bitmap_flags = BMP_XPARENT;
371 while(idx < inst->parent->num_keys){
372 if (( (inst->parent->keys[idx].frame_num-1) <= frame_num) && ( (inst->parent->keys[idx].frame_num-1) > key)) { // find closest key
373 key = inst->parent->keys[idx].frame_num - 1;
374 offset = inst->parent->keys[idx].offset;
376 if ( key == frame_num )
382 if ( key == frame_num ) {
383 inst->frame_num = key;
385 if ( anim_instance_is_streamed(inst) ) {
386 inst->file_offset = inst->parent->file_offset + offset;
388 inst->data = inst->parent->data + offset;
391 anim_check_for_palette_change(inst);
393 if ( anim_instance_is_streamed(inst) ) {
395 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation);
397 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, NULL);
401 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation);
403 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, NULL);
410 if (key > inst->frame_num) // best key is closer than current position
412 inst->frame_num = key;
414 if ( anim_instance_is_streamed(inst) ) {
415 inst->file_offset = inst->parent->file_offset + offset;
417 inst->data = inst->parent->data + offset;
420 anim_check_for_palette_change(inst);
422 if ( anim_instance_is_streamed(inst) ) {
424 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation);
426 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, NULL);
429 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation);
431 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, NULL);
435 while (inst->frame_num != frame_num) {
436 anim_check_for_palette_change(inst);
438 if ( anim_instance_is_streamed(inst) ) {
440 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation);
442 inst->file_offset = unpack_frame_from_file(inst, inst->frame, inst->parent->width*inst->parent->height, NULL);
445 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, inst->parent->palette_translation);
447 inst->data = unpack_frame(inst, inst->data, inst->frame, inst->parent->width*inst->parent->height, NULL);
454 bm = bm_create(16, inst->parent->width, inst->parent->height, inst->frame, bitmap_flags);
462 // frame = frame pixel data to pack
463 // save = memory to store packed data to
464 // size = number of bytes to pack
465 // max = maximum number of packed bytes (size of buffer)
466 // returns: actual number of bytes data packed to or -1 if error
467 int pack_key_frame(ubyte *frame, ubyte *save, long size, long max, int compress_type)
469 int last = -32768, count = 0;
470 long packed_size = 1;
472 switch ( compress_type ) {
473 case PACKING_METHOD_RLE_KEY:
474 *save++ = PACKING_METHOD_RLE_KEY;
476 if (*frame != last || count > 255) {
477 if (packed_size + 3 >= max)
481 if (last == packer_code) {
482 *save++ = (ubyte)packer_code;
483 *save++ = (ubyte)(count - 1);
488 *save++ = (ubyte)last;
493 *save++ = (ubyte)packer_code;
494 *save++ = (ubyte)(count - 1);
495 *save++ = (ubyte)last;
507 if (packed_size + 3 >= max)
511 if (last == packer_code) {
512 *save++ = (ubyte)packer_code;
513 *save++ = (ubyte)(count - 1);
518 *save++ = (ubyte)last;
523 *save++ = (ubyte)packer_code;
524 *save++ = (ubyte)(count - 1);
525 *save++ = (ubyte)last;
530 case PACKING_METHOD_STD_RLE_KEY: {
532 ubyte *dest_start = save;
539 *save++ = PACKING_METHOD_STD_RLE_KEY;
540 for (i=1; i < size; i++ ) {
542 if ( *frame != last ) {
545 if (packed_size + 2 >= max)
548 if ( (count == 1) && !(last & STD_RLE_CODE) ) {
549 *save++ = (ubyte)last;
551 SDL_assert( last != STD_RLE_CODE );
552 // printf("Just packed %d 1 times, since pixel change, no count included\n",last);
555 count |= STD_RLE_CODE;
556 *save++ = (ubyte)count;
557 *save++ = (ubyte)last;
559 // printf("Just packed %d %d times, since pixel change\n",last,count);
570 if ( count == 127 ) {
571 count |= STD_RLE_CODE;
572 *save++ = (ubyte)count;
573 *save++ = (ubyte)last;
576 // printf("Just packed %d %d times, since count overflow\n",last,count);
583 if (packed_size + 2 >= max)
586 if ( (count == 1) && !(last & STD_RLE_CODE) ) {
587 *save++ = (ubyte)last;
589 // printf("Just packed %d 1 times, at end since single pixel, no count\n",last);
590 SDL_assert( last != STD_RLE_CODE );
593 count |= STD_RLE_CODE;
594 *save++ = (ubyte)count;
595 *save++ = (ubyte)last;
597 // printf("Just packed %d %d times, at end since pixel change\n",last,count);
601 SDL_assert(packed_size == (save-dest_start) );
616 // frame = frame pixel data to pack
617 // frame2 = previous frame's pixel data
618 // save = memory to store packed data to
619 // size = number of bytes to pack
620 // max = maximum number of packed bytes (size of buffer)
621 // returns: actual number of bytes data packed to or -1 if error
622 int pack_frame(ubyte *frame, ubyte *frame2, ubyte *save, long size, long max, int compress_type)
624 int pixel, last = -32768, count = 0, i;
625 long packed_size = 1;
627 switch ( compress_type ) {
628 case PACKING_METHOD_RLE: // Hoffoss RLE regular frame
629 *save++ = PACKING_METHOD_RLE;
631 if (*frame != *frame2++)
634 pixel = transparent_code;
636 if (pixel != last || count > 255) {
637 if (packed_size + 3 >= max)
641 if (last == packer_code) {
642 *save++ = (ubyte)packer_code;
643 *save++ = (ubyte)(count - 1);
648 *save++ = (ubyte)last;
653 *save++ = (ubyte)packer_code;
654 *save++ = (ubyte)(count - 1);
655 *save++ = (ubyte)last;
667 if (packed_size + 3 >= max)
671 if (last == packer_code) {
672 *save++ = (ubyte)packer_code;
673 *save++ = (ubyte)(count - 1);
678 *save++ = (ubyte)last;
683 *save++ = (ubyte)(packer_code);
684 *save++ = (ubyte)(count - 1);
685 *save++ = (ubyte)(last);
690 case PACKING_METHOD_STD_RLE: { // high bit count regular RLE frame
692 ubyte *dest_start = save;
697 if (*frame++ != *frame2++)
700 last = transparent_code;
702 *save++ = PACKING_METHOD_STD_RLE;
703 for (i=1; i < size; i++ ) {
705 if (*frame != *frame2++)
708 pixel = transparent_code;
710 if ( pixel != last ) {
713 if (packed_size + 2 >= max)
716 if ( (count == 1) && !(last & STD_RLE_CODE) ) {
717 *save++ = (ubyte)last;
719 SDL_assert( last != STD_RLE_CODE );
722 count |= STD_RLE_CODE;
723 *save++ = (ubyte)count;
724 *save++ = (ubyte)last;
736 if ( count == 127 ) {
737 count |= STD_RLE_CODE;
738 *save++ = (ubyte)count;
739 *save++ = (ubyte)last;
747 if (packed_size + 2 >= max)
750 if ( (count == 1) && !(last & STD_RLE_CODE) ) {
751 *save++ = (ubyte)last;
753 SDL_assert( last != STD_RLE_CODE );
756 count |= STD_RLE_CODE;
757 *save++ = (ubyte)count;
758 *save++ = (ubyte)last;
763 SDL_assert(packed_size == (save-dest_start) );
778 // unpack a pixel given the passed index and the anim_instance's palette, return bytes stuffed
779 int unpack_pixel(anim_instance *ai, ubyte *data, ubyte pix, int aabitmap, int bpp)
786 anim *a = ai->parent;
789 // if this is an aabitmap, don't run through the palette
793 bit_16 = (ushort)pix;
802 // if the pixel value is 255, or is the xparent color, make it so
803 if(((a->palette[pix*3] == a->xparent_r) && (a->palette[pix*3+1] == a->xparent_g) && (a->palette[pix*3+2] == a->xparent_b)) ){
806 bm_set_components((ubyte*)&bit_16, &r, &g, &b, &al);
808 // stuff the 24 bit value
809 memcpy(&bit_24, &ai->parent->palette[pix * 3], 3);
812 bm_24_to_16(bit_24, &bit_16);
819 memcpy(data, &bit_16, sizeof(ushort));
820 return sizeof(ushort);
824 return sizeof(ubyte);
831 // unpack a pixel given the passed index and the anim_instance's palette, return bytes stuffed
832 int unpack_pixel_count(anim_instance *ai, ubyte *data, ubyte pix, int count, int aabitmap, int bpp)
839 anim *a = ai->parent;
843 // if this is an aabitmap, don't run through the palette
847 bit_16 = (ushort)pix;
856 // if the pixel value is 255, or is the xparent color, make it so
857 if(((a->palette[pix*3] == a->xparent_r) && (a->palette[pix*3+1] == a->xparent_g) && (a->palette[pix*3+2] == a->xparent_b)) ){
860 bm_set_components((ubyte*)&bit_16, &r, &g, &b, &al);
862 // stuff the 24 bit value
863 memcpy(&bit_24, &ai->parent->palette[pix * 3], 3);
866 bm_24_to_16(bit_24, &bit_16);
871 for(idx=0; idx<count; idx++){
874 memcpy(data + (idx*2), &bit_16, sizeof(ushort));
877 *(data + idx) = bit_8;
883 return sizeof(ushort) * count;
885 return sizeof(ubyte) * count;
888 // ptr = packed data to unpack
889 // frame = where to store unpacked data to
890 // size = total number of unpacked pixels requested
891 // pal_translate = color translation lookup table (NULL if no palette translation desired)
892 ubyte *unpack_frame(anim_instance *ai, ubyte *ptr, ubyte *frame, int size, ubyte *pal_translate, int aabitmap, int bpp)
894 int xlate_pal, value, count = 0;
896 int pixel_size = (bpp == 16) ? 2 : 1;
898 if ( pal_translate == NULL ) {
905 if (*ptr == PACKING_METHOD_RLE_KEY) { // key frame, Hoffoss's RLE format
909 if (value != packer_code) {
911 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
913 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
930 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
932 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
940 else if ( *ptr == PACKING_METHOD_STD_RLE_KEY) { // key frame, with high bit as count
944 if ( !(value & STD_RLE_CODE) ) {
946 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
948 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
954 count = value & (~STD_RLE_CODE);
958 SDL_assert(size >= 0);
961 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
963 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
970 else if (*ptr == PACKING_METHOD_RLE) { // normal frame, Hoffoss's RLE format
972 // test code, to show unused pixels
973 // memset(frame, 255, size);
978 if (value != packer_code) {
979 if (value != transparent_code) {
981 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
983 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
987 stuffed = pixel_size;
1000 if (++count > size){
1005 SDL_assert(size >= 0);
1007 if (value != transparent_code ) {
1009 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
1011 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
1014 stuffed = count * pixel_size;
1022 else if ( *ptr == PACKING_METHOD_STD_RLE) { // normal frame, with high bit as count
1026 if ( !(value & STD_RLE_CODE) ) {
1027 if (value != transparent_code) {
1029 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
1031 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
1034 stuffed = pixel_size;
1040 count = value & (~STD_RLE_CODE);
1044 SDL_assert(size >= 0);
1046 if (value != transparent_code) {
1048 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
1050 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
1053 stuffed = pixel_size * count;
1061 SDL_assert(0); // unknown packing method
1067 // ptr = packed data to unpack
1068 // frame = where to store unpacked data to
1069 // size = total number of unpacked pixels requested
1070 // pal_translate = color translation lookup table (NULL if no palette translation desired)
1071 int unpack_frame_from_file(anim_instance *ai, ubyte *frame, int size, ubyte *pal_translate, int aabitmap, int bpp)
1073 int xlate_pal, value, count = 0;
1076 int pixel_size = (bpp == 16) ? 2 : 1;
1078 if ( pal_translate == NULL ) {
1085 if (anim_instance_get_byte(ai,offset) == PACKING_METHOD_RLE_KEY) { // key frame, Hoffoss's RLE format
1088 value = anim_instance_get_byte(ai,offset);
1090 if (value != packer_code) {
1092 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
1094 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
1100 count = anim_instance_get_byte(ai,offset);
1103 value = packer_code;
1105 value = anim_instance_get_byte(ai,offset);
1109 if (++count > size){
1114 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
1116 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
1124 else if ( anim_instance_get_byte(ai,offset) == PACKING_METHOD_STD_RLE_KEY) { // key frame, with high bit as count
1127 value = anim_instance_get_byte(ai,offset);
1129 if ( !(value & STD_RLE_CODE) ) {
1131 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
1133 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
1139 count = value & (~STD_RLE_CODE);
1140 value = anim_instance_get_byte(ai,offset);
1144 SDL_assert(size >= 0);
1147 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
1149 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
1156 else if (anim_instance_get_byte(ai,offset) == PACKING_METHOD_RLE) { // normal frame, Hoffoss's RLE format
1158 // test code, to show unused pixels
1159 // memset(frame, 255, size);
1163 value = anim_instance_get_byte(ai,offset);
1165 if (value != packer_code) {
1166 if (value != transparent_code) {
1168 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
1170 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
1173 stuffed = pixel_size;
1179 count = anim_instance_get_byte(ai,offset);
1183 value = packer_code;
1185 value = anim_instance_get_byte(ai,offset);
1188 if (++count > size){
1193 SDL_assert(size >= 0);
1195 if (value != transparent_code ) {
1197 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
1199 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
1202 stuffed = pixel_size * count;
1210 else if ( anim_instance_get_byte(ai,offset) ) { // normal frame, with high bit as count
1213 value = anim_instance_get_byte(ai,offset);
1215 if ( !(value & STD_RLE_CODE) ) {
1216 if (value != transparent_code) {
1218 stuffed = unpack_pixel(ai, frame, pal_translate[value], aabitmap, bpp);
1220 stuffed = unpack_pixel(ai, frame, (ubyte)value, aabitmap, bpp);
1223 stuffed = pixel_size;
1229 count = value & (~STD_RLE_CODE);
1230 value = anim_instance_get_byte(ai,offset);
1234 SDL_assert(size >= 0);
1236 if (value != transparent_code) {
1238 stuffed = unpack_pixel_count(ai, frame, pal_translate[value], count, aabitmap, bpp);
1240 stuffed = unpack_pixel_count(ai, frame, (ubyte)value, count, aabitmap, bpp);
1243 stuffed = pixel_size * count;
1251 Int3(); // unknown packing method
1254 return ai->file_offset + offset;
1258 // TODO: actually convert the frame data to correct palette at this point
1259 void anim_set_palette(anim *ptr)
1261 int i, xparent_found = 0;
1263 // create the palette translation look-up table
1264 for ( i = 0; i < 256; i++ ) {
1266 //if ( (ptr->palette[i*3] == ptr->xparent_r) && (ptr->palette[i*3+1] == ptr->xparent_g) && (ptr->palette[i*3+2] == ptr->xparent_b) ) {
1267 // ptr->palette_translation[i] = 255;
1268 // xparent_found = 1;
1270 // ptr->palette_translation[i] = (ubyte)palette_find( ptr->palette[i*3], ptr->palette[i*3+1], ptr->palette[i*3+2] );
1271 ptr->palette_translation[i] = (ubyte)i;
1275 if ( xparent_found ) {
1276 ptr->flags |= ANF_XPARENT;
1279 ptr->flags &= ~ANF_XPARENT;