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