blob: 0002e961cf7f68bdc06af3dd6fecc17dcf41a5cf [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"
Mike Kleinc2f876b2017-08-09 18:23:25 -040013#include "SkColorSpacePriv.h"
raftiasa97a60c2016-12-16 17:24:30 -050014#include "SkColorSpace_A2B.h"
Matt Sarett8740d582016-11-11 13:59:14 -050015#include "SkColorSpace_XYZ.h"
msarettaabe15e2016-05-13 07:32:59 -070016#include "SkCommandLineFlags.h"
Hal Canarya8565e52017-06-19 12:43:28 -040017#include "SkICCPriv.h"
msarettaabe15e2016-05-13 07:32:59 -070018#include "SkImageEncoder.h"
19#include "SkMatrix44.h"
20#include "SkOSFile.h"
Mike Kleinc2f876b2017-08-09 18:23:25 -040021#include "SkRasterPipeline.h"
22#include "../src/jumper/SkJumper.h"
msarettaabe15e2016-05-13 07:32:59 -070023
Hal Canarydb683012016-11-23 08:55:18 -070024#include "sk_tool_utils.h"
25
Matt Sarett1fa986c2017-05-01 17:42:20 -040026#include <sstream>
27#include <string>
28#include <vector>
29
Matt Sarett687cc6c2017-05-01 17:52:56 -040030DEFINE_string(input, "input.png", "A path to the input image (or icc profile with --icc).");
Matt Sarett1fa986c2017-05-01 17:42:20 -040031DEFINE_string(output, ".", "A path to the output image directory.");
Matt Sarett687cc6c2017-05-01 17:52:56 -040032DEFINE_bool(icc, false, "Indicates that the input is an icc profile.");
raftiasa97a60c2016-12-16 17:24:30 -050033DEFINE_bool(sRGB_gamut, false, "Draws the sRGB gamut on the gamut visualization.");
34DEFINE_bool(adobeRGB, false, "Draws the Adobe RGB gamut on the gamut visualization.");
35DEFINE_bool(sRGB_gamma, false, "Draws the sRGB gamma on all gamma output images.");
msarett97205a42016-05-27 14:01:02 -070036DEFINE_string(uncorrected, "", "A path to reencode the uncorrected input image.");
msarettaabe15e2016-05-13 07:32:59 -070037
Matt Sarett1fa986c2017-05-01 17:42:20 -040038
39//-------------------------------------------------------------------------------------------------
40//------------------------------------ Gamma visualizations ---------------------------------------
41
raftiasa97a60c2016-12-16 17:24:30 -050042static const char* kRGBChannelNames[3] = {
Matt Sarett1fa986c2017-05-01 17:42:20 -040043 "Red ",
44 "Green",
45 "Blue "
46};
47static const SkColor kRGBChannelColors[3] = {
48 SkColorSetARGB(128, 255, 0, 0),
49 SkColorSetARGB(128, 0, 255, 0),
50 SkColorSetARGB(128, 0, 0, 255)
raftiasa97a60c2016-12-16 17:24:30 -050051};
52
Matt Sarett1fa986c2017-05-01 17:42:20 -040053static const char* kGrayChannelNames[1] = { "Gray"};
54static const SkColor kGrayChannelColors[1] = { SkColorSetRGB(128, 128, 128) };
55
56static const char* kCMYKChannelNames[4] = {
57 "Cyan ",
58 "Magenta",
59 "Yellow ",
60 "Black "
61};
62static const SkColor kCMYKChannelColors[4] = {
63 SkColorSetARGB(128, 0, 255, 255),
64 SkColorSetARGB(128, 255, 0, 255),
65 SkColorSetARGB(128, 255, 255, 0),
66 SkColorSetARGB(128, 16, 16, 16)
67};
68
69static const char*const*const kChannelNames[4] = {
70 kGrayChannelNames,
71 kRGBChannelNames,
72 kRGBChannelNames,
73 kCMYKChannelNames
74};
75static const SkColor*const kChannelColors[4] = {
76 kGrayChannelColors,
77 kRGBChannelColors,
78 kRGBChannelColors,
79 kCMYKChannelColors
raftiasa97a60c2016-12-16 17:24:30 -050080};
81
82static void dump_transfer_fn(SkGammaNamed gammaNamed) {
83 switch (gammaNamed) {
Matt Sarett8740d582016-11-11 13:59:14 -050084 case kSRGB_SkGammaNamed:
85 SkDebugf("Transfer Function: sRGB\n");
86 return;
87 case k2Dot2Curve_SkGammaNamed:
88 SkDebugf("Exponential Transfer Function: Exponent 2.2\n");
89 return;
90 case kLinear_SkGammaNamed:
91 SkDebugf("Transfer Function: Linear\n");
92 return;
93 default:
94 break;
95 }
96
raftiasa97a60c2016-12-16 17:24:30 -050097}
98
Matt Sarett1fa986c2017-05-01 17:42:20 -040099static constexpr int kGammaImageWidth = 500;
100static constexpr int kGammaImageHeight = 500;
101
raftiasa97a60c2016-12-16 17:24:30 -0500102static void dump_transfer_fn(const SkGammas& gammas) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400103 SkASSERT(gammas.channels() <= 4);
104 const char*const*const channels = kChannelNames[gammas.channels() - 1];
raftiasa97a60c2016-12-16 17:24:30 -0500105 for (int i = 0; i < gammas.channels(); i++) {
106 if (gammas.isNamed(i)) {
107 switch (gammas.data(i).fNamed) {
Matt Sarett8740d582016-11-11 13:59:14 -0500108 case kSRGB_SkGammaNamed:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400109 SkDebugf("%s Transfer Function: sRGB\n", channels[i]);
Matt Sarett8740d582016-11-11 13:59:14 -0500110 return;
111 case k2Dot2Curve_SkGammaNamed:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400112 SkDebugf("%s Transfer Function: Exponent 2.2\n", channels[i]);
Matt Sarett8740d582016-11-11 13:59:14 -0500113 return;
114 case kLinear_SkGammaNamed:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400115 SkDebugf("%s Transfer Function: Linear\n", channels[i]);
Matt Sarett8740d582016-11-11 13:59:14 -0500116 return;
117 default:
118 SkASSERT(false);
119 continue;
120 }
raftiasa97a60c2016-12-16 17:24:30 -0500121 } else if (gammas.isValue(i)) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400122 SkDebugf("%s Transfer Function: Exponent %.3f\n", channels[i], gammas.data(i).fValue);
raftiasa97a60c2016-12-16 17:24:30 -0500123 } else if (gammas.isParametric(i)) {
124 const SkColorSpaceTransferFn& fn = gammas.data(i).params(&gammas);
Matt Sarett8740d582016-11-11 13:59:14 -0500125 SkDebugf("%s Transfer Function: Parametric A = %.3f, B = %.3f, C = %.3f, D = %.3f, "
Matt Sarett1fa986c2017-05-01 17:42:20 -0400126 "E = %.3f, F = %.3f, G = %.3f\n", channels[i], fn.fA, fn.fB, fn.fC, fn.fD,
127 fn.fE, fn.fF, fn.fG);
Matt Sarett8740d582016-11-11 13:59:14 -0500128 } else {
raftiasa97a60c2016-12-16 17:24:30 -0500129 SkASSERT(gammas.isTable(i));
Matt Sarett1fa986c2017-05-01 17:42:20 -0400130 SkDebugf("%s Transfer Function: Table (%d entries)\n", channels[i],
raftiasa97a60c2016-12-16 17:24:30 -0500131 gammas.data(i).fTable.fSize);
Matt Sarett8740d582016-11-11 13:59:14 -0500132 }
133 }
134}
135
raftiasa97a60c2016-12-16 17:24:30 -0500136static inline float parametric(const SkColorSpaceTransferFn& fn, float x) {
Matt Sarett444c1bd2017-01-12 13:39:04 -0500137 return x >= fn.fD ? powf(fn.fA*x + fn.fB, fn.fG) + fn.fE
138 : fn.fC*x + fn.fF;
raftiasa97a60c2016-12-16 17:24:30 -0500139}
140
141static void draw_transfer_fn(SkCanvas* canvas, SkGammaNamed gammaNamed, const SkGammas* gammas,
Matt Sarett1fa986c2017-05-01 17:42:20 -0400142 SkColor color) {
raftiasa97a60c2016-12-16 17:24:30 -0500143 SkColorSpaceTransferFn fn[4];
144 struct TableInfo {
145 const float* fTable;
146 int fSize;
147 };
148 TableInfo table[4];
149 bool isTable[4] = {false, false, false, false};
150 const int channels = gammas ? gammas->channels() : 1;
151 SkASSERT(channels <= 4);
152 if (kNonStandard_SkGammaNamed != gammaNamed) {
153 dump_transfer_fn(gammaNamed);
154 for (int i = 0; i < channels; ++i) {
155 named_to_parametric(&fn[i], gammaNamed);
156 }
157 } else {
158 SkASSERT(gammas);
159 dump_transfer_fn(*gammas);
160 for (int i = 0; i < channels; ++i) {
161 if (gammas->isTable(i)) {
162 table[i].fTable = gammas->table(i);
163 table[i].fSize = gammas->data(i).fTable.fSize;
164 isTable[i] = true;
165 } else {
166 switch (gammas->type(i)) {
167 case SkGammas::Type::kNamed_Type:
168 named_to_parametric(&fn[i], gammas->data(i).fNamed);
169 break;
170 case SkGammas::Type::kValue_Type:
171 value_to_parametric(&fn[i], gammas->data(i).fValue);
172 break;
173 case SkGammas::Type::kParam_Type:
174 fn[i] = gammas->params(i);
175 break;
176 default:
177 SkASSERT(false);
178 }
179 }
180 }
181 }
182 SkPaint paint;
183 paint.setStyle(SkPaint::kStroke_Style);
184 paint.setColor(color);
185 paint.setStrokeWidth(2.0f);
186 // note: gamma has positive values going up in this image so this origin is
187 // the bottom left and we must subtract y instead of adding.
188 const float gap = 16.0f;
Matt Sarett1fa986c2017-05-01 17:42:20 -0400189 const float gammaWidth = kGammaImageWidth - 2 * gap;
190 const float gammaHeight = kGammaImageHeight - 2 * gap;
raftiasa97a60c2016-12-16 17:24:30 -0500191 // gamma origin point
Matt Sarett1fa986c2017-05-01 17:42:20 -0400192 const float ox = gap;
raftiasa97a60c2016-12-16 17:24:30 -0500193 const float oy = gap + gammaHeight;
194 for (int i = 0; i < channels; ++i) {
195 if (kNonStandard_SkGammaNamed == gammaNamed) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400196 paint.setColor(kChannelColors[channels - 1][i]);
raftiasa97a60c2016-12-16 17:24:30 -0500197 } else {
198 paint.setColor(color);
199 }
200 if (isTable[i]) {
201 auto tx = [&table,i](int index) {
202 return index / (table[i].fSize - 1.0f);
203 };
204 for (int ti = 1; ti < table[i].fSize; ++ti) {
205 canvas->drawLine(ox + gammaWidth * tx(ti - 1),
206 oy - gammaHeight * table[i].fTable[ti - 1],
207 ox + gammaWidth * tx(ti),
208 oy - gammaHeight * table[i].fTable[ti],
209 paint);
210 }
211 } else {
212 const float step = 0.01f;
213 float yPrev = parametric(fn[i], 0.0f);
214 for (float x = step; x <= 1.0f; x += step) {
215 const float y = parametric(fn[i], x);
216 canvas->drawLine(ox + gammaWidth * (x - step), oy - gammaHeight * yPrev,
217 ox + gammaWidth * x, oy - gammaHeight * y,
218 paint);
219 yPrev = y;
220 }
221 }
222 }
223 paint.setColor(0xFF000000);
224 paint.setStrokeWidth(3.0f);
Mike Reed3661bc92017-02-22 13:21:42 -0500225 canvas->drawRect({ ox, oy - gammaHeight, ox + gammaWidth, oy }, paint);
raftiasa97a60c2016-12-16 17:24:30 -0500226}
227
Matt Sarett1fa986c2017-05-01 17:42:20 -0400228//-------------------------------------------------------------------------------------------------
229//------------------------------------ CLUT visualizations ----------------------------------------
230static void dump_clut(const SkColorLookUpTable& clut) {
231 SkDebugf("CLUT: ");
232 for (int i = 0; i < clut.inputChannels(); ++i) {
233 SkDebugf("[%d]", clut.gridPoints(i));
234 }
235 SkDebugf(" -> [%d]\n", clut.outputChannels());
236}
237
238constexpr int kClutGap = 8;
239constexpr float kClutCanvasSize = 2000;
240
241static inline int usedGridPoints(const SkColorLookUpTable& clut, int dimension) {
242 const int gp = clut.gridPoints(dimension);
243 return gp <= 16 ? gp : 16;
244}
245
246// how many rows of cross-section cuts to display
247static inline int cut_rows(const SkColorLookUpTable& clut, int dimOrder[4]) {
248 // and vertical ones for the 4th dimension (if applicable)
249 return clut.inputChannels() >= 4 ? usedGridPoints(clut, dimOrder[3]) : 1;
250}
251
252// how many columns of cross-section cuts to display
253static inline int cut_cols(const SkColorLookUpTable& clut, int dimOrder[4]) {
254 // do horizontal cuts for the 3rd dimension (if applicable)
255 return clut.inputChannels() >= 3 ? usedGridPoints(clut, dimOrder[2]) : 1;
256}
257
258// gets the width/height to use for cross-sections of a CLUT
259static int cut_size(const SkColorLookUpTable& clut, int dimOrder[4]) {
260 const int rows = cut_rows(clut, dimOrder);
261 const int cols = cut_cols(clut, dimOrder);
262 // make sure the cross-section CLUT cuts are square still by using the
263 // smallest of the width/height, then adjust the gaps between accordingly
264 const int cutWidth = (kClutCanvasSize - kClutGap * (1 + cols)) / cols;
265 const int cutHeight = (kClutCanvasSize - kClutGap * (1 + rows)) / rows;
266 return cutWidth < cutHeight ? cutWidth : cutHeight;
267}
268
Mike Kleinc2f876b2017-08-09 18:23:25 -0400269static void clut_interp(const SkColorLookUpTable& clut, float out[3], const float in[4]) {
270 // This is kind of a toy implementation.
271 // You generally wouldn't want to do this 1 pixel at a time.
272
273 SkJumper_ColorLookupTableCtx ctx;
274 ctx.table = clut.table();
275 for (int i = 0; i < clut.inputChannels(); i++) {
276 ctx.limits[i] = clut.gridPoints(i);
277 }
278
279 SkSTArenaAlloc<256> alloc;
280 SkRasterPipeline p(&alloc);
281 p.append_constant_color(&alloc, in);
282 p.append(clut.inputChannels() == 3 ? SkRasterPipeline::clut_3D
283 : SkRasterPipeline::clut_4D, &ctx);
284 p.append(SkRasterPipeline::clamp_0);
285 p.append(SkRasterPipeline::clamp_1);
286 p.append(SkRasterPipeline::store_f32, &out);
287 p.run(0,0, 1,1);
288}
289
Matt Sarett1fa986c2017-05-01 17:42:20 -0400290static void draw_clut(SkCanvas* canvas, const SkColorLookUpTable& clut, int dimOrder[4]) {
291 dump_clut(clut);
292
293 const int cutSize = cut_size(clut, dimOrder);
294 const int rows = cut_rows(clut, dimOrder);
295 const int cols = cut_cols(clut, dimOrder);
296 const int cutHorizGap = (kClutCanvasSize - cutSize * cols) / (1 + cols);
297 const int cutVertGap = (kClutCanvasSize - cutSize * rows) / (1 + rows);
298
299 SkPaint paint;
300 for (int row = 0; row < rows; ++row) {
301 for (int col = 0; col < cols; ++col) {
302 // make sure to move at least one pixel, but otherwise move per-gridpoint
303 const float xStep = 1.0f / (SkTMin(cutSize, clut.gridPoints(dimOrder[0])) - 1);
304 const float yStep = 1.0f / (SkTMin(cutSize, clut.gridPoints(dimOrder[1])) - 1);
305 const float ox = clut.inputChannels() >= 3 ? (1 + col) * cutHorizGap + col * cutSize
306 : kClutGap;
307 const float oy = clut.inputChannels() >= 4 ? (1 + row) * cutVertGap + row * cutSize
308 : kClutGap;
309 // for each cross-section cut, draw a bunch of squares whose colour is the top-left's
310 // colour in the CLUT (usually this will just draw the gridpoints)
311 for (float x = 0.0f; x < 1.0f; x += xStep) {
312 for (float y = 0.0f; y < 1.0f; y += yStep) {
313 const float z = col / (cols - 1.0f);
314 const float w = row / (rows - 1.0f);
315 const float input[4] = {x, y, z, w};
316 float output[3];
Mike Kleinc2f876b2017-08-09 18:23:25 -0400317 clut_interp(clut, output, input);
Matt Sarett1fa986c2017-05-01 17:42:20 -0400318 paint.setColor(SkColorSetRGB(255*output[0], 255*output[1], 255*output[2]));
319 canvas->drawRect(SkRect::MakeLTRB(ox + cutSize * x, oy + cutSize * y,
320 ox + cutSize * (x + xStep),
321 oy + cutSize * (y + yStep)), paint);
322 }
323 }
324 }
325 }
326}
327
328
329//-------------------------------------------------------------------------------------------------
330//------------------------------------ Gamut visualizations ---------------------------------------
331static void dump_matrix(const SkMatrix44& m) {
332 for (int r = 0; r < 4; ++r) {
333 SkDebugf("|");
334 for (int c = 0; c < 4; ++c) {
335 SkDebugf(" %f ", m.get(r, c));
336 }
337 SkDebugf("|\n");
338 }
339}
340
msarettaabe15e2016-05-13 07:32:59 -0700341/**
342 * Loads the triangular gamut as a set of three points.
343 */
344static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) {
345 // rx = rX / (rX + rY + rZ)
346 // ry = rX / (rX + rY + rZ)
347 // gx, gy, bx, and gy are calulcated similarly.
brianosmande68d6c2016-09-09 10:36:17 -0700348 float rSum = xyz.get(0, 0) + xyz.get(1, 0) + xyz.get(2, 0);
349 float gSum = xyz.get(0, 1) + xyz.get(1, 1) + xyz.get(2, 1);
350 float bSum = xyz.get(0, 2) + xyz.get(1, 2) + xyz.get(2, 2);
msarettaabe15e2016-05-13 07:32:59 -0700351 rgb[0].fX = xyz.get(0, 0) / rSum;
brianosmande68d6c2016-09-09 10:36:17 -0700352 rgb[0].fY = xyz.get(1, 0) / rSum;
353 rgb[1].fX = xyz.get(0, 1) / gSum;
msarettaabe15e2016-05-13 07:32:59 -0700354 rgb[1].fY = xyz.get(1, 1) / gSum;
brianosmande68d6c2016-09-09 10:36:17 -0700355 rgb[2].fX = xyz.get(0, 2) / bSum;
356 rgb[2].fY = xyz.get(1, 2) / bSum;
msarettaabe15e2016-05-13 07:32:59 -0700357}
358
359/**
360 * Calculates the area of the triangular gamut.
361 */
msarett97205a42016-05-27 14:01:02 -0700362static float calculate_area(SkPoint abc[]) {
msarettaabe15e2016-05-13 07:32:59 -0700363 SkPoint a = abc[0];
364 SkPoint b = abc[1];
365 SkPoint c = abc[2];
366 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
367}
368
msarett97205a42016-05-27 14:01:02 -0700369static void draw_gamut(SkCanvas* canvas, const SkMatrix44& xyz, const char* name, SkColor color,
370 bool label) {
371 // Report the XYZ values.
372 SkDebugf("%s\n", name);
brianosmande68d6c2016-09-09 10:36:17 -0700373 SkDebugf(" R G B\n");
374 SkDebugf("X %.3f %.3f %.3f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2));
375 SkDebugf("Y %.3f %.3f %.3f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2));
376 SkDebugf("Z %.3f %.3f %.3f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2));
msarett97205a42016-05-27 14:01:02 -0700377
378 // Calculate the points in the gamut from the XYZ values.
379 SkPoint rgb[4];
380 load_gamut(rgb, xyz);
381
382 // Report the area of the gamut.
383 SkDebugf("Area of Gamut: %.3f\n\n", calculate_area(rgb));
384
385 // Magic constants that help us place the gamut triangles in the appropriate position
386 // on the canvas.
387 const float xScale = 2071.25f; // Num pixels from 0 to 1 in x
388 const float xOffset = 241.0f; // Num pixels until start of x-axis
389 const float yScale = 2067.78f; // Num pixels from 0 to 1 in y
390 const float yOffset = -144.78f; // Num pixels until start of y-axis
391 // (negative because y extends beyond image bounds)
392
393 // Now transform the points so they can be drawn on our canvas.
394 // Note that y increases as we move down the canvas.
395 rgb[0].fX = xOffset + xScale * rgb[0].fX;
396 rgb[0].fY = yOffset + yScale * (1.0f - rgb[0].fY);
397 rgb[1].fX = xOffset + xScale * rgb[1].fX;
398 rgb[1].fY = yOffset + yScale * (1.0f - rgb[1].fY);
399 rgb[2].fX = xOffset + xScale * rgb[2].fX;
400 rgb[2].fY = yOffset + yScale * (1.0f - rgb[2].fY);
401
402 // Repeat the first point to connect the polygon.
403 rgb[3] = rgb[0];
404 SkPaint paint;
405 paint.setColor(color);
406 paint.setStrokeWidth(6.0f);
407 paint.setTextSize(75.0f);
408 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint);
409 if (label) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400410 canvas->drawString("R", rgb[0].fX + 5.0f, rgb[0].fY + 75.0f, paint);
411 canvas->drawString("G", rgb[1].fX + 5.0f, rgb[1].fY - 5.0f, paint);
412 canvas->drawString("B", rgb[2].fX - 75.0f, rgb[2].fY - 5.0f, paint);
msarett97205a42016-05-27 14:01:02 -0700413 }
414}
415
Matt Sarett1fa986c2017-05-01 17:42:20 -0400416
417//-------------------------------------------------------------------------------------------------
418//----------------------------------------- Main code ---------------------------------------------
419static SkBitmap transparentBitmap(int width, int height) {
420 SkBitmap bitmap;
421 bitmap.allocN32Pixels(width, height);
422 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0));
423 return bitmap;
424}
425
426class OutputCanvas {
427public:
428 OutputCanvas(SkBitmap&& bitmap)
429 :fBitmap(bitmap)
430 ,fCanvas(fBitmap)
431 {}
432
433 bool save(std::vector<std::string>* output, const std::string& filename) {
434 // Finally, encode the result to the output file.
435 sk_sp<SkData> out = sk_tool_utils::EncodeImageToData(fBitmap, SkEncodedImageFormat::kPNG,
436 100);
437 if (!out) {
438 SkDebugf("Failed to encode %s output.\n", filename.c_str());
439 return false;
440 }
441 SkFILEWStream stream(filename.c_str());
442 if (!stream.write(out->data(), out->size())) {
443 SkDebugf("Failed to write %s output.\n", filename.c_str());
444 return false;
445 }
446 // record name of canvas
447 output->push_back(filename);
448 return true;
449 }
450
451 SkCanvas* canvas() { return &fCanvas; }
452
453private:
454 SkBitmap fBitmap;
455 SkCanvas fCanvas;
456};
457
msarettaabe15e2016-05-13 07:32:59 -0700458int main(int argc, char** argv) {
459 SkCommandLineFlags::SetUsage(
Matt Sarett687cc6c2017-05-01 17:52:56 -0400460 "Usage: colorspaceinfo --input <path to input image (or icc profile with --icc)> "
Matt Sarett1fa986c2017-05-01 17:42:20 -0400461 "--output <directory to output images> "
Matt Sarett687cc6c2017-05-01 17:52:56 -0400462 "--icc <indicates that the input is an icc profile>"
Matt Sarett1fa986c2017-05-01 17:42:20 -0400463 "--sRGB_gamut <draw canonical sRGB gamut> "
Matt Sarett8740d582016-11-11 13:59:14 -0500464 "--adobeRGB <draw canonical Adobe RGB gamut> "
Matt Sarett1fa986c2017-05-01 17:42:20 -0400465 "--sRGB_gamma <draw sRGB gamma> "
Matt Sarett8740d582016-11-11 13:59:14 -0500466 "--uncorrected <path to reencoded, uncorrected input image>\n"
raftiasa97a60c2016-12-16 17:24:30 -0500467 "Description: Writes visualizations of the color space to the output image(s) ."
msarett97205a42016-05-27 14:01:02 -0700468 "Also, if a path is provided, writes uncorrected bytes to an unmarked "
469 "png, for comparison with the input image.\n");
msarettaabe15e2016-05-13 07:32:59 -0700470 SkCommandLineFlags::Parse(argc, argv);
471 const char* input = FLAGS_input[0];
Matt Sarett1fa986c2017-05-01 17:42:20 -0400472 const char* output = FLAGS_output[0];
473 if (!input || !output) {
msarettaabe15e2016-05-13 07:32:59 -0700474 SkCommandLineFlags::PrintUsage();
475 return -1;
476 }
477
bungeman38d909e2016-08-02 14:40:46 -0700478 sk_sp<SkData> data(SkData::MakeFromFileName(input));
msarettaabe15e2016-05-13 07:32:59 -0700479 if (!data) {
480 SkDebugf("Cannot find input image.\n");
481 return -1;
482 }
Matt Sarett687cc6c2017-05-01 17:52:56 -0400483
484 std::unique_ptr<SkCodec> codec = nullptr;
Matt Sarett8740d582016-11-11 13:59:14 -0500485 sk_sp<SkColorSpace> colorSpace = nullptr;
Matt Sarett687cc6c2017-05-01 17:52:56 -0400486 if (FLAGS_icc) {
Matt Sarett8740d582016-11-11 13:59:14 -0500487 colorSpace = SkColorSpace::MakeICC(data->bytes(), data->size());
Matt Sarett687cc6c2017-05-01 17:52:56 -0400488 } else {
Mike Reedede7bac2017-07-23 15:30:02 -0400489 codec = SkCodec::MakeFromData(data);
Matt Sarett687cc6c2017-05-01 17:52:56 -0400490 colorSpace = sk_ref_sp(codec->getInfo().colorSpace());
Mike Kleinfd51b2b2017-07-24 10:15:10 -0400491 SkDebugf("SkCodec would naturally decode as colorType=%s\n",
492 sk_tool_utils::colortype_name(codec->getInfo().colorType()));
Matt Sarett8740d582016-11-11 13:59:14 -0500493 }
494
495 if (!colorSpace) {
496 SkDebugf("Cannot create codec or icc profile from input file.\n");
msarettaabe15e2016-05-13 07:32:59 -0700497 return -1;
498 }
499
Hal Canarya8565e52017-06-19 12:43:28 -0400500 {
501 SkColorSpaceTransferFn colorSpaceTransferFn;
Mike Kleinc722f792017-07-31 11:57:21 -0400502 SkMatrix44 toXYZD50(SkMatrix44::kIdentity_Constructor);
Hal Canarya8565e52017-06-19 12:43:28 -0400503 if (colorSpace->isNumericalTransferFn(&colorSpaceTransferFn) &&
504 colorSpace->toXYZD50(&toXYZD50)) {
505 SkString description = SkICCGetColorProfileTag(colorSpaceTransferFn, toXYZD50);
506 SkDebugf("Color Profile Description: \"%s\"\n", description.c_str());
507 }
508 }
509
Matt Sarett1fa986c2017-05-01 17:42:20 -0400510 // TODO: command line tweaking of this order
511 int dimOrder[4] = {0, 1, 2, 3};
raftiasa97a60c2016-12-16 17:24:30 -0500512
Matt Sarett1fa986c2017-05-01 17:42:20 -0400513 std::vector<std::string> outputFilenames;
msarettaabe15e2016-05-13 07:32:59 -0700514
Matt Sarett1fa986c2017-05-01 17:42:20 -0400515 auto createOutputFilename = [output](const char* category, int index) -> std::string {
516 std::stringstream ss;
517 ss << output << '/' << category << '_' << index << ".png";
518 return ss.str();
519 };
msarett97205a42016-05-27 14:01:02 -0700520
Matt Sarett8740d582016-11-11 13:59:14 -0500521 if (SkColorSpace_Base::Type::kXYZ == as_CSB(colorSpace)->type()) {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400522 SkDebugf("XYZ/TRC color space\n");
523
524 // Load a graph of the CIE XYZ color gamut.
525 SkBitmap gamutCanvasBitmap;
526 if (!GetResourceAsBitmap("gamut.png", &gamutCanvasBitmap)) {
527 SkDebugf("Program failure (could not load gamut.png).\n");
528 return -1;
529 }
530 OutputCanvas gamutCanvas(std::move(gamutCanvasBitmap));
531 // Draw the sRGB gamut if requested.
532 if (FLAGS_sRGB_gamut) {
533 sk_sp<SkColorSpace> sRGBSpace = SkColorSpace::MakeSRGB();
534 const SkMatrix44* mat = as_CSB(sRGBSpace)->toXYZD50();
535 SkASSERT(mat);
536 draw_gamut(gamutCanvas.canvas(), *mat, "sRGB", 0xFFFF9394, false);
537 }
538
539 // Draw the Adobe RGB gamut if requested.
540 if (FLAGS_adobeRGB) {
541 sk_sp<SkColorSpace> adobeRGBSpace = SkColorSpace::MakeRGB(
542 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kAdobeRGB_Gamut);
543 const SkMatrix44* mat = as_CSB(adobeRGBSpace)->toXYZD50();
544 SkASSERT(mat);
545 draw_gamut(gamutCanvas.canvas(), *mat, "Adobe RGB", 0xFF31a9e1, false);
546 }
Matt Sarett8740d582016-11-11 13:59:14 -0500547 const SkMatrix44* mat = as_CSB(colorSpace)->toXYZD50();
548 SkASSERT(mat);
raftiasa97a60c2016-12-16 17:24:30 -0500549 auto xyz = static_cast<SkColorSpace_XYZ*>(colorSpace.get());
Matt Sarett1fa986c2017-05-01 17:42:20 -0400550 draw_gamut(gamutCanvas.canvas(), *mat, input, 0xFF000000, true);
551 if (!gamutCanvas.save(&outputFilenames, createOutputFilename("gamut", 0))) {
552 return -1;
raftiasa97a60c2016-12-16 17:24:30 -0500553 }
Matt Sarett1fa986c2017-05-01 17:42:20 -0400554
555 OutputCanvas gammaCanvas(transparentBitmap(kGammaImageWidth, kGammaImageHeight));
556 if (FLAGS_sRGB_gamma) {
557 draw_transfer_fn(gammaCanvas.canvas(), kSRGB_SkGammaNamed, nullptr, 0xFFFF9394);
558 }
559 draw_transfer_fn(gammaCanvas.canvas(), xyz->gammaNamed(), xyz->gammas(), 0xFF000000);
560 if (!gammaCanvas.save(&outputFilenames, createOutputFilename("gamma", 0))) {
561 return -1;
562 }
Matt Sarett8740d582016-11-11 13:59:14 -0500563 } else {
Matt Sarett1fa986c2017-05-01 17:42:20 -0400564 SkDebugf("A2B color space");
565 SkColorSpace_A2B* a2b = static_cast<SkColorSpace_A2B*>(colorSpace.get());
566 SkDebugf("Conversion type: ");
567 switch (a2b->iccType()) {
Leon Scroggins IIIf78b55c2017-10-31 13:49:14 -0400568 case SkColorSpace::kRGB_Type:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400569 SkDebugf("RGB");
570 break;
Leon Scroggins IIIf78b55c2017-10-31 13:49:14 -0400571 case SkColorSpace::kCMYK_Type:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400572 SkDebugf("CMYK");
573 break;
Leon Scroggins IIIf78b55c2017-10-31 13:49:14 -0400574 case SkColorSpace::kGray_Type:
Matt Sarett1fa986c2017-05-01 17:42:20 -0400575 SkDebugf("Gray");
576 break;
577 default:
578 SkASSERT(false);
579 break;
580
581 }
582 SkDebugf(" -> ");
583 switch (a2b->pcs()) {
584 case SkColorSpace_A2B::PCS::kXYZ:
585 SkDebugf("XYZ\n");
586 break;
587 case SkColorSpace_A2B::PCS::kLAB:
588 SkDebugf("LAB\n");
589 break;
590 }
591 int clutCount = 0;
592 int gammaCount = 0;
593 for (int i = 0; i < a2b->count(); ++i) {
594 const SkColorSpace_A2B::Element& e = a2b->element(i);
595 switch (e.type()) {
596 case SkColorSpace_A2B::Element::Type::kGammaNamed: {
597 OutputCanvas gammaCanvas(transparentBitmap(kGammaImageWidth,
598 kGammaImageHeight));
599 if (FLAGS_sRGB_gamma) {
600 draw_transfer_fn(gammaCanvas.canvas(), kSRGB_SkGammaNamed, nullptr,
601 0xFFFF9394);
602 }
603 draw_transfer_fn(gammaCanvas.canvas(), e.gammaNamed(), nullptr,
604 0xFF000000);
605 if (!gammaCanvas.save(&outputFilenames,
606 createOutputFilename("gamma", gammaCount++))) {
607 return -1;
608 }
609 }
610 break;
611 case SkColorSpace_A2B::Element::Type::kGammas: {
612 OutputCanvas gammaCanvas(transparentBitmap(kGammaImageWidth,
613 kGammaImageHeight));
614 if (FLAGS_sRGB_gamma) {
615 draw_transfer_fn(gammaCanvas.canvas(), kSRGB_SkGammaNamed, nullptr,
616 0xFFFF9394);
617 }
618 draw_transfer_fn(gammaCanvas.canvas(), kNonStandard_SkGammaNamed,
619 &e.gammas(), 0xFF000000);
620 if (!gammaCanvas.save(&outputFilenames,
621 createOutputFilename("gamma", gammaCount++))) {
622 return -1;
623 }
624 }
625 break;
626 case SkColorSpace_A2B::Element::Type::kCLUT: {
627 const SkColorLookUpTable& clut = e.colorLUT();
628 const int cutSize = cut_size(clut, dimOrder);
629 const int clutWidth = clut.inputChannels() >= 3 ? kClutCanvasSize
630 : 2 * kClutGap + cutSize;
631 const int clutHeight = clut.inputChannels() >= 4 ? kClutCanvasSize
632 : 2 * kClutGap + cutSize;
633 OutputCanvas clutCanvas(transparentBitmap(clutWidth, clutHeight));
634 draw_clut(clutCanvas.canvas(), e.colorLUT(), dimOrder);
635 if (!clutCanvas.save(&outputFilenames,
636 createOutputFilename("clut", clutCount++))) {
637 return -1;
638 }
639 }
640 break;
641 case SkColorSpace_A2B::Element::Type::kMatrix:
642 dump_matrix(e.matrix());
643 break;
644 }
645 }
msarettaabe15e2016-05-13 07:32:59 -0700646 }
647
raftiasa97a60c2016-12-16 17:24:30 -0500648 // marker to tell the web-tool the names of all images output
649 SkDebugf("=========\n");
Matt Sarett1fa986c2017-05-01 17:42:20 -0400650 for (const std::string& filename : outputFilenames) {
651 SkDebugf("%s\n", filename.c_str());
raftiasa97a60c2016-12-16 17:24:30 -0500652 }
Matt Sarett687cc6c2017-05-01 17:52:56 -0400653 if (!FLAGS_icc) {
raftiasa97a60c2016-12-16 17:24:30 -0500654 SkDebugf("%s\n", input);
655 }
msarett97205a42016-05-27 14:01:02 -0700656 // Also, if requested, decode and reencode the uncorrected input image.
Matt Sarett687cc6c2017-05-01 17:52:56 -0400657 if (!FLAGS_uncorrected.isEmpty() && !FLAGS_icc) {
msarett97205a42016-05-27 14:01:02 -0700658 SkBitmap bitmap;
659 int width = codec->getInfo().width();
660 int height = codec->getInfo().height();
Matt Sarett8740d582016-11-11 13:59:14 -0500661 bitmap.allocN32Pixels(width, height, kOpaque_SkAlphaType == codec->getInfo().alphaType());
662 SkImageInfo decodeInfo = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
msarett97205a42016-05-27 14:01:02 -0700663 if (SkCodec::kSuccess != codec->getPixels(decodeInfo, bitmap.getPixels(),
664 bitmap.rowBytes())) {
665 SkDebugf("Could not decode input image.\n");
666 return -1;
667 }
raftiasa97a60c2016-12-16 17:24:30 -0500668 sk_sp<SkData> out = sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG,
669 100);
msarett97205a42016-05-27 14:01:02 -0700670 if (!out) {
671 SkDebugf("Failed to encode uncorrected image.\n");
672 return -1;
673 }
674 SkFILEWStream bitmapStream(FLAGS_uncorrected[0]);
raftiasa97a60c2016-12-16 17:24:30 -0500675 if (!bitmapStream.write(out->data(), out->size())) {
msarett97205a42016-05-27 14:01:02 -0700676 SkDebugf("Failed to write uncorrected image output.\n");
677 return -1;
678 }
raftiasa97a60c2016-12-16 17:24:30 -0500679 SkDebugf("%s\n", FLAGS_uncorrected[0]);
msarett97205a42016-05-27 14:01:02 -0700680 }
681
msarettaabe15e2016-05-13 07:32:59 -0700682 return 0;
683}