]> icculus.org git repositories - btb/d2x.git/blob - 2d/palette.c
remove rcs tags
[btb/d2x.git] / 2d / palette.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  * Graphical routines for setting the palette
16  *
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <conf.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "pstypes.h"
28 #include "u_mem.h"
29 #include "gr.h"
30 #include "grdef.h"
31 #include "cfile.h"
32 #include "error.h"
33 #include "mono.h"
34 #include "fix.h"
35 //added/remove by dph on 1/9/99
36 //#include "key.h"
37 //end remove
38
39 #include "palette.h"
40
41 extern int gr_installed;
42
43 #define SQUARE(x) ((x)*(x))
44
45 #define MAX_COMPUTED_COLORS     32
46
47 int     Num_computed_colors=0;
48
49 typedef struct {
50         ubyte   r,g,b,color_num;
51 } color_record;
52
53 color_record Computed_colors[MAX_COMPUTED_COLORS];
54
55 ubyte gr_palette[256*3];
56 ubyte gr_current_pal[256*3];
57 ubyte gr_fade_table[256*34];
58
59 ubyte gr_palette_gamma = 0;
60 int gr_palette_gamma_param = 0;
61 ubyte gr_palette_faded_out = 1;
62
63 int grd_fades_disabled=0;   // Used to skip fading for development
64
65 void gr_palette_set_gamma( int gamma )
66 {
67         if ( gamma < 0 ) gamma = 0;
68 //added/changed on 10/27/98 by Victor Rachels to increase brightness slider
69         if ( gamma > 16 ) gamma = 16;      //was 8
70 //end this section change - Victor Rachels
71
72         if (gr_palette_gamma_param != gamma )   {
73                 gr_palette_gamma_param = gamma;
74                 gr_palette_gamma = gamma;
75                 if (!gr_palette_faded_out)
76                         gr_palette_load( gr_palette );
77         }
78 }
79
80 int gr_palette_get_gamma()
81 {
82         return gr_palette_gamma_param;
83 }
84
85
86 void gr_copy_palette(ubyte *gr_palette, ubyte *pal, int size)
87 {
88                 memcpy(gr_palette, pal, size);
89
90                 Num_computed_colors = 0;
91 }
92
93
94 void gr_use_palette_table( char * filename )
95 {
96         CFILE *fp;
97         int i,fsize;
98 #ifdef SWAP_0_255
99         ubyte c;
100 #endif
101
102         fp = cfopen( filename, "rb" );
103
104         // the following is a hack to enable the loading of d2 levels
105         // even if only the d2 mac shareware datafiles are present.
106         // However, if the pig file is present but the palette file isn't,
107         // the textures in the level will look wierd...
108         if ( fp==NULL)
109                 fp = cfopen( DEFAULT_LEVEL_PALETTE, "rb" );
110         if ( fp==NULL)
111                 Error("Can open neither palette file <%s> "
112                       "nor default palette file <"
113                       DEFAULT_LEVEL_PALETTE
114                       ">.\n",
115                       filename);
116
117         fsize   = cfilelength( fp );
118         Assert( fsize == 9472 );
119         cfread( gr_palette, 256*3, 1, fp );
120         cfread( gr_fade_table, 256*34, 1, fp );
121         cfclose(fp);
122
123         // This is the TRANSPARENCY COLOR
124         for (i=0; i<GR_FADE_LEVELS; i++ )       {
125                 gr_fade_table[i*256+255] = 255;
126         }
127
128         Num_computed_colors = 0;        //      Flush palette cache.
129 // swap colors 0 and 255 of the palette along with fade table entries
130
131 #ifdef SWAP_0_255
132         for (i = 0; i < 3; i++) {
133                 c = gr_palette[i];
134                 gr_palette[i] = gr_palette[765+i];
135                 gr_palette[765+i] = c;
136         }
137
138         for (i = 0; i < GR_FADE_LEVELS * 256; i++) {
139                 if (gr_fade_table[i] == 0)
140                         gr_fade_table[i] = 255;
141         }
142         for (i=0; i<GR_FADE_LEVELS; i++)
143                 gr_fade_table[i*256] = TRANSPARENCY_COLOR;
144 #endif
145 }
146
147 //      Add a computed color (by gr_find_closest_color) to list of computed colors in Computed_colors.
148 //      If list wasn't full already, increment Num_computed_colors.
149 //      If was full, replace a random one.
150 void add_computed_color(int r, int g, int b, int color_num)
151 {
152         int     add_index;
153
154         if (Num_computed_colors < MAX_COMPUTED_COLORS) {
155                 add_index = Num_computed_colors;
156                 Num_computed_colors++;
157         } else
158                 add_index = (d_rand() * MAX_COMPUTED_COLORS) >> 15;
159
160         Computed_colors[add_index].r = r;
161         Computed_colors[add_index].g = g;
162         Computed_colors[add_index].b = b;
163         Computed_colors[add_index].color_num = color_num;
164 }
165
166 void init_computed_colors(void)
167 {
168         int     i;
169
170         for (i=0; i<MAX_COMPUTED_COLORS; i++)
171                 Computed_colors[i].r = 255;             //      Make impossible to match.
172 }
173
174 int gr_find_closest_color( int r, int g, int b )
175 {
176         int i, j;
177         int best_value, best_index, value;
178
179         if (Num_computed_colors == 0)
180                 init_computed_colors();
181
182         //      If we've already computed this color, return it!
183         for (i=0; i<Num_computed_colors; i++)
184                 if (r == Computed_colors[i].r)
185                         if (g == Computed_colors[i].g)
186                                 if (b == Computed_colors[i].b) {
187                                         if (i > 4) {
188                                                 color_record    trec;
189                                                 trec = Computed_colors[i-1];
190                                                 Computed_colors[i-1] = Computed_colors[i];
191                                                 Computed_colors[i] = trec;
192                                                 return Computed_colors[i-1].color_num;
193                                         }
194                                         return Computed_colors[i].color_num;
195                                 }
196
197 //      r &= 63;
198 //      g &= 63;
199 //      b &= 63;
200
201         best_value = SQUARE(r-gr_palette[0])+SQUARE(g-gr_palette[1])+SQUARE(b-gr_palette[2]);
202         best_index = 0;
203         if (best_value==0) {
204                 add_computed_color(r, g, b, best_index);
205                 return best_index;
206         }
207         j=0;
208         // only go to 255, 'cause we dont want to check the transparent color.
209         for (i=1; i<254; i++ )  {
210                 j += 3;
211                 value = SQUARE(r-gr_palette[j])+SQUARE(g-gr_palette[j+1])+SQUARE(b-gr_palette[j+2]);
212                 if ( value < best_value )       {
213                         if (value==0) {
214                                 add_computed_color(r, g, b, i);
215                                 return i;
216                         }
217                         best_value = value;
218                         best_index = i;
219                 }
220         }
221         add_computed_color(r, g, b, best_index);
222         return best_index;
223 }
224
225 int gr_find_closest_color_15bpp( int rgb )
226 {
227         return gr_find_closest_color( ((rgb>>10)&31)*2, ((rgb>>5)&31)*2, (rgb&31)*2 );
228 }
229
230
231 int gr_find_closest_color_current( int r, int g, int b )
232 {
233         int i, j;
234         int best_value, best_index, value;
235
236 //      r &= 63;
237 //      g &= 63;
238 //      b &= 63;
239
240         best_value = SQUARE(r-gr_current_pal[0])+SQUARE(g-gr_current_pal[1])+SQUARE(b-gr_current_pal[2]);
241         best_index = 0;
242         if (best_value==0)
243                 return best_index;
244
245         j=0;
246         // only go to 255, 'cause we dont want to check the transparent color.
247         for (i=1; i<254; i++ )  {
248                 j += 3;
249                 value = SQUARE(r-gr_current_pal[j])+SQUARE(g-gr_current_pal[j+1])+SQUARE(b-gr_current_pal[j+2]);
250                 if ( value < best_value )       {
251                         if (value==0)
252                                 return i;
253                         best_value = value;
254                         best_index = i;
255                 }
256         }
257         return best_index;
258 }
259
260 void gr_make_cthru_table(ubyte * table, ubyte r, ubyte g, ubyte b )
261 {
262         int i;
263         ubyte r1, g1, b1;
264
265         for (i=0; i<256; i++ )  {
266                 r1 = gr_palette[i*3+0] + r;
267                 if ( r1 > 63 ) r1 = 63;
268                 g1 = gr_palette[i*3+1] + g;
269                 if ( g1 > 63 ) g1 = 63;
270                 b1 = gr_palette[i*3+2] + b;
271                 if ( b1 > 63 ) b1 = 63;
272                 table[i] = gr_find_closest_color( r1, g1, b1 );
273         }
274 }
275
276 void gr_make_blend_table(ubyte *blend_table, ubyte r, ubyte g, ubyte b)
277 {
278         int i, j;
279         float alpha;
280         ubyte r1, g1, b1;
281
282         for (j = 0; j < GR_FADE_LEVELS; j++)
283         {
284                 alpha = 1.0 - (float)j / ((float)GR_FADE_LEVELS - 1);
285                 for (i = 0; i < 255; i++)
286                 {
287                         r1 = (ubyte)((1.0 - alpha) * (float)gr_palette[i * 3 + 0] + (alpha * (float)r));
288                         g1 = (ubyte)((1.0 - alpha) * (float)gr_palette[i * 3 + 1] + (alpha * (float)g));
289                         b1 = (ubyte)((1.0 - alpha) * (float)gr_palette[i * 3 + 2] + (alpha * (float)b));
290                         blend_table[i + j * 256] = gr_find_closest_color(r1, g1, b1);
291                 }
292                 blend_table[i + j * 256] = 255; // leave white alone
293         }
294 }