blob: 4d4d8c0be2005a96acfee749d104416525d6e468 [file] [log] [blame]
msarettaabe15e2016-05-13 07:32:59 -07001/*
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"
raftiasa97a60c2016-12-16 17:24:30 -050013#include "SkColorSpace_A2B.h"
Matt Sarett8740d582016-11-11 13:59:14 -050014#include "SkColorSpace_XYZ.h"
raftiasa97a60c2016-12-16 17:24:30 -050015#include "SkColorSpacePriv.h"
msarettaabe15e2016-05-13 07:32:59 -070016#include "SkCommandLineFlags.h"
msarettaabe15e2016-05-13 07:32:59 -070017#include "SkImageEncoder.h"
18#include "SkMatrix44.h"
19#include "SkOSFile.h"
20
Hal Canarydb683012016-11-23 08:55:18 -070021#include "sk_tool_utils.h"
22
Matt Sarett1fa986c2017-05-01 17:42:20 -040023#include <sstream>
24#include <string>
25#include <vector>
26
Matt Sarett687cc6c2017-05-01 17:52:56 -040027DEFINE_string(input, "input.png", "A path to the input image (or icc profile with --icc).");
Matt Sarett1fa986c2017-05-01 17:42:20 -040028DEFINE_string(output, ".", "A path to the output image directory.");
Matt Sarett687cc6c2017-05-01 17:52:56 -040029DEFINE_bool(icc, false, "Indicates that the input is an icc profile.");
raftiasa97a60c2016-12-16 17:24:30 -050030DEFINE_bool(sRGB_gamut, false, "Draws the sRGB gamut on the gamut visualization.");
31DEFINE_bool(adobeRGB, false, "Draws the Adobe RGB gamut on the gamut visualization.");
32DEFINE_bool(sRGB_gamma, false, "Draws the sRGB gamma on all gamma output images.");
msarett97205a42016-05-27 14:01:02 -070033DEFINE_string(uncorrected, "", "A path to reencode the uncorrected input image.");
msarettaabe15e2016-05-13 07:32:59 -070034
Matt Sarett1fa986c2017-05-01 17:42:20 -040035
36//-------------------------------------------------------------------------------------------------
37//------------------------------------ Gamma visualizations ---------------------------------------
38
raftiasa97a60c2016-12-16 17:24:30 -050039static const char* kRGBChannelNames[3] = {
Matt Sarett1fa986c2017-05-01 17:42:20 -040040 "Red ",
41 "Green",
42 "Blue "
43};
44static const SkColor kRGBChannelColors[3] = {
45 SkColorSetARGB(128, 255, 0, 0),
46 SkColorSetARGB(128, 0, 255, 0),
47 SkColorSetARGB(128, 0, 0, 255)
raftiasa97a60c2016-12-16 17:24:30 -050048};
49
Matt Sarett1fa986c2017-05-01 17:42:20 -040050static const char* kGrayChannelNames[1] = { "Gray"};
51static const SkColor kGrayChannelColors[1] = { SkColorSetRGB(128, 128, 128) };
52
53static const char* kCMYKChannelNames[4] = {
54 "Cyan ",
55 "Magenta",
56 "Yellow ",
57 "Black "
58};
59static 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
66static const char*const*const kChannelNames[4] = {
67 kGrayChannelNames,
68 kRGBChannelNames,
69 kRGBChannelNames,
70 kCMYKChannelNames
71};
72static const SkColor*const kChannelColors[4] = {
73 kGrayChannelColors,
74 kRGBChannelColors,
75 kRGBChannelColors,
76 kCMYKChannelColors
raftiasa97a60c2016-12-16 17:24:30 -050077};
78
79static void dump_transfer_fn(SkGammaNamed gammaNamed) {
80 switch (gammaNamed) {
Matt Sarett8740d582016-11-11 13:59:14 -050081 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
raftiasa97a60c2016-12-16 17:24:30 -050094}
95
Matt Sarett1fa986c2017-05-01 17:42:20 -040096static constexpr int kGammaImageWidth = 500;
97static constexpr int kGammaImageHeight = 500;
98
raftiasa97a60c2016-12-16 17:24:30 -050099static void dump_transfer_fn(const SkGammas& gammas) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400100 SkASSERT(gammas.channels() <= 4);
101 const char*const*const channels = kChannelNames[gammas.channels() - 1];
raftiasa97a60c2016-12-16 17:24:30 -0500102 for (int i = 0; i < gammas.channels(); i++) {
103 if (gammas.isNamed(i)) {
104 switch (gammas.data(i).fNamed) {
Matt Sarett8740d582016-11-11 13:59:14 -0500105 case kSRGB_SkGammaNamed:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400106 SkDebugf("%s Transfer Function: sRGB\n", channels[i]);
Matt Sarett8740d582016-11-11 13:59:14 -0500107 return;
108 case k2Dot2Curve_SkGammaNamed:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400109 SkDebugf("%s Transfer Function: Exponent 2.2\n", channels[i]);
Matt Sarett8740d582016-11-11 13:59:14 -0500110 return;
111 case kLinear_SkGammaNamed:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400112 SkDebugf("%s Transfer Function: Linear\n", channels[i]);
Matt Sarett8740d582016-11-11 13:59:14 -0500113 return;
114 default:
115 SkASSERT(false);
116 continue;
117 }
raftiasa97a60c2016-12-16 17:24:30 -0500118 } else if (gammas.isValue(i)) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400119 SkDebugf("%s Transfer Function: Exponent %.3f\n", channels[i], gammas.data(i).fValue);
raftiasa97a60c2016-12-16 17:24:30 -0500120 } else if (gammas.isParametric(i)) {
121 const SkColorSpaceTransferFn& fn = gammas.data(i).params(&gammas);
Matt Sarett8740d582016-11-11 13:59:14 -0500122 SkDebugf("%s Transfer Function: Parametric A = %.3f, B = %.3f, C = %.3f, D = %.3f, "
Matt Sarett1fa986c2017-05-01 17:42:20 -0400123 "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 Sarett8740d582016-11-11 13:59:14 -0500125 } else {
raftiasa97a60c2016-12-16 17:24:30 -0500126 SkASSERT(gammas.isTable(i));
Matt Sarett1fa986c2017-05-01 17:42:20 -0400127 SkDebugf("%s Transfer Function: Table (%d entries)\n", channels[i],
raftiasa97a60c2016-12-16 17:24:30 -0500128 gammas.data(i).fTable.fSize);
Matt Sarett8740d582016-11-11 13:59:14 -0500129 }
130 }
131}
132
raftiasa97a60c2016-12-16 17:24:30 -0500133static inline float parametric(const SkColorSpaceTransferFn& fn, float x) {
Matt Sarett444c1bd2017-01-12 13:39:04 -0500134 return x >= fn.fD ? powf(fn.fA*x + fn.fB, fn.fG) + fn.fE
135 : fn.fC*x + fn.fF;
raftiasa97a60c2016-12-16 17:24:30 -0500136}
137
138static void draw_transfer_fn(SkCanvas* canvas, SkGammaNamed gammaNamed, const SkGammas* gammas,
Matt Sarett1fa986c2017-05-01 17:42:20 -0400139 SkColor color) {
raftiasa97a60c2016-12-16 17:24:30 -0500140 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 Sarett1fa986c2017-05-01 17:42:20 -0400186 const float gammaWidth = kGammaImageWidth - 2 * gap;
187 const float gammaHeight = kGammaImageHeight - 2 * gap;
raftiasa97a60c2016-12-16 17:24:30 -0500188 // gamma origin point
Matt Sarett1fa986c2017-05-01 17:42:20 -0400189 const float ox = gap;
raftiasa97a60c2016-12-16 17:24:30 -0500190 const float oy = gap + gammaHeight;
191 for (int i = 0; i < channels; ++i) {
192 if (kNonStandard_SkGammaNamed == gammaNamed) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400193 paint.setColor(kChannelColors[channels - 1][i]);
raftiasa97a60c2016-12-16 17:24:30 -0500194 } 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 Reed3661bc92017-02-22 13:21:42 -0500222 canvas->drawRect({ ox, oy - gammaHeight, ox + gammaWidth, oy }, paint);
raftiasa97a60c2016-12-16 17:24:30 -0500223}
224
Matt Sarett1fa986c2017-05-01 17:42:20 -0400225//-------------------------------------------------------------------------------------------------
226//------------------------------------ CLUT visualizations ----------------------------------------
227static 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
235constexpr int kClutGap = 8;
236constexpr float kClutCanvasSize = 2000;
237
238static 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
244static 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
250static 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
256static 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
266static 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 ---------------------------------------
307static 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
msarettaabe15e2016-05-13 07:32:59 -0700317/**
318 * Loads the triangular gamut as a set of three points.
319 */
320static 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.
brianosmande68d6c2016-09-09 10:36:17 -0700324 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);
msarettaabe15e2016-05-13 07:32:59 -0700327 rgb[0].fX = xyz.get(0, 0) / rSum;
brianosmande68d6c2016-09-09 10:36:17 -0700328 rgb[0].fY = xyz.get(1, 0) / rSum;
329 rgb[1].fX = xyz.get(0, 1) / gSum;
msarettaabe15e2016-05-13 07:32:59 -0700330 rgb[1].fY = xyz.get(1, 1) / gSum;
brianosmande68d6c2016-09-09 10:36:17 -0700331 rgb[2].fX = xyz.get(0, 2) / bSum;
332 rgb[2].fY = xyz.get(1, 2) / bSum;
msarettaabe15e2016-05-13 07:32:59 -0700333}
334
335/**
336 * Calculates the area of the triangular gamut.
337 */
msarett97205a42016-05-27 14:01:02 -0700338static float calculate_area(SkPoint abc[]) {
msarettaabe15e2016-05-13 07:32:59 -0700339 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
msarett97205a42016-05-27 14:01:02 -0700345static 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);
brianosmande68d6c2016-09-09 10:36:17 -0700349 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));
msarett97205a42016-05-27 14:01:02 -0700353
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 Clark2a475ea2017-04-28 15:35:12 -0400386 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);
msarett97205a42016-05-27 14:01:02 -0700389 }
390}
391
Matt Sarett1fa986c2017-05-01 17:42:20 -0400392
393//-------------------------------------------------------------------------------------------------
394//----------------------------------------- Main code ---------------------------------------------
395static 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
402class OutputCanvas {
403public:
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
429private:
430 SkBitmap fBitmap;
431 SkCanvas fCanvas;
432};
433
msarettaabe15e2016-05-13 07:32:59 -0700434int main(int argc, char** argv) {
435 SkCommandLineFlags::SetUsage(
Matt Sarett687cc6c2017-05-01 17:52:56 -0400436 "Usage: colorspaceinfo --input <path to input image (or icc profile with --icc)> "
Matt Sarett1fa986c2017-05-01 17:42:20 -0400437 "--output <directory to output images> "
Matt Sarett687cc6c2017-05-01 17:52:56 -0400438 "--icc <indicates that the input is an icc profile>"
Matt Sarett1fa986c2017-05-01 17:42:20 -0400439 "--sRGB_gamut <draw canonical sRGB gamut> "
Matt Sarett8740d582016-11-11 13:59:14 -0500440 "--adobeRGB <draw canonical Adobe RGB gamut> "
Matt Sarett1fa986c2017-05-01 17:42:20 -0400441 "--sRGB_gamma <draw sRGB gamma> "
Matt Sarett8740d582016-11-11 13:59:14 -0500442 "--uncorrected <path to reencoded, uncorrected input image>\n"
raftiasa97a60c2016-12-16 17:24:30 -0500443 "Description: Writes visualizations of the color space to the output image(s) ."
msarett97205a42016-05-27 14:01:02 -0700444 "Also, if a path is provided, writes uncorrected bytes to an unmarked "
445 "png, for comparison with the input image.\n");
msarettaabe15e2016-05-13 07:32:59 -0700446 SkCommandLineFlags::Parse(argc, argv);
447 const char* input = FLAGS_input[0];
Matt Sarett1fa986c2017-05-01 17:42:20 -0400448 const char* output = FLAGS_output[0];
449 if (!input || !output) {
msarettaabe15e2016-05-13 07:32:59 -0700450 SkCommandLineFlags::PrintUsage();
451 return -1;
452 }
453
bungeman38d909e2016-08-02 14:40:46 -0700454 sk_sp<SkData> data(SkData::MakeFromFileName(input));
msarettaabe15e2016-05-13 07:32:59 -0700455 if (!data) {
456 SkDebugf("Cannot find input image.\n");
457 return -1;
458 }
Matt Sarett687cc6c2017-05-01 17:52:56 -0400459
460 std::unique_ptr<SkCodec> codec = nullptr;
Matt Sarett8740d582016-11-11 13:59:14 -0500461 sk_sp<SkColorSpace> colorSpace = nullptr;
Matt Sarett687cc6c2017-05-01 17:52:56 -0400462 if (FLAGS_icc) {
Matt Sarett8740d582016-11-11 13:59:14 -0500463 colorSpace = SkColorSpace::MakeICC(data->bytes(), data->size());
Matt Sarett687cc6c2017-05-01 17:52:56 -0400464 } else {
465 codec.reset(SkCodec::NewFromData(data));
466 colorSpace = sk_ref_sp(codec->getInfo().colorSpace());
Matt Sarett8740d582016-11-11 13:59:14 -0500467 }
468
469 if (!colorSpace) {
470 SkDebugf("Cannot create codec or icc profile from input file.\n");
msarettaabe15e2016-05-13 07:32:59 -0700471 return -1;
472 }
473
Matt Sarett1fa986c2017-05-01 17:42:20 -0400474 // TODO: command line tweaking of this order
475 int dimOrder[4] = {0, 1, 2, 3};
raftiasa97a60c2016-12-16 17:24:30 -0500476
Matt Sarett1fa986c2017-05-01 17:42:20 -0400477 std::vector<std::string> outputFilenames;
msarettaabe15e2016-05-13 07:32:59 -0700478
Matt Sarett1fa986c2017-05-01 17:42:20 -0400479 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 };
msarett97205a42016-05-27 14:01:02 -0700484
Matt Sarett8740d582016-11-11 13:59:14 -0500485 if (SkColorSpace_Base::Type::kXYZ == as_CSB(colorSpace)->type()) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400486 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 Sarett8740d582016-11-11 13:59:14 -0500511 const SkMatrix44* mat = as_CSB(colorSpace)->toXYZD50();
512 SkASSERT(mat);
raftiasa97a60c2016-12-16 17:24:30 -0500513 auto xyz = static_cast<SkColorSpace_XYZ*>(colorSpace.get());
Matt Sarett1fa986c2017-05-01 17:42:20 -0400514 draw_gamut(gamutCanvas.canvas(), *mat, input, 0xFF000000, true);
515 if (!gamutCanvas.save(&outputFilenames, createOutputFilename("gamut", 0))) {
516 return -1;
raftiasa97a60c2016-12-16 17:24:30 -0500517 }
Matt Sarett1fa986c2017-05-01 17:42:20 -0400518
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 Sarett8740d582016-11-11 13:59:14 -0500527 } else {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400528 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 }
msarettaabe15e2016-05-13 07:32:59 -0700610 }
611
raftiasa97a60c2016-12-16 17:24:30 -0500612 // marker to tell the web-tool the names of all images output
613 SkDebugf("=========\n");
Matt Sarett1fa986c2017-05-01 17:42:20 -0400614 for (const std::string& filename : outputFilenames) {
615 SkDebugf("%s\n", filename.c_str());
raftiasa97a60c2016-12-16 17:24:30 -0500616 }
Matt Sarett687cc6c2017-05-01 17:52:56 -0400617 if (!FLAGS_icc) {
raftiasa97a60c2016-12-16 17:24:30 -0500618 SkDebugf("%s\n", input);
619 }
msarett97205a42016-05-27 14:01:02 -0700620 // Also, if requested, decode and reencode the uncorrected input image.
Matt Sarett687cc6c2017-05-01 17:52:56 -0400621 if (!FLAGS_uncorrected.isEmpty() && !FLAGS_icc) {
msarett97205a42016-05-27 14:01:02 -0700622 SkBitmap bitmap;
623 int width = codec->getInfo().width();
624 int height = codec->getInfo().height();
Matt Sarett8740d582016-11-11 13:59:14 -0500625 bitmap.allocN32Pixels(width, height, kOpaque_SkAlphaType == codec->getInfo().alphaType());
626 SkImageInfo decodeInfo = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
msarett97205a42016-05-27 14:01:02 -0700627 if (SkCodec::kSuccess != codec->getPixels(decodeInfo, bitmap.getPixels(),
628 bitmap.rowBytes())) {
629 SkDebugf("Could not decode input image.\n");
630 return -1;
631 }
raftiasa97a60c2016-12-16 17:24:30 -0500632 sk_sp<SkData> out = sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG,
633 100);
msarett97205a42016-05-27 14:01:02 -0700634 if (!out) {
635 SkDebugf("Failed to encode uncorrected image.\n");
636 return -1;
637 }
638 SkFILEWStream bitmapStream(FLAGS_uncorrected[0]);
raftiasa97a60c2016-12-16 17:24:30 -0500639 if (!bitmapStream.write(out->data(), out->size())) {
msarett97205a42016-05-27 14:01:02 -0700640 SkDebugf("Failed to write uncorrected image output.\n");
641 return -1;
642 }
raftiasa97a60c2016-12-16 17:24:30 -0500643 SkDebugf("%s\n", FLAGS_uncorrected[0]);
msarett97205a42016-05-27 14:01:02 -0700644 }
645
msarettaabe15e2016-05-13 07:32:59 -0700646 return 0;
647}