]> icculus.org git repositories - btb/d2x.git/blob - 2d/palette.c
attempt at support for editor, makefile changes, etc
[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  * Graphical routines for setting the palette
15  *
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #include <conf.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #include "pstypes.h"
26 #include "u_mem.h"
27 #include "gr.h"
28 #include "grdef.h"
29 #include "cfile.h"
30 #include "error.h"
31 #include "mono.h"
32 #include "fix.h"
33 //added/remove by dph on 1/9/99
34 //#include "key.h"
35 //end remove
36
37 extern int gr_installed;
38
39 ubyte gr_palette[256*3];
40 ubyte gr_current_pal[256*3];
41 ubyte gr_fade_table[256*34];
42
43 ubyte gr_palette_gamma = 0;
44 int gr_palette_gamma_param = 0;
45 ubyte gr_palette_faded_out = 1;
46
47 extern void gr_palette_load( ubyte * pal );
48
49 void gr_palette_set_gamma( int gamma )
50 {
51         if ( gamma < 0 ) gamma = 0;
52 //added/changed on 10/27/98 by Victor Rachels to increase brightness slider
53         if ( gamma > 16 ) gamma = 16;      //was 8
54 //end this section change - Victor Rachels 
55
56         if (gr_palette_gamma_param != gamma )   {
57                 gr_palette_gamma_param = gamma;
58                 gr_palette_gamma = gamma;
59                 if (!gr_palette_faded_out)
60                         gr_palette_load( gr_palette );
61         }       
62 }
63
64 int gr_palette_get_gamma()
65 {
66         return gr_palette_gamma_param;
67 }
68
69
70 void gr_use_palette_table( char * filename )
71 {
72         CFILE *fp;
73         int i,fsize;
74
75         fp = cfopen( filename, "rb" );
76         if ( fp==NULL)
77                 Error("Can't open palette file <%s>",filename);
78
79         fsize   = cfilelength( fp );
80         Assert( fsize == 9472 );
81         cfread( gr_palette, 256*3, 1, fp );
82         cfread( gr_fade_table, 256*34, 1, fp );
83         cfclose(fp);
84
85         // This is the TRANSPARENCY COLOR
86         for (i=0; i<GR_FADE_LEVELS; i++ )       {
87                 gr_fade_table[i*256+255] = 255;
88         }
89
90 }
91
92 #define SQUARE(x) ((x)*(x))
93
94 #define MAX_COMPUTED_COLORS     32
95
96 int     Num_computed_colors=0;
97
98 typedef struct {
99         ubyte   r,g,b,color_num;
100 } color_record;
101
102 color_record Computed_colors[MAX_COMPUTED_COLORS];
103
104 //      Add a computed color (by gr_find_closest_color) to list of computed colors in Computed_colors.
105 //      If list wasn't full already, increment Num_computed_colors.
106 //      If was full, replace a random one.
107 void add_computed_color(int r, int g, int b, int color_num)
108 {
109         int     add_index;
110
111         if (Num_computed_colors < MAX_COMPUTED_COLORS) {
112                 add_index = Num_computed_colors;
113                 Num_computed_colors++;
114         } else
115                 add_index = (d_rand() * MAX_COMPUTED_COLORS) >> 15;
116
117         Computed_colors[add_index].r = r;
118         Computed_colors[add_index].g = g;
119         Computed_colors[add_index].b = b;
120         Computed_colors[add_index].color_num = color_num;
121 }
122
123 void init_computed_colors(void)
124 {
125         int     i;
126
127         for (i=0; i<MAX_COMPUTED_COLORS; i++)
128                 Computed_colors[i].r = 255;             //      Make impossible to match.
129 }
130
131 int gr_find_closest_color( int r, int g, int b )
132 {
133         int i, j;
134         int best_value, best_index, value;
135
136         if (Num_computed_colors == 0)
137                 init_computed_colors();
138
139         //      If we've already computed this color, return it!
140         for (i=0; i<Num_computed_colors; i++)
141                 if (r == Computed_colors[i].r)
142                         if (g == Computed_colors[i].g)
143                                 if (b == Computed_colors[i].b) {
144                                         if (i > 4) {
145                                                 color_record    trec;
146                                                 trec = Computed_colors[i-1];
147                                                 Computed_colors[i-1] = Computed_colors[i];
148                                                 Computed_colors[i] = trec;
149                                                 return Computed_colors[i-1].color_num;
150                                         }
151                                         return Computed_colors[i].color_num;
152                                 }
153
154 //      r &= 63;
155 //      g &= 63;
156 //      b &= 63;
157
158         best_value = SQUARE(r-gr_palette[0])+SQUARE(g-gr_palette[1])+SQUARE(b-gr_palette[2]);
159         best_index = 0;
160         if (best_value==0) {
161                 add_computed_color(r, g, b, best_index);
162                 return best_index;
163         }
164         j=0;
165         // only go to 255, 'cause we dont want to check the transparent color.
166         for (i=1; i<254; i++ )  {
167                 j += 3;
168                 value = SQUARE(r-gr_palette[j])+SQUARE(g-gr_palette[j+1])+SQUARE(b-gr_palette[j+2]);
169                 if ( value < best_value )       {
170                         if (value==0) {
171                                 add_computed_color(r, g, b, i);
172                                 return i;
173                         }
174                         best_value = value;
175                         best_index = i;
176                 }
177         }
178         add_computed_color(r, g, b, best_index);
179         return best_index;
180 }
181
182 int gr_find_closest_color_15bpp( int rgb )
183 {
184         return gr_find_closest_color( ((rgb>>10)&31)*2, ((rgb>>5)&31)*2, (rgb&31)*2 );
185 }
186
187
188 int gr_find_closest_color_current( int r, int g, int b )
189 {
190         int i, j;
191         int best_value, best_index, value;
192
193 //      r &= 63;
194 //      g &= 63;
195 //      b &= 63;
196
197         best_value = SQUARE(r-gr_current_pal[0])+SQUARE(g-gr_current_pal[1])+SQUARE(b-gr_current_pal[2]);
198         best_index = 0;
199         if (best_value==0)
200                 return best_index;
201
202         j=0;
203         // only go to 255, 'cause we dont want to check the transparent color.
204         for (i=1; i<254; i++ )  {
205                 j += 3;
206                 value = SQUARE(r-gr_current_pal[j])+SQUARE(g-gr_current_pal[j+1])+SQUARE(b-gr_current_pal[j+2]);
207                 if ( value < best_value )       {
208                         if (value==0)
209                                 return i;
210                         best_value = value;
211                         best_index = i;
212                 }
213         }
214         return best_index;
215 }
216
217 void gr_make_cthru_table(ubyte * table, ubyte r, ubyte g, ubyte b )
218 {
219         int i;
220         ubyte r1, g1, b1;
221
222         for (i=0; i<256; i++ )  {
223                 r1 = gr_palette[i*3+0] + r;
224                 if ( r1 > 63 ) r1 = 63;
225                 g1 = gr_palette[i*3+1] + g;
226                 if ( g1 > 63 ) g1 = 63;
227                 b1 = gr_palette[i*3+2] + b;
228                 if ( b1 > 63 ) b1 = 63;
229                 table[i] = gr_find_closest_color( r1, g1, b1 );
230         }
231 }
232