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