blob: 9751fbbc163bd5080b7461b7d3c2925117486777 [file] [log] [blame]
Matt Sarett2e61b182017-05-09 12:46:50 -04001/*
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"
Mike Reed7fcfb622018-02-09 13:26:46 -050010#include "SkImageInfoPriv.h"
Matt Sarett2e61b182017-05-09 12:46:50 -040011#include "SkJpegEncoder.h"
12
13#include "Resources.h"
14
15namespace skiagm {
16
17static 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
23static inline sk_sp<SkImage> encode_pixmap_and_make_image(const SkPixmap& src,
Brian Osmanb62f50c2018-07-12 14:44:27 -040024 SkJpegEncoder::AlphaOption alphaOption) {
Matt Sarett2e61b182017-05-09 12:46:50 -040025 SkDynamicMemoryWStream dst;
26 SkJpegEncoder::Options options;
27 options.fAlphaOption = alphaOption;
Matt Sarett2e61b182017-05-09 12:46:50 -040028 SkJpegEncoder::Encode(&dst, src, options);
29 return SkImage::MakeFromEncoded(dst.detachAsData());
30}
31
32class EncodeJpegAlphaOptsGM : public GM {
33public:
34 EncodeJpegAlphaOptsGM() {}
35
36protected:
37 SkString onShortName() override {
38 return SkString("encode-alpha-jpeg");
39 }
40
41 SkISize onISize() override {
42 return SkISize::Make(400, 200);
43 }
44
45 void onDraw(SkCanvas* canvas) override {
Hal Canaryc465d132017-12-08 10:21:31 -050046 sk_sp<SkImage> srcImg = GetResourceAsImage("images/rainbow-gradient.png");
Matt Sarett2e61b182017-05-09 12:46:50 -040047 fStorage.reset(srcImg->width() * srcImg->height() *
48 SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType));
49
50 SkPixmap src;
51 SkImageInfo info = SkImageInfo::MakeN32Premul(srcImg->width(), srcImg->height(),
52 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
53 read_into_pixmap(&src, info, fStorage.get(), srcImg);
54
Matt Sarett2e61b182017-05-09 12:46:50 -040055 // Encode 8888 premul.
Brian Osmanb62f50c2018-07-12 14:44:27 -040056 auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
57 auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
Matt Sarett2e61b182017-05-09 12:46:50 -040058 canvas->drawImage(img0, 0.0f, 0.0f);
59 canvas->drawImage(img1, 0.0f, 100.0f);
60
61 // Encode 8888 unpremul
62 info = info.makeAlphaType(kUnpremul_SkAlphaType);
63 read_into_pixmap(&src, info, fStorage.get(), srcImg);
Brian Osmanb62f50c2018-07-12 14:44:27 -040064 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
65 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
Matt Sarett2e61b182017-05-09 12:46:50 -040066 canvas->drawImage(img0, 100.0f, 0.0f);
67 canvas->drawImage(img1, 100.0f, 100.0f);
68
69 if (canvas->imageInfo().colorSpace()) {
70 // Encode F16 premul
71 info = SkImageInfo::Make(srcImg->width(), srcImg->height(), kRGBA_F16_SkColorType,
72 kPremul_SkAlphaType, SkColorSpace::MakeSRGBLinear());
73 read_into_pixmap(&src, info, fStorage.get(), srcImg);
Brian Osmanb62f50c2018-07-12 14:44:27 -040074 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
75 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
Matt Sarett2e61b182017-05-09 12:46:50 -040076 canvas->drawImage(img0, 200.0f, 0.0f);
77 canvas->drawImage(img1, 200.0f, 100.0f);
78
79 // Encode F16 unpremul
80 info = info.makeAlphaType(kUnpremul_SkAlphaType);
81 read_into_pixmap(&src, info, fStorage.get(), srcImg);
Brian Osmanb62f50c2018-07-12 14:44:27 -040082 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
83 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
Matt Sarett2e61b182017-05-09 12:46:50 -040084 canvas->drawImage(img0, 300.0f, 0.0f);
85 canvas->drawImage(img1, 300.0f, 100.0f);
86 }
87 }
88
89private:
90 SkAutoTMalloc<uint8_t> fStorage;
91
92 typedef GM INHERITED;
93};
94
95DEF_GM( return new EncodeJpegAlphaOptsGM; )
96
97};