• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkColorLookUpTable.h"
9 #include "SkColorSpaceXformPriv.h"
10 #include "SkFloatingPoint.h"
11 
interp(float * dst,const float * src) const12 void SkColorLookUpTable::interp(float* dst, const float* src) const {
13     if (fInputChannels == 3) {
14         interp3D(dst, src);
15     } else {
16         SkASSERT(dst != src);
17         // index gets initialized as the algorithm proceeds by interpDimension.
18         // It's just there to store the choice of low/high so far.
19         int index[kMaxColorChannels];
20         for (uint8_t outputDimension = 0; outputDimension < kOutputChannels; ++outputDimension) {
21             dst[outputDimension] = interpDimension(src, fInputChannels - 1, outputDimension,
22                                                    index);
23         }
24     }
25 }
26 
interp3D(float * dst,const float * src) const27 void SkColorLookUpTable::interp3D(float* dst, const float* src) const {
28     SkASSERT(3 == kOutputChannels);
29     // Call the src components x, y, and z.
30     const uint8_t maxX = fGridPoints[0] - 1;
31     const uint8_t maxY = fGridPoints[1] - 1;
32     const uint8_t maxZ = fGridPoints[2] - 1;
33 
34     // An approximate index into each of the three dimensions of the table.
35     const float x = src[0] * maxX;
36     const float y = src[1] * maxY;
37     const float z = src[2] * maxZ;
38 
39     // This gives us the low index for our interpolation.
40     int ix = sk_float_floor2int(x);
41     int iy = sk_float_floor2int(y);
42     int iz = sk_float_floor2int(z);
43 
44     // Make sure the low index is not also the max index.
45     ix = (maxX == ix) ? ix - 1 : ix;
46     iy = (maxY == iy) ? iy - 1 : iy;
47     iz = (maxZ == iz) ? iz - 1 : iz;
48 
49     // Weighting factors for the interpolation.
50     const float diffX = x - ix;
51     const float diffY = y - iy;
52     const float diffZ = z - iz;
53 
54     // Constants to help us navigate the 3D table.
55     // Ex: Assume x = a, y = b, z = c.
56     //     table[a * n001 + b * n010 + c * n100] logically equals table[a][b][c].
57     const int n000 = 0;
58     const int n001 = 3 * fGridPoints[1] * fGridPoints[2];
59     const int n010 = 3 * fGridPoints[2];
60     const int n011 = n001 + n010;
61     const int n100 = 3;
62     const int n101 = n100 + n001;
63     const int n110 = n100 + n010;
64     const int n111 = n110 + n001;
65 
66     // Base ptr into the table.
67     const float* ptr = &(table()[ix*n001 + iy*n010 + iz*n100]);
68 
69     // The code below performs a tetrahedral interpolation for each of the three
70     // dst components.  Once the tetrahedron containing the interpolation point is
71     // identified, the interpolation is a weighted sum of grid values at the
72     // vertices of the tetrahedron.  The claim is that tetrahedral interpolation
73     // provides a more accurate color conversion.
74     // blogs.mathworks.com/steve/2006/11/24/tetrahedral-interpolation-for-colorspace-conversion/
75     //
76     // I have one test image, and visually I can't tell the difference between
77     // tetrahedral and trilinear interpolation.  In terms of computation, the
78     // tetrahedral code requires more branches but less computation.  The
79     // SampleICC library provides an option for the client to choose either
80     // tetrahedral or trilinear.
81     for (int i = 0; i < 3; i++) {
82         if (diffZ < diffY) {
83             if (diffZ > diffX) {
84                 dst[i] = (ptr[n000] + diffZ * (ptr[n110] - ptr[n010]) +
85                                       diffY * (ptr[n010] - ptr[n000]) +
86                                       diffX * (ptr[n111] - ptr[n110]));
87             } else if (diffY < diffX) {
88                 dst[i] = (ptr[n000] + diffZ * (ptr[n111] - ptr[n011]) +
89                                       diffY * (ptr[n011] - ptr[n001]) +
90                                       diffX * (ptr[n001] - ptr[n000]));
91             } else {
92                 dst[i] = (ptr[n000] + diffZ * (ptr[n111] - ptr[n011]) +
93                                       diffY * (ptr[n010] - ptr[n000]) +
94                                       diffX * (ptr[n011] - ptr[n010]));
95             }
96         } else {
97             if (diffZ < diffX) {
98                 dst[i] = (ptr[n000] + diffZ * (ptr[n101] - ptr[n001]) +
99                                       diffY * (ptr[n111] - ptr[n101]) +
100                                       diffX * (ptr[n001] - ptr[n000]));
101             } else if (diffY < diffX) {
102                 dst[i] = (ptr[n000] + diffZ * (ptr[n100] - ptr[n000]) +
103                                       diffY * (ptr[n111] - ptr[n101]) +
104                                       diffX * (ptr[n101] - ptr[n100]));
105             } else {
106                 dst[i] = (ptr[n000] + diffZ * (ptr[n100] - ptr[n000]) +
107                                       diffY * (ptr[n110] - ptr[n100]) +
108                                       diffX * (ptr[n111] - ptr[n110]));
109             }
110         }
111 
112         // |src| is guaranteed to be in the 0-1 range as are all entries
113         // in the table.  For "increasing" tables, outputs will also be
114         // in the 0-1 range.  While this property is logical for color
115         // look up tables, we don't check for it.
116         // And for arbitrary, non-increasing tables, it is easy to see how
117         // the output might not be 0-1.  So we clamp here.
118         dst[i] = clamp_0_1(dst[i]);
119 
120         // Increment the table ptr in order to handle the next component.
121         // Note that this is the how table is designed: all of nXXX
122         // variables are multiples of 3 because there are 3 output
123         // components.
124         ptr++;
125     }
126 }
127 
interpDimension(const float * src,int inputDimension,int outputDimension,int index[kMaxColorChannels]) const128 float SkColorLookUpTable::interpDimension(const float* src, int inputDimension,
129                                           int outputDimension,
130                                           int index[kMaxColorChannels]) const {
131     // Base case. We've already decided whether to use the low or high point for each dimension
132     // which is stored inside of index[] where index[i] gives the point in the CLUT to use for
133     // input dimension i.
134     if (inputDimension < 0) {
135         // compute index into CLUT and look up the colour
136         int outputIndex = outputDimension;
137         int indexMultiplier = kOutputChannels;
138         for (int i = fInputChannels - 1; i >= 0; --i) {
139             outputIndex += index[i] * indexMultiplier;
140             indexMultiplier *= fGridPoints[i];
141         }
142         return table()[outputIndex];
143     }
144     // for each dimension (input channel), try both the low and high point for it
145     // and then do the same recursively for the later dimensions.
146     // Finally, we need to LERP the results. ie LERP X then LERP Y then LERP Z.
147     const float x = src[inputDimension] * (fGridPoints[inputDimension] - 1);
148     // try the low point for this dimension
149     index[inputDimension] = sk_float_floor2int(x);
150     const float diff = x - index[inputDimension];
151     // and recursively LERP all sub-dimensions with the current dimension fixed to the low point
152     const float lo = interpDimension(src, inputDimension - 1, outputDimension, index);
153     // now try the high point for this dimension
154     index[inputDimension] = sk_float_ceil2int(x);
155     // and recursively LERP all sub-dimensions with the current dimension fixed to the high point
156     const float hi = interpDimension(src, inputDimension - 1, outputDimension, index);
157     // then LERP the results based on the current dimension
158     return clamp_0_1((1 - diff) * lo + diff * hi);
159 }
160