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

Side by Side Diff: tools/visualize_color_gamut.cpp

Issue 2017153002: Add interesting features to visualize_color_gamut (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 6 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
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Resources.h" 8 #include "Resources.h"
9 9
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
11 #include "SkCanvas.h" 11 #include "SkCanvas.h"
12 #include "SkCodec.h" 12 #include "SkCodec.h"
13 #include "SkColorSpace.h" 13 #include "SkColorSpace.h"
14 #include "SkCommandLineFlags.h" 14 #include "SkCommandLineFlags.h"
15 #include "SkForceLinking.h" 15 #include "SkForceLinking.h"
16 #include "SkImageEncoder.h" 16 #include "SkImageEncoder.h"
17 #include "SkMatrix44.h" 17 #include "SkMatrix44.h"
18 #include "SkOSFile.h" 18 #include "SkOSFile.h"
19 19
20 __SK_FORCE_IMAGE_DECODER_LINKING; 20 __SK_FORCE_IMAGE_DECODER_LINKING;
21 21
22 DEFINE_string2(input, i, "input.png", "A path to the input image."); 22 DEFINE_string2(input, i, "input.png", "A path to the input image.");
23 DEFINE_string2(output, o, "output.png", "A path to the output image."); 23 DEFINE_string2(output, o, "output.png", "A path to the output image.");
24 DEFINE_bool(sRGB, false, "Draws the sRGB gamut.");
25 DEFINE_bool(adobeRGB, false, "Draws the Adobe RGB gamut.");
24 26
25 /** 27 /**
26 * Loads the triangular gamut as a set of three points. 28 * Loads the triangular gamut as a set of three points.
27 */ 29 */
28 static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) { 30 static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) {
29 // rx = rX / (rX + rY + rZ) 31 // rx = rX / (rX + rY + rZ)
30 // ry = rX / (rX + rY + rZ) 32 // ry = rX / (rX + rY + rZ)
31 // gx, gy, bx, and gy are calulcated similarly. 33 // gx, gy, bx, and gy are calulcated similarly.
32 float rSum = xyz.get(0, 0) + xyz.get(0, 1) + xyz.get(0, 2); 34 float rSum = xyz.get(0, 0) + xyz.get(0, 1) + xyz.get(0, 2);
33 float gSum = xyz.get(1, 0) + xyz.get(1, 1) + xyz.get(1, 2); 35 float gSum = xyz.get(1, 0) + xyz.get(1, 1) + xyz.get(1, 2);
34 float bSum = xyz.get(2, 0) + xyz.get(2, 1) + xyz.get(2, 2); 36 float bSum = xyz.get(2, 0) + xyz.get(2, 1) + xyz.get(2, 2);
35 rgb[0].fX = xyz.get(0, 0) / rSum; 37 rgb[0].fX = xyz.get(0, 0) / rSum;
36 rgb[0].fY = xyz.get(0, 1) / rSum; 38 rgb[0].fY = xyz.get(0, 1) / rSum;
37 rgb[1].fX = xyz.get(1, 0) / gSum; 39 rgb[1].fX = xyz.get(1, 0) / gSum;
38 rgb[1].fY = xyz.get(1, 1) / gSum; 40 rgb[1].fY = xyz.get(1, 1) / gSum;
39 rgb[2].fX = xyz.get(2, 0) / bSum; 41 rgb[2].fX = xyz.get(2, 0) / bSum;
40 rgb[2].fY = xyz.get(2, 1) / bSum; 42 rgb[2].fY = xyz.get(2, 1) / bSum;
41 } 43 }
42 44
43 /** 45 /**
44 * Calculates the area of the triangular gamut. 46 * Calculates the area of the triangular gamut.
45 */ 47 */
46 float calculate_area(SkPoint abc[]) { 48 static float calculate_area(SkPoint abc[]) {
47 SkPoint a = abc[0]; 49 SkPoint a = abc[0];
48 SkPoint b = abc[1]; 50 SkPoint b = abc[1];
49 SkPoint c = abc[2]; 51 SkPoint c = abc[2];
50 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a. fY); 52 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a. fY);
51 } 53 }
52 54
55 static void draw_gamut(SkCanvas* canvas, const SkMatrix44& xyz, const char* name , SkColor color,
56 bool label) {
57 // Report the XYZ values.
58 SkDebugf("%s\n", name);
59 SkDebugf(" X Y Z\n");
60 SkDebugf("Red %.3f %.3f %.3f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2));
61 SkDebugf("Green %.3f %.3f %.3f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2));
62 SkDebugf("Blue %.3f %.3f %.3f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2));
63
64 // Calculate the points in the gamut from the XYZ values.
65 SkPoint rgb[4];
66 load_gamut(rgb, xyz);
67
68 // Report the area of the gamut.
69 SkDebugf("Area of Gamut: %.3f\n\n", calculate_area(rgb));
70
71 // Magic constants that help us place the gamut triangles in the appropriate position
72 // on the canvas.
73 float xScale = 2071.25f; // Num pixels from 0 to 1 in x
74 float xOffset = 241.0f; // Num pixels until start of x-axis
75 float yScale = 2067.78f; // Num pixels from 0 to 1 in y
76 float yOffset = -144.78; // Num pixels until start of y-axis
77 // (negative because y extends beyond image bounds)
78
79 // Now transform the points so they can be drawn on our canvas.
80 // Note that y increases as we move down the canvas.
81 rgb[0].fX = xOffset + xScale * rgb[0].fX;
82 rgb[0].fY = yOffset + yScale * (1.0f - rgb[0].fY);
83 rgb[1].fX = xOffset + xScale * rgb[1].fX;
84 rgb[1].fY = yOffset + yScale * (1.0f - rgb[1].fY);
85 rgb[2].fX = xOffset + xScale * rgb[2].fX;
86 rgb[2].fY = yOffset + yScale * (1.0f - rgb[2].fY);
87
88 // Repeat the first point to connect the polygon.
89 rgb[3] = rgb[0];
90 SkPaint paint;
91 paint.setColor(color);
92 paint.setStrokeWidth(6.0f);
93 paint.setTextSize(75.0f);
94 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint);
95 if (label) {
96 canvas->drawText("R", 1, rgb[0].fX + 5.0f, rgb[0].fY + 75.0f, paint);
97 canvas->drawText("G", 1, rgb[1].fX + 5.0f, rgb[1].fY - 5.0f, paint);
98 canvas->drawText("B", 1, rgb[2].fX - 75.0f, rgb[2].fY - 5.0f, paint);
99 }
100 }
101
53 int main(int argc, char** argv) { 102 int main(int argc, char** argv) {
54 SkCommandLineFlags::SetUsage( 103 SkCommandLineFlags::SetUsage(
55 "Usage: visualize_color_gamut --input <path to input image>" 104 "Usage: visualize_color_gamut --input <path to input image> "
56 "--output <path to output image>\n" 105 "--output <path to output image> "
57 "Description: Writes a visualization of the color gamut to the outpu t image\n"); 106 "--sRGB <draw canonical sRGB gamut> "
107 "--adobeRGB <draw canonical Adobe RGB g amut>\n"
108 "Description: Writes a visualization of the color gamut to the outpu t image ."
109 "Also, writes uncorrected bytes to an unmarked png, for comparison "
110 "with the input image.\n");
58 SkCommandLineFlags::Parse(argc, argv); 111 SkCommandLineFlags::Parse(argc, argv);
59 const char* input = FLAGS_input[0]; 112 const char* input = FLAGS_input[0];
60 const char* output = FLAGS_output[0]; 113 const char* output = FLAGS_output[0];
61 if (!input || !output) { 114 if (!input || !output) {
62 SkCommandLineFlags::PrintUsage(); 115 SkCommandLineFlags::PrintUsage();
63 return -1; 116 return -1;
64 } 117 }
65 118
66 SkAutoTUnref<SkData> data(SkData::NewFromFileName(input)); 119 SkAutoTUnref<SkData> data(SkData::NewFromFileName(input));
67 if (!data) { 120 if (!data) {
68 SkDebugf("Cannot find input image.\n"); 121 SkDebugf("Cannot find input image.\n");
69 return -1; 122 return -1;
70 } 123 }
71 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); 124 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
72 if (!codec) { 125 if (!codec) {
73 SkDebugf("Invalid input image.\n"); 126 SkDebugf("Invalid input image.\n");
74 return -1; 127 return -1;
75 } 128 }
76 129
77 // Load a graph of the CIE XYZ color gamut. 130 // Load a graph of the CIE XYZ color gamut.
78 SkBitmap bitmap; 131 SkBitmap gamut;
79 if (!GetResourceAsBitmap("gamut.png", &bitmap)) { 132 if (!GetResourceAsBitmap("gamut.png", &gamut)) {
80 SkDebugf("Program failure.\n"); 133 SkDebugf("Program failure.\n");
81 return -1; 134 return -1;
82 } 135 }
83 SkCanvas canvas(bitmap); 136 SkCanvas canvas(gamut);
84 137
138 // Draw the sRGB gamut if requested.
139 if (FLAGS_sRGB) {
140 sk_sp<SkColorSpace> sRGBSpace = SkColorSpace::NewNamed(SkColorSpace::kSR GB_Named);
141 draw_gamut(&canvas, sRGBSpace->xyz(), "sRGB", 0xFFFF9394, false);
142 }
143
144 // Draw the Adobe RGB gamut if requested.
145 if (FLAGS_adobeRGB) {
146 sk_sp<SkColorSpace> adobeRGBSpace = SkColorSpace::NewNamed(SkColorSpace: :kAdobeRGB_Named);
147 draw_gamut(&canvas, adobeRGBSpace->xyz(), "Adobe RGB", 0xFF31a9e1, false );
148 }
149
150 // Draw gamut for the input image.
85 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(codec->getColorSpace()); 151 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(codec->getColorSpace());
86 if (!colorSpace) { 152 if (!colorSpace) {
87 SkDebugf("Image had no embedded color space information. Defaulting to sRGB.\n"); 153 SkDebugf("Image had no embedded color space information. Defaulting to sRGB.\n");
88 colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); 154 colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
89 } 155 }
156 draw_gamut(&canvas, colorSpace->xyz(), input, 0xFF000000, true);
90 157
91 // Calculate the points in the gamut from the XYZ values. 158 // Finally, encode the result to the output file.
92 SkMatrix44 xyz = colorSpace->xyz(); 159 SkAutoTUnref<SkData> out(SkImageEncoder::EncodeData(gamut, SkImageEncoder::k PNG_Type, 100));
93 SkPoint rgb[4];
94 load_gamut(rgb, xyz);
95
96 // Report the XYZ values.
97 SkDebugf(" X Y Z\n");
98 SkDebugf("Red %.2f %.2f %.2f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2));
99 SkDebugf("Green %.2f %.2f %.2f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2));
100 SkDebugf("Blue %.2f %.2f %.2f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2));
101
102 // Report the area of the gamut.
103 SkDebugf("Area of Gamut: %g\n", calculate_area(rgb));
104
105 // Now transform the points so they can be drawn on our canvas. We use 1000 pixels
106 // to represent the space from 0 to 1. Note that the graph is at an offset of (50, 50).
107 // Also note that y increases as we move down the canvas.
108 rgb[0].fX = 50 + 1000*rgb[0].fX;
109 rgb[0].fY = 50 + 1000*(1 - rgb[0].fY);
110 rgb[1].fX = 50 + 1000*rgb[1].fX;
111 rgb[1].fY = 50 + 1000*(1 - rgb[1].fY);
112 rgb[2].fX = 50 + 1000*rgb[2].fX;
113 rgb[2].fY = 50 + 1000*(1 - rgb[2].fY);
114
115 // Repeat the first point to connect the polygon.
116 rgb[3] = rgb[0];
117
118 SkPaint paint;
119 canvas.drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint);
120
121 // Finally, encode the result to out.png.
122 SkAutoTUnref<SkData> out(SkImageEncoder::EncodeData(bitmap, SkImageEncoder:: kPNG_Type, 100));
123 if (!out) { 160 if (!out) {
124 SkDebugf("Failed to encode output.\n"); 161 SkDebugf("Failed to encode gamut output.\n");
125 return -1; 162 return -1;
126 } 163 }
127 SkFILEWStream stream(output); 164 SkFILEWStream stream(output);
128 bool result = stream.write(out->data(), out->size()); 165 bool result = stream.write(out->data(), out->size());
129 if (!result) { 166 if (!result) {
130 SkDebugf("Failed to write output.\n"); 167 SkDebugf("Failed to write gamut output.\n");
131 return -1; 168 return -1;
132 } 169 }
133 170
171 // Also, decode and reencode the uncorrected input image.
172 SkBitmap bitmap;
173 int width = codec->getInfo().width();
174 int height = codec->getInfo().height();
175 SkAlphaType alphaType = codec->getInfo().alphaType();
176 bitmap.allocN32Pixels(width, height, kOpaque_SkAlphaType == alphaType);
177 SkImageInfo decodeInfo = SkImageInfo::MakeN32(width, height, alphaType);
178 if (SkCodec::kSuccess != codec->getPixels(decodeInfo, bitmap.getPixels(), bi tmap.rowBytes())) {
179 SkDebugf("Could not decode input image.\n");
180 return -1;
181 }
182 out.reset(SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100) );
183 if (!out) {
184 SkDebugf("Failed to encode uncorrected image.\n");
185 return -1;
186 }
187 SkString path(input);
jcgregorio 2016/05/27 20:00:38 The output path for the uncorrected input image sh
msarett 2016/05/27 20:30:59 Ok I've added the flag. There is no default, so w
188 path.append(".png");
189 SkFILEWStream bitmapStream(path.c_str());
190 result = bitmapStream.write(out->data(), out->size());
191 if (!result) {
192 SkDebugf("Failed to write uncorrected image output.\n");
193 return -1;
194 }
195
134 return 0; 196 return 0;
135 } 197 }
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