convert line ends
[canaan.git] / prj / tech / libsrc / dev2d / tluctab.c
blobd0d65224b67dd7c2da180885f6660ddec7947851
1 /*
2 * $Source: s:/prj/tech/libsrc/dev2d/RCS/tluctab.c $
3 * $Revision: 1.1 $
4 * $Author: KEVIN $
5 * $Date: 1996/04/10 16:10:29 $
7 * Routines for generating translucency cluts.
8 * This file is part of the dev2d library.
13 This is a routine to generate CLUTs to simulate
14 looking at colors through a translucent frammice.
15 To do this, we need to know three things:
16 The color of the frammice, how opaque it is, and
17 how effective it is as a color filter (purity).
18 Opacity and purity should vary from 0 to 1.
20 Essentially, we multiply the background color by
21 the frammice color, then take a weighted average
22 of the result and the background color, then take a
23 weighted average of the new result and the frammice
24 color. The two weights are purity and opacity,
25 respectively. (Apologia: Averaging the two factors in
26 sequentially like this may seem strange, but I found
27 it to be more intuitive than any other system I could
28 think of. Opacity means exactly what it sounds like, and
29 filter purity only applies to the light coming through.)
31 Increasing the opacity of the frammice will make it look
32 cloudier, whereas increasing the purity will make it
33 look more strongly colored. Put a green frammice on a
34 red background. If it has high opacity, it will be
35 green. If it has low opacity but high purity, it will
36 be black, as no red light gets through. If it has low
37 opacity and low purity, it will be transparent and
38 therefore look red. Somewhere in the middle of all this,
39 it will look brown, which is probably what you want.
42 #include <string.h>
43 #include <grd.h>
45 // convert red in a glomped rgb to a fixed point
46 #define rtof(b) ( ((b)&0x3ff)<<12)
47 // convert green in a glomped rgb to a fixed point
48 #define gtof(b) ( ((b)&0x1ff800)<<1)
49 // convert blue in a glomped rgb to a fixed point
50 #define btof(b) ( ((b)&0xffc00000)>>10)
52 #define WHITE (0x7fff)
54 /* Note: RGB values are 6-bit. */
56 uchar *gr_init_simple_translucency_table(uchar *p, fix opacity, grs_rgb color, uchar *pal)
58 fix baser, baseg, baseb;
59 uchar a,b,c;
60 int i;
61 fix transparency=FIX_UNIT-opacity;
63 gr_split_rgb(color, &a, &b, &c);
64 baser = a * opacity;
65 baseg = b * opacity;
66 baseb = c * opacity;
67 for (i=0; i<256; i++) {
68 fix r = pal[3*i] * transparency + baser;
69 fix g = pal[3*i+1] * transparency + baseg;
70 fix b = pal[3*i+2] * transparency + baseb;
71 p[i] = grd_ipal[(ulong )gr_index_rgb(r, g, b)];
73 return p;
76 /* Fills table with groovy values, and incedentally returns it. */
77 uchar *gr_init_translucency_table(uchar *p, fix opacity, fix purity, grs_rgb color)
79 grs_rgb background;
80 int i;
81 fix r, g, b;
82 fix baser, baseg, baseb; /* surface component */
83 fix filterr, filterg, filterb; /* translucent component before addition of background */
84 fix filter, clarity;
85 long thingge;
87 baser = fix_mul(rtof(color), opacity);
88 baseg = fix_mul(gtof(color), opacity);
89 baseb = fix_mul(btof(color), opacity);
90 filter = fix_mul(0x10000 - opacity, purity);
91 clarity = 0x10000 - opacity - filter;
92 filterr = clarity + (fix_mul(rtof(color), filter) >> 6);
93 filterg = clarity + (fix_mul(gtof(color), filter) >> 6);
94 filterb = clarity + (fix_mul(btof(color), filter) >> 6);
95 for (i=0; i<256; i++) {
96 background = grd_bpal[i];
97 r = fix_mul(rtof(background), filterr) + baser;
98 g = fix_mul(gtof(background), filterg) + baseg;
99 b = fix_mul(btof(background), filterb) + baseb;
100 thingge = gr_index_rgb(r<<2, g<<2, b<<2);
101 p[i] = grd_ipal[thingge];
103 return p;
106 //uchar *gr_init_lit_translucency_table(uchar *p, fix opacity, fix purity, grs_rgb color, grs_rgb light)
108 // grs_rgb background;
109 // int i;
110 // fix r, g, b;
111 // fix baser, baseg, baseb; /* surface component */
112 // fix filterr, filterg, filterb; /* translucent component before addition of background */
113 // fix filter, clarity;
114 // long thingge;
116 // filter = fix_mul(0x10000 - opacity, purity);
117 // clarity = 0x10000 - opacity - filter;
118 // filterr = clarity + (fix_mul(rtof(color), filter) >> 6);
119 // filterg = clarity + (fix_mul(gtof(color), filter) >> 6);
120 // filterb = clarity + (fix_mul(btof(color), filter) >> 6);
121 // baser = fix_mul(fix_mul(rtof(color), rtof(light)), opacity) >> 6;
122 // baseg = fix_mul(fix_mul(gtof(color), gtof(light)), opacity) >> 6;
123 // baseb = fix_mul(fix_mul(btof(color), btof(light)), opacity) >> 6;
124 // for (i=0; i<256; i++) {
125 // background = grd_bpal[i];
126 // r = fix_mul(rtof(background), filterr) + baser;
127 // g = fix_mul(gtof(background), filterg) + baseg;
128 // b = fix_mul(btof(background), filterb) + baseb;
129 // thingge = gr_index_rgb(r<<2, g<<2, b<<2);
130 // p[i] = grd_ipal[thingge];
131 // }
132 // return p;
135 //uchar *gr_init_lit_translucency_tables(uchar *p, fix opacity, fix purity, grs_rgb color, int n)
137 // int k;
138 // grs_rgb light;
139 // if (n == 0) return NULL;
140 // for (k=0; k<n; k++) {
141 // light = grd_bpal[grd_screen->ltab[k*256+grd_ipal[WHITE]]];
142 // gr_init_lit_translucency_table(p+256*k, opacity, purity, color, light);
143 // }
144 // return p;
147 //int gr_dump_tluc8_table(uchar *buf, int nlit)
149 // uchar *p;
150 // int k, lsize = nlit*256;
151 // p = buf;
152 // p += sizeof(int);
153 // *(p++) = nlit;
154 // *(p++) = tluc8nstab;
155 // memcpy(p, tluc8stab, tluc8nstab*256);
156 // for (k=0; k<256; k++) {
157 // if (tluc8tab[k]) {
158 // *(p++) = k;
159 // memcpy(p, tluc8tab[k], 256);
160 // p += 256;
161 // memcpy(p, tluc8ltab[k], lsize);
162 // p += lsize;
163 // }
164 // }
165 // *(int *)buf = p-buf;
166 // return p-buf;
169 //void gr_read_tluc8_table(uchar *buf)
171 // uchar *p, *end;
172 // int lsize, k;
173 // end = buf + *(int *)buf;
174 // p = buf + sizeof(int);
175 // lsize = *(p++) * 256;
176 // tluc8nstab = *(p++);
177 // tluc8stab=p;
178 // p += 256*tluc8nstab;
179 // while (p < end) {
180 // k = *(p++);
181 // tluc8tab[k] = p;
182 // p += 256;
183 // if (lsize) tluc8ltab[k] = p;
184 // p += lsize;
185 // }