Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColorSpace.h" |
| 11 | #include "include/core/SkData.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/encode/SkJpegEncoder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/private/SkTemplates.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/Resources.h" |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 23 | |
| 24 | namespace skiagm { |
| 25 | |
| 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()); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 29 | src->readPixels(nullptr, *dst, 0, 0, SkImage::CachingHint::kDisallow_CachingHint); |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static inline sk_sp<SkImage> encode_pixmap_and_make_image(const SkPixmap& src, |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 33 | SkJpegEncoder::AlphaOption alphaOption) { |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 34 | SkDynamicMemoryWStream dst; |
| 35 | SkJpegEncoder::Options options; |
| 36 | options.fAlphaOption = alphaOption; |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 37 | SkJpegEncoder::Encode(&dst, src, options); |
| 38 | return SkImage::MakeFromEncoded(dst.detachAsData()); |
| 39 | } |
| 40 | |
| 41 | class EncodeJpegAlphaOptsGM : public GM { |
| 42 | public: |
| 43 | EncodeJpegAlphaOptsGM() {} |
| 44 | |
| 45 | protected: |
| 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 54 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 55 | sk_sp<SkImage> srcImg = GetResourceAsImage("images/rainbow-gradient.png"); |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 56 | if (!srcImg) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 57 | *errorMsg = "Could not load images/rainbow-gradient.png. " |
| 58 | "Did you forget to set the resourcePath?"; |
| 59 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 60 | } |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 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 | |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 69 | // Encode 8888 premul. |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 70 | auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore); |
| 71 | auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack); |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 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); |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 78 | img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore); |
| 79 | img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack); |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 80 | canvas->drawImage(img0, 100.0f, 0.0f); |
| 81 | canvas->drawImage(img1, 100.0f, 100.0f); |
| 82 | |
Mike Reed | d106110 | 2019-03-06 10:05:34 -0500 | [diff] [blame] | 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); |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 91 | |
Mike Reed | d106110 | 2019-03-06 10:05:34 -0500 | [diff] [blame] | 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 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 100 | return DrawResult::kOk; |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | SkAutoTMalloc<uint8_t> fStorage; |
| 105 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 106 | using INHERITED = GM; |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | DEF_GM( return new EncodeJpegAlphaOptsGM; ) |
| 110 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 111 | } // namespace skiagm |