msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 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 "Resources.h" |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkCodec.h" |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 13 | #include "SkColorSpace_A2B.h" |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 14 | #include "SkColorSpace_XYZ.h" |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 15 | #include "SkColorSpacePriv.h" |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 16 | #include "SkCommandLineFlags.h" |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 17 | #include "SkImageEncoder.h" |
| 18 | #include "SkMatrix44.h" |
| 19 | #include "SkOSFile.h" |
| 20 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 21 | #include "sk_tool_utils.h" |
| 22 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 27 | DEFINE_string(input, "input.png", "A path to the input image (or icc profile with --icc)."); |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 28 | DEFINE_string(output, ".", "A path to the output image directory."); |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 29 | DEFINE_bool(icc, false, "Indicates that the input is an icc profile."); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 30 | DEFINE_bool(sRGB_gamut, false, "Draws the sRGB gamut on the gamut visualization."); |
| 31 | DEFINE_bool(adobeRGB, false, "Draws the Adobe RGB gamut on the gamut visualization."); |
| 32 | DEFINE_bool(sRGB_gamma, false, "Draws the sRGB gamma on all gamma output images."); |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 33 | DEFINE_string(uncorrected, "", "A path to reencode the uncorrected input image."); |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 34 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 35 | |
| 36 | //------------------------------------------------------------------------------------------------- |
| 37 | //------------------------------------ Gamma visualizations --------------------------------------- |
| 38 | |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 39 | static const char* kRGBChannelNames[3] = { |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 40 | "Red ", |
| 41 | "Green", |
| 42 | "Blue " |
| 43 | }; |
| 44 | static const SkColor kRGBChannelColors[3] = { |
| 45 | SkColorSetARGB(128, 255, 0, 0), |
| 46 | SkColorSetARGB(128, 0, 255, 0), |
| 47 | SkColorSetARGB(128, 0, 0, 255) |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 48 | }; |
| 49 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 50 | static const char* kGrayChannelNames[1] = { "Gray"}; |
| 51 | static const SkColor kGrayChannelColors[1] = { SkColorSetRGB(128, 128, 128) }; |
| 52 | |
| 53 | static const char* kCMYKChannelNames[4] = { |
| 54 | "Cyan ", |
| 55 | "Magenta", |
| 56 | "Yellow ", |
| 57 | "Black " |
| 58 | }; |
| 59 | static const SkColor kCMYKChannelColors[4] = { |
| 60 | SkColorSetARGB(128, 0, 255, 255), |
| 61 | SkColorSetARGB(128, 255, 0, 255), |
| 62 | SkColorSetARGB(128, 255, 255, 0), |
| 63 | SkColorSetARGB(128, 16, 16, 16) |
| 64 | }; |
| 65 | |
| 66 | static const char*const*const kChannelNames[4] = { |
| 67 | kGrayChannelNames, |
| 68 | kRGBChannelNames, |
| 69 | kRGBChannelNames, |
| 70 | kCMYKChannelNames |
| 71 | }; |
| 72 | static const SkColor*const kChannelColors[4] = { |
| 73 | kGrayChannelColors, |
| 74 | kRGBChannelColors, |
| 75 | kRGBChannelColors, |
| 76 | kCMYKChannelColors |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static void dump_transfer_fn(SkGammaNamed gammaNamed) { |
| 80 | switch (gammaNamed) { |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 81 | case kSRGB_SkGammaNamed: |
| 82 | SkDebugf("Transfer Function: sRGB\n"); |
| 83 | return; |
| 84 | case k2Dot2Curve_SkGammaNamed: |
| 85 | SkDebugf("Exponential Transfer Function: Exponent 2.2\n"); |
| 86 | return; |
| 87 | case kLinear_SkGammaNamed: |
| 88 | SkDebugf("Transfer Function: Linear\n"); |
| 89 | return; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 96 | static constexpr int kGammaImageWidth = 500; |
| 97 | static constexpr int kGammaImageHeight = 500; |
| 98 | |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 99 | static void dump_transfer_fn(const SkGammas& gammas) { |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 100 | SkASSERT(gammas.channels() <= 4); |
| 101 | const char*const*const channels = kChannelNames[gammas.channels() - 1]; |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 102 | for (int i = 0; i < gammas.channels(); i++) { |
| 103 | if (gammas.isNamed(i)) { |
| 104 | switch (gammas.data(i).fNamed) { |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 105 | case kSRGB_SkGammaNamed: |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 106 | SkDebugf("%s Transfer Function: sRGB\n", channels[i]); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 107 | return; |
| 108 | case k2Dot2Curve_SkGammaNamed: |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 109 | SkDebugf("%s Transfer Function: Exponent 2.2\n", channels[i]); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 110 | return; |
| 111 | case kLinear_SkGammaNamed: |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 112 | SkDebugf("%s Transfer Function: Linear\n", channels[i]); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 113 | return; |
| 114 | default: |
| 115 | SkASSERT(false); |
| 116 | continue; |
| 117 | } |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 118 | } else if (gammas.isValue(i)) { |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 119 | SkDebugf("%s Transfer Function: Exponent %.3f\n", channels[i], gammas.data(i).fValue); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 120 | } else if (gammas.isParametric(i)) { |
| 121 | const SkColorSpaceTransferFn& fn = gammas.data(i).params(&gammas); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 122 | SkDebugf("%s Transfer Function: Parametric A = %.3f, B = %.3f, C = %.3f, D = %.3f, " |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 123 | "E = %.3f, F = %.3f, G = %.3f\n", channels[i], fn.fA, fn.fB, fn.fC, fn.fD, |
| 124 | fn.fE, fn.fF, fn.fG); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 125 | } else { |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 126 | SkASSERT(gammas.isTable(i)); |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 127 | SkDebugf("%s Transfer Function: Table (%d entries)\n", channels[i], |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 128 | gammas.data(i).fTable.fSize); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 133 | static inline float parametric(const SkColorSpaceTransferFn& fn, float x) { |
Matt Sarett | 444c1bd | 2017-01-12 13:39:04 -0500 | [diff] [blame] | 134 | return x >= fn.fD ? powf(fn.fA*x + fn.fB, fn.fG) + fn.fE |
| 135 | : fn.fC*x + fn.fF; |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void draw_transfer_fn(SkCanvas* canvas, SkGammaNamed gammaNamed, const SkGammas* gammas, |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 139 | SkColor color) { |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 140 | SkColorSpaceTransferFn fn[4]; |
| 141 | struct TableInfo { |
| 142 | const float* fTable; |
| 143 | int fSize; |
| 144 | }; |
| 145 | TableInfo table[4]; |
| 146 | bool isTable[4] = {false, false, false, false}; |
| 147 | const int channels = gammas ? gammas->channels() : 1; |
| 148 | SkASSERT(channels <= 4); |
| 149 | if (kNonStandard_SkGammaNamed != gammaNamed) { |
| 150 | dump_transfer_fn(gammaNamed); |
| 151 | for (int i = 0; i < channels; ++i) { |
| 152 | named_to_parametric(&fn[i], gammaNamed); |
| 153 | } |
| 154 | } else { |
| 155 | SkASSERT(gammas); |
| 156 | dump_transfer_fn(*gammas); |
| 157 | for (int i = 0; i < channels; ++i) { |
| 158 | if (gammas->isTable(i)) { |
| 159 | table[i].fTable = gammas->table(i); |
| 160 | table[i].fSize = gammas->data(i).fTable.fSize; |
| 161 | isTable[i] = true; |
| 162 | } else { |
| 163 | switch (gammas->type(i)) { |
| 164 | case SkGammas::Type::kNamed_Type: |
| 165 | named_to_parametric(&fn[i], gammas->data(i).fNamed); |
| 166 | break; |
| 167 | case SkGammas::Type::kValue_Type: |
| 168 | value_to_parametric(&fn[i], gammas->data(i).fValue); |
| 169 | break; |
| 170 | case SkGammas::Type::kParam_Type: |
| 171 | fn[i] = gammas->params(i); |
| 172 | break; |
| 173 | default: |
| 174 | SkASSERT(false); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | SkPaint paint; |
| 180 | paint.setStyle(SkPaint::kStroke_Style); |
| 181 | paint.setColor(color); |
| 182 | paint.setStrokeWidth(2.0f); |
| 183 | // note: gamma has positive values going up in this image so this origin is |
| 184 | // the bottom left and we must subtract y instead of adding. |
| 185 | const float gap = 16.0f; |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 186 | const float gammaWidth = kGammaImageWidth - 2 * gap; |
| 187 | const float gammaHeight = kGammaImageHeight - 2 * gap; |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 188 | // gamma origin point |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 189 | const float ox = gap; |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 190 | const float oy = gap + gammaHeight; |
| 191 | for (int i = 0; i < channels; ++i) { |
| 192 | if (kNonStandard_SkGammaNamed == gammaNamed) { |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 193 | paint.setColor(kChannelColors[channels - 1][i]); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 194 | } else { |
| 195 | paint.setColor(color); |
| 196 | } |
| 197 | if (isTable[i]) { |
| 198 | auto tx = [&table,i](int index) { |
| 199 | return index / (table[i].fSize - 1.0f); |
| 200 | }; |
| 201 | for (int ti = 1; ti < table[i].fSize; ++ti) { |
| 202 | canvas->drawLine(ox + gammaWidth * tx(ti - 1), |
| 203 | oy - gammaHeight * table[i].fTable[ti - 1], |
| 204 | ox + gammaWidth * tx(ti), |
| 205 | oy - gammaHeight * table[i].fTable[ti], |
| 206 | paint); |
| 207 | } |
| 208 | } else { |
| 209 | const float step = 0.01f; |
| 210 | float yPrev = parametric(fn[i], 0.0f); |
| 211 | for (float x = step; x <= 1.0f; x += step) { |
| 212 | const float y = parametric(fn[i], x); |
| 213 | canvas->drawLine(ox + gammaWidth * (x - step), oy - gammaHeight * yPrev, |
| 214 | ox + gammaWidth * x, oy - gammaHeight * y, |
| 215 | paint); |
| 216 | yPrev = y; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | paint.setColor(0xFF000000); |
| 221 | paint.setStrokeWidth(3.0f); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 222 | canvas->drawRect({ ox, oy - gammaHeight, ox + gammaWidth, oy }, paint); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 225 | //------------------------------------------------------------------------------------------------- |
| 226 | //------------------------------------ CLUT visualizations ---------------------------------------- |
| 227 | static void dump_clut(const SkColorLookUpTable& clut) { |
| 228 | SkDebugf("CLUT: "); |
| 229 | for (int i = 0; i < clut.inputChannels(); ++i) { |
| 230 | SkDebugf("[%d]", clut.gridPoints(i)); |
| 231 | } |
| 232 | SkDebugf(" -> [%d]\n", clut.outputChannels()); |
| 233 | } |
| 234 | |
| 235 | constexpr int kClutGap = 8; |
| 236 | constexpr float kClutCanvasSize = 2000; |
| 237 | |
| 238 | static inline int usedGridPoints(const SkColorLookUpTable& clut, int dimension) { |
| 239 | const int gp = clut.gridPoints(dimension); |
| 240 | return gp <= 16 ? gp : 16; |
| 241 | } |
| 242 | |
| 243 | // how many rows of cross-section cuts to display |
| 244 | static inline int cut_rows(const SkColorLookUpTable& clut, int dimOrder[4]) { |
| 245 | // and vertical ones for the 4th dimension (if applicable) |
| 246 | return clut.inputChannels() >= 4 ? usedGridPoints(clut, dimOrder[3]) : 1; |
| 247 | } |
| 248 | |
| 249 | // how many columns of cross-section cuts to display |
| 250 | static inline int cut_cols(const SkColorLookUpTable& clut, int dimOrder[4]) { |
| 251 | // do horizontal cuts for the 3rd dimension (if applicable) |
| 252 | return clut.inputChannels() >= 3 ? usedGridPoints(clut, dimOrder[2]) : 1; |
| 253 | } |
| 254 | |
| 255 | // gets the width/height to use for cross-sections of a CLUT |
| 256 | static int cut_size(const SkColorLookUpTable& clut, int dimOrder[4]) { |
| 257 | const int rows = cut_rows(clut, dimOrder); |
| 258 | const int cols = cut_cols(clut, dimOrder); |
| 259 | // make sure the cross-section CLUT cuts are square still by using the |
| 260 | // smallest of the width/height, then adjust the gaps between accordingly |
| 261 | const int cutWidth = (kClutCanvasSize - kClutGap * (1 + cols)) / cols; |
| 262 | const int cutHeight = (kClutCanvasSize - kClutGap * (1 + rows)) / rows; |
| 263 | return cutWidth < cutHeight ? cutWidth : cutHeight; |
| 264 | } |
| 265 | |
| 266 | static void draw_clut(SkCanvas* canvas, const SkColorLookUpTable& clut, int dimOrder[4]) { |
| 267 | dump_clut(clut); |
| 268 | |
| 269 | const int cutSize = cut_size(clut, dimOrder); |
| 270 | const int rows = cut_rows(clut, dimOrder); |
| 271 | const int cols = cut_cols(clut, dimOrder); |
| 272 | const int cutHorizGap = (kClutCanvasSize - cutSize * cols) / (1 + cols); |
| 273 | const int cutVertGap = (kClutCanvasSize - cutSize * rows) / (1 + rows); |
| 274 | |
| 275 | SkPaint paint; |
| 276 | for (int row = 0; row < rows; ++row) { |
| 277 | for (int col = 0; col < cols; ++col) { |
| 278 | // make sure to move at least one pixel, but otherwise move per-gridpoint |
| 279 | const float xStep = 1.0f / (SkTMin(cutSize, clut.gridPoints(dimOrder[0])) - 1); |
| 280 | const float yStep = 1.0f / (SkTMin(cutSize, clut.gridPoints(dimOrder[1])) - 1); |
| 281 | const float ox = clut.inputChannels() >= 3 ? (1 + col) * cutHorizGap + col * cutSize |
| 282 | : kClutGap; |
| 283 | const float oy = clut.inputChannels() >= 4 ? (1 + row) * cutVertGap + row * cutSize |
| 284 | : kClutGap; |
| 285 | // for each cross-section cut, draw a bunch of squares whose colour is the top-left's |
| 286 | // colour in the CLUT (usually this will just draw the gridpoints) |
| 287 | for (float x = 0.0f; x < 1.0f; x += xStep) { |
| 288 | for (float y = 0.0f; y < 1.0f; y += yStep) { |
| 289 | const float z = col / (cols - 1.0f); |
| 290 | const float w = row / (rows - 1.0f); |
| 291 | const float input[4] = {x, y, z, w}; |
| 292 | float output[3]; |
| 293 | clut.interp(output, input); |
| 294 | paint.setColor(SkColorSetRGB(255*output[0], 255*output[1], 255*output[2])); |
| 295 | canvas->drawRect(SkRect::MakeLTRB(ox + cutSize * x, oy + cutSize * y, |
| 296 | ox + cutSize * (x + xStep), |
| 297 | oy + cutSize * (y + yStep)), paint); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | |
| 305 | //------------------------------------------------------------------------------------------------- |
| 306 | //------------------------------------ Gamut visualizations --------------------------------------- |
| 307 | static void dump_matrix(const SkMatrix44& m) { |
| 308 | for (int r = 0; r < 4; ++r) { |
| 309 | SkDebugf("|"); |
| 310 | for (int c = 0; c < 4; ++c) { |
| 311 | SkDebugf(" %f ", m.get(r, c)); |
| 312 | } |
| 313 | SkDebugf("|\n"); |
| 314 | } |
| 315 | } |
| 316 | |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Loads the triangular gamut as a set of three points. |
| 319 | */ |
| 320 | static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) { |
| 321 | // rx = rX / (rX + rY + rZ) |
| 322 | // ry = rX / (rX + rY + rZ) |
| 323 | // gx, gy, bx, and gy are calulcated similarly. |
brianosman | de68d6c | 2016-09-09 10:36:17 -0700 | [diff] [blame] | 324 | float rSum = xyz.get(0, 0) + xyz.get(1, 0) + xyz.get(2, 0); |
| 325 | float gSum = xyz.get(0, 1) + xyz.get(1, 1) + xyz.get(2, 1); |
| 326 | float bSum = xyz.get(0, 2) + xyz.get(1, 2) + xyz.get(2, 2); |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 327 | rgb[0].fX = xyz.get(0, 0) / rSum; |
brianosman | de68d6c | 2016-09-09 10:36:17 -0700 | [diff] [blame] | 328 | rgb[0].fY = xyz.get(1, 0) / rSum; |
| 329 | rgb[1].fX = xyz.get(0, 1) / gSum; |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 330 | rgb[1].fY = xyz.get(1, 1) / gSum; |
brianosman | de68d6c | 2016-09-09 10:36:17 -0700 | [diff] [blame] | 331 | rgb[2].fX = xyz.get(0, 2) / bSum; |
| 332 | rgb[2].fY = xyz.get(1, 2) / bSum; |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Calculates the area of the triangular gamut. |
| 337 | */ |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 338 | static float calculate_area(SkPoint abc[]) { |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 339 | SkPoint a = abc[0]; |
| 340 | SkPoint b = abc[1]; |
| 341 | SkPoint c = abc[2]; |
| 342 | return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY); |
| 343 | } |
| 344 | |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 345 | static void draw_gamut(SkCanvas* canvas, const SkMatrix44& xyz, const char* name, SkColor color, |
| 346 | bool label) { |
| 347 | // Report the XYZ values. |
| 348 | SkDebugf("%s\n", name); |
brianosman | de68d6c | 2016-09-09 10:36:17 -0700 | [diff] [blame] | 349 | SkDebugf(" R G B\n"); |
| 350 | SkDebugf("X %.3f %.3f %.3f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2)); |
| 351 | SkDebugf("Y %.3f %.3f %.3f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2)); |
| 352 | SkDebugf("Z %.3f %.3f %.3f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2)); |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 353 | |
| 354 | // Calculate the points in the gamut from the XYZ values. |
| 355 | SkPoint rgb[4]; |
| 356 | load_gamut(rgb, xyz); |
| 357 | |
| 358 | // Report the area of the gamut. |
| 359 | SkDebugf("Area of Gamut: %.3f\n\n", calculate_area(rgb)); |
| 360 | |
| 361 | // Magic constants that help us place the gamut triangles in the appropriate position |
| 362 | // on the canvas. |
| 363 | const float xScale = 2071.25f; // Num pixels from 0 to 1 in x |
| 364 | const float xOffset = 241.0f; // Num pixels until start of x-axis |
| 365 | const float yScale = 2067.78f; // Num pixels from 0 to 1 in y |
| 366 | const float yOffset = -144.78f; // Num pixels until start of y-axis |
| 367 | // (negative because y extends beyond image bounds) |
| 368 | |
| 369 | // Now transform the points so they can be drawn on our canvas. |
| 370 | // Note that y increases as we move down the canvas. |
| 371 | rgb[0].fX = xOffset + xScale * rgb[0].fX; |
| 372 | rgb[0].fY = yOffset + yScale * (1.0f - rgb[0].fY); |
| 373 | rgb[1].fX = xOffset + xScale * rgb[1].fX; |
| 374 | rgb[1].fY = yOffset + yScale * (1.0f - rgb[1].fY); |
| 375 | rgb[2].fX = xOffset + xScale * rgb[2].fX; |
| 376 | rgb[2].fY = yOffset + yScale * (1.0f - rgb[2].fY); |
| 377 | |
| 378 | // Repeat the first point to connect the polygon. |
| 379 | rgb[3] = rgb[0]; |
| 380 | SkPaint paint; |
| 381 | paint.setColor(color); |
| 382 | paint.setStrokeWidth(6.0f); |
| 383 | paint.setTextSize(75.0f); |
| 384 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint); |
| 385 | if (label) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 386 | canvas->drawString("R", rgb[0].fX + 5.0f, rgb[0].fY + 75.0f, paint); |
| 387 | canvas->drawString("G", rgb[1].fX + 5.0f, rgb[1].fY - 5.0f, paint); |
| 388 | canvas->drawString("B", rgb[2].fX - 75.0f, rgb[2].fY - 5.0f, paint); |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 392 | |
| 393 | //------------------------------------------------------------------------------------------------- |
| 394 | //----------------------------------------- Main code --------------------------------------------- |
| 395 | static SkBitmap transparentBitmap(int width, int height) { |
| 396 | SkBitmap bitmap; |
| 397 | bitmap.allocN32Pixels(width, height); |
| 398 | bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
| 399 | return bitmap; |
| 400 | } |
| 401 | |
| 402 | class OutputCanvas { |
| 403 | public: |
| 404 | OutputCanvas(SkBitmap&& bitmap) |
| 405 | :fBitmap(bitmap) |
| 406 | ,fCanvas(fBitmap) |
| 407 | {} |
| 408 | |
| 409 | bool save(std::vector<std::string>* output, const std::string& filename) { |
| 410 | // Finally, encode the result to the output file. |
| 411 | sk_sp<SkData> out = sk_tool_utils::EncodeImageToData(fBitmap, SkEncodedImageFormat::kPNG, |
| 412 | 100); |
| 413 | if (!out) { |
| 414 | SkDebugf("Failed to encode %s output.\n", filename.c_str()); |
| 415 | return false; |
| 416 | } |
| 417 | SkFILEWStream stream(filename.c_str()); |
| 418 | if (!stream.write(out->data(), out->size())) { |
| 419 | SkDebugf("Failed to write %s output.\n", filename.c_str()); |
| 420 | return false; |
| 421 | } |
| 422 | // record name of canvas |
| 423 | output->push_back(filename); |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | SkCanvas* canvas() { return &fCanvas; } |
| 428 | |
| 429 | private: |
| 430 | SkBitmap fBitmap; |
| 431 | SkCanvas fCanvas; |
| 432 | }; |
| 433 | |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 434 | int main(int argc, char** argv) { |
| 435 | SkCommandLineFlags::SetUsage( |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 436 | "Usage: colorspaceinfo --input <path to input image (or icc profile with --icc)> " |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 437 | "--output <directory to output images> " |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 438 | "--icc <indicates that the input is an icc profile>" |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 439 | "--sRGB_gamut <draw canonical sRGB gamut> " |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 440 | "--adobeRGB <draw canonical Adobe RGB gamut> " |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 441 | "--sRGB_gamma <draw sRGB gamma> " |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 442 | "--uncorrected <path to reencoded, uncorrected input image>\n" |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 443 | "Description: Writes visualizations of the color space to the output image(s) ." |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 444 | "Also, if a path is provided, writes uncorrected bytes to an unmarked " |
| 445 | "png, for comparison with the input image.\n"); |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 446 | SkCommandLineFlags::Parse(argc, argv); |
| 447 | const char* input = FLAGS_input[0]; |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 448 | const char* output = FLAGS_output[0]; |
| 449 | if (!input || !output) { |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 450 | SkCommandLineFlags::PrintUsage(); |
| 451 | return -1; |
| 452 | } |
| 453 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 454 | sk_sp<SkData> data(SkData::MakeFromFileName(input)); |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 455 | if (!data) { |
| 456 | SkDebugf("Cannot find input image.\n"); |
| 457 | return -1; |
| 458 | } |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 459 | |
| 460 | std::unique_ptr<SkCodec> codec = nullptr; |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 461 | sk_sp<SkColorSpace> colorSpace = nullptr; |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 462 | if (FLAGS_icc) { |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 463 | colorSpace = SkColorSpace::MakeICC(data->bytes(), data->size()); |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 464 | } else { |
| 465 | codec.reset(SkCodec::NewFromData(data)); |
| 466 | colorSpace = sk_ref_sp(codec->getInfo().colorSpace()); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (!colorSpace) { |
| 470 | SkDebugf("Cannot create codec or icc profile from input file.\n"); |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 471 | return -1; |
| 472 | } |
| 473 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 474 | // TODO: command line tweaking of this order |
| 475 | int dimOrder[4] = {0, 1, 2, 3}; |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 476 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 477 | std::vector<std::string> outputFilenames; |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 478 | |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 479 | auto createOutputFilename = [output](const char* category, int index) -> std::string { |
| 480 | std::stringstream ss; |
| 481 | ss << output << '/' << category << '_' << index << ".png"; |
| 482 | return ss.str(); |
| 483 | }; |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 484 | |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 485 | if (SkColorSpace_Base::Type::kXYZ == as_CSB(colorSpace)->type()) { |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 486 | SkDebugf("XYZ/TRC color space\n"); |
| 487 | |
| 488 | // Load a graph of the CIE XYZ color gamut. |
| 489 | SkBitmap gamutCanvasBitmap; |
| 490 | if (!GetResourceAsBitmap("gamut.png", &gamutCanvasBitmap)) { |
| 491 | SkDebugf("Program failure (could not load gamut.png).\n"); |
| 492 | return -1; |
| 493 | } |
| 494 | OutputCanvas gamutCanvas(std::move(gamutCanvasBitmap)); |
| 495 | // Draw the sRGB gamut if requested. |
| 496 | if (FLAGS_sRGB_gamut) { |
| 497 | sk_sp<SkColorSpace> sRGBSpace = SkColorSpace::MakeSRGB(); |
| 498 | const SkMatrix44* mat = as_CSB(sRGBSpace)->toXYZD50(); |
| 499 | SkASSERT(mat); |
| 500 | draw_gamut(gamutCanvas.canvas(), *mat, "sRGB", 0xFFFF9394, false); |
| 501 | } |
| 502 | |
| 503 | // Draw the Adobe RGB gamut if requested. |
| 504 | if (FLAGS_adobeRGB) { |
| 505 | sk_sp<SkColorSpace> adobeRGBSpace = SkColorSpace::MakeRGB( |
| 506 | SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kAdobeRGB_Gamut); |
| 507 | const SkMatrix44* mat = as_CSB(adobeRGBSpace)->toXYZD50(); |
| 508 | SkASSERT(mat); |
| 509 | draw_gamut(gamutCanvas.canvas(), *mat, "Adobe RGB", 0xFF31a9e1, false); |
| 510 | } |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 511 | const SkMatrix44* mat = as_CSB(colorSpace)->toXYZD50(); |
| 512 | SkASSERT(mat); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 513 | auto xyz = static_cast<SkColorSpace_XYZ*>(colorSpace.get()); |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 514 | draw_gamut(gamutCanvas.canvas(), *mat, input, 0xFF000000, true); |
| 515 | if (!gamutCanvas.save(&outputFilenames, createOutputFilename("gamut", 0))) { |
| 516 | return -1; |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 517 | } |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 518 | |
| 519 | OutputCanvas gammaCanvas(transparentBitmap(kGammaImageWidth, kGammaImageHeight)); |
| 520 | if (FLAGS_sRGB_gamma) { |
| 521 | draw_transfer_fn(gammaCanvas.canvas(), kSRGB_SkGammaNamed, nullptr, 0xFFFF9394); |
| 522 | } |
| 523 | draw_transfer_fn(gammaCanvas.canvas(), xyz->gammaNamed(), xyz->gammas(), 0xFF000000); |
| 524 | if (!gammaCanvas.save(&outputFilenames, createOutputFilename("gamma", 0))) { |
| 525 | return -1; |
| 526 | } |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 527 | } else { |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 528 | SkDebugf("A2B color space"); |
| 529 | SkColorSpace_A2B* a2b = static_cast<SkColorSpace_A2B*>(colorSpace.get()); |
| 530 | SkDebugf("Conversion type: "); |
| 531 | switch (a2b->iccType()) { |
| 532 | case SkColorSpace_Base::kRGB_ICCTypeFlag: |
| 533 | SkDebugf("RGB"); |
| 534 | break; |
| 535 | case SkColorSpace_Base::kCMYK_ICCTypeFlag: |
| 536 | SkDebugf("CMYK"); |
| 537 | break; |
| 538 | case SkColorSpace_Base::kGray_ICCTypeFlag: |
| 539 | SkDebugf("Gray"); |
| 540 | break; |
| 541 | default: |
| 542 | SkASSERT(false); |
| 543 | break; |
| 544 | |
| 545 | } |
| 546 | SkDebugf(" -> "); |
| 547 | switch (a2b->pcs()) { |
| 548 | case SkColorSpace_A2B::PCS::kXYZ: |
| 549 | SkDebugf("XYZ\n"); |
| 550 | break; |
| 551 | case SkColorSpace_A2B::PCS::kLAB: |
| 552 | SkDebugf("LAB\n"); |
| 553 | break; |
| 554 | } |
| 555 | int clutCount = 0; |
| 556 | int gammaCount = 0; |
| 557 | for (int i = 0; i < a2b->count(); ++i) { |
| 558 | const SkColorSpace_A2B::Element& e = a2b->element(i); |
| 559 | switch (e.type()) { |
| 560 | case SkColorSpace_A2B::Element::Type::kGammaNamed: { |
| 561 | OutputCanvas gammaCanvas(transparentBitmap(kGammaImageWidth, |
| 562 | kGammaImageHeight)); |
| 563 | if (FLAGS_sRGB_gamma) { |
| 564 | draw_transfer_fn(gammaCanvas.canvas(), kSRGB_SkGammaNamed, nullptr, |
| 565 | 0xFFFF9394); |
| 566 | } |
| 567 | draw_transfer_fn(gammaCanvas.canvas(), e.gammaNamed(), nullptr, |
| 568 | 0xFF000000); |
| 569 | if (!gammaCanvas.save(&outputFilenames, |
| 570 | createOutputFilename("gamma", gammaCount++))) { |
| 571 | return -1; |
| 572 | } |
| 573 | } |
| 574 | break; |
| 575 | case SkColorSpace_A2B::Element::Type::kGammas: { |
| 576 | OutputCanvas gammaCanvas(transparentBitmap(kGammaImageWidth, |
| 577 | kGammaImageHeight)); |
| 578 | if (FLAGS_sRGB_gamma) { |
| 579 | draw_transfer_fn(gammaCanvas.canvas(), kSRGB_SkGammaNamed, nullptr, |
| 580 | 0xFFFF9394); |
| 581 | } |
| 582 | draw_transfer_fn(gammaCanvas.canvas(), kNonStandard_SkGammaNamed, |
| 583 | &e.gammas(), 0xFF000000); |
| 584 | if (!gammaCanvas.save(&outputFilenames, |
| 585 | createOutputFilename("gamma", gammaCount++))) { |
| 586 | return -1; |
| 587 | } |
| 588 | } |
| 589 | break; |
| 590 | case SkColorSpace_A2B::Element::Type::kCLUT: { |
| 591 | const SkColorLookUpTable& clut = e.colorLUT(); |
| 592 | const int cutSize = cut_size(clut, dimOrder); |
| 593 | const int clutWidth = clut.inputChannels() >= 3 ? kClutCanvasSize |
| 594 | : 2 * kClutGap + cutSize; |
| 595 | const int clutHeight = clut.inputChannels() >= 4 ? kClutCanvasSize |
| 596 | : 2 * kClutGap + cutSize; |
| 597 | OutputCanvas clutCanvas(transparentBitmap(clutWidth, clutHeight)); |
| 598 | draw_clut(clutCanvas.canvas(), e.colorLUT(), dimOrder); |
| 599 | if (!clutCanvas.save(&outputFilenames, |
| 600 | createOutputFilename("clut", clutCount++))) { |
| 601 | return -1; |
| 602 | } |
| 603 | } |
| 604 | break; |
| 605 | case SkColorSpace_A2B::Element::Type::kMatrix: |
| 606 | dump_matrix(e.matrix()); |
| 607 | break; |
| 608 | } |
| 609 | } |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 610 | } |
| 611 | |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 612 | // marker to tell the web-tool the names of all images output |
| 613 | SkDebugf("=========\n"); |
Matt Sarett | 1fa986c | 2017-05-01 17:42:20 -0400 | [diff] [blame] | 614 | for (const std::string& filename : outputFilenames) { |
| 615 | SkDebugf("%s\n", filename.c_str()); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 616 | } |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 617 | if (!FLAGS_icc) { |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 618 | SkDebugf("%s\n", input); |
| 619 | } |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 620 | // Also, if requested, decode and reencode the uncorrected input image. |
Matt Sarett | 687cc6c | 2017-05-01 17:52:56 -0400 | [diff] [blame] | 621 | if (!FLAGS_uncorrected.isEmpty() && !FLAGS_icc) { |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 622 | SkBitmap bitmap; |
| 623 | int width = codec->getInfo().width(); |
| 624 | int height = codec->getInfo().height(); |
Matt Sarett | 8740d58 | 2016-11-11 13:59:14 -0500 | [diff] [blame] | 625 | bitmap.allocN32Pixels(width, height, kOpaque_SkAlphaType == codec->getInfo().alphaType()); |
| 626 | SkImageInfo decodeInfo = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType); |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 627 | if (SkCodec::kSuccess != codec->getPixels(decodeInfo, bitmap.getPixels(), |
| 628 | bitmap.rowBytes())) { |
| 629 | SkDebugf("Could not decode input image.\n"); |
| 630 | return -1; |
| 631 | } |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 632 | sk_sp<SkData> out = sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG, |
| 633 | 100); |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 634 | if (!out) { |
| 635 | SkDebugf("Failed to encode uncorrected image.\n"); |
| 636 | return -1; |
| 637 | } |
| 638 | SkFILEWStream bitmapStream(FLAGS_uncorrected[0]); |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 639 | if (!bitmapStream.write(out->data(), out->size())) { |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 640 | SkDebugf("Failed to write uncorrected image output.\n"); |
| 641 | return -1; |
| 642 | } |
raftias | a97a60c | 2016-12-16 17:24:30 -0500 | [diff] [blame] | 643 | SkDebugf("%s\n", FLAGS_uncorrected[0]); |
msarett | 97205a4 | 2016-05-27 14:01:02 -0700 | [diff] [blame] | 644 | } |
| 645 | |
msarett | aabe15e | 2016-05-13 07:32:59 -0700 | [diff] [blame] | 646 | return 0; |
| 647 | } |