• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,SkString * errorMsg)45     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
46         sk_sp<SkImage> srcImg = GetResourceAsImage("images/rainbow-gradient.png");
47         if (!srcImg) {
48             *errorMsg = "Could not load images/rainbow-gradient.png. "
49                         "Did you forget to set the resourcePath?";
50             return DrawResult::kFail;
51         }
52         fStorage.reset(srcImg->width() * srcImg->height() *
53                 SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType));
54 
55         SkPixmap src;
56         SkImageInfo info = SkImageInfo::MakeN32Premul(srcImg->width(), srcImg->height(),
57                 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
58         read_into_pixmap(&src, info, fStorage.get(), srcImg);
59 
60         // Encode 8888 premul.
61         auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
62         auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
63         canvas->drawImage(img0, 0.0f, 0.0f);
64         canvas->drawImage(img1, 0.0f, 100.0f);
65 
66         // Encode 8888 unpremul
67         info = info.makeAlphaType(kUnpremul_SkAlphaType);
68         read_into_pixmap(&src, info, fStorage.get(), srcImg);
69         img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
70         img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
71         canvas->drawImage(img0, 100.0f, 0.0f);
72         canvas->drawImage(img1, 100.0f, 100.0f);
73 
74         // Encode F16 premul
75         info = SkImageInfo::Make(srcImg->width(), srcImg->height(), kRGBA_F16_SkColorType,
76                 kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
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, 200.0f, 0.0f);
81         canvas->drawImage(img1, 200.0f, 100.0f);
82 
83         // Encode F16 unpremul
84         info = info.makeAlphaType(kUnpremul_SkAlphaType);
85         read_into_pixmap(&src, info, fStorage.get(), srcImg);
86         img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
87         img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
88         canvas->drawImage(img0, 300.0f, 0.0f);
89         canvas->drawImage(img1, 300.0f, 100.0f);
90 
91         return DrawResult::kOk;
92     }
93 
94 private:
95     SkAutoTMalloc<uint8_t> fStorage;
96 
97     typedef GM INHERITED;
98 };
99 
100 DEF_GM( return new EncodeJpegAlphaOptsGM; )
101 
102 };
103