Flutter Engine
The Flutter Engine
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
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
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
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
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 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)
static SkString resource(SkPDFResourceType type, int index)
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
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
Definition: encode_srgb.cpp:96
EncodeSRGBGM(SkEncodedImageFormat format)
Definition: encode_srgb.cpp:91
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
uint32_t uint32_t * format
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_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
Definition: bitmap.py:1
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
Definition: encode_srgb.cpp:35
static const int imageHeight
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static sk_sp< SkData > encode_data(const SkBitmap &bitmap, SkEncodedImageFormat format)
Definition: encode_srgb.cpp:65
static const int imageWidth
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
SkImageInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.h:475
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63