blob: be3cd62f8b72bd78d3f88e759dcac928bcbdeb61 [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"
Florin Malitaab244f02017-05-03 19:16:58 +000015#include "SkImage.h"
Matt Sarett84014f02017-01-10 11:28:54 -050016#include "SkImageEncoderPriv.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040017#include "SkJpegEncoder.h"
Matt Sarettc367d032017-05-05 11:13:26 -040018#include "SkPngEncoder.h"
Matt Sarett84014f02017-01-10 11:28:54 -050019#include "SkPM4f.h"
20#include "SkSRGB.h"
Matt Sarett04c37312017-05-05 14:02:13 -040021#include "SkWebpEncoder.h"
Matt Sarett84014f02017-01-10 11:28:54 -050022
23namespace skiagm {
24
25static const int imageWidth = 128;
26static const int imageHeight = 128;
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) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -050031 return SkColorSpace::MakeSRGBLinear();
Matt Sarett1da27ef2017-01-19 17:14:07 -050032 }
33
34 return as_CSB(colorSpace)->makeLinearGamma();
35 }
36
37 return colorSpace;
38}
39
Matt Sarett84014f02017-01-10 11:28:54 -050040static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType,
41 sk_sp<SkColorSpace> colorSpace) {
Matt Sarette95941f2017-01-27 18:16:40 -050042 const char* resource;
43 switch (colorType) {
Matt Sarette95941f2017-01-27 18:16:40 -050044 case kGray_8_SkColorType:
45 resource = "grayscale.jpg";
46 alphaType = kOpaque_SkAlphaType;
47 break;
48 case kRGB_565_SkColorType:
49 resource = "color_wheel.jpg";
50 alphaType = kOpaque_SkAlphaType;
51 break;
52 default:
53 resource = (kOpaque_SkAlphaType == alphaType) ? "color_wheel.jpg"
54 : "color_wheel.png";
55 break;
Matt Sarett62bb2802017-01-23 12:28:02 -050056 }
57
Matt Sarett1da27ef2017-01-19 17:14:07 -050058 sk_sp<SkData> data = GetResourceAsData(resource);
Mike Reedede7bac2017-07-23 15:30:02 -040059 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
Matt Sarett84014f02017-01-10 11:28:54 -050060 SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
61 .makeAlphaType(alphaType)
Matt Sarett1da27ef2017-01-19 17:14:07 -050062 .makeColorSpace(fix_for_colortype(colorSpace, colorType));
Matt Sarett84014f02017-01-10 11:28:54 -050063 bitmap->allocPixels(dstInfo);
64 codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
65}
66
Matt Sarett62bb2802017-01-23 12:28:02 -050067static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) {
Matt Sarett84014f02017-01-10 11:28:54 -050068 SkPixmap src;
69 if (!bitmap.peekPixels(&src)) {
70 return nullptr;
71 }
72 SkDynamicMemoryWStream buf;
Matt Sarett62bb2802017-01-23 12:28:02 -050073
Matt Sarettc367d032017-05-05 11:13:26 -040074 SkPngEncoder::Options pngOptions;
Matt Sarett04c37312017-05-05 14:02:13 -040075 SkWebpEncoder::Options webpOptions;
Brian Osman39ef5562017-07-11 11:59:48 -040076 SkTransferFunctionBehavior behavior = bitmap.colorSpace()
77 ? SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
78 pngOptions.fUnpremulBehavior = behavior;
79 webpOptions.fUnpremulBehavior = behavior;
Matt Sarett62bb2802017-01-23 12:28:02 -050080
81 switch (format) {
82 case SkEncodedImageFormat::kPNG:
Matt Sarettc367d032017-05-05 11:13:26 -040083 SkAssertResult(SkPngEncoder::Encode(&buf, src, pngOptions));
Matt Sarett62bb2802017-01-23 12:28:02 -050084 break;
85 case SkEncodedImageFormat::kWEBP:
Matt Sarett04c37312017-05-05 14:02:13 -040086 SkAssertResult(SkWebpEncoder::Encode(&buf, src, webpOptions));
Matt Sarette95941f2017-01-27 18:16:40 -050087 break;
88 case SkEncodedImageFormat::kJPEG:
Matt Sarett26b44df2017-05-02 16:04:56 -040089 SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050090 break;
91 default:
92 break;
93 }
Matt Sarett84014f02017-01-10 11:28:54 -050094 return buf.detachAsData();
95}
96
97class EncodeSRGBGM : public GM {
98public:
Matt Sarett62bb2802017-01-23 12:28:02 -050099 EncodeSRGBGM(SkEncodedImageFormat format)
100 : fEncodedFormat(format)
101 {}
Matt Sarett84014f02017-01-10 11:28:54 -0500102
103protected:
104 SkString onShortName() override {
Matt Sarett62bb2802017-01-23 12:28:02 -0500105 const char* format = nullptr;
106 switch (fEncodedFormat) {
107 case SkEncodedImageFormat::kPNG:
Matt Sarette95941f2017-01-27 18:16:40 -0500108 format = "png";
Matt Sarett62bb2802017-01-23 12:28:02 -0500109 break;
110 case SkEncodedImageFormat::kWEBP:
Matt Sarette95941f2017-01-27 18:16:40 -0500111 format = "webp";
112 break;
113 case SkEncodedImageFormat::kJPEG:
114 format = "jpg";
Matt Sarett62bb2802017-01-23 12:28:02 -0500115 break;
116 default:
117 break;
118 }
Matt Sarette95941f2017-01-27 18:16:40 -0500119 return SkStringPrintf("encode-srgb-%s", format);
Matt Sarett84014f02017-01-10 11:28:54 -0500120 }
121
122 SkISize onISize() override {
Matt Sarette95941f2017-01-27 18:16:40 -0500123 return SkISize::Make(imageWidth * 2, imageHeight * 15);
Matt Sarett84014f02017-01-10 11:28:54 -0500124 }
125
126 void onDraw(SkCanvas* canvas) override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500127 const SkColorType colorTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400128 kN32_SkColorType, kRGBA_F16_SkColorType, kGray_8_SkColorType, kRGB_565_SkColorType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500129 };
130 const SkAlphaType alphaTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400131 kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500132 };
Matt Sarett84014f02017-01-10 11:28:54 -0500133 const sk_sp<SkColorSpace> colorSpaces[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400134 nullptr, SkColorSpace::MakeSRGB(),
Matt Sarett84014f02017-01-10 11:28:54 -0500135 };
136
137 SkBitmap bitmap;
138 for (SkColorType colorType : colorTypes) {
139 for (SkAlphaType alphaType : alphaTypes) {
140 canvas->save();
141 for (sk_sp<SkColorSpace> colorSpace : colorSpaces) {
142 make(&bitmap, colorType, alphaType, colorSpace);
Matt Sarett62bb2802017-01-23 12:28:02 -0500143 auto image = SkImage::MakeFromEncoded(encode_data(bitmap, fEncodedFormat));
Matt Sarett84014f02017-01-10 11:28:54 -0500144 canvas->drawImage(image.get(), 0.0f, 0.0f);
145 canvas->translate((float) imageWidth, 0.0f);
146 }
147 canvas->restore();
148 canvas->translate(0.0f, (float) imageHeight);
149 }
150 }
151 }
152
153private:
Matt Sarett62bb2802017-01-23 12:28:02 -0500154 SkEncodedImageFormat fEncodedFormat;
155
Matt Sarett84014f02017-01-10 11:28:54 -0500156 typedef GM INHERITED;
157};
158
Matt Sarett62bb2802017-01-23 12:28:02 -0500159DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
160DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
Matt Sarette95941f2017-01-27 18:16:40 -0500161DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
Matt Sarett84014f02017-01-10 11:28:54 -0500162}