blob: bef029fbedf0e483053bc46dfbb94eb080fd8af5 [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"
13#include "SkData.h"
Florin Malitaab244f02017-05-03 19:16:58 +000014#include "SkImage.h"
Matt Sarett84014f02017-01-10 11:28:54 -050015#include "SkImageEncoderPriv.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040016#include "SkJpegEncoder.h"
Matt Sarettc367d032017-05-05 11:13:26 -040017#include "SkPngEncoder.h"
Matt Sarett84014f02017-01-10 11:28:54 -050018#include "SkPM4f.h"
Matt Sarett04c37312017-05-05 14:02:13 -040019#include "SkWebpEncoder.h"
Matt Sarett84014f02017-01-10 11:28:54 -050020
21namespace skiagm {
22
23static const int imageWidth = 128;
24static const int imageHeight = 128;
25
Matt Sarett84014f02017-01-10 11:28:54 -050026static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType,
27 sk_sp<SkColorSpace> colorSpace) {
Matt Sarette95941f2017-01-27 18:16:40 -050028 const char* resource;
29 switch (colorType) {
Matt Sarette95941f2017-01-27 18:16:40 -050030 case kGray_8_SkColorType:
Hal Canaryc465d132017-12-08 10:21:31 -050031 resource = "images/grayscale.jpg";
Matt Sarette95941f2017-01-27 18:16:40 -050032 alphaType = kOpaque_SkAlphaType;
33 break;
34 case kRGB_565_SkColorType:
Hal Canaryc465d132017-12-08 10:21:31 -050035 resource = "images/color_wheel.jpg";
Matt Sarette95941f2017-01-27 18:16:40 -050036 alphaType = kOpaque_SkAlphaType;
37 break;
38 default:
Hal Canaryc465d132017-12-08 10:21:31 -050039 resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg"
40 : "images/color_wheel.png";
Matt Sarette95941f2017-01-27 18:16:40 -050041 break;
Matt Sarett62bb2802017-01-23 12:28:02 -050042 }
43
Matt Sarett1da27ef2017-01-19 17:14:07 -050044 sk_sp<SkData> data = GetResourceAsData(resource);
Mike Reedede7bac2017-07-23 15:30:02 -040045 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
Brian Osman6b622962018-08-27 19:16:02 +000046 if (kRGBA_F16_SkColorType == colorType && !colorSpace) {
47 colorSpace = SkColorSpace::MakeSRGB();
48 }
Matt Sarett84014f02017-01-10 11:28:54 -050049 SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
50 .makeAlphaType(alphaType)
Brian Osman6b622962018-08-27 19:16:02 +000051 .makeColorSpace(colorSpace);
Matt Sarett84014f02017-01-10 11:28:54 -050052 bitmap->allocPixels(dstInfo);
53 codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
54}
55
Matt Sarett62bb2802017-01-23 12:28:02 -050056static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) {
Matt Sarett84014f02017-01-10 11:28:54 -050057 SkPixmap src;
58 if (!bitmap.peekPixels(&src)) {
59 return nullptr;
60 }
61 SkDynamicMemoryWStream buf;
Matt Sarett62bb2802017-01-23 12:28:02 -050062
Matt Sarett62bb2802017-01-23 12:28:02 -050063 switch (format) {
64 case SkEncodedImageFormat::kPNG:
Brian Osmanb62f50c2018-07-12 14:44:27 -040065 SkAssertResult(SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050066 break;
67 case SkEncodedImageFormat::kWEBP:
Brian Osmanb62f50c2018-07-12 14:44:27 -040068 SkAssertResult(SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()));
Matt Sarette95941f2017-01-27 18:16:40 -050069 break;
70 case SkEncodedImageFormat::kJPEG:
Matt Sarett26b44df2017-05-02 16:04:56 -040071 SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050072 break;
73 default:
74 break;
75 }
Matt Sarett84014f02017-01-10 11:28:54 -050076 return buf.detachAsData();
77}
78
79class EncodeSRGBGM : public GM {
80public:
Matt Sarett62bb2802017-01-23 12:28:02 -050081 EncodeSRGBGM(SkEncodedImageFormat format)
82 : fEncodedFormat(format)
83 {}
Matt Sarett84014f02017-01-10 11:28:54 -050084
85protected:
86 SkString onShortName() override {
Matt Sarett62bb2802017-01-23 12:28:02 -050087 const char* format = nullptr;
88 switch (fEncodedFormat) {
89 case SkEncodedImageFormat::kPNG:
Matt Sarette95941f2017-01-27 18:16:40 -050090 format = "png";
Matt Sarett62bb2802017-01-23 12:28:02 -050091 break;
92 case SkEncodedImageFormat::kWEBP:
Matt Sarette95941f2017-01-27 18:16:40 -050093 format = "webp";
94 break;
95 case SkEncodedImageFormat::kJPEG:
96 format = "jpg";
Matt Sarett62bb2802017-01-23 12:28:02 -050097 break;
98 default:
99 break;
100 }
Matt Sarette95941f2017-01-27 18:16:40 -0500101 return SkStringPrintf("encode-srgb-%s", format);
Matt Sarett84014f02017-01-10 11:28:54 -0500102 }
103
104 SkISize onISize() override {
Matt Sarette95941f2017-01-27 18:16:40 -0500105 return SkISize::Make(imageWidth * 2, imageHeight * 15);
Matt Sarett84014f02017-01-10 11:28:54 -0500106 }
107
108 void onDraw(SkCanvas* canvas) override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500109 const SkColorType colorTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400110 kN32_SkColorType, kRGBA_F16_SkColorType, kGray_8_SkColorType, kRGB_565_SkColorType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500111 };
112 const SkAlphaType alphaTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400113 kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500114 };
Matt Sarett84014f02017-01-10 11:28:54 -0500115 const sk_sp<SkColorSpace> colorSpaces[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400116 nullptr, SkColorSpace::MakeSRGB(),
Matt Sarett84014f02017-01-10 11:28:54 -0500117 };
118
119 SkBitmap bitmap;
120 for (SkColorType colorType : colorTypes) {
121 for (SkAlphaType alphaType : alphaTypes) {
122 canvas->save();
123 for (sk_sp<SkColorSpace> colorSpace : colorSpaces) {
124 make(&bitmap, colorType, alphaType, colorSpace);
Matt Sarett62bb2802017-01-23 12:28:02 -0500125 auto image = SkImage::MakeFromEncoded(encode_data(bitmap, fEncodedFormat));
Matt Sarett84014f02017-01-10 11:28:54 -0500126 canvas->drawImage(image.get(), 0.0f, 0.0f);
127 canvas->translate((float) imageWidth, 0.0f);
128 }
129 canvas->restore();
130 canvas->translate(0.0f, (float) imageHeight);
131 }
132 }
133 }
134
135private:
Matt Sarett62bb2802017-01-23 12:28:02 -0500136 SkEncodedImageFormat fEncodedFormat;
137
Matt Sarett84014f02017-01-10 11:28:54 -0500138 typedef GM INHERITED;
139};
140
Matt Sarett62bb2802017-01-23 12:28:02 -0500141DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
142DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
Matt Sarette95941f2017-01-27 18:16:40 -0500143DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
Matt Sarett84014f02017-01-10 11:28:54 -0500144}