OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 #include "gm.h" | 9 #include "gm.h" |
10 #include "Resources.h" | 10 #include "Resources.h" |
11 #include "SkCodec.h" | 11 #include "SkCodec.h" |
12 #include "SkColorSpace_Base.h" | 12 #include "SkColorSpace_Base.h" |
13 #include "SkColorSpace_A2B.h" | 13 #include "SkColorSpace_A2B.h" |
14 #include "SkColorSpacePriv.h" | 14 #include "SkColorSpacePriv.h" |
15 #include "SkData.h" | 15 #include "SkData.h" |
16 #include "SkFloatingPoint.h" | 16 #include "SkFloatingPoint.h" |
17 #include "SkImageInfo.h" | 17 #include "SkImageInfo.h" |
18 #include "SkScalar.h" | 18 #include "SkScalar.h" |
19 #include "SkSRGB.h" | 19 #include "SkSRGB.h" |
20 #include "SkStream.h" | 20 #include "SkStream.h" |
21 #include "SkSurface.h" | 21 #include "SkSurface.h" |
22 #include "SkTypes.h" | 22 #include "SkTypes.h" |
23 | 23 |
24 static inline void interp_3d_clut(float dst[3], float src[3], const SkColorLookU
pTable* colorLUT) { | |
25 // Call the src components x, y, and z. | |
26 uint8_t maxX = colorLUT->fGridPoints[0] - 1; | |
27 uint8_t maxY = colorLUT->fGridPoints[1] - 1; | |
28 uint8_t maxZ = colorLUT->fGridPoints[2] - 1; | |
29 | |
30 // An approximate index into each of the three dimensions of the table. | |
31 float x = src[0] * maxX; | |
32 float y = src[1] * maxY; | |
33 float z = src[2] * maxZ; | |
34 | |
35 // This gives us the low index for our interpolation. | |
36 int ix = sk_float_floor2int(x); | |
37 int iy = sk_float_floor2int(y); | |
38 int iz = sk_float_floor2int(z); | |
39 | |
40 // Make sure the low index is not also the max index. | |
41 ix = (maxX == ix) ? ix - 1 : ix; | |
42 iy = (maxY == iy) ? iy - 1 : iy; | |
43 iz = (maxZ == iz) ? iz - 1 : iz; | |
44 | |
45 // Weighting factors for the interpolation. | |
46 float diffX = x - ix; | |
47 float diffY = y - iy; | |
48 float diffZ = z - iz; | |
49 | |
50 // Constants to help us navigate the 3D table. | |
51 // Ex: Assume x = a, y = b, z = c. | |
52 // table[a * n001 + b * n010 + c * n100] logically equals table[a][b][c]
. | |
53 const int n000 = 0; | |
54 const int n001 = 3 * colorLUT->fGridPoints[1] * colorLUT->fGridPoints[2]; | |
55 const int n010 = 3 * colorLUT->fGridPoints[2]; | |
56 const int n011 = n001 + n010; | |
57 const int n100 = 3; | |
58 const int n101 = n100 + n001; | |
59 const int n110 = n100 + n010; | |
60 const int n111 = n110 + n001; | |
61 | |
62 // Base ptr into the table. | |
63 const float* ptr = &(colorLUT->table()[ix*n001 + iy*n010 + iz*n100]); | |
64 | |
65 // The code below performs a tetrahedral interpolation for each of the three | |
66 // dst components. Once the tetrahedron containing the interpolation point
is | |
67 // identified, the interpolation is a weighted sum of grid values at the | |
68 // vertices of the tetrahedron. The claim is that tetrahedral interpolation | |
69 // provides a more accurate color conversion. | |
70 // blogs.mathworks.com/steve/2006/11/24/tetrahedral-interpolation-for-colors
pace-conversion/ | |
71 // | |
72 // I have one test image, and visually I can't tell the difference between | |
73 // tetrahedral and trilinear interpolation. In terms of computation, the | |
74 // tetrahedral code requires more branches but less computation. The | |
75 // SampleICC library provides an option for the client to choose either | |
76 // tetrahedral or trilinear. | |
77 for (int i = 0; i < 3; i++) { | |
78 if (diffZ < diffY) { | |
79 if (diffZ < diffX) { | |
80 dst[i] = (ptr[n000] + diffZ * (ptr[n110] - ptr[n010]) + | |
81 diffY * (ptr[n010] - ptr[n000]) + | |
82 diffX * (ptr[n111] - ptr[n110])); | |
83 } else if (diffY < diffX) { | |
84 dst[i] = (ptr[n000] + diffZ * (ptr[n111] - ptr[n011]) + | |
85 diffY * (ptr[n011] - ptr[n001]) + | |
86 diffX * (ptr[n001] - ptr[n000])); | |
87 } else { | |
88 dst[i] = (ptr[n000] + diffZ * (ptr[n111] - ptr[n011]) + | |
89 diffY * (ptr[n010] - ptr[n000]) + | |
90 diffX * (ptr[n011] - ptr[n010])); | |
91 } | |
92 } else { | |
93 if (diffZ < diffX) { | |
94 dst[i] = (ptr[n000] + diffZ * (ptr[n101] - ptr[n001]) + | |
95 diffY * (ptr[n111] - ptr[n101]) + | |
96 diffX * (ptr[n001] - ptr[n000])); | |
97 } else if (diffY < diffX) { | |
98 dst[i] = (ptr[n000] + diffZ * (ptr[n100] - ptr[n000]) + | |
99 diffY * (ptr[n111] - ptr[n101]) + | |
100 diffX * (ptr[n101] - ptr[n100])); | |
101 } else { | |
102 dst[i] = (ptr[n000] + diffZ * (ptr[n100] - ptr[n000]) + | |
103 diffY * (ptr[n110] - ptr[n100]) + | |
104 diffX * (ptr[n111] - ptr[n110])); | |
105 } | |
106 } | |
107 | |
108 // Increment the table ptr in order to handle the next component. | |
109 // Note that this is the how table is designed: all of nXXX | |
110 // variables are multiples of 3 because there are 3 output | |
111 // components. | |
112 ptr++; | |
113 } | |
114 } | |
115 | |
116 | |
117 /** | 24 /** |
118 * This tests decoding from a Lab source image and displays on the left | 25 * This tests decoding from a Lab source image and displays on the left |
119 * the image as raw RGB values, and on the right a Lab PCS. | 26 * the image as raw RGB values, and on the right a Lab PCS. |
120 * It currently does NOT apply a/b/m-curves, as in the .icc profile | 27 * It currently does NOT apply a/b/m-curves, as in the .icc profile |
121 * We are testing it on these are all identity transforms. | 28 * We are testing it on these are all identity transforms. |
122 */ | 29 */ |
123 class LabPCSDemoGM : public skiagm::GM { | 30 class LabPCSDemoGM : public skiagm::GM { |
124 public: | 31 public: |
125 LabPCSDemoGM() | 32 LabPCSDemoGM() |
126 : fWidth(1080) | 33 : fWidth(1080) |
(...skipping 18 matching lines...) Expand all Loading... |
145 renderImage(canvas, filename, 1, true); | 52 renderImage(canvas, filename, 1, true); |
146 } | 53 } |
147 | 54 |
148 void renderImage(SkCanvas* canvas, const char* filename, int col, bool conve
rtLabToXYZ) { | 55 void renderImage(SkCanvas* canvas, const char* filename, int col, bool conve
rtLabToXYZ) { |
149 SkBitmap bitmap; | 56 SkBitmap bitmap; |
150 SkStream* stream(GetResourceAsStream(filename)); | 57 SkStream* stream(GetResourceAsStream(filename)); |
151 if (stream == nullptr) { | 58 if (stream == nullptr) { |
152 return; | 59 return; |
153 } | 60 } |
154 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream)); | 61 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream)); |
155 | 62 |
156 | 63 |
157 // srgb_lab_pcs.icc is an elaborate way to specify sRGB but uses | 64 // srgb_lab_pcs.icc is an elaborate way to specify sRGB but uses |
158 // Lab as the PCS, so we can take any arbitrary image that should | 65 // Lab as the PCS, so we can take any arbitrary image that should |
159 // be sRGB and this should show a reasonable image | 66 // be sRGB and this should show a reasonable image |
160 const SkString iccFilename(GetResourcePath("icc_profiles/srgb_lab_pcs.ic
c")); | 67 const SkString iccFilename(GetResourcePath("icc_profiles/srgb_lab_pcs.ic
c")); |
161 sk_sp<SkData> iccData = SkData::MakeFromFileName(iccFilename.c_str()); | 68 sk_sp<SkData> iccData = SkData::MakeFromFileName(iccFilename.c_str()); |
162 if (iccData == nullptr) { | 69 if (iccData == nullptr) { |
163 return; | 70 return; |
164 } | 71 } |
165 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(iccData->bytes(),
iccData->size()); | 72 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(iccData->bytes(),
iccData->size()); |
166 | 73 |
167 const int imageWidth = codec->getInfo().width(); | 74 const int imageWidth = codec->getInfo().width(); |
168 const int imageHeight = codec->getInfo().height(); | 75 const int imageHeight = codec->getInfo().height(); |
169 // Using nullptr as the color space instructs the codec to decode in leg
acy mode, | 76 // Using nullptr as the color space instructs the codec to decode in leg
acy mode, |
170 // meaning that we will get the raw encoded bytes without any color corr
ection. | 77 // meaning that we will get the raw encoded bytes without any color corr
ection. |
171 SkImageInfo imageInfo = SkImageInfo::Make(imageWidth, imageHeight, kN32_
SkColorType, | 78 SkImageInfo imageInfo = SkImageInfo::Make(imageWidth, imageHeight, kN32_
SkColorType, |
172 kOpaque_SkAlphaType, nullptr); | 79 kOpaque_SkAlphaType, nullptr); |
173 bitmap.allocPixels(imageInfo); | 80 bitmap.allocPixels(imageInfo); |
174 codec->getPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes()); | 81 codec->getPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes()); |
175 if (convertLabToXYZ) { | 82 if (convertLabToXYZ) { |
176 SkASSERT(SkColorSpace_Base::Type::kA2B == as_CSB(colorSpace)->type()
); | 83 SkASSERT(SkColorSpace_Base::Type::kA2B == as_CSB(colorSpace)->type()
); |
177 SkColorSpace_A2B& cs = *static_cast<SkColorSpace_A2B*>(colorSpace.ge
t()); | 84 SkColorSpace_A2B& cs = *static_cast<SkColorSpace_A2B*>(colorSpace.ge
t()); |
178 const SkColorLookUpTable* colorLUT = nullptr; | 85 const SkColorLookUpTable* colorLUT = nullptr; |
179 bool printConversions = false; | 86 bool printConversions = false; |
180 // We're skipping evaluating the TRCs and the matrix here since they
aren't | 87 // We're skipping evaluating the TRCs and the matrix here since they
aren't |
181 // in the ICC profile initially used here. | 88 // in the ICC profile initially used here. |
182 for (size_t e = 0; e < cs.count(); ++e) { | 89 for (int e = 0; e < cs.count(); ++e) { |
183 switch (cs.element(e).type()) { | 90 switch (cs.element(e).type()) { |
184 case SkColorSpace_A2B::Element::Type::kGammaNamed: | 91 case SkColorSpace_A2B::Element::Type::kGammaNamed: |
185 SkASSERT(kLinear_SkGammaNamed == cs.element(e).gammaName
d()); | 92 SkASSERT(kLinear_SkGammaNamed == cs.element(e).gammaName
d()); |
186 break; | 93 break; |
187 case SkColorSpace_A2B::Element::Type::kGammas: | 94 case SkColorSpace_A2B::Element::Type::kGammas: |
188 SkASSERT(false); | 95 SkASSERT(false); |
189 break; | 96 break; |
190 case SkColorSpace_A2B::Element::Type::kCLUT: | 97 case SkColorSpace_A2B::Element::Type::kCLUT: |
191 colorLUT = &cs.element(e).colorLUT(); | 98 colorLUT = &cs.element(e).colorLUT(); |
192 break; | 99 break; |
193 case SkColorSpace_A2B::Element::Type::kMatrix: | 100 case SkColorSpace_A2B::Element::Type::kMatrix: |
194 SkASSERT(cs.element(e).matrix().isIdentity()); | 101 SkASSERT(cs.element(e).matrix().isIdentity()); |
195 break; | 102 break; |
196 } | 103 } |
197 } | 104 } |
198 SkASSERT(colorLUT); | 105 SkASSERT(colorLUT); |
199 for (int y = 0; y < imageHeight; ++y) { | 106 for (int y = 0; y < imageHeight; ++y) { |
200 for (int x = 0; x < imageWidth; ++x) { | 107 for (int x = 0; x < imageWidth; ++x) { |
201 uint32_t& p = *bitmap.getAddr32(x, y); | 108 uint32_t& p = *bitmap.getAddr32(x, y); |
202 const int r = SkColorGetR(p); | 109 const int r = SkColorGetR(p); |
203 const int g = SkColorGetG(p); | 110 const int g = SkColorGetG(p); |
204 const int b = SkColorGetB(p); | 111 const int b = SkColorGetB(p); |
205 if (printConversions) { | 112 if (printConversions) { |
206 SkColorSpacePrintf("\nraw = (%d, %d, %d)\t", r, g, b); | 113 SkColorSpacePrintf("\nraw = (%d, %d, %d)\t", r, g, b); |
207 } | 114 } |
208 | 115 |
209 float lab[4] = { r * (1.f/255.f), g * (1.f/255.f), b * (1.f/
255.f), 1.f }; | 116 float lab[4] = { r * (1.f/255.f), g * (1.f/255.f), b * (1.f/
255.f), 1.f }; |
210 | 117 |
211 interp_3d_clut(lab, lab, colorLUT); | 118 colorLUT->interp3D(lab, lab); |
212 | 119 |
213 // Lab has ranges [0,100] for L and [-128,127] for a and b | 120 // Lab has ranges [0,100] for L and [-128,127] for a and b |
214 // but the ICC profile loader stores as [0,1]. The ICC | 121 // but the ICC profile loader stores as [0,1]. The ICC |
215 // specifies an offset of -128 to convert. | 122 // specifies an offset of -128 to convert. |
216 // note: formula could be adjusted to remove this conversion
, | 123 // note: formula could be adjusted to remove this conversion
, |
217 // but for now let's keep it like this for clarity unt
il | 124 // but for now let's keep it like this for clarity unt
il |
218 // an optimized version is added. | 125 // an optimized version is added. |
219 lab[0] *= 100.f; | 126 lab[0] *= 100.f; |
220 lab[1] = 255.f * lab[1] - 128.f; | 127 lab[1] = 255.f * lab[1] - 128.f; |
221 lab[2] = 255.f * lab[2] - 128.f; | 128 lab[2] = 255.f * lab[2] - 128.f; |
222 if (printConversions) { | 129 if (printConversions) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 } | 183 } |
277 | 184 |
278 private: | 185 private: |
279 const int fWidth; | 186 const int fWidth; |
280 const int fHeight; | 187 const int fHeight; |
281 | 188 |
282 typedef skiagm::GM INHERITED; | 189 typedef skiagm::GM INHERITED; |
283 }; | 190 }; |
284 | 191 |
285 DEF_GM( return new LabPCSDemoGM; ) | 192 DEF_GM( return new LabPCSDemoGM; ) |
OLD | NEW |