From 97205a4b419e0b7b1864db1945c4098c01874bbc Mon Sep 17 00:00:00 2001 From: msarett Date: Fri, 27 May 2016 14:01:02 -0700 Subject: Add interesting features to visualize_color_gamut (1) Draw triangles onto a D50 color gamut so they are actually in the correct place. (2) Add options to display canonical sRGB and Adobe RGB gamuts. (3) Label the R, G, and B points of the color gamut. (4) Decode and reencode the input image without color correction, so we can compare to the corrected version. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2017153002 Review-Url: https://codereview.chromium.org/2017153002 --- resources/gamut.png | Bin 131981 -> 479210 bytes tools/visualize_color_gamut.cpp | 152 ++++++++++++++++++++++++++++------------ 2 files changed, 109 insertions(+), 43 deletions(-) diff --git a/resources/gamut.png b/resources/gamut.png index 8fb2ce9b7f..7e3782b3f6 100644 Binary files a/resources/gamut.png and b/resources/gamut.png differ diff --git a/tools/visualize_color_gamut.cpp b/tools/visualize_color_gamut.cpp index 8fdd962185..bc40152a44 100644 --- a/tools/visualize_color_gamut.cpp +++ b/tools/visualize_color_gamut.cpp @@ -19,8 +19,11 @@ __SK_FORCE_IMAGE_DECODER_LINKING; -DEFINE_string2(input, i, "input.png", "A path to the input image."); -DEFINE_string2(output, o, "output.png", "A path to the output image."); +DEFINE_string(input, "input.png", "A path to the input image."); +DEFINE_string(output, "output.png", "A path to the output image."); +DEFINE_bool(sRGB, false, "Draws the sRGB gamut."); +DEFINE_bool(adobeRGB, false, "Draws the Adobe RGB gamut."); +DEFINE_string(uncorrected, "", "A path to reencode the uncorrected input image."); /** * Loads the triangular gamut as a set of three points. @@ -43,18 +46,71 @@ static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) { /** * Calculates the area of the triangular gamut. */ -float calculate_area(SkPoint abc[]) { +static float calculate_area(SkPoint abc[]) { SkPoint a = abc[0]; SkPoint b = abc[1]; SkPoint c = abc[2]; return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY); } +static void draw_gamut(SkCanvas* canvas, const SkMatrix44& xyz, const char* name, SkColor color, + bool label) { + // Report the XYZ values. + SkDebugf("%s\n", name); + SkDebugf(" X Y Z\n"); + SkDebugf("Red %.3f %.3f %.3f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2)); + SkDebugf("Green %.3f %.3f %.3f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2)); + SkDebugf("Blue %.3f %.3f %.3f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2)); + + // Calculate the points in the gamut from the XYZ values. + SkPoint rgb[4]; + load_gamut(rgb, xyz); + + // Report the area of the gamut. + SkDebugf("Area of Gamut: %.3f\n\n", calculate_area(rgb)); + + // Magic constants that help us place the gamut triangles in the appropriate position + // on the canvas. + const float xScale = 2071.25f; // Num pixels from 0 to 1 in x + const float xOffset = 241.0f; // Num pixels until start of x-axis + const float yScale = 2067.78f; // Num pixels from 0 to 1 in y + const float yOffset = -144.78f; // Num pixels until start of y-axis + // (negative because y extends beyond image bounds) + + // Now transform the points so they can be drawn on our canvas. + // Note that y increases as we move down the canvas. + rgb[0].fX = xOffset + xScale * rgb[0].fX; + rgb[0].fY = yOffset + yScale * (1.0f - rgb[0].fY); + rgb[1].fX = xOffset + xScale * rgb[1].fX; + rgb[1].fY = yOffset + yScale * (1.0f - rgb[1].fY); + rgb[2].fX = xOffset + xScale * rgb[2].fX; + rgb[2].fY = yOffset + yScale * (1.0f - rgb[2].fY); + + // Repeat the first point to connect the polygon. + rgb[3] = rgb[0]; + SkPaint paint; + paint.setColor(color); + paint.setStrokeWidth(6.0f); + paint.setTextSize(75.0f); + canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint); + if (label) { + canvas->drawText("R", 1, rgb[0].fX + 5.0f, rgb[0].fY + 75.0f, paint); + canvas->drawText("G", 1, rgb[1].fX + 5.0f, rgb[1].fY - 5.0f, paint); + canvas->drawText("B", 1, rgb[2].fX - 75.0f, rgb[2].fY - 5.0f, paint); + } +} + int main(int argc, char** argv) { SkCommandLineFlags::SetUsage( - "Usage: visualize_color_gamut --input " - "--output \n" - "Description: Writes a visualization of the color gamut to the output image\n"); + "Usage: visualize_color_gamut --input " + "--output " + "--sRGB " + "--adobeRGB " + "--uncorrected \n" + "Description: Writes a visualization of the color gamut to the output image ." + "Also, if a path is provided, writes uncorrected bytes to an unmarked " + "png, for comparison with the input image.\n"); SkCommandLineFlags::Parse(argc, argv); const char* input = FLAGS_input[0]; const char* output = FLAGS_output[0]; @@ -75,61 +131,71 @@ int main(int argc, char** argv) { } // Load a graph of the CIE XYZ color gamut. - SkBitmap bitmap; - if (!GetResourceAsBitmap("gamut.png", &bitmap)) { + SkBitmap gamut; + if (!GetResourceAsBitmap("gamut.png", &gamut)) { SkDebugf("Program failure.\n"); return -1; } - SkCanvas canvas(bitmap); + SkCanvas canvas(gamut); + + // Draw the sRGB gamut if requested. + if (FLAGS_sRGB) { + sk_sp sRGBSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); + draw_gamut(&canvas, sRGBSpace->xyz(), "sRGB", 0xFFFF9394, false); + } + // Draw the Adobe RGB gamut if requested. + if (FLAGS_adobeRGB) { + sk_sp adobeRGBSpace = SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named); + draw_gamut(&canvas, adobeRGBSpace->xyz(), "Adobe RGB", 0xFF31a9e1, false); + } + + // Draw gamut for the input image. sk_sp colorSpace = sk_ref_sp(codec->getColorSpace()); if (!colorSpace) { SkDebugf("Image had no embedded color space information. Defaulting to sRGB.\n"); colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); } + draw_gamut(&canvas, colorSpace->xyz(), input, 0xFF000000, true); - // Calculate the points in the gamut from the XYZ values. - SkMatrix44 xyz = colorSpace->xyz(); - SkPoint rgb[4]; - load_gamut(rgb, xyz); - - // Report the XYZ values. - SkDebugf(" X Y Z\n"); - SkDebugf("Red %.2f %.2f %.2f\n", xyz.get(0, 0), xyz.get(0, 1), xyz.get(0, 2)); - SkDebugf("Green %.2f %.2f %.2f\n", xyz.get(1, 0), xyz.get(1, 1), xyz.get(1, 2)); - SkDebugf("Blue %.2f %.2f %.2f\n", xyz.get(2, 0), xyz.get(2, 1), xyz.get(2, 2)); - - // Report the area of the gamut. - SkDebugf("Area of Gamut: %g\n", calculate_area(rgb)); - - // Now transform the points so they can be drawn on our canvas. We use 1000 pixels - // to represent the space from 0 to 1. Note that the graph is at an offset of (50, 50). - // Also note that y increases as we move down the canvas. - rgb[0].fX = 50 + 1000*rgb[0].fX; - rgb[0].fY = 50 + 1000*(1 - rgb[0].fY); - rgb[1].fX = 50 + 1000*rgb[1].fX; - rgb[1].fY = 50 + 1000*(1 - rgb[1].fY); - rgb[2].fX = 50 + 1000*rgb[2].fX; - rgb[2].fY = 50 + 1000*(1 - rgb[2].fY); - - // Repeat the first point to connect the polygon. - rgb[3] = rgb[0]; - - SkPaint paint; - canvas.drawPoints(SkCanvas::kPolygon_PointMode, 4, rgb, paint); - - // Finally, encode the result to out.png. - SkAutoTUnref out(SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100)); + // Finally, encode the result to the output file. + SkAutoTUnref out(SkImageEncoder::EncodeData(gamut, SkImageEncoder::kPNG_Type, 100)); if (!out) { - SkDebugf("Failed to encode output.\n"); + SkDebugf("Failed to encode gamut output.\n"); return -1; } SkFILEWStream stream(output); bool result = stream.write(out->data(), out->size()); if (!result) { - SkDebugf("Failed to write output.\n"); + SkDebugf("Failed to write gamut output.\n"); return -1; } + // Also, if requested, decode and reencode the uncorrected input image. + if (!FLAGS_uncorrected.isEmpty()) { + SkBitmap bitmap; + int width = codec->getInfo().width(); + int height = codec->getInfo().height(); + SkAlphaType alphaType = codec->getInfo().alphaType(); + bitmap.allocN32Pixels(width, height, kOpaque_SkAlphaType == alphaType); + SkImageInfo decodeInfo = SkImageInfo::MakeN32(width, height, alphaType); + if (SkCodec::kSuccess != codec->getPixels(decodeInfo, bitmap.getPixels(), + bitmap.rowBytes())) { + SkDebugf("Could not decode input image.\n"); + return -1; + } + out.reset(SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100)); + if (!out) { + SkDebugf("Failed to encode uncorrected image.\n"); + return -1; + } + SkFILEWStream bitmapStream(FLAGS_uncorrected[0]); + result = bitmapStream.write(out->data(), out->size()); + if (!result) { + SkDebugf("Failed to write uncorrected image output.\n"); + return -1; + } + } + return 0; } -- cgit v1.2.3