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/pal8.hh"
|
---|
10 | #include "error/error.hh"
|
---|
11 |
|
---|
12 | i4_pal8_manager i4_pal8_man;
|
---|
13 |
|
---|
14 | i4_pal8_manager::i4_pal8_manager()
|
---|
15 | {
|
---|
16 | memset(palette_references,0,sizeof(palette_references)); // no reference to any palettes yet
|
---|
17 | }
|
---|
18 |
|
---|
19 | w8 *i4_pal8_manager::get_remap_table(i4_pal8_handle from, i4_pal8_handle to)
|
---|
20 | {
|
---|
21 | w16 index=from.id*MAX_PALETTES+to.id;
|
---|
22 |
|
---|
23 | w8 *data=remap_tables+index*256;
|
---|
24 |
|
---|
25 | if (tables_calculated.get(index))
|
---|
26 | return data;
|
---|
27 | else
|
---|
28 | {
|
---|
29 | w32 *from_pal=palette_tables+from.id*256;
|
---|
30 | w32 *to_pal=palette_tables+to.id*256;
|
---|
31 |
|
---|
32 | w16 x,y;
|
---|
33 | w32 c1,c2;
|
---|
34 | sw16 r1,g1,b1,
|
---|
35 | r2,g2,b2,nd,d=-1,best;
|
---|
36 |
|
---|
37 | for (x=0;x<256;x++)
|
---|
38 | {
|
---|
39 | c1=*from_pal;
|
---|
40 |
|
---|
41 | r1=(c1&0xff0000)>>16;
|
---|
42 | g1=(c1&0xff00)>>8;
|
---|
43 | b1=(c1&0xff);
|
---|
44 |
|
---|
45 |
|
---|
46 | for (y=0;y<256;y++)
|
---|
47 | {
|
---|
48 | c2=to_pal[y];
|
---|
49 |
|
---|
50 | r2=(c2&0xff0000)>>16;
|
---|
51 | g2=(c2&0xff00)>>8;
|
---|
52 | b2=(c2&0xff);
|
---|
53 | nd=(r1-r2)*(r1-r2)+(g1-g2)*(g1-g2)+(b1-b2)*(b1-b2);
|
---|
54 | if (nd<d)
|
---|
55 | {
|
---|
56 | d=nd;
|
---|
57 | best=y;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | *(data++)=best;
|
---|
61 | }
|
---|
62 | return remap_tables+index*256;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | i4_pal8_handle i4_pal8_manager::register_pal(w32 *palette_data)
|
---|
67 | {
|
---|
68 | int x;
|
---|
69 | for (x=0;x<MAX_PALETTES;x++)
|
---|
70 | {
|
---|
71 | if (!palettes_allocated.get(x))
|
---|
72 | {
|
---|
73 | palettes_allocated.set(x,i4_T);
|
---|
74 | memcpy(palette_tables+x*256,palette_data,256*4);
|
---|
75 | i4_pal8_handle ret(x);
|
---|
76 | return ret;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | i4_error("Max palette allocation = %d, exceeded",MAX_PALETTES);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void i4_pal8_manager::unregister_pal(i4_pal8_handle id)
|
---|
84 | {
|
---|
85 | if (!palettes_allocated.get(id.id))
|
---|
86 | i4_error("unregistering bad palette");
|
---|
87 | else if (palette_references[id.id])
|
---|
88 | i4_error("unregistering referenced palette");
|
---|
89 | else
|
---|
90 | palettes_allocated.set(id.id,i4_F);
|
---|
91 |
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|