Flutter Engine
The Flutter Engine
encode_alpha_jpeg.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkData.h"
16#include "include/core/SkSize.h"
22#include "tools/DecodeUtils.h"
23#include "tools/Resources.h"
24
25using namespace skia_private;
26
27namespace skiagm {
28
29static inline void read_into_pixmap(SkPixmap* dst, SkImageInfo dstInfo, void* dstPixels,
31 dst->reset(dstInfo, dstPixels, dstInfo.minRowBytes());
32 src->readPixels(nullptr, *dst, 0, 0, SkImage::CachingHint::kDisallow_CachingHint);
33}
34
36 SkJpegEncoder::AlphaOption alphaOption) {
39 options.fAlphaOption = alphaOption;
41 return SkImages::DeferredFromEncodedData(dst.detachAsData());
42}
43
44class EncodeJpegAlphaOptsGM : public GM {
45public:
47
48protected:
49 SkString getName() const override { return SkString("encode-alpha-jpeg"); }
50
51 SkISize getISize() override { return SkISize::Make(400, 200); }
52
53 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
54 sk_sp<SkImage> srcImg = ToolUtils::GetResourceAsImage("images/rainbow-gradient.png");
55 if (!srcImg) {
56 *errorMsg = "Could not load images/rainbow-gradient.png. "
57 "Did you forget to set the resourcePath?";
58 return DrawResult::kFail;
59 }
60 fStorage.reset(srcImg->width() * srcImg->height() *
62
65 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
66 read_into_pixmap(&src, info, fStorage.get(), srcImg);
67
68 // Encode 8888 premul.
71 canvas->drawImage(img0, 0.0f, 0.0f);
72 canvas->drawImage(img1, 0.0f, 100.0f);
73
74 // Encode 8888 unpremul
75 info = info.makeAlphaType(kUnpremul_SkAlphaType);
76 read_into_pixmap(&src, info, fStorage.get(), srcImg);
79 canvas->drawImage(img0, 100.0f, 0.0f);
80 canvas->drawImage(img1, 100.0f, 100.0f);
81
82 // Encode F16 premul
85 read_into_pixmap(&src, info, fStorage.get(), srcImg);
88 canvas->drawImage(img0, 200.0f, 0.0f);
89 canvas->drawImage(img1, 200.0f, 100.0f);
90
91 // Encode F16 unpremul
92 info = info.makeAlphaType(kUnpremul_SkAlphaType);
93 read_into_pixmap(&src, info, fStorage.get(), srcImg);
96 canvas->drawImage(img0, 300.0f, 0.0f);
97 canvas->drawImage(img1, 300.0f, 100.0f);
98
99 return DrawResult::kOk;
100 }
101
102private:
103 AutoTMalloc<uint8_t> fStorage;
104
105 using INHERITED = GM;
106};
107
108DEF_GM( return new EncodeJpegAlphaOptsGM; )
109
110} // namespace skiagm
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorSpace > MakeSRGB()
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
T * reset(size_t count=0)
Definition: SkTemplates.h:296
SkString getName() const override
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
dst
Definition: cp.py:12
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static sk_sp< SkImage > encode_pixmap_and_make_image(const SkPixmap &src, SkJpegEncoder::AlphaOption alphaOption)
DrawResult
Definition: gm.h:104
static void read_into_pixmap(SkPixmap *dst, SkImageInfo dstInfo, void *dstPixels, sk_sp< SkImage > src)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
size_t minRowBytes() const
Definition: SkImageInfo.h:517
SkColorSpace * colorSpace() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)