Coverage Report

Created: 2026-08-08 08:00

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