]> icculus.org git repositories - btb/d2x.git/blob - 2d/bitmap.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / 2d / bitmap.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Graphical routines for manipulating grs_bitmaps.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include "u_mem.h"
28 #include "gr.h"
29 #include "u_dpmi.h"
30 #include "dxxerror.h"
31
32 #ifdef OGL
33 #include "ogl_init.h"
34 #endif
35
36
37 void gr_set_bitmap_data (grs_bitmap *bm, unsigned char *data)
38 {
39 #ifdef OGL
40 //      if (bm->bm_data!=data)
41                 ogl_freebmtexture(bm);
42 #endif
43         bm->bm_data = data;
44 }
45
46 grs_bitmap *gr_create_bitmap(int w, int h )
47 {
48         return gr_create_bitmap_raw (w, h, d_malloc( MAX_BMP_SIZE(w, h) ));
49 }
50
51 grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
52 {
53     grs_bitmap *new;
54
55     new = (grs_bitmap *)d_malloc( sizeof(grs_bitmap) );
56         gr_init_bitmap (new, 0, 0, 0, w, h, w, raw_data);
57
58     return new;
59 }
60
61
62 void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data ) // TODO: virtualize
63 {
64         bm->bm_x = x;
65         bm->bm_y = y;
66         bm->bm_w = w;
67         bm->bm_h = h;
68         bm->bm_flags = 0;
69         bm->bm_type = mode;
70         bm->bm_rowsize = bytesperline;
71
72         bm->bm_data = NULL;
73 #ifdef OGL
74         bm->bm_parent=NULL;bm->gltexture=NULL;
75 #endif
76
77 //      if (data != 0)
78                 gr_set_bitmap_data (bm, data);
79 /*
80         else
81                 gr_set_bitmap_data (bm, d_malloc( MAX_BMP_SIZE(w, h) ));
82 */
83
84 #ifdef BITMAP_SELECTOR
85         bm->bm_selector = 0;
86 #endif
87 }
88
89 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline)
90 {
91         gr_init_bitmap(bm, mode, x, y, w, h, bytesperline, 0);
92         gr_set_bitmap_data(bm, d_malloc( MAX_BMP_SIZE(w, h) ));
93 }
94
95 void gr_init_bitmap_data (grs_bitmap *bm) // TODO: virtulize
96 {
97         bm->bm_data = NULL;
98 #ifdef OGL
99 //      ogl_freebmtexture(bm);//not what we want here.
100         bm->bm_parent=NULL;bm->gltexture=NULL;
101 #endif
102 }
103
104 grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
105 {
106     grs_bitmap *new;
107
108     new = (grs_bitmap *)d_malloc( sizeof(grs_bitmap) );
109         gr_init_sub_bitmap (new, bm, x, y, w, h);
110
111         return new;
112 }
113
114 void gr_free_bitmap(grs_bitmap *bm )
115 {
116         gr_free_bitmap_data (bm);
117         if (bm!=NULL)
118                 d_free(bm);
119 }
120
121 void gr_free_sub_bitmap(grs_bitmap *bm )
122 {
123         if (bm!=NULL)
124         {
125                 d_free(bm);
126         }
127 }
128
129
130 void gr_free_bitmap_data (grs_bitmap *bm) // TODO: virtulize
131 {
132 #ifdef OGL
133         ogl_freebmtexture(bm);
134 #endif
135         if (bm->bm_data != NULL)
136                 d_free (bm->bm_data);
137         bm->bm_data = NULL;
138 }
139
140 void gr_init_sub_bitmap (grs_bitmap *bm, grs_bitmap *bmParent, int x, int y, int w, int h )     // TODO: virtualize
141 {
142         bm->bm_x = x + bmParent->bm_x;
143         bm->bm_y = y + bmParent->bm_y;
144         bm->bm_w = w;
145         bm->bm_h = h;
146         bm->bm_flags = bmParent->bm_flags;
147         bm->bm_type = bmParent->bm_type;
148         bm->bm_rowsize = bmParent->bm_rowsize;
149
150 #ifdef OGL
151         bm->gltexture=bmParent->gltexture;
152         bm->bm_parent=bmParent;
153 #endif
154         {
155                 bm->bm_data = bmParent->bm_data+(unsigned int)((y*bmParent->bm_rowsize)+x);
156         }
157
158 }
159
160 void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
161
162 #if !defined(NO_ASM) && defined(__WATCOMC__)
163
164 #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
165 "again_ddn:"                                                    \
166         "xor    eax,eax"                                \
167         "mov    al,[esi]"                       \
168         "inc    dword ptr [ebx+eax*4]"          \
169         "mov    al,[edi+eax]"           \
170         "mov    [esi],al"                       \
171         "inc    esi"                                    \
172         "dec    ecx"                                    \
173         "jne    again_ddn"
174
175 #elif !defined(NO_ASM) && defined(__GNUC__)
176
177 inline void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count ) {
178         int dummy[4];
179    __asm__ __volatile__ (
180     "xorl   %%eax,%%eax;"
181 "0:;"
182     "movb   (%%esi), %%al;"
183     "incl   (%%ebx, %%eax, 4);"
184     "movb   (%%edi, %%eax), %%al;"
185     "movb   %%al, (%%esi);"
186     "incl   %%esi;"
187     "decl   %%ecx;"
188     "jne    0b"
189     : "=S" (dummy[0]), "=c" (dummy[1]), "=D" (dummy[2]), "=b" (dummy[3])
190         : "0" (data), "1" (num_pixels), "2" (colormap), "3" (count)
191         : "%eax");
192 }
193
194 #elif !defined(NO_ASM) && defined(_MSC_VER)
195
196 __inline void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count )
197 {
198   __asm {
199         mov esi,[data]
200         mov ecx,[num_pixels]
201         mov edi,[colormap]
202         mov ebx,[count]
203 again_ddn:
204         xor eax,eax
205         mov al,[esi]
206         inc dword ptr [ebx+eax*4]
207         mov al,[edi+eax]
208         mov [esi],al
209         inc esi
210         dec ecx
211         jne again_ddn
212   }
213 }
214
215 #else // NO_ASM or unknown compiler
216
217 void decode_data_asm(ubyte *data, int num_pixels, ubyte *colormap, int *count)
218 {
219         int i;
220         ubyte mapped;
221
222         for (i = 0; i < num_pixels; i++) {
223                 count[*data]++;
224                 mapped = *data;
225                 *data = colormap[mapped];
226                 data++;
227         }
228 }
229
230 #endif
231
232 void gr_set_bitmap_flags (grs_bitmap *pbm, int flags)
233 {
234         pbm->bm_flags = flags;
235 }
236
237 void gr_set_transparent (grs_bitmap *pbm, int bTransparent)
238 {
239         if (bTransparent)
240         {
241                 gr_set_bitmap_flags (pbm, pbm->bm_flags | BM_FLAG_TRANSPARENT);
242         }
243         else
244         {
245                 gr_set_bitmap_flags (pbm, pbm->bm_flags & ~BM_FLAG_TRANSPARENT);
246         }
247 }
248
249 void gr_set_super_transparent (grs_bitmap *pbm, int bTransparent)
250 {
251         if (bTransparent)
252         {
253                 gr_set_bitmap_flags (pbm, pbm->bm_flags & ~BM_FLAG_SUPER_TRANSPARENT);
254         }
255         else
256         {
257                 gr_set_bitmap_flags (pbm, pbm->bm_flags | BM_FLAG_SUPER_TRANSPARENT);
258         }
259 }
260
261 void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
262 {
263         int i, r, g, b;
264
265         for (i=0; i<256; i++ )  {
266                 r = *palette++;
267                 g = *palette++;
268                 b = *palette++;
269                 *colormap++ = gr_find_closest_color( r, g, b );
270                 *freq++ = 0;
271         }
272 }
273
274 void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
275 {
276         ubyte colormap[256];
277         int freq[256];
278
279         if (bmp->bm_type != BM_LINEAR)
280                 return;  //can't do it
281
282         // This should be build_colormap_asm, but we're not using invert table, so...
283         build_colormap_good( palette, colormap, freq );
284
285         if ( (super_transparent_color>=0) && (super_transparent_color<=255))
286                 colormap[super_transparent_color] = 254;
287
288         if ( (transparent_color>=0) && (transparent_color<=255))
289                 colormap[transparent_color] = TRANSPARENCY_COLOR;
290
291         decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
292
293         if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
294                 gr_set_transparent (bmp, 1);
295
296         if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
297                 gr_set_super_transparent (bmp, 0);
298 }
299
300 void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
301 {
302         ubyte colormap[256];
303         int freq[256];
304
305         if (bmp->bm_type != BM_LINEAR)
306                 return; // can't do it
307
308         build_colormap_good( palette, colormap, freq );
309
310         if ( (super_transparent_color>=0) && (super_transparent_color<=255))
311                 colormap[super_transparent_color] = 254;
312
313         if ( (transparent_color>=0) && (transparent_color<=255))
314                 colormap[transparent_color] = TRANSPARENCY_COLOR;
315
316         if (bmp->bm_w == bmp->bm_rowsize)
317                 decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
318         else {
319                 int y;
320                 ubyte *p = bmp->bm_data;
321                 for (y=0;y<bmp->bm_h;y++,p+=bmp->bm_rowsize)
322                         decode_data_asm(p, bmp->bm_w, colormap, freq );
323         }
324
325         if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
326                 gr_set_transparent (bmp, 1);
327
328         if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
329                 gr_set_super_transparent (bmp, 1);
330 }
331
332 #ifdef BITMAP_SELECTOR
333 int gr_bitmap_assign_selector( grs_bitmap * bmp )
334 {
335         if (!dpmi_allocate_selector( bmp->bm_data, bmp->bm_w*bmp->bm_h, &bmp->bm_selector )) {
336                 bmp->bm_selector = 0;
337                 return 1;
338         }
339         return 0;
340 }
341 #endif
342
343 void gr_bitmap_check_transparency( grs_bitmap * bmp )
344 {
345         int x, y;
346         ubyte * data;
347
348         data = bmp->bm_data;
349
350         for (y=0; y<bmp->bm_h; y++ )    {
351                 for (x=0; x<bmp->bm_w; x++ )    {
352                         if (*data++ == TRANSPARENCY_COLOR )     {
353                                 gr_set_transparent (bmp, 1);
354                                 return;
355                         }
356                 }
357                 data += bmp->bm_rowsize - bmp->bm_w;
358         }
359
360         bmp->bm_flags = 0;
361
362 }