xref: /plan9/sys/src/cmd/gs/src/gxctable.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gxctable.c,v 1.5 2002/02/21 22:24:53 giles Exp $ */
18 /* Color table lookup and interpolation */
19 #include "gx.h"
20 #include "gxfixed.h"
21 #include "gxfrac.h"
22 #include "gxctable.h"
23 
24 /* See gxctable.h for the API and structure definitions. */
25 
26 /*
27  * Define an implementation that simply picks the nearest value without
28  * any interpolation.
29  */
30 void
gx_color_interpolate_nearest(const fixed * pi,const gx_color_lookup_table * pclt,frac * pv)31 gx_color_interpolate_nearest(const fixed * pi,
32 			     const gx_color_lookup_table * pclt, frac * pv)
33 {
34     const int *pdim = pclt->dims;
35     int m = pclt->m;
36     const gs_const_string *table = pclt->table;
37 
38     if (pclt->n > 3) {
39 	table += fixed2int_var_rounded(pi[0]) * pdim[1];
40 	++pi, ++pdim;
41     } {
42 	int ic = fixed2int_var_rounded(pi[2]);
43 	int ib = fixed2int_var_rounded(pi[1]);
44 	int ia = fixed2int_var_rounded(pi[0]);
45 	const byte *p = pclt->table[ia].data + (ib * pdim[2] + ic) * m;
46 	int j;
47 
48 	for (j = 0; j < m; ++j, ++p)
49 	    pv[j] = byte2frac(*p);
50     }
51 }
52 
53 /*
54  * Define an implementation that uses trilinear interpolation.
55  */
56 private void
interpolate_accum(const fixed * pi,const gx_color_lookup_table * pclt,frac * pv,fixed factor)57 interpolate_accum(const fixed * pi, const gx_color_lookup_table * pclt,
58 		  frac * pv, fixed factor)
59 {
60     const int *pdim = pclt->dims;
61     int m = pclt->m;
62 
63     if (pclt->n > 3) {
64 	/* Do two 3-D interpolations, interpolating between them. */
65 	gx_color_lookup_table clt3;
66 	int ix = fixed2int_var(pi[0]);
67 	fixed fx = fixed_fraction(pi[0]);
68 
69 	clt3.n = 3;
70 	clt3.dims[0] = pdim[1];	/* needed only for range checking */
71 	clt3.dims[1] = pdim[2];
72 	clt3.dims[2] = pdim[3];
73 	clt3.m = m;
74 	clt3.table = pclt->table + ix * pdim[1];
75 	interpolate_accum(pi + 1, &clt3, pv, fixed_1);
76 	if (ix == pdim[0] - 1)
77 	    return;
78 	clt3.table += pdim[1];
79 	interpolate_accum(pi + 1, &clt3, pv, fx);
80     } else {
81 	int ic = fixed2int_var(pi[2]);
82 	fixed fc = fixed_fraction(pi[2]);
83 	uint dc1 = (ic == pdim[2] - 1 ? 0 : m);
84 	int ib = fixed2int_var(pi[1]);
85 	fixed fb = fixed_fraction(pi[1]);
86 	uint db1 = (ib == pdim[1] - 1 ? 0 : pdim[2] * m);
87 	uint dbc = (ib * pdim[2] + ic) * m;
88 	uint dbc1 = db1 + dc1;
89 	int ia = fixed2int_var(pi[0]);
90 	fixed fa = fixed_fraction(pi[0]);
91 	const byte *pa0 = pclt->table[ia].data + dbc;
92 	const byte *pa1 =
93 	    (ia == pdim[0] - 1 ? pa0 : pclt->table[ia + 1].data + dbc);
94 	int j;
95 
96 	/* The values to be interpolated are */
97 	/* pa{0,1}[{0,db1,dc1,dbc1}]. */
98 	for (j = 0; j < m; ++j, ++pa0, ++pa1) {
99 	    frac v000 = byte2frac(pa0[0]);
100 	    frac v001 = byte2frac(pa0[dc1]);
101 	    frac v010 = byte2frac(pa0[db1]);
102 	    frac v011 = byte2frac(pa0[dbc1]);
103 	    frac v100 = byte2frac(pa1[0]);
104 	    frac v101 = byte2frac(pa1[dc1]);
105 	    frac v110 = byte2frac(pa1[db1]);
106 	    frac v111 = byte2frac(pa1[dbc1]);
107 	    frac rv;
108 
109 	    frac v00 = v000 +
110 		(frac) arith_rshift((long)fc * (v001 - v000),
111 				    _fixed_shift);
112 	    frac v01 = v010 +
113 		(frac) arith_rshift((long)fc * (v011 - v010),
114 				    _fixed_shift);
115 	    frac v10 = v100 +
116 		(frac) arith_rshift((long)fc * (v101 - v100),
117 				    _fixed_shift);
118 	    frac v11 = v110 +
119 		(frac) arith_rshift((long)fc * (v111 - v110),
120 				    _fixed_shift);
121 
122 	    frac v0 = v00 +
123 		(frac) arith_rshift((long)fb * (v01 - v00),
124 				    _fixed_shift);
125 	    frac v1 = v10 +
126 		(frac) arith_rshift((long)fb * (v11 - v10),
127 				    _fixed_shift);
128 
129 	    rv = v0 +
130 		(frac) arith_rshift((long)fa * (v1 - v0),
131 				    _fixed_shift);
132 	    if (factor == fixed_1)
133 		pv[j] = rv;
134 	    else
135 		pv[j] += (frac) arith_rshift((long)factor * (rv - pv[j]),
136 					     _fixed_shift);
137 	}
138     }
139 }
140 void
gx_color_interpolate_linear(const fixed * pi,const gx_color_lookup_table * pclt,frac * pv)141 gx_color_interpolate_linear(const fixed * pi,
142 			    const gx_color_lookup_table * pclt, frac * pv)
143 {
144     interpolate_accum(pi, pclt, pv, fixed_1);
145 }
146