blob: d4bd782bf24fcfca5895c74285c404351d3545a9 [file] [log] [blame]
Matt Sarett84014f02017-01-10 11:28:54 -05001/*
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 "gm.h"
9
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkCodec.h"
Matt Sarett1da27ef2017-01-19 17:14:07 -050013#include "SkColorSpace_Base.h"
Matt Sarett84014f02017-01-10 11:28:54 -050014#include "SkData.h"
15#include "SkImageEncoderPriv.h"
16#include "SkPM4f.h"
17#include "SkSRGB.h"
18
19namespace skiagm {
20
21static const int imageWidth = 128;
22static const int imageHeight = 128;
23
24static inline int div_round_up(int a, int b) {
25 return (a + b - 1) / b;
26}
27
Matt Sarett1da27ef2017-01-19 17:14:07 -050028sk_sp<SkColorSpace> fix_for_colortype(sk_sp<SkColorSpace> colorSpace, SkColorType colorType) {
29 if (kRGBA_F16_SkColorType == colorType) {
30 if (!colorSpace) {
31 return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
32 }
33
34 return as_CSB(colorSpace)->makeLinearGamma();
35 }
36
37 return colorSpace;
38}
39
Matt Sarett84014f02017-01-10 11:28:54 -050040static void make_index8(SkBitmap* bitmap, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
41 const SkColor colors[] = {
42 0x800000FF, 0x8000FF00, 0x80FF0000, 0x80FFFF00,
43 };
44
45 auto toPMColor = [alphaType, colorSpace](SkColor color) {
46 // In the unpremul case, just convert to SkPMColor ordering.
47 if (kUnpremul_SkAlphaType == alphaType) {
48 return SkSwizzle_BGRA_to_PMColor(color);
49 }
50
51 // Linear premultiply.
52 if (colorSpace) {
53 uint32_t result;
54 Sk4f pmFloat = SkColor4f::FromColor(color).premul().to4f_pmorder();
55 SkNx_cast<uint8_t>(sk_linear_to_srgb_needs_trunc(pmFloat)).store(&result);
56 result = (result & 0x00FFFFFF) | (color & 0xFF000000);
57 return result;
58 }
59
60 // Legacy premultiply.
61 return SkPreMultiplyColor(color);
62 };
63
64 // Note that these are not necessarily premultiplied, but they are platform byte ordering.
65 SkPMColor pmColors[SK_ARRAY_COUNT(colors)];
66 for (int i = 0; i < (int) SK_ARRAY_COUNT(colors); i++) {
67 pmColors[i] = toPMColor(colors[i]);
68 }
69
70 sk_sp<SkColorTable> colorTable(new SkColorTable(pmColors, SK_ARRAY_COUNT(pmColors)));
71 SkImageInfo info = SkImageInfo::Make(imageWidth, imageHeight, kIndex_8_SkColorType,
72 alphaType, colorSpace);
73 bitmap->allocPixels(info, nullptr, colorTable.get());
74 for (int y = 0; y < imageHeight; y++) {
75 for (int x = 0; x < imageWidth; x++) {
76 *bitmap->getAddr8(x, y) = (x / div_round_up(imageWidth, 2)) +
77 (y / div_round_up(imageHeight, 3));
78 }
79 }
80}
81
82static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType,
83 sk_sp<SkColorSpace> colorSpace) {
84 if (kIndex_8_SkColorType == colorType) {
85 make_index8(bitmap, alphaType, colorSpace);
86 return;
87 }
88
Matt Sarett1da27ef2017-01-19 17:14:07 -050089 const char* resource = (kOpaque_SkAlphaType == alphaType) ? "color_wheel.jpg"
90 : "color_wheel.png";
91 sk_sp<SkData> data = GetResourceAsData(resource);
Matt Sarett84014f02017-01-10 11:28:54 -050092 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
93 SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
94 .makeAlphaType(alphaType)
Matt Sarett1da27ef2017-01-19 17:14:07 -050095 .makeColorSpace(fix_for_colortype(colorSpace, colorType));
Matt Sarett84014f02017-01-10 11:28:54 -050096 bitmap->allocPixels(dstInfo);
97 codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
98}
99
100static sk_sp<SkData> encode_data(const SkBitmap& bitmap) {
101 SkAutoLockPixels autoLockPixels(bitmap);
102 SkPixmap src;
103 if (!bitmap.peekPixels(&src)) {
104 return nullptr;
105 }
106 SkDynamicMemoryWStream buf;
107 SkEncodeOptions options;
108 if (bitmap.colorSpace()) {
109 options.fPremulBehavior = SkEncodeOptions::PremulBehavior::kGammaCorrect;
110 }
111 SkAssertResult(SkEncodeImageAsPNG(&buf, src, options));
112 return buf.detachAsData();
113}
114
115class EncodeSRGBGM : public GM {
116public:
117 EncodeSRGBGM() {}
118
119protected:
120 SkString onShortName() override {
121 return SkString("encode-srgb");
122 }
123
124 SkISize onISize() override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500125 return SkISize::Make(imageWidth * 2, imageHeight * 9);
Matt Sarett84014f02017-01-10 11:28:54 -0500126 }
127
128 void onDraw(SkCanvas* canvas) override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500129 const SkColorType colorTypes[] = {
130 kN32_SkColorType, kRGBA_F16_SkColorType, kIndex_8_SkColorType,
131 };
132 const SkAlphaType alphaTypes[] = {
133 kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
134 };
Matt Sarett84014f02017-01-10 11:28:54 -0500135 const sk_sp<SkColorSpace> colorSpaces[] = {
136 nullptr, SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named),
137 };
138
139 SkBitmap bitmap;
140 for (SkColorType colorType : colorTypes) {
141 for (SkAlphaType alphaType : alphaTypes) {
142 canvas->save();
143 for (sk_sp<SkColorSpace> colorSpace : colorSpaces) {
144 make(&bitmap, colorType, alphaType, colorSpace);
145 auto image = SkImage::MakeFromEncoded(encode_data(bitmap));
146 canvas->drawImage(image.get(), 0.0f, 0.0f);
147 canvas->translate((float) imageWidth, 0.0f);
148 }
149 canvas->restore();
150 canvas->translate(0.0f, (float) imageHeight);
151 }
152 }
153 }
154
155private:
156 typedef GM INHERITED;
157};
158
159DEF_GM( return new EncodeSRGBGM; )
160}