OLD | NEW |
(Empty) | |
| 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 <cmath> |
| 9 #include "gm.h" |
| 10 #include "Resources.h" |
| 11 #include "SkCodec.h" |
| 12 #include "SkColorSpace_Base.h" |
| 13 #include "SkColorSpace_A2B0.h" |
| 14 #include "SkColorSpacePriv.h" |
| 15 #include "SkColorSpaceXformPriv.h" |
| 16 #include "SkData.h" |
| 17 #include "SkFloatingPoint.h" |
| 18 #include "SkImageInfo.h" |
| 19 #include "SkScalar.h" |
| 20 #include "SkSRGB.h" |
| 21 #include "SkStream.h" |
| 22 #include "SkSurface.h" |
| 23 #include "SkTypes.h" |
| 24 |
| 25 /** |
| 26 * This tests decoding from a Lab source image and displays on the left |
| 27 * the image as raw RGB values, and on the right a Lab PCS. |
| 28 * It currently does NOT apply a/b/m-curves, as in the .icc profile |
| 29 * We are testing it on these are all identity transforms. |
| 30 */ |
| 31 class LabPCSDemoGM : public skiagm::GM { |
| 32 public: |
| 33 LabPCSDemoGM() |
| 34 : fWidth(1080) |
| 35 , fHeight(480) |
| 36 {} |
| 37 |
| 38 protected: |
| 39 |
| 40 |
| 41 SkString onShortName() override { |
| 42 return SkString("labpcsdemo"); |
| 43 } |
| 44 |
| 45 SkISize onISize() override { |
| 46 return SkISize::Make(fWidth, fHeight); |
| 47 } |
| 48 |
| 49 void onDraw(SkCanvas* canvas) override { |
| 50 canvas->drawColor(SK_ColorGREEN); |
| 51 const char* filename = "brickwork-texture.jpg"; |
| 52 renderImage(canvas, filename, 0, false); |
| 53 renderImage(canvas, filename, 1, true); |
| 54 } |
| 55 |
| 56 void renderImage(SkCanvas* canvas, const char* filename, int col, bool conve
rtLabToXYZ) { |
| 57 SkBitmap bitmap; |
| 58 SkStream* stream(GetResourceAsStream(filename)); |
| 59 if (stream == nullptr) { |
| 60 return; |
| 61 } |
| 62 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream)); |
| 63 |
| 64 |
| 65 // srgb_lab_pcs.icc is an elaborate way to specify sRGB but uses |
| 66 // Lab as the PCS, so we can take any arbitrary image that should |
| 67 // be sRGB and this should show a reasonable image |
| 68 const SkString iccFilename(GetResourcePath("icc_profiles/srgb_lab_pcs.ic
c")); |
| 69 sk_sp<SkData> iccData = SkData::MakeFromFileName(iccFilename.c_str()); |
| 70 if (iccData == nullptr) { |
| 71 return; |
| 72 } |
| 73 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewICC(iccData->bytes(),
iccData->size()); |
| 74 |
| 75 const int imageWidth = codec->getInfo().width(); |
| 76 const int imageHeight = codec->getInfo().height(); |
| 77 // Using nullptr as the color space instructs the codec to decode in leg
acy mode, |
| 78 // meaning that we will get the raw encoded bytes without any color corr
ection. |
| 79 SkImageInfo imageInfo = SkImageInfo::Make(imageWidth, imageHeight, kN32_
SkColorType, |
| 80 kOpaque_SkAlphaType, nullptr); |
| 81 bitmap.allocPixels(imageInfo); |
| 82 codec->getPixels(imageInfo, bitmap.getPixels(), bitmap.rowBytes()); |
| 83 if (convertLabToXYZ) { |
| 84 SkASSERT(as_CSB(colorSpace)->type() == SkColorSpace_Base::Type::kA2B
0); |
| 85 const SkColorSpace_A2B0& cs = *static_cast<const SkColorSpace_A2B0*>
(colorSpace.get()); |
| 86 bool printConversions = false; |
| 87 SkASSERT(cs.colorLUT()); |
| 88 for (int y = 0; y < imageHeight; ++y) { |
| 89 for (int x = 0; x < imageWidth; ++x) { |
| 90 uint32_t& p = *bitmap.getAddr32(x, y); |
| 91 const int r = SkColorGetR(p); |
| 92 const int g = SkColorGetG(p); |
| 93 const int b = SkColorGetB(p); |
| 94 if (printConversions) { |
| 95 SkColorSpacePrintf("\nraw = (%d, %d, %d)\t", r, g, b); |
| 96 } |
| 97 |
| 98 float lab[4] = { r * (1.f/255.f), g * (1.f/255.f), b * (1.f/
255.f), 1.f }; |
| 99 |
| 100 interp_3d_clut(lab, lab, cs.colorLUT()); |
| 101 |
| 102 cs.toPCS().mapScalars(lab, lab); |
| 103 |
| 104 // Lab has ranges [0,100] for L and [-128,128] for a and b |
| 105 // but the ICC profile loader stores as [0,1] |
| 106 // note: formula could be adjusted to remove this conversion
, |
| 107 // but for now let's keep it like this for clarity unt
il |
| 108 // an optimized version is added. |
| 109 lab[0] *= 100.f; |
| 110 lab[1] = 128.f*2.f * (lab[1] - 0.5f); |
| 111 lab[2] = 128.f*2.f * (lab[2] - 0.5f); |
| 112 if (printConversions) { |
| 113 SkColorSpacePrintf("Lab = < %f, %f, %f >\n", lab[0], lab
[1], lab[2]); |
| 114 } |
| 115 |
| 116 // convert from Lab to XYZ |
| 117 float Y = (lab[0] + 16.f) * (1.f/116.f); |
| 118 float X = lab[1] * (1.f/500.f) + Y; |
| 119 float Z = Y - (lab[2] * (1.f/200.f)); |
| 120 float cubed; |
| 121 cubed = X*X*X; |
| 122 if (cubed > 0.008856f) |
| 123 X = cubed; |
| 124 else |
| 125 X = (X - (16.f/116.f)) * (1.f/7.787f); |
| 126 cubed = Y*Y*Y; |
| 127 if (cubed > 0.008856f) |
| 128 Y = cubed; |
| 129 else |
| 130 Y = (Y - (16.f/116.f)) * (1.f/7.787f); |
| 131 cubed = Z*Z*Z; |
| 132 if (cubed > 0.008856f) |
| 133 Z = cubed; |
| 134 else |
| 135 Z = (Z - (16.f/116.f)) * (1.f/7.787f); |
| 136 |
| 137 // adjust to D50 illuminant |
| 138 X *= 0.96422f; |
| 139 Y *= 1.00000f; |
| 140 Z *= 0.82521f; |
| 141 |
| 142 if (printConversions) { |
| 143 SkColorSpacePrintf("XYZ = (%4f, %4f, %4f)\t", X, Y, Z); |
| 144 } |
| 145 |
| 146 // convert XYZ -> linear sRGB |
| 147 Sk4f lRGB( 3.1338561f*X - 1.6168667f*Y - 0.4906146f*Z, |
| 148 -0.9787684f*X + 1.9161415f*Y + 0.0334540f*Z, |
| 149 0.0719453f*X - 0.2289914f*Y + 1.4052427f*Z, |
| 150 1.f); |
| 151 // and apply sRGB gamma |
| 152 Sk4i sRGB = sk_linear_to_srgb(lRGB); |
| 153 if (printConversions) { |
| 154 SkColorSpacePrintf("sRGB = (%d, %d, %d)\n", sRGB[0], sRG
B[1], sRGB[2]); |
| 155 } |
| 156 p = SkColorSetRGB(sRGB[0], sRGB[1], sRGB[2]); |
| 157 } |
| 158 } |
| 159 } |
| 160 const int freeWidth = fWidth - 2*imageWidth; |
| 161 const int freeHeight = fHeight - imageHeight; |
| 162 canvas->drawBitmap(bitmap, |
| 163 static_cast<SkScalar>((col+1) * (freeWidth / 3) + col
*imageWidth), |
| 164 static_cast<SkScalar>(freeHeight / 2)); |
| 165 ++col; |
| 166 } |
| 167 |
| 168 private: |
| 169 const int fWidth; |
| 170 const int fHeight; |
| 171 |
| 172 typedef skiagm::GM INHERITED; |
| 173 }; |
| 174 |
| 175 DEF_GM( return new LabPCSDemoGM; ) |
OLD | NEW |