1 /*
2 * Copyright 2017 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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkStream.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "include/encode/SkJpegEncoder.h"
21 #include "include/private/SkTemplates.h"
22 #include "tools/Resources.h"
23
24 namespace skiagm {
25
read_into_pixmap(SkPixmap * dst,SkImageInfo dstInfo,void * dstPixels,sk_sp<SkImage> src)26 static inline void read_into_pixmap(SkPixmap* dst, SkImageInfo dstInfo, void* dstPixels,
27 sk_sp<SkImage> src) {
28 dst->reset(dstInfo, dstPixels, dstInfo.minRowBytes());
29 src->readPixels(nullptr, *dst, 0, 0, SkImage::CachingHint::kDisallow_CachingHint);
30 }
31
encode_pixmap_and_make_image(const SkPixmap & src,SkJpegEncoder::AlphaOption alphaOption)32 static inline sk_sp<SkImage> encode_pixmap_and_make_image(const SkPixmap& src,
33 SkJpegEncoder::AlphaOption alphaOption) {
34 SkDynamicMemoryWStream dst;
35 SkJpegEncoder::Options options;
36 options.fAlphaOption = alphaOption;
37 SkJpegEncoder::Encode(&dst, src, options);
38 return SkImage::MakeFromEncoded(dst.detachAsData());
39 }
40
41 class EncodeJpegAlphaOptsGM : public GM {
42 public:
EncodeJpegAlphaOptsGM()43 EncodeJpegAlphaOptsGM() {}
44
45 protected:
onShortName()46 SkString onShortName() override {
47 return SkString("encode-alpha-jpeg");
48 }
49
onISize()50 SkISize onISize() override {
51 return SkISize::Make(400, 200);
52 }
53
onDraw(SkCanvas * canvas,SkString * errorMsg)54 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
55 sk_sp<SkImage> srcImg = GetResourceAsImage("images/rainbow-gradient.png");
56 if (!srcImg) {
57 *errorMsg = "Could not load images/rainbow-gradient.png. "
58 "Did you forget to set the resourcePath?";
59 return DrawResult::kFail;
60 }
61 fStorage.reset(srcImg->width() * srcImg->height() *
62 SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType));
63
64 SkPixmap src;
65 SkImageInfo info = SkImageInfo::MakeN32Premul(srcImg->width(), srcImg->height(),
66 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
67 read_into_pixmap(&src, info, fStorage.get(), srcImg);
68
69 // Encode 8888 premul.
70 auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
71 auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
72 canvas->drawImage(img0, 0.0f, 0.0f);
73 canvas->drawImage(img1, 0.0f, 100.0f);
74
75 // Encode 8888 unpremul
76 info = info.makeAlphaType(kUnpremul_SkAlphaType);
77 read_into_pixmap(&src, info, fStorage.get(), srcImg);
78 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
79 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
80 canvas->drawImage(img0, 100.0f, 0.0f);
81 canvas->drawImage(img1, 100.0f, 100.0f);
82
83 // Encode F16 premul
84 info = SkImageInfo::Make(srcImg->width(), srcImg->height(), kRGBA_F16_SkColorType,
85 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
86 read_into_pixmap(&src, info, fStorage.get(), srcImg);
87 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
88 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
89 canvas->drawImage(img0, 200.0f, 0.0f);
90 canvas->drawImage(img1, 200.0f, 100.0f);
91
92 // Encode F16 unpremul
93 info = info.makeAlphaType(kUnpremul_SkAlphaType);
94 read_into_pixmap(&src, info, fStorage.get(), srcImg);
95 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
96 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
97 canvas->drawImage(img0, 300.0f, 0.0f);
98 canvas->drawImage(img1, 300.0f, 100.0f);
99
100 return DrawResult::kOk;
101 }
102
103 private:
104 SkAutoTMalloc<uint8_t> fStorage;
105
106 using INHERITED = GM;
107 };
108
109 DEF_GM( return new EncodeJpegAlphaOptsGM; )
110
111 } // namespace skiagm
112