convert line ends
[canaan.git] / prj / cam / libsrc / portal / pt_clut.c
blob8d5002c1c7331b7872cf51c633cfc21331e0ee25
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 // $Header: r:/t2repos/thief2/libsrc/portold/pt_clut.c,v 1.3 1996/09/10 18:25:19 buzzard Exp $
8 #include <lg.h>
9 #include <port.h>
10 #include <pt_clut.h>
11 #include <string.h>
13 // pointer to user cluts
14 uchar *pt_clut_list[256];
16 // number of cluts we keep cached
17 #define CLUT_CACHE_SIZE 4
19 // length of clut chain we cache
20 #define CLUT_CACHE_IDSIZE 8
22 // storage for the clut cache
23 static uchar pt_clut_cache[CLUT_CACHE_SIZE][256];
25 static uchar pt_clut_cache_idlist[CLUT_CACHE_SIZE][CLUT_CACHE_IDSIZE];
26 static uchar pt_clut_cache_idlist_len[CLUT_CACHE_SIZE];
28 // compare a clut chain with an idlist, assuming first one already matched
29 static bool compare_clut_chain(ClutChain *cc, int n)
31 int i=0, len = pt_clut_cache_idlist_len[n];
32 while (i < len) {
33 if (cc->clut_id != pt_clut_cache_idlist[n][i])
34 return FALSE;
35 ++i;
36 if (cc->clut_id2) {
37 if (i >= len) return FALSE;
39 if (cc->clut_id2 != pt_clut_cache_idlist[n][i])
40 return FALSE;
41 ++i;
43 cc = cc->next;
44 if (!cc)
45 return i == n;
47 return FALSE;
50 uchar *pt_get_clut(ClutChain *cc)
52 static int ref;
53 int i, c;
55 // check if it's just a single clut
56 if (!cc->next && !cc->clut_id2)
57 return pt_clut_list[cc->clut_id];
59 // check if we already have this one cached
60 c = cc->clut_id;
61 for (i=0; i < CLUT_CACHE_SIZE; ++i)
62 if (pt_clut_cache_idlist[i][0] == c && pt_clut_cache_idlist_len[i])
63 if (compare_clut_chain(cc, i))
64 return pt_clut_cache[i];
66 // cycle through to the next cache entry
67 // note lack of LRUness... who cares,
68 // this should be super rare if it every happens at all
69 if (++ref == CLUT_CACHE_SIZE) ref = 0;
72 uchar *dest = pt_clut_cache[ref];
73 uchar *s1 = pt_clut_list[cc->clut_id];
74 uchar *s2;
75 int j, n;
77 pt_clut_cache_idlist[ref][0] = cc->clut_id;
79 if (cc->clut_id2) {
80 s2 = pt_clut_list[cc->clut_id2];
81 pt_clut_cache_idlist[ref][1] = cc->clut_id2;
82 for (j=0; j < 256; ++j)
83 dest[j] = s2[s1[j]];
84 n = 2;
85 } else {
86 memcpy(dest, s1, 256);
87 n = 1;
90 while (cc->next) {
91 cc = cc->next;
92 if (n < CLUT_CACHE_IDSIZE)
93 pt_clut_cache_idlist[ref][n] = cc->clut_id;
94 ++n;
96 s2 = pt_clut_list[cc->clut_id];
97 for (j=0; j < 256; ++j)
98 dest[j] = s2[dest[j]];
100 if (cc->clut_id2) {
101 if (n < CLUT_CACHE_IDSIZE)
102 pt_clut_cache_idlist[ref][n] = cc->clut_id2;
103 ++n;
105 s2 = pt_clut_list[cc->clut_id2];
106 for (j=0; j < 256; ++j)
107 dest[j] = s2[dest[j]];
111 pt_clut_cache_idlist_len[ref] = (n <= CLUT_CACHE_IDSIZE ? n : 0);
114 return pt_clut_cache[ref];