| 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/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColorSpace.h" |
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkImage.h" |
| 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" |
| 20 |
#include "include/encode/SkJpegEncoder.h" |
| 21 |
#include "include/private/base/SkTemplates.h" |
| 22 |
#include "tools/DecodeUtils.h" |
| 23 |
#include "tools/Resources.h" |
| 24 |
|
| 25 |
using namespace skia_private; |
| 26 |
|
| 27 |
namespace skiagm { |
| 28 |
|
| 29 |
static inline void read_into_pixmap(SkPixmap* dst, SkImageInfo dstInfo, void* dstPixels, |
| 30 |
sk_sp<SkImage> src) { |
| 31 |
dst->reset(dstInfo, dstPixels, dstInfo.minRowBytes()); |
| 32 |
src->readPixels(nullptr, *dst, 0, 0, SkImage::CachingHint::kDisallow_CachingHint); |
| 33 |
} |
| 34 |
|
| 35 |
static inline sk_sp<SkImage> encode_pixmap_and_make_image(const SkPixmap& src, |
| 36 |
SkJpegEncoder::AlphaOption alphaOption) { |
| 37 |
SkJpegEncoder::Options options; |
| 38 |
options.fAlphaOption = alphaOption; |
| 39 |
sk_sp<SkData> data = SkJpegEncoder::Encode(src, options); |
| 40 |
return SkImages::DeferredFromEncodedData(data); |
| 41 |
} |
| 42 |
|
| 43 |
class EncodeJpegAlphaOptsGM : public GM { |
| 44 |
public: |
| 45 |
EncodeJpegAlphaOptsGM() {} |
| 46 |
|
| 47 |
protected: |
| 48 |
SkString getName() const override { return SkString("encode-alpha-jpeg"); } |
| 49 |
|
| 50 |
SkISize getISize() override { return SkISize::Make(400, 200); } |
| 51 |
|
| 52 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 53 |
sk_sp<SkImage> srcImg = ToolUtils::GetResourceAsImage("images/rainbow-gradient.png"); |
| 54 |
if (!srcImg) { |
| 55 |
*errorMsg = "Could not load images/rainbow-gradient.png. " |
| 56 |
"Did you forget to set the resourcePath?"; |
| 57 |
return DrawResult::kFail; |
| 58 |
} |
| 59 |
fStorage.reset(srcImg->width() * srcImg->height() * |
| 60 |
SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType)); |
| 61 |
|
| 62 |
SkPixmap src; |
| 63 |
SkImageInfo info = SkImageInfo::MakeN32Premul(srcImg->width(), srcImg->height(), |
| 64 |
canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr); |
| 65 |
read_into_pixmap(&src, info, fStorage.get(), srcImg); |
| 66 |
|
| 67 |
// Encode 8888 premul. |
| 68 |
auto img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore); |
| 69 |
auto img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack); |
| 70 |
canvas->drawImage(img0, 0.0f, 0.0f); |
| 71 |
canvas->drawImage(img1, 0.0f, 100.0f); |
| 72 |
|
| 73 |
// Encode 8888 unpremul |
| 74 |
info = info.makeAlphaType(kUnpremul_SkAlphaType); |
| 75 |
read_into_pixmap(&src, info, fStorage.get(), srcImg); |
| 76 |
img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore); |
| 77 |
img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack); |
| 78 |
canvas->drawImage(img0, 100.0f, 0.0f); |
| 79 |
canvas->drawImage(img1, 100.0f, 100.0f); |
| 80 |
|
| 81 |
// Encode F16 premul |
| 82 |
info = SkImageInfo::Make(srcImg->width(), srcImg->height(), kRGBA_F16_SkColorType, |
| 83 |
kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| 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, 200.0f, 0.0f); |
| 88 |
canvas->drawImage(img1, 200.0f, 100.0f); |
| 89 |
|
| 90 |
// Encode F16 unpremul |
| 91 |
info = info.makeAlphaType(kUnpremul_SkAlphaType); |
| 92 |
read_into_pixmap(&src, info, fStorage.get(), srcImg); |
| 93 |
img0 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kIgnore); |
| 94 |
img1 = encode_pixmap_and_make_image(src, SkJpegEncoder::AlphaOption::kBlendOnBlack); |
| 95 |
canvas->drawImage(img0, 300.0f, 0.0f); |
| 96 |
canvas->drawImage(img1, 300.0f, 100.0f); |
| 97 |
|
| 98 |
return DrawResult::kOk; |
| 99 |
} |
| 100 |
|
| 101 |
private: |
| 102 |
AutoTMalloc<uint8_t> fStorage; |
| 103 |
|
| 104 |
using INHERITED = GM; |
| 105 |
}; |
| 106 |
|
| 107 |
DEF_GM( return new EncodeJpegAlphaOptsGM; ) |
| 108 |
|
| 109 |
} // namespace skiagm |