1 /* $Id: bitmap.c,v 1.9 2005-08-02 06:15:08 chris 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 * Graphical routines for manipulating grs_bitmaps.
40 void gr_set_bitmap_data (grs_bitmap *bm, unsigned char *data)
43 // if (bm->bm_data!=data)
44 ogl_freebmtexture(bm);
49 grs_bitmap *gr_create_bitmap(int w, int h )
51 return gr_create_bitmap_raw (w, h, d_malloc( MAX_BMP_SIZE(w, h) ));
54 grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
58 new = (grs_bitmap *)d_malloc( sizeof(grs_bitmap) );
59 gr_init_bitmap (new, 0, 0, 0, w, h, w, raw_data);
65 void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data ) // TODO: virtualize
73 bm->bm_rowsize = bytesperline;
77 bm->bm_parent=NULL;bm->gltexture=NULL;
81 gr_set_bitmap_data (bm, data);
84 gr_set_bitmap_data (bm, d_malloc( MAX_BMP_SIZE(w, h) ));
87 #ifdef BITMAP_SELECTOR
92 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline)
94 gr_init_bitmap(bm, mode, x, y, w, h, bytesperline, 0);
95 gr_set_bitmap_data(bm, d_malloc( MAX_BMP_SIZE(w, h) ));
98 void gr_init_bitmap_data (grs_bitmap *bm) // TODO: virtulize
102 // ogl_freebmtexture(bm);//not what we want here.
103 bm->bm_parent=NULL;bm->gltexture=NULL;
107 grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
111 new = (grs_bitmap *)d_malloc( sizeof(grs_bitmap) );
112 gr_init_sub_bitmap (new, bm, x, y, w, h);
117 void gr_free_bitmap(grs_bitmap *bm )
119 gr_free_bitmap_data (bm);
124 void gr_free_sub_bitmap(grs_bitmap *bm )
133 void gr_free_bitmap_data (grs_bitmap *bm) // TODO: virtulize
136 ogl_freebmtexture(bm);
138 if (bm->bm_data != NULL)
139 d_free (bm->bm_data);
143 void gr_init_sub_bitmap (grs_bitmap *bm, grs_bitmap *bmParent, int x, int y, int w, int h ) // TODO: virtualize
145 bm->bm_x = x + bmParent->bm_x;
146 bm->bm_y = y + bmParent->bm_y;
149 bm->bm_flags = bmParent->bm_flags;
150 bm->bm_type = bmParent->bm_type;
151 bm->bm_rowsize = bmParent->bm_rowsize;
154 bm->gltexture=bmParent->gltexture;
155 bm->bm_parent=bmParent;
158 bm->bm_data = bmParent->bm_data+(unsigned int)((y*bmParent->bm_rowsize)+x);
163 void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
165 #if !defined(NO_ASM) && defined(__WATCOMC__)
167 #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
171 "inc dword ptr [ebx+eax*4]" \
178 #elif !defined(NO_ASM) && defined(__GNUC__)
180 inline void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count ) {
182 __asm__ __volatile__ (
185 "movb (%%esi), %%al;"
186 "incl (%%ebx, %%eax, 4);"
187 "movb (%%edi, %%eax), %%al;"
188 "movb %%al, (%%esi);"
192 : "=S" (dummy[0]), "=c" (dummy[1]), "=D" (dummy[2]), "=b" (dummy[3])
193 : "0" (data), "1" (num_pixels), "2" (colormap), "3" (count)
197 #elif !defined(NO_ASM) && defined(_MSC_VER)
199 __inline void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count )
209 inc dword ptr [ebx+eax*4]
218 #else // NO_ASM or unknown compiler
220 void decode_data_asm(ubyte *data, int num_pixels, ubyte *colormap, int *count)
225 for (i = 0; i < num_pixels; i++) {
228 *data = colormap[mapped];
235 void gr_set_bitmap_flags (grs_bitmap *pbm, int flags)
237 pbm->bm_flags = flags;
240 void gr_set_transparent (grs_bitmap *pbm, int bTransparent)
244 gr_set_bitmap_flags (pbm, pbm->bm_flags | BM_FLAG_TRANSPARENT);
248 gr_set_bitmap_flags (pbm, pbm->bm_flags & ~BM_FLAG_TRANSPARENT);
252 void gr_set_super_transparent (grs_bitmap *pbm, int bTransparent)
256 gr_set_bitmap_flags (pbm, pbm->bm_flags & ~BM_FLAG_SUPER_TRANSPARENT);
260 gr_set_bitmap_flags (pbm, pbm->bm_flags | BM_FLAG_SUPER_TRANSPARENT);
264 void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
268 for (i=0; i<256; i++ ) {
272 *colormap++ = gr_find_closest_color( r, g, b );
277 void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
282 if (bmp->bm_type != BM_LINEAR)
283 return; //can't do it
285 // This should be build_colormap_asm, but we're not using invert table, so...
286 build_colormap_good( palette, colormap, freq );
288 if ( (super_transparent_color>=0) && (super_transparent_color<=255))
289 colormap[super_transparent_color] = 254;
291 if ( (transparent_color>=0) && (transparent_color<=255))
292 colormap[transparent_color] = TRANSPARENCY_COLOR;
294 decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
296 if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
297 gr_set_transparent (bmp, 1);
299 if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
300 gr_set_super_transparent (bmp, 0);
303 void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
307 build_colormap_good( palette, colormap, freq );
309 if ( (super_transparent_color>=0) && (super_transparent_color<=255))
310 colormap[super_transparent_color] = 254;
312 if ( (transparent_color>=0) && (transparent_color<=255))
313 colormap[transparent_color] = TRANSPARENCY_COLOR;
315 if (bmp->bm_w == bmp->bm_rowsize)
316 decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
319 ubyte *p = bmp->bm_data;
320 for (y=0;y<bmp->bm_h;y++,p+=bmp->bm_rowsize)
321 decode_data_asm(p, bmp->bm_w, colormap, freq );
324 if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
325 gr_set_transparent (bmp, 1);
327 if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
328 gr_set_super_transparent (bmp, 1);
331 #ifdef BITMAP_SELECTOR
332 int gr_bitmap_assign_selector( grs_bitmap * bmp )
334 if (!dpmi_allocate_selector( bmp->bm_data, bmp->bm_w*bmp->bm_h, &bmp->bm_selector )) {
335 bmp->bm_selector = 0;
342 void gr_bitmap_check_transparency( grs_bitmap * bmp )
349 for (y=0; y<bmp->bm_h; y++ ) {
350 for (x=0; x<bmp->bm_w; x++ ) {
351 if (*data++ == TRANSPARENCY_COLOR ) {
352 gr_set_transparent (bmp, 1);
356 data += bmp->bm_rowsize - bmp->bm_w;