skia2/gm/makecolorspace.cpp

88 lines
3.4 KiB
C++
Raw Normal View History

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "Resources.h"
#include "SkCodec.h"
#include "SkImage.h"
#include "SkImagePriv.h"
sk_sp<SkImage> make_raster_image(const char* path, SkTransferFunctionBehavior behavior) {
Revert "Revert "resources: remove most uses of GetResourcePath()"" This reverts commit cca230055921d2df8708ed6f9abcc2d43468dc7f. Reason for revert: think I guessed wrong about g32 -- unreverting Original change's description: > Revert "resources: remove most uses of GetResourcePath()" > > This reverts commit 5093a539def3ae09df324018f2343827009b2e05. > > Reason for revert: google3 seems broken > > Original change's description: > > resources: remove most uses of GetResourcePath() > > > > Going forward, we will standardize on GetResourceAsData(), which will > > make it easier to run tests in environments without access to the > > filesystem. > > > > Also: GetResourceAsData() complains when a resource is missing. > > This is usually an error. > > > > Change-Id: Iaf70b71b0ca5ed8cd1a5538a60ef185ae8736188 > > Reviewed-on: https://skia-review.googlesource.com/82642 > > Reviewed-by: Hal Canary <halcanary@google.com> > > Commit-Queue: Hal Canary <halcanary@google.com> > > TBR=halcanary@google.com,scroggo@google.com > > Change-Id: Ic5a7c0167c995a672e6b06dc92abe00564432214 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Reviewed-on: https://skia-review.googlesource.com/83001 > Reviewed-by: Mike Reed <reed@google.com> > Commit-Queue: Mike Reed <reed@google.com> TBR=halcanary@google.com,scroggo@google.com,reed@google.com Change-Id: I5a46e4de61186a8a5eb9cacd3275e24e311d5a07 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://skia-review.googlesource.com/82942 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Mike Reed <reed@google.com>
2017-12-09 01:27:41 +00:00
sk_sp<SkData> resourceData = GetResourceAsData(path);
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
SkBitmap bitmap;
bitmap.allocPixels(codec->getInfo());
SkCodec::Options opts;
opts.fPremulBehavior = behavior;
codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes(), &opts);
return SkImage::MakeFromBitmap(bitmap);
}
sk_sp<SkImage> make_color_space(sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace,
SkTransferFunctionBehavior behavior) {
sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace, behavior);
// Assign an sRGB color space on the xformed image, so we can see the effects of the xform
// when we draw.
sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
if (colorSpace->gammaIsLinear()) {
srgb = SkColorSpace::MakeSRGBLinear();
}
return SkImageMakeRasterCopyAndAssignColorSpace(xform.get(), srgb.get());
}
class MakeCSGM : public skiagm::GM {
public:
MakeCSGM() {}
protected:
SkString onShortName() override {
return SkString("makecolorspace");
}
SkISize onISize() override {
return SkISize::Make(128*3, 128*4);
}
void onDraw(SkCanvas* canvas) override {
SkTransferFunctionBehavior behavior = canvas->imageInfo().colorSpace() ?
SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
sk_sp<SkColorSpace> wideGamut = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
SkColorSpace::kAdobeRGB_Gamut);
sk_sp<SkColorSpace> wideGamutLinear = wideGamut->makeLinearGamma();
// Lazy images
sk_sp<SkImage> opaqueImage = GetResourceAsImage("images/mandrill_128.png");
sk_sp<SkImage> premulImage = GetResourceAsImage("images/color_wheel.png");
canvas->drawImage(opaqueImage, 0.0f, 0.0f);
canvas->drawImage(make_color_space(opaqueImage, wideGamut, behavior), 128.0f, 0.0f);
canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, behavior), 256.0f, 0.0f);
canvas->drawImage(premulImage, 0.0f, 128.0f);
canvas->drawImage(make_color_space(premulImage, wideGamut, behavior), 128.0f, 128.0f);
canvas->drawImage(make_color_space(premulImage, wideGamutLinear, behavior), 256.0f, 128.0f);
canvas->translate(0.0f, 256.0f);
// Raster images
opaqueImage = make_raster_image("images/mandrill_128.png", behavior);
premulImage = make_raster_image("images/color_wheel.png", behavior);
canvas->drawImage(opaqueImage, 0.0f, 0.0f);
canvas->drawImage(make_color_space(opaqueImage, wideGamut, behavior), 128.0f, 0.0f);
canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, behavior), 256.0f, 0.0f);
canvas->drawImage(premulImage, 0.0f, 128.0f);
canvas->drawImage(make_color_space(premulImage, wideGamut, behavior), 128.0f, 128.0f);
canvas->drawImage(make_color_space(premulImage, wideGamutLinear, behavior), 256.0f, 128.0f);
}
private:
typedef skiagm::GM INHERITED;
};
DEF_GM(return new MakeCSGM;)