Flutter Engine
The Flutter Engine
AvifTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
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
9
10#ifdef SK_CODEC_DECODES_AVIF
13#include "tests/Test.h"
14#include "tools/Resources.h"
15#include "tools/ToolUtils.h"
16
17struct AvifTestCase {
18 const char* path;
19 int imageWidth;
20 int imageHeight;
21 int bitmapWidth;
22 int bitmapHeight;
23 int expectedFrameCount;
24 int expectedFrameDuration;
27};
28
29static void run_avif_test(skiatest::Reporter* r, const AvifTestCase& t) {
30 auto data = GetResourceAsData(t.path);
31 if (!data) {
32 ERRORF(r, "failed to find %s", t.path);
33 return;
34 }
35
36 auto codec = SkCodec::MakeFromData(std::move(data));
37 if (!codec) {
38 ERRORF(r, "Could not create codec from %s", t.path);
39 return;
40 }
41
42 REPORTER_ASSERT(r, codec->getFrameCount() == t.expectedFrameCount);
43 auto info = codec->getInfo();
44 REPORTER_ASSERT(r, info.width() == t.imageWidth);
45 REPORTER_ASSERT(r, info.height() == t.imageHeight);
46
47 SkImageInfo imageInfo =
48 SkImageInfo::Make(t.bitmapWidth, t.bitmapHeight, t.color_type, t.alpha_type);
49 SkBitmap bm;
50 bm.allocPixels(imageInfo);
51
52 for (int i = 0; i < codec->getFrameCount(); i++) {
54 options.fFrameIndex = i;
55 auto result = codec->getPixels(imageInfo, bm.getPixels(), bm.rowBytes(), &options);
57 ERRORF(r,
58 "Failed to decode %s frame %i - error %s",
59 t.path,
60 i,
62 return;
63 }
64
65 if (codec->getFrameCount() > 1) {
66 SkCodec::FrameInfo frameInfo;
67 if (!codec->getFrameInfo(i, &frameInfo)) {
68 ERRORF(r, "Failed to getFrameInfo for %s frame %i", t.path, i);
69 return;
70 }
71 REPORTER_ASSERT(r, frameInfo.fAlphaType == t.alpha_type);
72 REPORTER_ASSERT(r, frameInfo.fDuration == t.expectedFrameDuration);
73 }
74 }
75}
76
77DEF_TEST(AvifDecodeBasic, r) {
78 AvifTestCase t = {.path = "images/dog.avif",
79 .imageWidth = 180,
80 .imageHeight = 180,
81 .bitmapWidth = 180,
82 .bitmapHeight = 180,
83 .expectedFrameCount = 1,
84 .expectedFrameDuration = 0,
85 .color_type = kRGBA_8888_SkColorType,
86 .alpha_type = kOpaque_SkAlphaType};
87 run_avif_test(r, t);
88}
89
90DEF_TEST(AvifDecodeOddDimensions, r) {
91 AvifTestCase t = {.path = "images/ducky.avif",
92 .imageWidth = 489,
93 .imageHeight = 537,
94 .bitmapWidth = 489,
95 .bitmapHeight = 537,
96 .expectedFrameCount = 1,
97 .expectedFrameDuration = 0,
98 .color_type = kRGBA_8888_SkColorType,
99 .alpha_type = kOpaque_SkAlphaType};
100 run_avif_test(r, t);
101}
102
103DEF_TEST(AvifDecodeScaleDown, r) {
104 AvifTestCase t = {.path = "images/dog.avif",
105 .imageWidth = 180,
106 .imageHeight = 180,
107 .bitmapWidth = 20,
108 .bitmapHeight = 20,
109 .expectedFrameCount = 1,
110 .expectedFrameDuration = 0,
111 .color_type = kRGBA_8888_SkColorType,
112 .alpha_type = kOpaque_SkAlphaType};
113 run_avif_test(r, t);
114}
115
116DEF_TEST(AvifDecode10BitToRGBA8888Bitmap, r) {
117 AvifTestCase t = {.path = "images/example_3_10bit.avif",
118 .imageWidth = 512,
119 .imageHeight = 512,
120 .bitmapWidth = 512,
121 .bitmapHeight = 512,
122 .expectedFrameCount = 1,
123 .expectedFrameDuration = 0,
124 .color_type = kRGBA_8888_SkColorType,
125 .alpha_type = kOpaque_SkAlphaType};
126 run_avif_test(r, t);
127}
128
129DEF_TEST(AvifDecode10BitToRGBAF16Bitmap, r) {
130 AvifTestCase t = {.path = "images/example_3_10bit.avif",
131 .imageWidth = 512,
132 .imageHeight = 512,
133 .bitmapWidth = 512,
134 .bitmapHeight = 512,
135 .expectedFrameCount = 1,
136 .expectedFrameDuration = 0,
137 .color_type = kRGBA_F16_SkColorType,
138 .alpha_type = kOpaque_SkAlphaType};
139 run_avif_test(r, t);
140}
141
142DEF_TEST(AvifDecode10BitToRGBAF16BitmapDownscale, r) {
143 AvifTestCase t = {.path = "images/example_3_10bit.avif",
144 .imageWidth = 512,
145 .imageHeight = 512,
146 .bitmapWidth = 100,
147 .bitmapHeight = 100,
148 .expectedFrameCount = 1,
149 .expectedFrameDuration = 0,
150 .color_type = kRGBA_F16_SkColorType,
151 .alpha_type = kOpaque_SkAlphaType};
152 run_avif_test(r, t);
153}
154
155DEF_TEST(AvifDecode12BitToRGBA8888Bitmap, r) {
156 AvifTestCase t = {.path = "images/example_3_12bit.avif",
157 .imageWidth = 512,
158 .imageHeight = 512,
159 .bitmapWidth = 512,
160 .bitmapHeight = 512,
161 .expectedFrameCount = 1,
162 .expectedFrameDuration = 0,
163 .color_type = kRGBA_8888_SkColorType,
164 .alpha_type = kOpaque_SkAlphaType};
165 run_avif_test(r, t);
166}
167
168DEF_TEST(AvifDecode12BitToRGBAF16Bitmap, r) {
169 AvifTestCase t = {.path = "images/example_3_12bit.avif",
170 .imageWidth = 512,
171 .imageHeight = 512,
172 .bitmapWidth = 512,
173 .bitmapHeight = 512,
174 .expectedFrameCount = 1,
175 .expectedFrameDuration = 0,
176 .color_type = kRGBA_F16_SkColorType,
177 .alpha_type = kOpaque_SkAlphaType};
178 run_avif_test(r, t);
179}
180
181DEF_TEST(AvifDecode12BitToRGBAF16BitmapDownscale, r) {
182 AvifTestCase t = {.path = "images/example_3_12bit.avif",
183 .imageWidth = 512,
184 .imageHeight = 512,
185 .bitmapWidth = 100,
186 .bitmapHeight = 100,
187 .expectedFrameCount = 1,
188 .expectedFrameDuration = 0,
189 .color_type = kRGBA_F16_SkColorType,
190 .alpha_type = kOpaque_SkAlphaType};
191 run_avif_test(r, t);
192}
193
194DEF_TEST(AvifDecodeImageWithAlpha, r) {
195 AvifTestCase t = {.path = "images/baby_tux.avif",
196 .imageWidth = 240,
197 .imageHeight = 246,
198 .bitmapWidth = 240,
199 .bitmapHeight = 246,
200 .expectedFrameCount = 1,
201 .expectedFrameDuration = 0,
202 .color_type = kRGBA_8888_SkColorType,
203 .alpha_type = kUnpremul_SkAlphaType};
204 run_avif_test(r, t);
205}
206
207DEF_TEST(AvifDecodeAnimation, r) {
208 AvifTestCase t = {.path = "images/alphabetAnim.avif",
209 .imageWidth = 100,
210 .imageHeight = 100,
211 .bitmapWidth = 100,
212 .bitmapHeight = 100,
213 .expectedFrameCount = 13,
214 .expectedFrameDuration = 100,
215 .color_type = kRGBA_8888_SkColorType,
216 .alpha_type = kOpaque_SkAlphaType};
217 run_avif_test(r, t);
218}
219
220DEF_TEST(AvifDecodeAnimationWithAlpha, r) {
221 AvifTestCase t = {.path = "images/example_1_animated.avif",
222 .imageWidth = 256,
223 .imageHeight = 256,
224 .bitmapWidth = 256,
225 .bitmapHeight = 256,
226 .expectedFrameCount = 8,
227 .expectedFrameDuration = 33,
228 .color_type = kRGBA_8888_SkColorType,
229 .alpha_type = kUnpremul_SkAlphaType};
230 run_avif_test(r, t);
231}
232
233#endif // SK_CODEC_DECODES_AVIF
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
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
SkColorType
Definition: SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
#define DEF_TEST(name, reporter)
Definition: Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
size_t rowBytes() const
Definition: SkBitmap.h:238
void * getPixels() const
Definition: SkBitmap.h:283
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition: SkCodec.cpp:241
static const char * ResultToString(Result)
Definition: SkCodec.cpp:880
@ kSuccess
Definition: SkCodec.h:80
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
static const int imageHeight
static const int imageWidth
uint32_t color_type
uint32_t alpha_type
SkAlphaType fAlphaType
Definition: SkCodec.h:689
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63