Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
encode_srgb.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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"
14#include "include/core/SkData.h"
19#include "include/core/SkSize.h"
26#include "tools/Resources.h"
27
28#include <memory>
29
30namespace skiagm {
31
32static const int imageWidth = 128;
33static const int imageHeight = 128;
34
36 sk_sp<SkColorSpace> colorSpace) {
37 const char* resource;
38 switch (colorType) {
40 resource = "images/grayscale.jpg";
41 alphaType = kOpaque_SkAlphaType;
42 break;
44 resource = "images/color_wheel.jpg";
45 alphaType = kOpaque_SkAlphaType;
46 break;
47 default:
48 resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg"
49 : "images/color_wheel.png";
50 break;
51 }
52
53 sk_sp<SkData> data = GetResourceAsData(resource);
54 if (!data) {
55 return;
56 }
57 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
58 SkImageInfo dstInfo = codec->getInfo().makeColorType(colorType)
59 .makeAlphaType(alphaType)
60 .makeColorSpace(colorSpace);
61 bitmap->allocPixels(dstInfo);
62 codec->getPixels(dstInfo, bitmap->getPixels(), bitmap->rowBytes());
63}
64
66 SkPixmap src;
67 if (!bitmap.peekPixels(&src)) {
68 return nullptr;
69 }
71
72 switch (format) {
75 break;
78 break;
81 break;
82 default:
83 SK_ABORT("Unsupported format %d", (int)format);
84 break;
85 }
86 return buf.detachAsData();
87}
88
89class EncodeSRGBGM : public GM {
90public:
92 : fEncodedFormat(format)
93 {}
94
95protected:
96 SkString getName() const override {
97 const char* format = nullptr;
98 switch (fEncodedFormat) {
100 format = "png";
101 break;
103 format = "webp";
104 break;
106 format = "jpg";
107 break;
108 default:
109 break;
110 }
111 return SkStringPrintf("encode-srgb-%s", format);
112 }
113
114 SkISize getISize() override { return SkISize::Make(imageWidth * 2, imageHeight * 15); }
115
116 void onDraw(SkCanvas* canvas) override {
117 const SkColorType colorTypes[] = {
118 kN32_SkColorType, kRGBA_F16_SkColorType,
119#if !defined(SK_ENABLE_NDK_IMAGES)
120 // These fail with the NDK encoders because there is a mismatch between
121 // Gray_8 and Alpha_8
123#endif
125 };
126 const SkAlphaType alphaTypes[] = {
128 };
129 const sk_sp<SkColorSpace> colorSpaces[] = {
130 nullptr, SkColorSpace::MakeSRGB(),
131 };
132
134 for (SkColorType colorType : colorTypes) {
135 for (SkAlphaType alphaType : alphaTypes) {
136 canvas->save();
137 for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) {
138 make(&bitmap, colorType, alphaType, colorSpace);
139 auto data = encode_data(bitmap, fEncodedFormat);
141 canvas->drawImage(image.get(), 0.0f, 0.0f);
142 canvas->translate((float) imageWidth, 0.0f);
143 }
144 canvas->restore();
145 canvas->translate(0.0f, (float) imageHeight);
146 }
147 }
148 }
149
150private:
151 SkEncodedImageFormat fEncodedFormat;
152
153 using INHERITED = GM;
154};
155
156DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kPNG); )
157DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kWEBP); )
158DEF_GM( return new EncodeSRGBGM(SkEncodedImageFormat::kJPEG); )
159} // namespace skiagm
kUnpremul_SkAlphaType
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
SkAlphaType
Definition SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SK_ABORT(message,...)
Definition SkAssert.h:70
SkColorType
Definition SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
SkEncodedImageFormat
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition SkCodec.cpp:241
static sk_sp< SkColorSpace > MakeSRGB()
sk_sp< SkData > detachAsData()
Definition SkStream.cpp:707
T * get() const
Definition SkRefCnt.h:303
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
EncodeSRGBGM(SkEncodedImageFormat format)
sk_sp< SkImage > image
Definition examples.cpp:29
uint32_t uint32_t * format
#define DEF_GM(CODE)
Definition gm.h:40
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_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
static const int imageHeight
static sk_sp< SkData > encode_data(const SkBitmap &bitmap, SkEncodedImageFormat format)
static const int imageWidth
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
SkImageInfo makeColorType(SkColorType newColorType) const