blob: 0db8dc1079ccbba91bb1a6a5a1ae35610ce7d757 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/codec/SkCodec.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColorSpace.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkData.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkEncodedImageFormat.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkImageInfo.h"
17#include "include/core/SkPixmap.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkStream.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/encode/SkJpegEncoder.h"
24#include "include/encode/SkPngEncoder.h"
25#include "include/encode/SkWebpEncoder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "tools/Resources.h"
Matt Sarett84014f02017-01-10 11:28:54 -050027
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include <memory>
29
Matt Sarett84014f02017-01-10 11:28:54 -050030namespace skiagm {
31
32static const int imageWidth = 128;
33static const int imageHeight = 128;
34
Matt Sarett84014f02017-01-10 11:28:54 -050035static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType,
36 sk_sp<SkColorSpace> colorSpace) {
Matt Sarette95941f2017-01-27 18:16:40 -050037 const char* resource;
38 switch (colorType) {
Matt Sarette95941f2017-01-27 18:16:40 -050039 case kGray_8_SkColorType:
Hal Canaryc465d132017-12-08 10:21:31 -050040 resource = "images/grayscale.jpg";
Matt Sarette95941f2017-01-27 18:16:40 -050041 alphaType = kOpaque_SkAlphaType;
42 break;
43 case kRGB_565_SkColorType:
Hal Canaryc465d132017-12-08 10:21:31 -050044 resource = "images/color_wheel.jpg";
Matt Sarette95941f2017-01-27 18:16:40 -050045 alphaType = kOpaque_SkAlphaType;
46 break;
47 default:
Hal Canaryc465d132017-12-08 10:21:31 -050048 resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg"
49 : "images/color_wheel.png";
Matt Sarette95941f2017-01-27 18:16:40 -050050 break;
Matt Sarett62bb2802017-01-23 12:28:02 -050051 }
52
Matt Sarett1da27ef2017-01-19 17:14:07 -050053 sk_sp<SkData> data = GetResourceAsData(resource);
Hal Canarybaa2a282018-11-26 15:34:12 -050054 if (!data) {
55 return;
56 }
Mike Reedede7bac2017-07-23 15:30:02 -040057 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
Matt Sarett84014f02017-01-10 11:28:54 -050058 SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
59 .makeAlphaType(alphaType)
Brian Osman6b622962018-08-27 19:16:02 +000060 .makeColorSpace(colorSpace);
Matt Sarett84014f02017-01-10 11:28:54 -050061 bitmap->allocPixels(dstInfo);
62 codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
63}
64
Matt Sarett62bb2802017-01-23 12:28:02 -050065static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) {
Matt Sarett84014f02017-01-10 11:28:54 -050066 SkPixmap src;
67 if (!bitmap.peekPixels(&src)) {
68 return nullptr;
69 }
70 SkDynamicMemoryWStream buf;
Matt Sarett62bb2802017-01-23 12:28:02 -050071
Matt Sarett62bb2802017-01-23 12:28:02 -050072 switch (format) {
73 case SkEncodedImageFormat::kPNG:
Brian Osmanb62f50c2018-07-12 14:44:27 -040074 SkAssertResult(SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050075 break;
76 case SkEncodedImageFormat::kWEBP:
Brian Osmanb62f50c2018-07-12 14:44:27 -040077 SkAssertResult(SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()));
Matt Sarette95941f2017-01-27 18:16:40 -050078 break;
79 case SkEncodedImageFormat::kJPEG:
Matt Sarett26b44df2017-05-02 16:04:56 -040080 SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050081 break;
82 default:
83 break;
84 }
Matt Sarett84014f02017-01-10 11:28:54 -050085 return buf.detachAsData();
86}
87
88class EncodeSRGBGM : public GM {
89public:
Matt Sarett62bb2802017-01-23 12:28:02 -050090 EncodeSRGBGM(SkEncodedImageFormat format)
91 : fEncodedFormat(format)
92 {}
Matt Sarett84014f02017-01-10 11:28:54 -050093
94protected:
95 SkString onShortName() override {
Matt Sarett62bb2802017-01-23 12:28:02 -050096 const char* format = nullptr;
97 switch (fEncodedFormat) {
98 case SkEncodedImageFormat::kPNG:
Matt Sarette95941f2017-01-27 18:16:40 -050099 format = "png";
Matt Sarett62bb2802017-01-23 12:28:02 -0500100 break;
101 case SkEncodedImageFormat::kWEBP:
Matt Sarette95941f2017-01-27 18:16:40 -0500102 format = "webp";
103 break;
104 case SkEncodedImageFormat::kJPEG:
105 format = "jpg";
Matt Sarett62bb2802017-01-23 12:28:02 -0500106 break;
107 default:
108 break;
109 }
Matt Sarette95941f2017-01-27 18:16:40 -0500110 return SkStringPrintf("encode-srgb-%s", format);
Matt Sarett84014f02017-01-10 11:28:54 -0500111 }
112
113 SkISize onISize() override {
Matt Sarette95941f2017-01-27 18:16:40 -0500114 return SkISize::Make(imageWidth * 2, imageHeight * 15);
Matt Sarett84014f02017-01-10 11:28:54 -0500115 }
116
117 void onDraw(SkCanvas* canvas) override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500118 const SkColorType colorTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400119 kN32_SkColorType, kRGBA_F16_SkColorType, kGray_8_SkColorType, kRGB_565_SkColorType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500120 };
121 const SkAlphaType alphaTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400122 kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500123 };
Matt Sarett84014f02017-01-10 11:28:54 -0500124 const sk_sp<SkColorSpace> colorSpaces[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400125 nullptr, SkColorSpace::MakeSRGB(),
Matt Sarett84014f02017-01-10 11:28:54 -0500126 };
127
128 SkBitmap bitmap;
129 for (SkColorType colorType : colorTypes) {
130 for (SkAlphaType alphaType : alphaTypes) {
131 canvas->save();
132 for (sk_sp<SkColorSpace> colorSpace : colorSpaces) {
133 make(&bitmap, colorType, alphaType, colorSpace);
Matt Sarett62bb2802017-01-23 12:28:02 -0500134 auto image = SkImage::MakeFromEncoded(encode_data(bitmap, fEncodedFormat));
Matt Sarett84014f02017-01-10 11:28:54 -0500135 canvas->drawImage(image.get(), 0.0f, 0.0f);
136 canvas->translate((float) imageWidth, 0.0f);
137 }
138 canvas->restore();
139 canvas->translate(0.0f, (float) imageHeight);
140 }
141 }
142 }
143
144private:
Matt Sarett62bb2802017-01-23 12:28:02 -0500145 SkEncodedImageFormat fEncodedFormat;
146
Matt Sarett84014f02017-01-10 11:28:54 -0500147 typedef GM INHERITED;
148};
149
Matt Sarett62bb2802017-01-23 12:28:02 -0500150DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
151DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
Matt Sarette95941f2017-01-27 18:16:40 -0500152DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
Matt Sarett84014f02017-01-10 11:28:54 -0500153}