Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: tools/visualize_color_gamut.cpp

Issue 1972343002: Simple program for visualizing gamuts (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « resources/gamut.png ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Resources.h"
9
10 #include "SkBitmap.h"
11 #include "SkCanvas.h"
12 #include "SkCodec.h"
13 #include "SkColorSpace.h"
14 #include "SkCommandLineFlags.h"
15 #include "SkForceLinking.h"
16 #include "SkImageEncoder.h"
17 #include "SkMatrix44.h"
18 #include "SkOSFile.h"
19
20 __SK_FORCE_IMAGE_DECODER_LINKING;
21
22 DEFINE_string2(input, i, "input", "A path to the input image.");
23
24 /**
25 * Loads the triangular gamut as a set of three points.
26 */
27 static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) {
28 // rx = rX / (rX + rY + rZ)
29 // ry = rX / (rX + rY + rZ)
30 // gx, gy, bx, and gy are calulcated similarly.
31 float rSum = xyz.get(0, 0) + xyz.get(0, 1) + xyz.get(0, 2);
32 float gSum = xyz.get(1, 0) + xyz.get(1, 1) + xyz.get(1, 2);
33 float bSum = xyz.get(2, 0) + xyz.get(2, 1) + xyz.get(2, 2);
34 rgb[0].fX = xyz.get(0, 0) / rSum;
35 rgb[0].fY = xyz.get(0, 1) / rSum;
36 rgb[1].fX = xyz.get(1, 0) / gSum;
37 rgb[1].fY = xyz.get(1, 1) / gSum;
38 rgb[2].fX = xyz.get(2, 0) / bSum;
39 rgb[2].fY = xyz.get(2, 1) / bSum;
40 }
41
42 /**
43 * Calculates the area of the triangular gamut.
44 */
45 float calculate_area(SkPoint abc[]) {
46 SkPoint a = abc[0];
47 SkPoint b = abc[1];
48 SkPoint c = abc[2];
49 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a. fY);
50 }
51
52 int main(int argc, char** argv) {
53 SkCommandLineFlags::SetUsage(
54 "Usage: visualize_color_gamut -i <path to input input>\n"
55 "Output: Outputs visualization of the color gamut to image_gamut.png \n");
jcgregorio 2016/05/13 13:36:36 Comment doesn't match actual code, currently write
msarett 2016/05/13 14:02:05 Thanks! Fixed this to use output filename passed
56 SkCommandLineFlags::Parse(argc, argv);
57 const char* image = FLAGS_input[0];
58
59 SkAutoTUnref<SkData> data(SkData::NewFromFileName(image));
60 if (!data) {
61 SkDebugf("Cannot find input image.\n");
62 return -1;
63 }
64 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
65 if (!codec) {
66 SkDebugf("Invalid input image.\n");
67 return -1;
68 }
69
70 // Load a graph of the CIE XYZ color gamut.
71 SkBitmap bitmap;
72 if (!GetResourceAsBitmap("gamut.png", &bitmap)) {
73 SkDebugf("Program failure.\n");
74 return -1;
75 }
76 SkCanvas canvas(bitmap);
77
78 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(codec->getColorSpace());
79 if (!colorSpace) {
80 SkDebugf("Image had no embedded color space information. Defaulting to sRGB.\n");
81 colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
82 }
83
84 // Calculate the points in the gamut from the XYZ values.
85 SkMatrix44 xyz = colorSpace->xyz();
86 SkPoint rgb[4];
87 load_gamut(rgb, xyz);
88
89 // Report the XYZ values.
90 SkDebugf(" X Y Z\n");
91 SkDebugf("Red %.2f %.2f %.2f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2));
92 SkDebugf("Green %.2f %.2f %.2f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2));
93 SkDebugf("Blue %.2f %.2f %.2f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2));
94
95 // Report the area of the gamut.
96 SkDebugf("Area of Gamut: %g\n", calculate_area(rgb));
97
98 // Now transform the points so they can be drawn on our canvas. We use 1000 pixels
99 // to represent the space from 0 to 1. Note that the graph is at an offset of (50, 50).
100 // Also note that y increases as we move down the canvas.
101 rgb[0].fX = 50 + 1000*rgb[0].fX;
102 rgb[0].fY = 50 + 1000*(1 - rgb[0].fY);
103 rgb[1].fX = 50 + 1000*rgb[1].fX;
104 rgb[1].fY = 50 + 1000*(1 - rgb[1].fY);
105 rgb[2].fX = 50 + 1000*rgb[2].fX;
106 rgb[2].fY = 50 + 1000*(1 - rgb[2].fY);
107
108 // Repeat the first point to connect the polygon.
109 rgb[3] = rgb[0];
110
111 SkPaint paint;
112 canvas.drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint);
113
114 // Finally, encode the result to out.png.
115 SkAutoTUnref<SkData> out(SkImageEncoder::EncodeData(bitmap, SkImageEncoder:: kPNG_Type, 100));
116 if (!out) {
117 SkDebugf("Failed to encode output.\n");
118 return -1;
119 }
120 SkFILEWStream stream("out.png");
jcgregorio 2016/05/13 13:36:36 Can the output filename be made into a flag?
msarett 2016/05/13 14:02:05 Yes! Done.
121 bool result = stream.write(out->data(), out->size());
122 if (!result) {
123 SkDebugf("Failed to write output.\n");
124 return -1;
125 }
126
127 return 0;
128 }
OLDNEW
« no previous file with comments | « resources/gamut.png ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698