OLD | NEW |
(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.png", "A path to the input image."); |
| 23 DEFINE_string2(output, o, "output.png", "A path to the output image."); |
| 24 |
| 25 /** |
| 26 * Loads the triangular gamut as a set of three points. |
| 27 */ |
| 28 static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) { |
| 29 // rx = rX / (rX + rY + rZ) |
| 30 // ry = rX / (rX + rY + rZ) |
| 31 // gx, gy, bx, and gy are calulcated similarly. |
| 32 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); |
| 34 float bSum = xyz.get(2, 0) + xyz.get(2, 1) + xyz.get(2, 2); |
| 35 rgb[0].fX = xyz.get(0, 0) / rSum; |
| 36 rgb[0].fY = xyz.get(0, 1) / rSum; |
| 37 rgb[1].fX = xyz.get(1, 0) / gSum; |
| 38 rgb[1].fY = xyz.get(1, 1) / gSum; |
| 39 rgb[2].fX = xyz.get(2, 0) / bSum; |
| 40 rgb[2].fY = xyz.get(2, 1) / bSum; |
| 41 } |
| 42 |
| 43 /** |
| 44 * Calculates the area of the triangular gamut. |
| 45 */ |
| 46 float calculate_area(SkPoint abc[]) { |
| 47 SkPoint a = abc[0]; |
| 48 SkPoint b = abc[1]; |
| 49 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); |
| 51 } |
| 52 |
| 53 int main(int argc, char** argv) { |
| 54 SkCommandLineFlags::SetUsage( |
| 55 "Usage: visualize_color_gamut --input <path to input image>" |
| 56 "--output <path to output image>\n" |
| 57 "Description: Writes a visualization of the color gamut to the outpu
t image\n"); |
| 58 SkCommandLineFlags::Parse(argc, argv); |
| 59 const char* input = FLAGS_input[0]; |
| 60 const char* output = FLAGS_output[0]; |
| 61 if (!input || !output) { |
| 62 SkCommandLineFlags::PrintUsage(); |
| 63 return -1; |
| 64 } |
| 65 |
| 66 SkAutoTUnref<SkData> data(SkData::NewFromFileName(input)); |
| 67 if (!data) { |
| 68 SkDebugf("Cannot find input image.\n"); |
| 69 return -1; |
| 70 } |
| 71 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
| 72 if (!codec) { |
| 73 SkDebugf("Invalid input image.\n"); |
| 74 return -1; |
| 75 } |
| 76 |
| 77 // Load a graph of the CIE XYZ color gamut. |
| 78 SkBitmap bitmap; |
| 79 if (!GetResourceAsBitmap("gamut.png", &bitmap)) { |
| 80 SkDebugf("Program failure.\n"); |
| 81 return -1; |
| 82 } |
| 83 SkCanvas canvas(bitmap); |
| 84 |
| 85 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(codec->getColorSpace()); |
| 86 if (!colorSpace) { |
| 87 SkDebugf("Image had no embedded color space information. Defaulting to
sRGB.\n"); |
| 88 colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 89 } |
| 90 |
| 91 // Calculate the points in the gamut from the XYZ values. |
| 92 SkMatrix44 xyz = colorSpace->xyz(); |
| 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) { |
| 124 SkDebugf("Failed to encode output.\n"); |
| 125 return -1; |
| 126 } |
| 127 SkFILEWStream stream(output); |
| 128 bool result = stream.write(out->data(), out->size()); |
| 129 if (!result) { |
| 130 SkDebugf("Failed to write output.\n"); |
| 131 return -1; |
| 132 } |
| 133 |
| 134 return 0; |
| 135 } |
OLD | NEW |