blob: b738690f5fc5dc19f1ccc44ba6370bc26ea73984 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColorSpace.h"
11#include "include/core/SkData.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/encode/SkJpegEncoder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/Resources.h"
Matt Sarett2e61b182017-05-09 12:46:50 -040023
24namespace skiagm {
25
26static 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(*dst, 0, 0, SkImage::CachingHint::kDisallow_CachingHint);
30}
31
32static inline sk_sp<SkImage> encode_pixmap_and_make_image(const SkPixmap& src,
Brian Osmanb62f50c2018-07-12 14:44:27 -040033 SkJpegEncoder::AlphaOption alphaOption) {
Matt Sarett2e61b182017-05-09 12:46:50 -040034 SkDynamicMemoryWStream dst;
35 SkJpegEncoder::Options options;
36 options.fAlphaOption = alphaOption;
Matt Sarett2e61b182017-05-09 12:46:50 -040037 SkJpegEncoder::Encode(&dst, src, options);
38 return SkImage::MakeFromEncoded(dst.detachAsData());
39}
40
41class EncodeJpegAlphaOptsGM : public GM {
42public:
43 EncodeJpegAlphaOptsGM() {}
44
45protected:
46 SkString onShortName() override {
47 return SkString("encode-alpha-jpeg");
48 }
49
50 SkISize onISize() override {
51 return SkISize::Make(400, 200);
52 }
53
Chris Dalton50e24d72019-02-07 16:20:09 -070054 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Hal Canaryc465d132017-12-08 10:21:31 -050055 sk_sp<SkImage> srcImg = GetResourceAsImage("images/rainbow-gradient.png");
Hal Canarybaa2a282018-11-26 15:34:12 -050056 if (!srcImg) {
Chris Dalton50e24d72019-02-07 16:20:09 -070057 *errorMsg = "Could not load images/rainbow-gradient.png. "
58 "Did you forget to set the resourcePath?";
59 return DrawResult::kFail;
Hal Canarybaa2a282018-11-26 15:34:12 -050060 }
Matt Sarett2e61b182017-05-09 12:46:50 -040061 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
Matt Sarett2e61b182017-05-09 12:46:50 -040069 // Encode 8888 premul.
Brian Osmanb62f50c2018-07-12 14:44:27 -040070 auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
71 auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
Matt Sarett2e61b182017-05-09 12:46:50 -040072 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);
Brian Osmanb62f50c2018-07-12 14:44:27 -040078 img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore);
79 img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack);
Matt Sarett2e61b182017-05-09 12:46:50 -040080 canvas->drawImage(img0, 100.0f, 0.0f);
81 canvas->drawImage(img1, 100.0f, 100.0f);
82
Mike Reedd1061102019-03-06 10:05:34 -050083 // 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);
Matt Sarett2e61b182017-05-09 12:46:50 -040091
Mike Reedd1061102019-03-06 10:05:34 -050092 // 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
Chris Dalton50e24d72019-02-07 16:20:09 -0700100 return DrawResult::kOk;
Matt Sarett2e61b182017-05-09 12:46:50 -0400101 }
102
103private:
104 SkAutoTMalloc<uint8_t> fStorage;
105
106 typedef GM INHERITED;
107};
108
109DEF_GM( return new EncodeJpegAlphaOptsGM; )
110
John Stilesa6841be2020-08-06 14:11:56 -0400111} // namespace skiagm