blob: e610e3cf2d653da14db4f018b7320916b932d609 [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"
Kevin Lubick3b233922023-03-23 09:26:34 -040010#include "include/codec/SkEncodedImageFormat.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkColorSpace.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkData.h"
15#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:
Kevin Lubick21b75382023-04-12 15:01:17 -040083 SK_ABORT("Unsupported format %d", (int)format);
Matt Sarett62bb2802017-01-23 12:28:02 -050084 break;
85 }
Matt Sarett84014f02017-01-10 11:28:54 -050086 return buf.detachAsData();
87}
88
89class EncodeSRGBGM : public GM {
90public:
Matt Sarett62bb2802017-01-23 12:28:02 -050091 EncodeSRGBGM(SkEncodedImageFormat format)
92 : fEncodedFormat(format)
93 {}
Matt Sarett84014f02017-01-10 11:28:54 -050094
95protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000096 SkString getName() const override {
Matt Sarett62bb2802017-01-23 12:28:02 -050097 const char* format = nullptr;
98 switch (fEncodedFormat) {
99 case SkEncodedImageFormat::kPNG:
Matt Sarette95941f2017-01-27 18:16:40 -0500100 format = "png";
Matt Sarett62bb2802017-01-23 12:28:02 -0500101 break;
102 case SkEncodedImageFormat::kWEBP:
Matt Sarette95941f2017-01-27 18:16:40 -0500103 format = "webp";
104 break;
105 case SkEncodedImageFormat::kJPEG:
106 format = "jpg";
Matt Sarett62bb2802017-01-23 12:28:02 -0500107 break;
108 default:
109 break;
110 }
Matt Sarette95941f2017-01-27 18:16:40 -0500111 return SkStringPrintf("encode-srgb-%s", format);
Matt Sarett84014f02017-01-10 11:28:54 -0500112 }
113
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000114 SkISize getISize() override { return SkISize::Make(imageWidth * 2, imageHeight * 15); }
Matt Sarett84014f02017-01-10 11:28:54 -0500115
116 void onDraw(SkCanvas* canvas) override {
Matt Sarett1da27ef2017-01-19 17:14:07 -0500117 const SkColorType colorTypes[] = {
Kevin Lubick21b75382023-04-12 15:01:17 -0400118 kN32_SkColorType, kRGBA_F16_SkColorType,
119#if !defined(SK_ENABLE_NDK_IMAGES)
120 // These fail with the NDK encoders because there is a mismatch between
121 // Gray_8 and Alpha_8
122 kGray_8_SkColorType,
123#endif
124 kRGB_565_SkColorType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500125 };
126 const SkAlphaType alphaTypes[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400127 kUnpremul_SkAlphaType, kPremul_SkAlphaType, kOpaque_SkAlphaType,
Matt Sarett1da27ef2017-01-19 17:14:07 -0500128 };
Matt Sarett84014f02017-01-10 11:28:54 -0500129 const sk_sp<SkColorSpace> colorSpaces[] = {
Mike Reed304a07c2017-07-12 15:10:28 -0400130 nullptr, SkColorSpace::MakeSRGB(),
Matt Sarett84014f02017-01-10 11:28:54 -0500131 };
132
133 SkBitmap bitmap;
134 for (SkColorType colorType : colorTypes) {
135 for (SkAlphaType alphaType : alphaTypes) {
136 canvas->save();
John Stilesbd3ffa42020-07-30 20:24:57 -0400137 for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) {
Matt Sarett84014f02017-01-10 11:28:54 -0500138 make(&bitmap, colorType, alphaType, colorSpace);
Kevin Lubick21b75382023-04-12 15:01:17 -0400139 auto data = encode_data(bitmap, fEncodedFormat);
140 auto image = SkImages::DeferredFromEncodedData(data);
Matt Sarett84014f02017-01-10 11:28:54 -0500141 canvas->drawImage(image.get(), 0.0f, 0.0f);
142 canvas->translate((float) imageWidth, 0.0f);
143 }
144 canvas->restore();
145 canvas->translate(0.0f, (float) imageHeight);
146 }
147 }
148 }
149
150private:
Matt Sarett62bb2802017-01-23 12:28:02 -0500151 SkEncodedImageFormat fEncodedFormat;
152
John Stiles7571f9e2020-09-02 22:42:33 -0400153 using INHERITED = GM;
Matt Sarett84014f02017-01-10 11:28:54 -0500154};
155
Matt Sarett62bb2802017-01-23 12:28:02 -0500156DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
157DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
Matt Sarette95941f2017-01-27 18:16:40 -0500158DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
John Stilesa6841be2020-08-06 14:11:56 -0400159} // namespace skiagm