1 /* $Id: rle.c,v 1.8 2002-08-22 02:43:35 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Routines to do run length encoding/decoding
20 * Changed shorts to ints in parameters.
24 * Revision 1.10 1995/08/14 14:26:34 allender
25 * changed transparency color to 0
27 * Revision 1.9 1995/07/05 16:07:49 allender
28 * transparency/kitchen chagnes
30 * Revision 1.8 1995/05/12 11:54:03 allender
31 * changed memory stuff again
33 * Revision 1.7 1995/05/11 12:49:19 allender
34 * change transparency color
36 * Revision 1.6 1995/05/04 20:00:30 allender
37 * use NewPtr instead of malloc. Fix gr_rle_scanline (which caused me
38 * *many* hours of frustration!!!!)
40 * Revision 1.5 1995/05/01 08:40:32 allender
41 * trying to find memory bug....this stuff works -- it's johns code
43 * Revision 1.4 1995/04/27 07:38:24 allender
44 * new rle code ala John
46 * Revision 1.3 1995/04/19 14:36:06 allender
47 * *** empty log message ***
49 * Revision 1.2 1995/04/18 12:08:30 allender
50 * *** empty log message ***
52 * Revision 1.1 1995/03/09 09:23:04 allender
56 * --- PC RCS information ---
57 * Revision 1.19 1995/01/14 19:18:31 john
58 * Added assert to check for paged out bitmap.
60 * Revision 1.18 1995/01/14 11:32:07 john
61 * Added rle_cache_flush function.
63 * Revision 1.17 1994/12/13 10:58:27 john
64 * Fixed bug with 2 consecutive calls to get_expanded_Texture
65 * with 2 different bitmaps, returning the same rle texture,
66 * causing doors to disapper.
68 * Revision 1.16 1994/11/30 00:55:03 mike
71 * Revision 1.15 1994/11/24 13:24:44 john
72 * Made sure that some rep movs had the cld set first.
73 * Took some unused functions out.
75 * Revision 1.14 1994/11/23 16:03:46 john
76 * Fixed generic rle'ing to use new bit method.
78 * Revision 1.13 1994/11/23 15:45:51 john
79 * Changed to a 3 bit rle scheme.
81 * Revision 1.12 1994/11/18 22:50:24 john
82 * Changed shorts to ints in parameters.
84 * Revision 1.11 1994/11/14 17:06:13 john
87 * Revision 1.10 1994/11/14 15:54:09 john
88 * Put code in for maybe checking bogus rle data.
90 * Revision 1.9 1994/11/14 15:51:58 john
91 * Added rle_disable_caching variable to prove the stability of my rle
92 * caching code to any non-believers.
94 * Revision 1.8 1994/11/10 10:31:20 john
95 * Reduce cache buffers to 16.
97 * Revision 1.7 1994/11/09 19:53:43 john
98 * Added texture rle caching.
100 * Revision 1.6 1994/11/09 17:41:44 john
101 * Made a slow version of rle bitblt to svga, modex.
103 * Revision 1.5 1994/11/09 17:07:50 john
104 * Fixed bug with bitmap that gets bigger with rle.
106 * Revision 1.4 1994/11/09 16:35:17 john
107 * First version with working RLE bitmaps.
109 * Revision 1.3 1994/10/26 12:54:47 john
110 * Fixed bug with decode that used rep movsd instead of
113 * Revision 1.2 1994/10/06 17:05:25 john
114 * First version of rle stuff.
116 * Revision 1.1 1994/10/06 16:53:34 john
128 static char rcsid[] = "$Id: rle.c,v 1.8 2002-08-22 02:43:35 btb Exp $";
146 #define RLE_CODE 0xE0
147 #define NOT_RLE_CODE 31
149 void rle_expand_texture_sub( grs_bitmap * bmp, grs_bitmap * rle_temp_bitmap_1 );
153 int gr_rle_decode_asm( ubyte * src, ubyte * dest );
154 #pragma aux gr_rle_decode_asm parm [esi] [edi] value [edi] modify exact [eax ebx ecx edx esi edi] = \
190 void rle_stosb(char *dest, int len, int color);
191 #pragma aux rle_stosb = "cld rep stosb" parm [edi] [ecx] [eax] modify exact [edi ecx];
192 #elif defined __GNUC__
193 static inline int gr_rle_decode_asm( ubyte * src, ubyte * dest ) {
196 __asm__ __volatile__ (
198 " xorl %%ecx, %%ecx;"
201 " movb %%al,(%%edi);"
204 " movb (%%esi), %%al;"
213 " movb (%%esi), %%al;"
219 " movb %%al, (%%edi);"
223 : "=D" (__ret), "=S" (dummy) : "1" (src), "D" (dest) : "%eax", "%ecx");
227 static inline void rle_stosb(char *dest, int len, int color) {
229 __asm__ __volatile__ (
231 : "=D" (dummy[0]), "=c" (dummy[1])
232 : "0" (dest), "1" (len), "a" (color) );
234 #elif defined _MSC_VER
235 __inline int gr_rle_decode_asm( ubyte * src, ubyte * dest )
274 __inline void rle_stosb(char *dest, int len, int color)
288 /* Well, if inline assembler is not supported for this compiler, we don't
289 **really** want ASM... */
294 void rle_stosb(ubyte *dest, int len, int color)
297 for (i=0; i<len; i++ )
302 void gr_rle_decode( ubyte * src, ubyte * dest )
306 ubyte data, count = 0;
310 if ( (data & RLE_CODE) != RLE_CODE ) {
313 count = data & NOT_RLE_CODE;
314 if (count==0) return;
316 for (i=0; i<count; i++ )
321 gr_rle_decode_asm( src, dest );
325 // Given pointer to start of one scanline of rle data, uncompress it to
326 // dest, from source pixels x1 to x2.
327 void gr_rle_expand_scanline_masked( ubyte *dest, ubyte *src, int x1, int x2 )
333 if ( x2 < x1 ) return;
337 if ( color == RLE_CODE ) return;
338 if ( (color & RLE_CODE)==RLE_CODE ) {
339 count = color & (~RLE_CODE);
349 // we know have '*count' pixels of 'color'.
351 if ( x1+count > x2 ) {
353 if ( color != TRANSPARENCY_COLOR ) rle_stosb( dest, count, color );
357 if ( color != TRANSPARENCY_COLOR ) rle_stosb( dest, count, color );
363 if ( color == RLE_CODE ) return;
364 if ( (color & RLE_CODE) == (RLE_CODE) ) {
365 count = color & (~RLE_CODE);
371 // we know have '*count' pixels of 'color'.
372 if ( i+count <= x2 ) {
373 if ( color != TRANSPARENCY_COLOR )rle_stosb( dest, count, color );
378 if ( color != TRANSPARENCY_COLOR )rle_stosb( dest, count, color );
386 void gr_rle_expand_scanline( ubyte *dest, ubyte *src, int x1, int x2 )
392 if ( x2 < x1 ) return;
396 if ( color == RLE_CODE ) return;
397 if ( (color & RLE_CODE)==RLE_CODE ) {
398 count = color & (~RLE_CODE);
408 // we know have '*count' pixels of 'color'.
410 if ( x1+count > x2 ) {
412 rle_stosb( dest, count, color );
416 rle_stosb( dest, count, color );
422 if ( color == RLE_CODE ) return;
423 if ( (color & RLE_CODE)==RLE_CODE ) {
424 count = color & (~RLE_CODE);
430 // we know have '*count' pixels of 'color'.
431 if ( i+count <= x2 ) {
432 rle_stosb( dest, count, color );
437 rle_stosb( dest, count, color );
445 int gr_rle_encode( int org_size, ubyte *src, ubyte *dest )
456 for (i=1; i<org_size; i++ ) {
460 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
462 Assert( oc != RLE_CODE );
473 if ( count == NOT_RLE_CODE ) {
481 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
483 Assert( oc != RLE_CODE );
492 return dest-dest_start;
496 int gr_rle_getsize( int org_size, ubyte *src )
506 for (i=1; i<org_size; i++ ) {
510 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
521 if ( count == NOT_RLE_CODE ) {
528 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
540 int gr_bitmap_rle_compress( grs_bitmap * bmp )
547 // first must check to see if this is large bitmap.
549 for (y=0; y<bmp->bm_h; y++ ) {
550 d1= gr_rle_getsize( bmp->bm_w, &bmp->bm_data[bmp->bm_w*y] );
557 rle_data=d_malloc( (bmp->bm_w+1) * bmp->bm_h );
558 if (rle_data==NULL) return 0;
560 doffset = 4 + bmp->bm_h;
562 doffset = 4 + (2 * bmp->bm_h); // each row of rle'd bitmap has short instead of byte offset now
564 for (y=0; y<bmp->bm_h; y++ ) {
565 d1= gr_rle_getsize( bmp->bm_w, &bmp->bm_data[bmp->bm_w*y] );
566 if ( ((doffset+d1) > bmp->bm_w*bmp->bm_h) || (d1 > (large_rle?32767:255) ) ) {
570 d = gr_rle_encode( bmp->bm_w, &bmp->bm_data[bmp->bm_w*y], &rle_data[doffset] );
574 *((short *)&(rle_data[(y*2)+4])) = (short)d;
578 //mprintf( 0, "Bitmap of size %dx%d, (%d bytes) went down to %d bytes\n", bmp->bm_w, bmp->bm_h, bmp->bm_h*bmp->bm_w, doffset );
579 memcpy( rle_data, &doffset, 4 );
580 memcpy( bmp->bm_data, rle_data, doffset );
582 bmp->bm_flags |= BM_FLAG_RLE;
584 bmp->bm_flags |= BM_FLAG_RLE_BIG;
588 #define MAX_CACHE_BITMAPS 32
590 typedef struct rle_cache_element {
591 grs_bitmap * rle_bitmap;
593 grs_bitmap * expanded_bitmap;
597 int rle_cache_initialized = 0;
600 rle_cache_element rle_cache[MAX_CACHE_BITMAPS];
605 void rle_cache_close(void)
607 if (rle_cache_initialized) {
609 rle_cache_initialized = 0;
610 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
611 gr_free_bitmap(rle_cache[i].expanded_bitmap);
616 void rle_cache_init()
619 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
620 rle_cache[i].rle_bitmap = NULL;
621 rle_cache[i].expanded_bitmap = gr_create_bitmap( 64, 64 );
622 rle_cache[i].last_used = 0;
623 Assert( rle_cache[i].expanded_bitmap != NULL );
625 rle_cache_initialized = 1;
626 atexit( rle_cache_close );
629 void rle_cache_flush()
632 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
633 rle_cache[i].rle_bitmap = NULL;
634 rle_cache[i].last_used = 0;
640 grs_bitmap * rle_expand_texture( grs_bitmap * bmp )
643 int lowest_count, lc;
644 int least_recently_used;
646 if (!rle_cache_initialized) rle_cache_init();
648 Assert( !(bmp->bm_flags & BM_FLAG_PAGED_OUT) );
652 if ( rle_counter < lc ) {
653 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
654 rle_cache[i].rle_bitmap = NULL;
655 rle_cache[i].last_used = 0;
659 // if (((rle_counter % 100)==1) && (rle_hits+rle_misses > 0))
660 // mprintf(( 0, "RLE-CACHE %d%%, H:%d, M:%d\n", (rle_misses*100)/(rle_hits+rle_misses), rle_hits, rle_misses ));
662 lowest_count = rle_cache[rle_next].last_used;
663 least_recently_used = rle_next;
665 if ( rle_next >= MAX_CACHE_BITMAPS )
668 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
669 if (rle_cache[i].rle_bitmap == bmp) {
671 rle_cache[i].last_used = rle_counter;
672 return rle_cache[i].expanded_bitmap;
674 if ( rle_cache[i].last_used < lowest_count ) {
675 lowest_count = rle_cache[i].last_used;
676 least_recently_used = i;
680 Assert(bmp->bm_w<=64 && bmp->bm_h<=64); //dest buffer is 64x64
682 rle_expand_texture_sub( bmp, rle_cache[least_recently_used].expanded_bitmap );
683 rle_cache[least_recently_used].rle_bitmap = bmp;
684 rle_cache[least_recently_used].last_used = rle_counter;
685 return rle_cache[least_recently_used].expanded_bitmap;
689 void rle_expand_texture_sub( grs_bitmap * bmp, grs_bitmap * rle_temp_bitmap_1 )
691 unsigned char * dbits;
692 unsigned char * sbits;
695 unsigned char * dbits1;
699 Assert (bmp->iMagic == BM_MAGIC_NUMBER);
702 sbits = &bmp->bm_data[4 + bmp->bm_h];
703 dbits = rle_temp_bitmap_1->bm_data;
705 rle_temp_bitmap_1->bm_flags = bmp->bm_flags & (~BM_FLAG_RLE);
707 for (i=0; i < bmp->bm_h; i++ ) {
709 dbits1=(unsigned char *)gr_rle_decode_asm( sbits, dbits );
711 gr_rle_decode( sbits, dbits );
713 sbits += (int)bmp->bm_data[4+i];
716 Assert( dbits == dbits1 ); // Get John, bogus rle data!
720 gr_set_bitmap_data (rle_temp_bitmap_1, rle_temp_bitmap_1->bm_data);
724 void gr_rle_expand_scanline_generic( grs_bitmap * dest, int dx, int dy, ubyte *src, int x1, int x2, int masked )
730 if ( x2 < x1 ) return;
734 if ( color == RLE_CODE ) return;
735 if ( (color & RLE_CODE) == RLE_CODE ) {
736 count = color & NOT_RLE_CODE;
746 // we know have '*count' pixels of 'color'.
748 if ( x1+count > x2 ) {
750 if (!masked || color != 255)
751 for ( j=0; j<count; j++ )
752 gr_bm_pixel( dest, dx++, dy, color );
756 if (masked && color == 255)
759 for ( j=0; j<count; j++ )
760 gr_bm_pixel( dest, dx++, dy, color );
765 if ( color == RLE_CODE ) return;
766 if ( (color & RLE_CODE) == RLE_CODE ) {
767 count = color & NOT_RLE_CODE;
773 // we know have '*count' pixels of 'color'.
774 if ( i+count <= x2 ) {
775 if (masked && color == 255)
778 for ( j=0; j<count; j++ )
779 gr_bm_pixel( dest, dx++, dy, color );
783 if (masked && color == 255)
786 for ( j=0; j<count; j++ )
787 gr_bm_pixel( dest, dx++, dy, color );
794 * swaps entries 0 and 255 in an RLE bitmap without uncompressing it
796 * doesn't handle RLE_BIG yet, but neither does anything else...
797 * LEAKS LOTS OF MEMORY!
799 void rle_swap_0_255(grs_bitmap *bmp)
802 unsigned char *ptr, *ptr2, *temp, *start;
804 if (bmp->bm_flags & BM_FLAG_RLE_BIG)
807 temp = d_malloc(4 + bmp->bm_h + (bmp->bm_w + 1) * bmp->bm_h);
810 ptr = bmp->bm_data + 4 + bmp->bm_h;
811 ptr2 = temp + 4 + bmp->bm_h;
812 for (i = 0; i < bmp->bm_h; i++) {
814 for (j = 0; j < bmp->bm_data[4 + i]; j++) {
815 if ((ptr[j] & RLE_CODE) != RLE_CODE) {
817 *ptr2++ = RLE_CODE | 1;
823 if ((ptr[j] & NOT_RLE_CODE) == 0)
828 else if (ptr[j] == 255)
834 temp[4 + i] = ptr2 - start;
835 *((int *)temp) += ptr2 - start;
836 ptr += bmp->bm_data[4 + i];