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