1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #include "palette/pal.hh"
|
---|
10 | #include "math/num_type.hh"
|
---|
11 | #include "memory/malloc.hh"
|
---|
12 | #include "file/file.hh"
|
---|
13 | #include <memory.h>
|
---|
14 |
|
---|
15 | i4_pal_manager_class i4_pal_man;
|
---|
16 |
|
---|
17 | int i4_lk_cmp_size[]={0,0,256*4, 16*4, 2*4};
|
---|
18 |
|
---|
19 | void i4_pixel_format::default_format()
|
---|
20 | {
|
---|
21 |
|
---|
22 | red_mask = 0x00ff0000;
|
---|
23 | green_mask = 0x0000ff00;
|
---|
24 | blue_mask = 0x000000ff;
|
---|
25 | alpha_mask = 0xff000000;
|
---|
26 |
|
---|
27 | calc_shift();
|
---|
28 |
|
---|
29 | lookup = 0;
|
---|
30 | pixel_depth = I4_32BIT;
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | w32 i4_pixel_format::write(i4_file_class *f)
|
---|
36 | {
|
---|
37 | if (!f)
|
---|
38 | return (13*4);
|
---|
39 |
|
---|
40 | f->write_32(red_mask);
|
---|
41 | f->write_32(red_shift);
|
---|
42 | f->write_32(red_bits);
|
---|
43 |
|
---|
44 | f->write_32(green_mask);
|
---|
45 | f->write_32(green_shift);
|
---|
46 | f->write_32(green_bits);
|
---|
47 |
|
---|
48 | f->write_32(blue_mask);
|
---|
49 | f->write_32(blue_shift);
|
---|
50 | f->write_32(blue_bits);
|
---|
51 |
|
---|
52 | f->write_32(alpha_mask);
|
---|
53 | f->write_32(alpha_shift);
|
---|
54 | f->write_32(alpha_bits);
|
---|
55 |
|
---|
56 | f->write_32(pixel_depth);
|
---|
57 |
|
---|
58 | return (13*4); //(# of bytes written)
|
---|
59 | }
|
---|
60 |
|
---|
61 | void i4_pixel_format::read(i4_file_class *f)
|
---|
62 | {
|
---|
63 | red_mask = f->read_32();
|
---|
64 | red_shift = f->read_32();
|
---|
65 | red_bits = f->read_32();
|
---|
66 |
|
---|
67 | green_mask = f->read_32();
|
---|
68 | green_shift = f->read_32();
|
---|
69 | green_bits = f->read_32();
|
---|
70 |
|
---|
71 | blue_mask = f->read_32();
|
---|
72 | blue_shift = f->read_32();
|
---|
73 | blue_bits = f->read_32();
|
---|
74 |
|
---|
75 | alpha_mask = f->read_32();
|
---|
76 | alpha_shift = f->read_32();
|
---|
77 | alpha_bits = f->read_32();
|
---|
78 |
|
---|
79 | pixel_depth = (enum i4_pixel_depth)f->read_32();
|
---|
80 |
|
---|
81 | lookup = 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | i4_bool i4_lookup_pal::can_convert(const i4_pixel_format *source_fmt,
|
---|
85 | const i4_pixel_format *dest_fmt) const
|
---|
86 | {
|
---|
87 | return i4_T;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | i4_color i4_lookup_pal::convert(const i4_color source_pixel,
|
---|
92 | const i4_pixel_format *dest) const
|
---|
93 | {
|
---|
94 | w32 rgb=source.lookup[source_pixel];
|
---|
95 |
|
---|
96 | w32 r=((rgb>>16) & ((dest->red_mask>>dest->red_shift)<<(8-dest->red_bits)));
|
---|
97 | w32 g=((rgb>>8) & ((dest->green_mask>>dest->green_shift)<<(8-dest->green_bits)));
|
---|
98 | w32 b=((rgb>>0) & ((dest->blue_mask>>dest->blue_shift)<<(8-dest->blue_bits)));
|
---|
99 |
|
---|
100 | r = (r>>(8-dest->red_bits))<<dest->red_shift;
|
---|
101 | g = (g>>(8-dest->green_bits))<<dest->green_shift;
|
---|
102 | b = (b>>(8-dest->blue_bits))<<dest->blue_shift;
|
---|
103 |
|
---|
104 | return r|g|b;
|
---|
105 | }
|
---|
106 |
|
---|
107 | i4_lookup_pal::i4_lookup_pal(i4_pixel_format *source_fmt)
|
---|
108 | {
|
---|
109 | source=*source_fmt;
|
---|
110 | int size=i4_lk_cmp_size[source.pixel_depth];
|
---|
111 | source.lookup = (w32 *)i4_malloc(size, "pal memory");
|
---|
112 | memcpy(source.lookup, source_fmt->lookup, size);
|
---|
113 | }
|
---|
114 |
|
---|
115 | i4_lookup_pal::~i4_lookup_pal()
|
---|
116 | {
|
---|
117 | i4_free(source.lookup);
|
---|
118 | }
|
---|
119 |
|
---|
120 | i4_rgb_pal::i4_rgb_pal(i4_pixel_format *source_fmt)
|
---|
121 | {
|
---|
122 | source=*source_fmt;
|
---|
123 | source.lookup=0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | i4_bool i4_rgb_pal::can_convert(const i4_pixel_format *source_fmt,
|
---|
127 | const i4_pixel_format *dest_fmt) const
|
---|
128 | {
|
---|
129 | if ((source_fmt->pixel_depth == I4_32BIT ||
|
---|
130 | source_fmt->pixel_depth == I4_16BIT) &&
|
---|
131 | (dest_fmt->pixel_depth == I4_32BIT ||
|
---|
132 | dest_fmt->pixel_depth == I4_16BIT))
|
---|
133 | return i4_T;
|
---|
134 | else
|
---|
135 | return i4_F;
|
---|
136 | }
|
---|
137 |
|
---|
138 | i4_color i4_rgb_pal::convert(const i4_color source_pixel,
|
---|
139 | const i4_pixel_format *dest) const
|
---|
140 | {
|
---|
141 | w32 argb = source_pixel;
|
---|
142 |
|
---|
143 | w32 a = ((argb & source.alpha_mask)>>source.alpha_shift)<<(8-source.alpha_bits);
|
---|
144 | w32 r = ((argb & source.red_mask)>>source.red_shift)<<(8-source.red_bits);
|
---|
145 | w32 g = ((argb & source.green_mask)>>source.green_shift)<<(8-source.green_bits);
|
---|
146 | w32 b = ((argb & source.blue_mask)>>source.blue_shift)<<(8-source.blue_bits);
|
---|
147 |
|
---|
148 | a = (a>>(8-dest->alpha_bits))<<dest->alpha_shift;
|
---|
149 | r = (r>>(8-dest->red_bits))<<dest->red_shift;
|
---|
150 | g = (g>>(8-dest->green_bits))<<dest->green_shift;
|
---|
151 | b = (b>>(8-dest->blue_bits))<<dest->blue_shift;
|
---|
152 |
|
---|
153 | return a|r|g|b;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 | i4_pal *i4_pal_manager_class::register_pal(i4_pixel_format *format)
|
---|
159 | {
|
---|
160 | i4_pixel_format dest;
|
---|
161 | dest.default_format();
|
---|
162 |
|
---|
163 | i4_pal *p;
|
---|
164 |
|
---|
165 | i4_isl_list<i4_pal>::iterator i;
|
---|
166 | for (i=pal_list.begin(); i!=pal_list.end(); ++i)
|
---|
167 | {
|
---|
168 | if (format->pixel_depth == i->source.pixel_depth)
|
---|
169 | {
|
---|
170 | if (format->pixel_depth == I4_8BIT ||
|
---|
171 | format->pixel_depth == I4_4BIT ||
|
---|
172 | format->pixel_depth == I4_2BIT)
|
---|
173 | {
|
---|
174 | if (memcmp(format->lookup,
|
---|
175 | i->source.lookup,
|
---|
176 | i4_lk_cmp_size[format->pixel_depth])==0)
|
---|
177 | {
|
---|
178 | p=&(*i);
|
---|
179 | return p;
|
---|
180 | }
|
---|
181 | } else if (format->red_mask == i->source.red_mask &&
|
---|
182 | format->green_mask == i->source.green_mask &&
|
---|
183 | format->blue_mask == i->source.blue_mask &&
|
---|
184 | format->alpha_mask == i->source.alpha_mask)
|
---|
185 | {
|
---|
186 | p=&(*i);
|
---|
187 | return p;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (format->pixel_depth == I4_8BIT ||
|
---|
193 | format->pixel_depth == I4_4BIT ||
|
---|
194 | format->pixel_depth == I4_2BIT)
|
---|
195 | {
|
---|
196 | i4_lookup_pal *l= new i4_lookup_pal(format);
|
---|
197 | pal_list.insert(*l);
|
---|
198 | p=l;
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | i4_rgb_pal *l= new i4_rgb_pal(format);
|
---|
203 | pal_list.insert(*l);
|
---|
204 | p=l;
|
---|
205 | }
|
---|
206 |
|
---|
207 | return p;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void i4_pal_manager_class::init()
|
---|
211 | {
|
---|
212 | i4_pixel_format d;
|
---|
213 | d.default_format();
|
---|
214 |
|
---|
215 | def32=register_pal(&d);
|
---|
216 |
|
---|
217 | d.alpha_mask=0;
|
---|
218 | d.calc_shift();
|
---|
219 |
|
---|
220 | def_na_32=register_pal(&d);
|
---|
221 |
|
---|
222 |
|
---|
223 | w32 tmp[256];
|
---|
224 | d.pixel_depth=I4_8BIT;
|
---|
225 | d.lookup=tmp;
|
---|
226 | def8=register_pal(&d);
|
---|
227 |
|
---|
228 | }
|
---|
229 |
|
---|
230 | void i4_pal_manager_class::uninit()
|
---|
231 | {
|
---|
232 | pal_list.destroy_all();
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | i4_color i4_pal_manager_class::convert_32_to(i4_color c,
|
---|
237 | const i4_pixel_format *dest_format) const
|
---|
238 | {
|
---|
239 | return def32->convert(c, dest_format);
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | i4_color i4_pal_manager_class::convert_to_32(i4_color c, const i4_pal *source_format)
|
---|
244 | const
|
---|
245 | {
|
---|
246 | return source_format->convert(c, &def32->source);
|
---|
247 | }
|
---|
248 |
|
---|
249 | void i4_pixel_format::calc_shift()
|
---|
250 | {
|
---|
251 | w32 c;
|
---|
252 |
|
---|
253 | c=red_mask;
|
---|
254 | for (red_shift=0; (c&1)==0; red_shift++, c=c>>1);
|
---|
255 | for (red_bits=0; (c&1)==1; red_bits++, c=c>>1);
|
---|
256 |
|
---|
257 | c=green_mask;
|
---|
258 | for (green_shift=0; (c&1)==0; green_shift++, c=c>>1);
|
---|
259 | for (green_bits=0; (c&1)==1; green_bits++, c=c>>1);
|
---|
260 |
|
---|
261 | c=blue_mask;
|
---|
262 | for (blue_shift=0; (c&1)==0; blue_shift++, c=c>>1);
|
---|
263 | for (blue_bits=0; (c&1)==1; blue_bits++, c=c>>1);
|
---|
264 |
|
---|
265 | c=alpha_mask;
|
---|
266 | for (alpha_shift=0; (c&1)==0 && alpha_shift<99; alpha_shift++, c=c>>1);
|
---|
267 | if (alpha_shift==99) alpha_shift=0;
|
---|
268 |
|
---|
269 | for (alpha_bits=0; (c&1)==1 && alpha_bits<99; alpha_bits++, c=c>>1);
|
---|
270 | if (alpha_bits==99) alpha_bits=0;
|
---|
271 | }
|
---|
272 |
|
---|
273 | w32 g1_light_color(w32 color, float intensity)
|
---|
274 | {
|
---|
275 | float r=(color>>16)&0xff;
|
---|
276 | float g=(color>>8)&0xff;
|
---|
277 | float b=(color>>0)&0xff;
|
---|
278 |
|
---|
279 | r*=intensity;
|
---|
280 | g*=intensity;
|
---|
281 | b*=intensity;
|
---|
282 |
|
---|
283 | return ((i4_f_to_i(r)<<16) | (i4_f_to_i(g)<<8) | (i4_f_to_i(b)));
|
---|
284 | }
|
---|