]> icculus.org git repositories - btb/d2x.git/blob - 2d/bitmap.c
cd755519c0d93b84d729b395c7de316ab9c941d5
[btb/d2x.git] / 2d / bitmap.c
1 /* $Id: bitmap.c,v 1.8 2005-07-30 01:51:42 chris Exp $ */
2 /*
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.
13 */
14
15 /*
16  *
17  * Graphical routines for manipulating grs_bitmaps.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include "u_mem.h"
29
30
31 #include "gr.h"
32 #include "grdef.h"
33 #include "u_dpmi.h"
34 #include "error.h"
35
36 #ifdef OGL
37 #include "ogl_init.h"
38 #endif
39
40 void gr_set_bitmap_data (grs_bitmap *bm, unsigned char *data)
41 {
42 #ifdef OGL
43 //      if (bm->bm_data!=data)
44                 ogl_freebmtexture(bm);
45 #endif
46         bm->bm_data = data;
47 #ifdef D1XD3D
48         Assert (bm->iMagic == BM_MAGIC_NUMBER);
49         Win32_SetTextureBits (bm, data, bm->bm_flags & BM_FLAG_RLE);
50 #endif
51 }
52
53 grs_bitmap *gr_create_bitmap(int w, int h )
54 {
55         return gr_create_bitmap_raw (w, h, d_malloc( MAX_BMP_SIZE(w, h) ));
56 }
57
58 grs_bitmap *gr_create_bitmap_raw(int w, int h, unsigned char * raw_data )
59 {
60     grs_bitmap *new;
61
62     new = (grs_bitmap *)d_malloc( sizeof(grs_bitmap) );
63         gr_init_bitmap (new, 0, 0, 0, w, h, w, raw_data);
64
65     return new;
66 }
67
68
69 void gr_init_bitmap( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline, unsigned char * data ) // TODO: virtualize
70 {
71 #ifdef D1XD3D
72         Assert (bm->iMagic != BM_MAGIC_NUMBER || bm->pvSurface == NULL);
73 #endif
74
75         bm->bm_x = x;
76         bm->bm_y = y;
77         bm->bm_w = w;
78         bm->bm_h = h;
79         bm->bm_flags = 0;
80         bm->bm_type = mode;
81         bm->bm_rowsize = bytesperline;
82
83         bm->bm_data = NULL;
84 #ifdef D1XD3D
85         bm->iMagic = BM_MAGIC_NUMBER;
86         bm->pvSurface = NULL;
87 #endif
88
89 #ifdef D1XD3D
90         Win32_CreateTexture (bm);
91 #endif
92 #ifdef OGL
93         bm->bm_parent=NULL;bm->gltexture=NULL;
94 #endif
95
96 //      if (data != 0)
97                 gr_set_bitmap_data (bm, data);
98 /*
99         else
100                 gr_set_bitmap_data (bm, d_malloc( MAX_BMP_SIZE(w, h) ));
101 */
102
103 #ifdef BITMAP_SELECTOR
104         bm->bm_selector = 0;
105 #endif
106 }
107
108 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline)
109 {
110         gr_init_bitmap(bm, mode, x, y, w, h, bytesperline, 0);
111         gr_set_bitmap_data(bm, d_malloc( MAX_BMP_SIZE(w, h) ));
112 }
113
114 void gr_init_bitmap_data (grs_bitmap *bm) // TODO: virtulize
115 {
116         bm->bm_data = NULL;
117 #ifdef D1XD3D
118         Assert (bm->iMagic != BM_MAGIC_NUMBER);
119         bm->iMagic = BM_MAGIC_NUMBER;
120         bm->pvSurface = NULL;
121 #endif
122 #ifdef OGL
123 //      ogl_freebmtexture(bm);//not what we want here.
124         bm->bm_parent=NULL;bm->gltexture=NULL;
125 #endif
126 }
127
128 grs_bitmap *gr_create_sub_bitmap(grs_bitmap *bm, int x, int y, int w, int h )
129 {
130     grs_bitmap *new;
131
132     new = (grs_bitmap *)d_malloc( sizeof(grs_bitmap) );
133         gr_init_sub_bitmap (new, bm, x, y, w, h);
134
135         return new;
136 }
137
138 void gr_free_bitmap(grs_bitmap *bm )
139 {
140         gr_free_bitmap_data (bm);
141         if (bm!=NULL)
142                 d_free(bm);
143 }
144
145 void gr_free_sub_bitmap(grs_bitmap *bm )
146 {
147         if (bm!=NULL)
148         {
149 #ifdef D1XD3D
150                 bm->iMagic = 0;
151 #endif
152                 d_free(bm);
153         }
154 }
155
156
157 void gr_free_bitmap_data (grs_bitmap *bm) // TODO: virtulize
158 {
159 #ifdef D1XD3D
160         Assert (bm->iMagic == BM_MAGIC_NUMBER);
161
162         Win32_FreeTexture (bm);
163         bm->iMagic = 0;
164         if (bm->bm_data == BM_D3D_RENDER)
165                 bm->bm_data = NULL;
166 #endif
167 #ifdef OGL
168         ogl_freebmtexture(bm);
169 #endif
170         if (bm->bm_data != NULL)
171                 d_free (bm->bm_data);
172         bm->bm_data = NULL;
173 }
174
175 void gr_init_sub_bitmap (grs_bitmap *bm, grs_bitmap *bmParent, int x, int y, int w, int h )     // TODO: virtualize
176 {
177         bm->bm_x = x + bmParent->bm_x;
178         bm->bm_y = y + bmParent->bm_y;
179         bm->bm_w = w;
180         bm->bm_h = h;
181         bm->bm_flags = bmParent->bm_flags;
182         bm->bm_type = bmParent->bm_type;
183         bm->bm_rowsize = bmParent->bm_rowsize;
184
185 #ifdef OGL
186         bm->gltexture=bmParent->gltexture;
187         bm->bm_parent=bmParent;
188 #endif
189 #ifdef D1XD3D
190         Assert (bmParent->iMagic == BM_MAGIC_NUMBER);
191         bm->iMagic = BM_MAGIC_NUMBER;
192         bm->pvSurface = bmParent->pvSurface;
193         if (bm->bm_type == BM_DIRECTX)
194         {
195                 bm->bm_data = bmParent->bm_data;
196         }
197         else
198 #endif
199         {
200                 bm->bm_data = bmParent->bm_data+(unsigned int)((y*bmParent->bm_rowsize)+x);
201         }
202
203 }
204
205 void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
206
207 #if !defined(NO_ASM) && defined(__WATCOMC__)
208
209 #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
210 "again_ddn:"                                                    \
211         "xor    eax,eax"                                \
212         "mov    al,[esi]"                       \
213         "inc    dword ptr [ebx+eax*4]"          \
214         "mov    al,[edi+eax]"           \
215         "mov    [esi],al"                       \
216         "inc    esi"                                    \
217         "dec    ecx"                                    \
218         "jne    again_ddn"
219
220 #elif !defined(NO_ASM) && defined(__GNUC__)
221
222 inline void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count ) {
223         int dummy[4];
224    __asm__ __volatile__ (
225     "xorl   %%eax,%%eax;"
226 "0:;"
227     "movb   (%%esi), %%al;"
228     "incl   (%%ebx, %%eax, 4);"
229     "movb   (%%edi, %%eax), %%al;"
230     "movb   %%al, (%%esi);"
231     "incl   %%esi;"
232     "decl   %%ecx;"
233     "jne    0b"
234     : "=S" (dummy[0]), "=c" (dummy[1]), "=D" (dummy[2]), "=b" (dummy[3])
235         : "0" (data), "1" (num_pixels), "2" (colormap), "3" (count)
236         : "%eax");
237 }
238
239 #elif !defined(NO_ASM) && defined(_MSC_VER)
240
241 __inline void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count )
242 {
243   __asm {
244         mov esi,[data]
245         mov ecx,[num_pixels]
246         mov edi,[colormap]
247         mov ebx,[count]
248 again_ddn:
249         xor eax,eax
250         mov al,[esi]
251         inc dword ptr [ebx+eax*4]
252         mov al,[edi+eax]
253         mov [esi],al
254         inc esi
255         dec ecx
256         jne again_ddn
257   }
258 }
259
260 #else // NO_ASM or unknown compiler
261
262 void decode_data_asm(ubyte *data, int num_pixels, ubyte *colormap, int *count)
263 {
264         int i;
265         ubyte mapped;
266
267         for (i = 0; i < num_pixels; i++) {
268                 count[*data]++;
269                 mapped = *data;
270                 *data = colormap[mapped];
271                 data++;
272         }
273 }
274
275 #endif
276
277 void gr_set_bitmap_flags (grs_bitmap *pbm, int flags)
278 {
279 #ifdef D1XD3D
280         Assert (pbm->iMagic == BM_MAGIC_NUMBER);
281
282         if (pbm->pvSurface)
283         {
284                 if ((flags & BM_FLAG_TRANSPARENT) != (pbm->bm_flags & BM_FLAG_TRANSPARENT))
285                 {
286                         Win32_SetTransparent (pbm->pvSurface, flags & BM_FLAG_TRANSPARENT);
287                 }
288         }
289 #endif
290         pbm->bm_flags = flags;
291 }
292
293 void gr_set_transparent (grs_bitmap *pbm, int bTransparent)
294 {
295         if (bTransparent)
296         {
297                 gr_set_bitmap_flags (pbm, pbm->bm_flags | BM_FLAG_TRANSPARENT);
298         }
299         else
300         {
301                 gr_set_bitmap_flags (pbm, pbm->bm_flags & ~BM_FLAG_TRANSPARENT);
302         }
303 }
304
305 void gr_set_super_transparent (grs_bitmap *pbm, int bTransparent)
306 {
307         if (bTransparent)
308         {
309                 gr_set_bitmap_flags (pbm, pbm->bm_flags & ~BM_FLAG_SUPER_TRANSPARENT);
310         }
311         else
312         {
313                 gr_set_bitmap_flags (pbm, pbm->bm_flags | BM_FLAG_SUPER_TRANSPARENT);
314         }
315 }
316
317 void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq )
318 {
319         int i, r, g, b;
320
321         for (i=0; i<256; i++ )  {
322                 r = *palette++;
323                 g = *palette++;
324                 b = *palette++;
325                 *colormap++ = gr_find_closest_color( r, g, b );
326                 *freq++ = 0;
327         }
328 }
329
330 void gr_remap_bitmap( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
331 {
332         ubyte colormap[256];
333         int freq[256];
334
335         if (bmp->bm_type != BM_LINEAR)
336                 return;  //can't do it
337
338         // This should be build_colormap_asm, but we're not using invert table, so...
339         build_colormap_good( palette, colormap, freq );
340
341         if ( (super_transparent_color>=0) && (super_transparent_color<=255))
342                 colormap[super_transparent_color] = 254;
343
344         if ( (transparent_color>=0) && (transparent_color<=255))
345                 colormap[transparent_color] = TRANSPARENCY_COLOR;
346
347         decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
348
349         if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
350                 gr_set_transparent (bmp, 1);
351
352         if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
353                 gr_set_super_transparent (bmp, 0);
354 }
355
356 void gr_remap_bitmap_good( grs_bitmap * bmp, ubyte * palette, int transparent_color, int super_transparent_color )
357 {
358         ubyte colormap[256];
359         int freq[256];
360         build_colormap_good( palette, colormap, freq );
361
362         if ( (super_transparent_color>=0) && (super_transparent_color<=255))
363                 colormap[super_transparent_color] = 254;
364
365         if ( (transparent_color>=0) && (transparent_color<=255))
366                 colormap[transparent_color] = TRANSPARENCY_COLOR;
367
368         if (bmp->bm_w == bmp->bm_rowsize)
369                 decode_data_asm(bmp->bm_data, bmp->bm_w * bmp->bm_h, colormap, freq );
370         else {
371                 int y;
372                 ubyte *p = bmp->bm_data;
373                 for (y=0;y<bmp->bm_h;y++,p+=bmp->bm_rowsize)
374                         decode_data_asm(p, bmp->bm_w, colormap, freq );
375         }
376
377         if ( (transparent_color>=0) && (transparent_color<=255) && (freq[transparent_color]>0) )
378                 gr_set_transparent (bmp, 1);
379
380         if ( (super_transparent_color>=0) && (super_transparent_color<=255) && (freq[super_transparent_color]>0) )
381                 gr_set_super_transparent (bmp, 1);
382 }
383
384 #ifdef BITMAP_SELECTOR
385 int gr_bitmap_assign_selector( grs_bitmap * bmp )
386 {
387         if (!dpmi_allocate_selector( bmp->bm_data, bmp->bm_w*bmp->bm_h, &bmp->bm_selector )) {
388                 bmp->bm_selector = 0;
389                 return 1;
390         }
391         return 0;
392 }
393 #endif
394
395 void gr_bitmap_check_transparency( grs_bitmap * bmp )
396 {
397         int x, y;
398         ubyte * data;
399
400         data = bmp->bm_data;
401
402         for (y=0; y<bmp->bm_h; y++ )    {
403                 for (x=0; x<bmp->bm_w; x++ )    {
404                         if (*data++ == TRANSPARENCY_COLOR )     {
405                                 gr_set_transparent (bmp, 1);
406                                 return;
407                         }
408                 }
409                 data += bmp->bm_rowsize - bmp->bm_w;
410         }
411
412         bmp->bm_flags = 0;
413
414 }