blob: 23f36e40dbd9934f9bc643fe56ee55655013b999 [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 Sarett04c37312017-05-05 14:02:13 -040018#include "SkWebpEncoder.h"
Matt Sarett84014f02017-01-10 11:28:54 -050019
20namespace skiagm {
21
22static const int imageWidth = 128;
23static const int imageHeight = 128;
24
Matt Sarett84014f02017-01-10 11:28:54 -050025static void make(SkBitmap* bitmap, SkColorType colorType, SkAlphaType alphaType,
26 sk_sp<SkColorSpace> colorSpace) {
Matt Sarette95941f2017-01-27 18:16:40 -050027 const char* resource;
28 switch (colorType) {
Matt Sarette95941f2017-01-27 18:16:40 -050029 case kGray_8_SkColorType:
Hal Canaryc465d132017-12-08 10:21:31 -050030 resource = "images/grayscale.jpg";
Matt Sarette95941f2017-01-27 18:16:40 -050031 alphaType = kOpaque_SkAlphaType;
32 break;
33 case kRGB_565_SkColorType:
Hal Canaryc465d132017-12-08 10:21:31 -050034 resource = "images/color_wheel.jpg";
Matt Sarette95941f2017-01-27 18:16:40 -050035 alphaType = kOpaque_SkAlphaType;
36 break;
37 default:
Hal Canaryc465d132017-12-08 10:21:31 -050038 resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg"
39 : "images/color_wheel.png";
Matt Sarette95941f2017-01-27 18:16:40 -050040 break;
Matt Sarett62bb2802017-01-23 12:28:02 -050041 }
42
Matt Sarett1da27ef2017-01-19 17:14:07 -050043 sk_sp<SkData> data = GetResourceAsData(resource);
Hal Canarybaa2a282018-11-26 15:34:12 -050044 if (!data) {
45 return;
46 }
Mike Reedede7bac2017-07-23 15:30:02 -040047 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
Matt Sarett84014f02017-01-10 11:28:54 -050048 SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
49 .makeAlphaType(alphaType)
Brian Osman6b622962018-08-27 19:16:02 +000050 .makeColorSpace(colorSpace);
Matt Sarett84014f02017-01-10 11:28:54 -050051 bitmap->allocPixels(dstInfo);
52 codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
53}
54
Matt Sarett62bb2802017-01-23 12:28:02 -050055static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat format) {
Matt Sarett84014f02017-01-10 11:28:54 -050056 SkPixmap src;
57 if (!bitmap.peekPixels(&src)) {
58 return nullptr;
59 }
60 SkDynamicMemoryWStream buf;
Matt Sarett62bb2802017-01-23 12:28:02 -050061
Matt Sarett62bb2802017-01-23 12:28:02 -050062 switch (format) {
63 case SkEncodedImageFormat::kPNG:
Brian Osmanb62f50c2018-07-12 14:44:27 -040064 SkAssertResult(SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050065 break;
66 case SkEncodedImageFormat::kWEBP:
Brian Osmanb62f50c2018-07-12 14:44:27 -040067 SkAssertResult(SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()));
Matt Sarette95941f2017-01-27 18:16:40 -050068 break;
69 case SkEncodedImageFormat::kJPEG:
Matt Sarett26b44df2017-05-02 16:04:56 -040070 SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));
Matt Sarett62bb2802017-01-23 12:28:02 -050071 break;
72 default:
73 break;
74 }
Matt Sarett84014f02017-01-10 11:28:54 -050075 return buf.detachAsData();
76}
77
78class EncodeSRGBGM : public GM {
79public:
Matt Sarett62bb2802017-01-23 12:28:02 -050080 EncodeSRGBGM(SkEncodedImageFormat format)
81 : fEncodedFormat(format)
82 {}
Matt Sarett84014f02017-01-10 11:28:54 -050083
84protected:
85 SkString onShortName() override {
Matt Sarett62bb2802017-01-23 12:28:02 -050086 const char* format = nullptr;
87 switch (fEncodedFormat) {
88 case SkEncodedImageFormat::kPNG:
Matt Sarette95941f2017-01-27 18:16:40 -050089 format = "png";
Matt Sarett62bb2802017-01-23 12:28:02 -050090 break;
91 case SkEncodedImageFormat::kWEBP:
Matt Sarette95941f2017-01-27 18:16:40 -050092 format = "webp";
93 break;
94 case SkEncodedImageFormat::kJPEG:
95 format = "jpg";
Matt Sarett62bb2802017-01-23 12:28:02 -050096 break;
97 default:
98 break;
99 }
Matt Sarette95941f2017-01-27 18:16:40 -0500100 return SkStringPrintf("encode-srgb-%s", format);
Matt Sarett84014f02017-01-10 11:28:54 -0500101 }
102
103 SkISize onISize() override {
Matt Sarette95941f2017-01-27 18:16:40 -0500104 return SkISize::Make(imageWidth * 2, imageHeight * 15);
Matt Sarett84014f02017-01-10 11:28:54 -0500105 }
106
107 void onDraw(SkCanvas* canvas) override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500108 const SkColorType colorTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400109 kN32_SkColorType, kRGBA_F16_SkColorType, kGray_8_SkColorType, kRGB_565_SkColorType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500110 };
111 const SkAlphaType alphaTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400112 kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500113 };
Matt Sarett84014f02017-01-10 11:28:54 -0500114 const sk_sp<SkColorSpace> colorSpaces[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400115 nullptr, SkColorSpace::MakeSRGB(),
Matt Sarett84014f02017-01-10 11:28:54 -0500116 };
117
118 SkBitmap bitmap;
119 for (SkColorType colorType : colorTypes) {
120 for (SkAlphaType alphaType : alphaTypes) {
121 canvas->save();
122 for (sk_sp<SkColorSpace> colorSpace : colorSpaces) {
123 make(&bitmap, colorType, alphaType, colorSpace);
Matt Sarett62bb2802017-01-23 12:28:02 -0500124 auto image = SkImage::MakeFromEncoded(encode_data(bitmap, fEncodedFormat));
Matt Sarett84014f02017-01-10 11:28:54 -0500125 canvas->drawImage(image.get(), 0.0f, 0.0f);
126 canvas->translate((float) imageWidth, 0.0f);
127 }
128 canvas->restore();
129 canvas->translate(0.0f, (float) imageHeight);
130 }
131 }
132 }
133
134private:
Matt Sarett62bb2802017-01-23 12:28:02 -0500135 SkEncodedImageFormat fEncodedFormat;
136
Matt Sarett84014f02017-01-10 11:28:54 -0500137 typedef GM INHERITED;
138};
139
Matt Sarett62bb2802017-01-23 12:28:02 -0500140DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
141DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
Matt Sarette95941f2017-01-27 18:16:40 -0500142DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
Matt Sarett84014f02017-01-10 11:28:54 -0500143}