2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkColorSpace_Base.h"
13 #include "SkImagePriv.h"
15 sk_sp<SkImage> make_raster_image(const char* path, SkTransferFunctionBehavior behavior) {
16 SkString resourcePath = GetResourcePath(path);
17 sk_sp<SkData> resourceData = SkData::MakeFromFileName(resourcePath.c_str());
18 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(resourceData));
21 bitmap.allocPixels(codec->getInfo());
23 SkCodec::Options opts;
24 opts.fPremulBehavior = behavior;
25 codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes(), &opts,
27 return SkImage::MakeFromBitmap(bitmap);
30 sk_sp<SkImage> make_color_space(sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace,
31 SkTransferFunctionBehavior behavior) {
32 sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace, behavior);
34 // Assign an sRGB color space on the xformed image, so we can see the effects of the xform
36 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
37 if (colorSpace->gammaIsLinear()) {
38 srgb = SkColorSpace::MakeSRGBLinear();
40 return SkImageMakeRasterCopyAndAssignColorSpace(xform.get(), srgb.get());
43 class MakeCSGM : public skiagm::GM {
48 SkString onShortName() override {
49 return SkString("makecolorspace");
52 SkISize onISize() override {
53 return SkISize::Make(128*3, 128*4);
56 void onDraw(SkCanvas* canvas) override {
57 SkTransferFunctionBehavior behavior = canvas->imageInfo().colorSpace() ?
58 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
60 sk_sp<SkColorSpace> wideGamut = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
61 SkColorSpace::kAdobeRGB_Gamut);
62 sk_sp<SkColorSpace> wideGamutLinear = as_CSB(wideGamut)->makeLinearGamma();
65 sk_sp<SkImage> opaqueImage = GetResourceAsImage("mandrill_128.png");
66 sk_sp<SkImage> premulImage = GetResourceAsImage("color_wheel.png");
67 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
68 canvas->drawImage(make_color_space(opaqueImage, wideGamut, behavior), 128.0f, 0.0f);
69 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, behavior), 256.0f, 0.0f);
70 canvas->drawImage(premulImage, 0.0f, 128.0f);
71 canvas->drawImage(make_color_space(premulImage, wideGamut, behavior), 128.0f, 128.0f);
72 canvas->drawImage(make_color_space(premulImage, wideGamutLinear, behavior), 256.0f, 128.0f);
73 canvas->translate(0.0f, 256.0f);
76 opaqueImage = make_raster_image("mandrill_128.png", behavior);
77 premulImage = make_raster_image("color_wheel.png", behavior);
78 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
79 canvas->drawImage(make_color_space(opaqueImage, wideGamut, behavior), 128.0f, 0.0f);
80 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, behavior), 256.0f, 0.0f);
81 canvas->drawImage(premulImage, 0.0f, 128.0f);
82 canvas->drawImage(make_color_space(premulImage, wideGamut, behavior), 128.0f, 128.0f);
83 canvas->drawImage(make_color_space(premulImage, wideGamutLinear, behavior), 256.0f, 128.0f);
87 typedef skiagm::GM INHERITED;
90 DEF_GM(return new MakeCSGM;)