1 /* $Id: rle.c,v 1.10 2002-09-05 07:57:11 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.10 2002-09-05 07:57:11 btb Exp $";
146 #define RLE_CODE 0xE0
147 #define NOT_RLE_CODE 31
149 #if !defined(NO_ASM) && defined(__WATCOMC__)
150 #define RLE_DECODE_ASM
152 ubyte *gr_rle_decode_asm( ubyte * src, ubyte * dest );
153 #pragma aux gr_rle_decode_asm parm [esi] [edi] value [edi] modify exact [eax ebx ecx edx esi edi] = \
189 #elif !defined(NO_ASM) && defined(__GNUC__)
190 #define RLE_DECODE_ASM
192 static inline int gr_rle_decode_asm( ubyte * src, ubyte * dest ) {
195 __asm__ __volatile__ (
197 " xorl %%ecx, %%ecx;"
200 " movb %%al,(%%edi);"
203 " movb (%%esi), %%al;"
212 " movb (%%esi), %%al;"
218 " movb %%al, (%%edi);"
222 : "=D" (__ret), "=S" (dummy) : "1" (src), "D" (dest) : "%eax", "%ecx");
226 #elif !defined(NO_ASM) && defined(_MSC_VER)
227 #define RLE_DECODE_ASM
229 __inline int gr_rle_decode_asm( ubyte * src, ubyte * dest )
270 #ifdef RLE_DECODE_ASM
272 void gr_rle_decode( ubyte * src, ubyte * dest, int dest_len )
276 dest_end = (ubyte *)gr_rle_decode_asm( src, dest );
278 Assert(dest_end-src < dest_len);
281 #else // NO_ASM or unknown compiler
283 void gr_rle_decode( ubyte * src, ubyte * dest )
286 ubyte data, count = 0;
290 if ( (data & RLE_CODE) != RLE_CODE ) {
293 count = data & NOT_RLE_CODE;
297 for (i = 0; i < count; i++)
305 void rle_stosb(char *dest, int len, int color);
307 #if !defined(NO_ASM) && defined(__WATCOMC__)
309 #pragma aux rle_stosb = "cld rep stosb" parm [edi] [ecx] [eax] modify exact [edi ecx];
311 #elif !defined(NO_ASM) && defined(__GNUC__)
313 inline void rle_stosb(char *dest, int len, int color) {
315 __asm__ __volatile__ (
317 : "=D" (dummy[0]), "=c" (dummy[1])
318 : "0" (dest), "1" (len), "a" (color) );
321 #elif !defined(NO_ASM) && defined(_MSC_VER)
323 __inline void rle_stosb(char *dest, int len, int color)
334 #else // NO_ASM or unknown compiler
336 void rle_stosb(char *dest, int len, int color)
339 for (i=0; i<len; i++ )
345 // Given pointer to start of one scanline of rle data, uncompress it to
346 // dest, from source pixels x1 to x2.
347 void gr_rle_expand_scanline_masked( ubyte *dest, ubyte *src, int x1, int x2 )
353 if ( x2 < x1 ) return;
358 if ( color == RLE_CODE ) return;
359 if ( (color & RLE_CODE)==RLE_CODE ) {
360 count = color & (~RLE_CODE);
370 // we know have '*count' pixels of 'color'.
372 if ( x1+count > x2 ) {
374 if ( color != TRANSPARENCY_COLOR ) rle_stosb( dest, count, color );
378 if ( color != TRANSPARENCY_COLOR ) rle_stosb( dest, count, color );
384 if ( color == RLE_CODE ) return;
385 if ( (color & RLE_CODE) == (RLE_CODE) ) {
386 count = color & (~RLE_CODE);
392 // we know have '*count' pixels of 'color'.
393 if ( i+count <= x2 ) {
394 if ( color != TRANSPARENCY_COLOR )rle_stosb( dest, count, color );
399 if ( color != TRANSPARENCY_COLOR )rle_stosb( dest, count, color );
407 void gr_rle_expand_scanline( ubyte *dest, ubyte *src, int x1, int x2 )
413 if ( x2 < x1 ) return;
418 if ( color == RLE_CODE ) return;
419 if ( (color & RLE_CODE)==RLE_CODE ) {
420 count = color & (~RLE_CODE);
430 // we know have '*count' pixels of 'color'.
432 if ( x1+count > x2 ) {
434 rle_stosb( dest, count, color );
438 rle_stosb( dest, count, color );
444 if ( color == RLE_CODE ) return;
445 if ( (color & RLE_CODE)==RLE_CODE ) {
446 count = color & (~RLE_CODE);
452 // we know have '*count' pixels of 'color'.
453 if ( i+count <= x2 ) {
454 rle_stosb( dest, count, color );
459 rle_stosb( dest, count, color );
467 int gr_rle_encode( int org_size, ubyte *src, ubyte *dest )
478 for (i=1; i<org_size; i++ ) {
482 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
484 Assert( oc != RLE_CODE );
495 if ( count == NOT_RLE_CODE ) {
503 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
505 Assert( oc != RLE_CODE );
514 return dest-dest_start;
518 int gr_rle_getsize( int org_size, ubyte *src )
528 for (i=1; i<org_size; i++ ) {
532 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
543 if ( count == NOT_RLE_CODE ) {
550 if ( (count==1) && ((oc & RLE_CODE)!=RLE_CODE) ) {
562 int gr_bitmap_rle_compress( grs_bitmap * bmp )
569 // first must check to see if this is large bitmap.
571 for (y=0; y<bmp->bm_h; y++ ) {
572 d1= gr_rle_getsize( bmp->bm_w, &bmp->bm_data[bmp->bm_w*y] );
579 rle_data=d_malloc( (bmp->bm_w+1) * bmp->bm_h );
580 if (rle_data==NULL) return 0;
582 doffset = 4 + bmp->bm_h;
584 doffset = 4 + (2 * bmp->bm_h); // each row of rle'd bitmap has short instead of byte offset now
586 for (y=0; y<bmp->bm_h; y++ ) {
587 d1= gr_rle_getsize( bmp->bm_w, &bmp->bm_data[bmp->bm_w*y] );
588 if ( ((doffset+d1) > bmp->bm_w*bmp->bm_h) || (d1 > (large_rle?32767:255) ) ) {
592 d = gr_rle_encode( bmp->bm_w, &bmp->bm_data[bmp->bm_w*y], &rle_data[doffset] );
596 *((short *)&(rle_data[(y*2)+4])) = (short)d;
600 //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 );
601 memcpy( rle_data, &doffset, 4 );
602 memcpy( bmp->bm_data, rle_data, doffset );
604 bmp->bm_flags |= BM_FLAG_RLE;
606 bmp->bm_flags |= BM_FLAG_RLE_BIG;
610 #define MAX_CACHE_BITMAPS 32
612 typedef struct rle_cache_element {
613 grs_bitmap * rle_bitmap;
615 grs_bitmap * expanded_bitmap;
619 int rle_cache_initialized = 0;
622 rle_cache_element rle_cache[MAX_CACHE_BITMAPS];
627 void rle_cache_close(void)
629 if (rle_cache_initialized) {
631 rle_cache_initialized = 0;
632 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
633 gr_free_bitmap(rle_cache[i].expanded_bitmap);
638 void rle_cache_init()
641 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
642 rle_cache[i].rle_bitmap = NULL;
643 rle_cache[i].expanded_bitmap = gr_create_bitmap( 64, 64 );
644 rle_cache[i].last_used = 0;
645 Assert( rle_cache[i].expanded_bitmap != NULL );
647 rle_cache_initialized = 1;
648 atexit( rle_cache_close );
651 void rle_cache_flush()
654 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
655 rle_cache[i].rle_bitmap = NULL;
656 rle_cache[i].last_used = 0;
660 void rle_expand_texture_sub( grs_bitmap * bmp, grs_bitmap * rle_temp_bitmap_1 )
662 unsigned char * dbits;
663 unsigned char * sbits;
665 #ifdef RLE_DECODE_ASM
666 unsigned char * dbits1;
670 Assert (bmp->iMagic == BM_MAGIC_NUMBER);
673 sbits = &bmp->bm_data[4 + bmp->bm_h];
674 dbits = rle_temp_bitmap_1->bm_data;
676 rle_temp_bitmap_1->bm_flags = bmp->bm_flags & (~BM_FLAG_RLE);
678 for (i=0; i < bmp->bm_h; i++ ) {
679 #ifdef RLE_DECODE_ASM
680 dbits1=(unsigned char *)gr_rle_decode_asm( sbits, dbits );
682 gr_rle_decode( sbits, dbits );
684 sbits += (int)bmp->bm_data[4+i];
686 #ifdef RLE_DECODE_ASM
687 Assert( dbits == dbits1 ); // Get John, bogus rle data!
691 gr_set_bitmap_data (rle_temp_bitmap_1, rle_temp_bitmap_1->bm_data);
695 #if defined(POLY_ACC)
696 grs_bitmap *rle_get_id_sub(grs_bitmap *bmp)
700 for (i=0;i<MAX_CACHE_BITMAPS;i++) {
701 if (rle_cache[i].expanded_bitmap == bmp) {
702 return rle_cache[i].rle_bitmap;
710 grs_bitmap * rle_expand_texture( grs_bitmap * bmp )
713 int lowest_count, lc;
714 int least_recently_used;
716 if (!rle_cache_initialized) rle_cache_init();
718 Assert( !(bmp->bm_flags & BM_FLAG_PAGED_OUT) );
722 if ( rle_counter < lc ) {
723 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
724 rle_cache[i].rle_bitmap = NULL;
725 rle_cache[i].last_used = 0;
729 // if (((rle_counter % 100)==1) && (rle_hits+rle_misses > 0))
730 // mprintf(( 0, "RLE-CACHE %d%%, H:%d, M:%d\n", (rle_misses*100)/(rle_hits+rle_misses), rle_hits, rle_misses ));
732 lowest_count = rle_cache[rle_next].last_used;
733 least_recently_used = rle_next;
735 if ( rle_next >= MAX_CACHE_BITMAPS )
738 for (i=0; i<MAX_CACHE_BITMAPS; i++ ) {
739 if (rle_cache[i].rle_bitmap == bmp) {
741 rle_cache[i].last_used = rle_counter;
742 return rle_cache[i].expanded_bitmap;
744 if ( rle_cache[i].last_used < lowest_count ) {
745 lowest_count = rle_cache[i].last_used;
746 least_recently_used = i;
750 Assert(bmp->bm_w<=64 && bmp->bm_h<=64); //dest buffer is 64x64
752 rle_expand_texture_sub( bmp, rle_cache[least_recently_used].expanded_bitmap );
753 rle_cache[least_recently_used].rle_bitmap = bmp;
754 rle_cache[least_recently_used].last_used = rle_counter;
755 return rle_cache[least_recently_used].expanded_bitmap;
759 void gr_rle_expand_scanline_generic( grs_bitmap * dest, int dx, int dy, ubyte *src, int x1, int x2 )
765 if ( x2 < x1 ) return;
770 if ( color == RLE_CODE ) return;
771 if ( (color & RLE_CODE) == RLE_CODE ) {
772 count = color & NOT_RLE_CODE;
782 // we know have '*count' pixels of 'color'.
784 if ( x1+count > x2 ) {
786 for ( j=0; j<count; j++ )
787 gr_bm_pixel( dest, dx++, dy, color );
791 for ( j=0; j<count; j++ )
792 gr_bm_pixel( dest, dx++, dy, color );
797 if ( color == RLE_CODE ) return;
798 if ( (color & RLE_CODE) == RLE_CODE ) {
799 count = color & NOT_RLE_CODE;
805 // we know have '*count' pixels of 'color'.
806 if ( i+count <= x2 ) {
807 for ( j=0; j<count; j++ )
808 gr_bm_pixel( dest, dx++, dy, color );
812 for ( j=0; j<count; j++ )
813 gr_bm_pixel( dest, dx++, dy, color );
819 void gr_rle_expand_scanline_generic_masked( grs_bitmap * dest, int dx, int dy, ubyte *src, int x1, int x2 )
825 if ( x2 < x1 ) return;
830 if ( color == RLE_CODE ) return;
831 if ( (color & RLE_CODE) == RLE_CODE ) {
832 count = color & NOT_RLE_CODE;
842 // we know have '*count' pixels of 'color'.
844 if ( x1+count > x2 ) {
846 if (color != TRANSPARENCY_COLOR) {
847 for ( j=0; j<count; j++ )
848 gr_bm_pixel( dest, dx++, dy, color );
853 if ( color != TRANSPARENCY_COLOR ) {
854 for ( j=0; j<count; j++ )
855 gr_bm_pixel( dest, dx++, dy, color );
862 if ( color == RLE_CODE ) return;
863 if ( (color & RLE_CODE) == RLE_CODE ) {
864 count = color & NOT_RLE_CODE;
870 // we know have '*count' pixels of 'color'.
871 if ( i+count <= x2 ) {
872 if ( color != TRANSPARENCY_COLOR ) {
873 for ( j=0; j<count; j++ )
874 gr_bm_pixel( dest, dx++, dy, color );
880 if ( color != TRANSPARENCY_COLOR ) {
881 for ( j=0; j<count; j++ )
882 gr_bm_pixel( dest, dx++, dy, color );
892 void gr_rle_expand_scanline_svga_masked( grs_bitmap * dest, int dx, int dy, ubyte *src, int x1, int x2 )
897 ubyte * vram = (ubyte *)0xA0000;
898 int VideoLocation,page,offset;
900 if ( x2 < x1 ) return;
902 VideoLocation = (unsigned int)dest->bm_data + (dest->bm_rowsize * dy) + dx;
903 page = VideoLocation >> 16;
904 offset = VideoLocation & 0xFFFF;
906 gr_vesa_setpage( page );
908 if ( (offset + (x2-x1+1)) < 65536 ) {
909 // We don't cross a svga page, so blit it fast!
910 gr_rle_expand_scanline_masked( &vram[offset], src, x1, x2 );
917 if ( color == RLE_CODE ) return;
918 if ( (color & RLE_CODE) == RLE_CODE ) {
919 count = color & NOT_RLE_CODE;
929 // we know have '*count' pixels of 'color'.
931 if ( x1+count > x2 ) {
933 if (color != TRANSPARENCY_COLOR) {
934 for ( j=0; j<count; j++ ) {
935 vram[offset++] = color;
936 if ( offset >= 65536 ) {
939 gr_vesa_setpage(page);
946 if ( color != TRANSPARENCY_COLOR ) {
947 for ( j=0; j<count; j++ ) {
948 vram[offset++] = color;
949 if ( offset >= 65536 ) {
952 gr_vesa_setpage(page);
957 if ( offset >= 65536 ) {
960 gr_vesa_setpage(page);
967 if ( color == RLE_CODE ) return;
968 if ( (color & RLE_CODE) == RLE_CODE ) {
969 count = color & NOT_RLE_CODE;
975 // we know have '*count' pixels of 'color'.
976 if ( i+count <= x2 ) {
977 if ( color != TRANSPARENCY_COLOR ) {
978 for ( j=0; j<count; j++ ) {
979 vram[offset++] = color;
980 if ( offset >= 65536 ) {
983 gr_vesa_setpage(page);
988 if ( offset >= 65536 ) {
991 gr_vesa_setpage(page);
997 if ( color != TRANSPARENCY_COLOR ) {
998 for ( j=0; j<count; j++ ) {
999 vram[offset++] = color;
1000 if ( offset >= 65536 ) {
1003 gr_vesa_setpage(page);
1008 if ( offset >= 65536 ) {
1011 gr_vesa_setpage(page);
1022 * swaps entries 0 and 255 in an RLE bitmap without uncompressing it
1024 * doesn't handle RLE_BIG yet, but neither does anything else...
1025 * LEAKS LOTS OF MEMORY!
1027 void rle_swap_0_255(grs_bitmap *bmp)
1030 unsigned char *ptr, *ptr2, *temp, *start;
1032 if (bmp->bm_flags & BM_FLAG_RLE_BIG)
1035 temp = d_malloc(4 + bmp->bm_h + (bmp->bm_w + 1) * bmp->bm_h);
1038 ptr = bmp->bm_data + 4 + bmp->bm_h;
1039 ptr2 = temp + 4 + bmp->bm_h;
1040 for (i = 0; i < bmp->bm_h; i++) {
1042 for (j = 0; j < bmp->bm_data[4 + i]; j++) {
1043 if ((ptr[j] & RLE_CODE) != RLE_CODE) {
1045 *ptr2++ = RLE_CODE | 1;
1051 if ((ptr[j] & NOT_RLE_CODE) == 0)
1056 else if (ptr[j] == 255)
1062 temp[4 + i] = ptr2 - start;
1063 *((int *)temp) += ptr2 - start;
1064 ptr += bmp->bm_data[4 + i];
1066 bmp->bm_data = temp;