xref: /aosp_15_r20/external/skia/tests/AvifTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "include/core/SkTypes.h"
9 
10 #ifdef SK_CODEC_DECODES_AVIF
11 #include "include/codec/SkCodec.h"
12 #include "include/core/SkBitmap.h"
13 #include "tests/Test.h"
14 #include "tools/Resources.h"
15 #include "tools/ToolUtils.h"
16 
17 struct AvifTestCase {
18     const char* path;
19     int imageWidth;
20     int imageHeight;
21     int bitmapWidth;
22     int bitmapHeight;
23     int expectedFrameCount;
24     int expectedFrameDuration;
25     SkColorType color_type;
26     SkAlphaType alpha_type;
27 };
28 
run_avif_test(skiatest::Reporter * r,const AvifTestCase & t)29 static 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++) {
53         SkCodec::Options options;
54         options.fFrameIndex = i;
55         auto result = codec->getPixels(imageInfo, bm.getPixels(), bm.rowBytes(), &options);
56         if (result != SkCodec::kSuccess) {
57             ERRORF(r,
58                    "Failed to decode %s frame %i - error %s",
59                    t.path,
60                    i,
61                    SkCodec::ResultToString(result));
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 
DEF_TEST(AvifDecodeBasic,r)77 DEF_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 
DEF_TEST(AvifDecodeOddDimensions,r)90 DEF_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 
DEF_TEST(AvifDecodeScaleDown,r)103 DEF_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 
DEF_TEST(AvifDecode10BitToRGBA8888Bitmap,r)116 DEF_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 
DEF_TEST(AvifDecode10BitToRGBAF16Bitmap,r)129 DEF_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 
DEF_TEST(AvifDecode10BitToRGBAF16BitmapDownscale,r)142 DEF_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 
DEF_TEST(AvifDecode12BitToRGBA8888Bitmap,r)155 DEF_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 
DEF_TEST(AvifDecode12BitToRGBAF16Bitmap,r)168 DEF_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 
DEF_TEST(AvifDecode12BitToRGBAF16BitmapDownscale,r)181 DEF_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 
DEF_TEST(AvifDecodeImageWithAlpha,r)194 DEF_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 
DEF_TEST(AvifDecodeAnimation,r)207 DEF_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 
DEF_TEST(AvifDecodeAnimationWithAlpha,r)220 DEF_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
234