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 "SkAndroidCodec.h"
9 #include "SkBitmap.h"
10 #include "SkCodec.h"
11 #include "SkStream.h"
12
13 #include "CodecPriv.h"
14 #include "Resources.h"
15 #include "Test.h"
16 #include "sk_tool_utils.h"
17
18 #include <initializer_list>
19 #include <vector>
20
DEF_TEST(Codec_trunc,r)21 DEF_TEST(Codec_trunc, r) {
22 sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
23 if (!data) {
24 return;
25 }
26 SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
27 }
28
29 // 565 does not support alpha, but there is no reason for it not to support an
30 // animated image with a frame that has alpha but then blends onto an opaque
31 // frame making the result opaque. Test that we can decode such a frame.
DEF_TEST(Codec_565,r)32 DEF_TEST(Codec_565, r) {
33 sk_sp<SkData> data(GetResourceAsData("images/blendBG.webp"));
34 if (!data) {
35 return;
36 }
37 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data)));
38 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
39 SkBitmap bm;
40 bm.allocPixels(info);
41
42 SkCodec::Options options;
43 options.fFrameIndex = 1;
44 options.fPriorFrame = SkCodec::kNone;
45
46 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
47 &options);
48 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
49 }
50
restore_previous(const SkCodec::FrameInfo & info)51 static bool restore_previous(const SkCodec::FrameInfo& info) {
52 return info.fDisposalMethod == SkCodecAnimation::DisposalMethod::kRestorePrevious;
53 }
54
DEF_TEST(Codec_frames,r)55 DEF_TEST(Codec_frames, r) {
56 #define kOpaque kOpaque_SkAlphaType
57 #define kUnpremul kUnpremul_SkAlphaType
58 #define kKeep SkCodecAnimation::DisposalMethod::kKeep
59 #define kRestoreBG SkCodecAnimation::DisposalMethod::kRestoreBGColor
60 #define kRestorePrev SkCodecAnimation::DisposalMethod::kRestorePrevious
61 static const struct {
62 const char* fName;
63 int fFrameCount;
64 // One less than fFramecount, since the first frame is always
65 // independent.
66 std::vector<int> fRequiredFrames;
67 // Same, since the first frame should match getInfo
68 std::vector<SkAlphaType> fAlphas;
69 // The size of this one should match fFrameCount for animated, empty
70 // otherwise.
71 std::vector<int> fDurations;
72 int fRepetitionCount;
73 std::vector<SkCodecAnimation::DisposalMethod> fDisposalMethods;
74 } gRecs[] = {
75 { "images/required.gif", 7,
76 { 0, 1, 2, 3, 4, 5 },
77 { kOpaque, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
78 { 100, 100, 100, 100, 100, 100, 100 },
79 0,
80 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
81 { "images/alphabetAnim.gif", 13,
82 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
83 SkCodec::kNone, 9, 10, 11 },
84 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
85 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
86 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
87 0,
88 { kKeep, kRestorePrev, kRestorePrev, kRestorePrev, kRestorePrev,
89 kRestoreBG, kKeep, kRestoreBG, kRestoreBG, kKeep, kKeep,
90 kRestoreBG, kKeep } },
91 { "images/randPixelsAnim2.gif", 4,
92 // required frames
93 { 0, 0, 1 },
94 // alphas
95 { kOpaque, kOpaque, kOpaque },
96 // durations
97 { 0, 1000, 170, 40 },
98 // repetition count
99 0,
100 { kKeep, kKeep, kRestorePrev, kKeep } },
101 { "images/randPixelsAnim.gif", 13,
102 // required frames
103 { 0, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
104 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
105 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
106 // durations
107 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
108 // repetition count
109 0,
110 { kKeep, kKeep, kKeep, kKeep, kRestoreBG, kRestoreBG, kRestoreBG,
111 kRestoreBG, kRestorePrev, kRestoreBG, kRestorePrev, kRestorePrev,
112 kRestorePrev, } },
113 { "images/box.gif", 1, {}, {}, {}, 0, { kKeep } },
114 { "images/color_wheel.gif", 1, {}, {}, {}, 0, { kKeep } },
115 { "images/test640x479.gif", 4, { 0, 1, 2 },
116 { kOpaque, kOpaque, kOpaque },
117 { 200, 200, 200, 200 },
118 SkCodec::kRepetitionCountInfinite,
119 { kKeep, kKeep, kKeep, kKeep } },
120 { "images/colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5,
121 { kKeep, kKeep } },
122
123 { "images/arrow.png", 1, {}, {}, {}, 0, {} },
124 { "images/google_chrome.ico", 1, {}, {}, {}, 0, {} },
125 { "images/brickwork-texture.jpg", 1, {}, {}, {}, 0, {} },
126 #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
127 { "images/dng_with_preview.dng", 1, {}, {}, {}, 0, {} },
128 #endif
129 { "images/mandrill.wbmp", 1, {}, {}, {}, 0, {} },
130 { "images/randPixels.bmp", 1, {}, {}, {}, 0, {} },
131 { "images/yellow_rose.webp", 1, {}, {}, {}, 0, {} },
132 { "images/webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
133 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite,
134 { kKeep, kKeep, kKeep } },
135 { "images/blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone,
136 4, 4 },
137 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
138 { 525, 500, 525, 437, 609, 729, 444 }, 7,
139 { kKeep, kKeep, kKeep, kKeep, kKeep, kKeep, kKeep } },
140 { "images/required.webp", 7,
141 { 0, 1, 1, SkCodec::kNone, 4, 4 },
142 { kOpaque, kUnpremul, kUnpremul, kOpaque, kOpaque, kOpaque },
143 { 100, 100, 100, 100, 100, 100, 100 },
144 1,
145 { kKeep, kRestoreBG, kKeep, kKeep, kKeep, kRestoreBG, kKeep } },
146 };
147 #undef kOpaque
148 #undef kUnpremul
149 #undef kKeep
150 #undef kRestorePrev
151 #undef kRestoreBG
152
153 for (const auto& rec : gRecs) {
154 sk_sp<SkData> data(GetResourceAsData(rec.fName));
155 if (!data) {
156 // Useful error statement, but sometimes people run tests without
157 // resources, and they do not want to see these messages.
158 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
159 continue;
160 }
161
162 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
163 if (!codec) {
164 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
165 continue;
166 }
167
168 {
169 SkCodec::FrameInfo frameInfo;
170 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
171 }
172
173 const int repetitionCount = codec->getRepetitionCount();
174 if (repetitionCount != rec.fRepetitionCount) {
175 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
176 rec.fName, rec.fRepetitionCount, repetitionCount);
177 }
178
179 const int expected = rec.fFrameCount;
180 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
181 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
182 rec.fName, expected - 1, rec.fRequiredFrames.size());
183 continue;
184 }
185
186 if (expected > 1) {
187 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
188 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
189 rec.fName, expected, rec.fDurations.size());
190 continue;
191 }
192
193 if (rec.fAlphas.size() + 1 != static_cast<size_t>(expected)) {
194 ERRORF(r, "'%s' has wrong number entries in fAlphas; expected: %i\tactual: %i",
195 rec.fName, expected - 1, rec.fAlphas.size());
196 continue;
197 }
198
199 if (rec.fDisposalMethods.size() != static_cast<size_t>(expected)) {
200 ERRORF(r, "'%s' has wrong number entries in fDisposalMethods; "
201 "expected %i\tactual: %i",
202 rec.fName, expected, rec.fDisposalMethods.size());
203 continue;
204 }
205 }
206
207 enum class TestMode {
208 kVector,
209 kIndividual,
210 };
211
212 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
213 // Re-create the codec to reset state and test parsing.
214 codec = SkCodec::MakeFromData(data);
215
216 int frameCount;
217 std::vector<SkCodec::FrameInfo> frameInfos;
218 switch (mode) {
219 case TestMode::kVector:
220 frameInfos = codec->getFrameInfo();
221 // getFrameInfo returns empty set for non-animated.
222 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
223 break;
224 case TestMode::kIndividual:
225 frameCount = codec->getFrameCount();
226 break;
227 }
228
229 if (frameCount != expected) {
230 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
231 rec.fName, expected, frameCount);
232 continue;
233 }
234
235 // From here on, we are only concerned with animated images.
236 if (1 == frameCount) {
237 continue;
238 }
239
240 for (int i = 0; i < frameCount; i++) {
241 SkCodec::FrameInfo frameInfo;
242 switch (mode) {
243 case TestMode::kVector:
244 frameInfo = frameInfos[i];
245 break;
246 case TestMode::kIndividual:
247 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
248 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
249 break;
250 }
251
252 if (rec.fDurations[i] != frameInfo.fDuration) {
253 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
254 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
255 }
256
257 auto to_string = [](SkAlphaType alpha) {
258 switch (alpha) {
259 case kUnpremul_SkAlphaType:
260 return "unpremul";
261 case kOpaque_SkAlphaType:
262 return "opaque";
263 default:
264 SkASSERT(false);
265 return "unknown";
266 }
267 };
268
269 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphas[i-1];
270 auto alpha = frameInfo.fAlphaType;
271 if (expectedAlpha != alpha) {
272 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
273 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
274 }
275
276 if (0 == i) {
277 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
278 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
279 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
280 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
281 }
282
283 REPORTER_ASSERT(r, frameInfo.fDisposalMethod == rec.fDisposalMethods[i]);
284 }
285
286 if (TestMode::kIndividual == mode) {
287 // No need to test decoding twice.
288 continue;
289 }
290
291 // Compare decoding in multiple ways:
292 // - Start from scratch for each frame. |codec| will have to decode the required frame
293 // (and any it depends on) to decode. This is stored in |cachedFrames|.
294 // - Provide the frame that a frame depends on, so |codec| just has to blend.
295 // - Provide a frame after the required frame, which will be covered up by the newest
296 // frame.
297 // All should look the same.
298 std::vector<SkBitmap> cachedFrames(frameCount);
299 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
300
301 auto decode = [&](SkBitmap* bm, int index, int cachedIndex) {
302 auto decodeInfo = info;
303 if (index > 0) {
304 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
305 }
306 bm->allocPixels(decodeInfo);
307 if (cachedIndex != SkCodec::kNone) {
308 // First copy the pixels from the cached frame
309 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
310 cachedFrames[cachedIndex]);
311 REPORTER_ASSERT(r, success);
312 }
313 SkCodec::Options opts;
314 opts.fFrameIndex = index;
315 opts.fPriorFrame = cachedIndex;
316 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
317 &opts);
318 if (cachedIndex != SkCodec::kNone && restore_previous(frameInfos[cachedIndex])) {
319 if (result == SkCodec::kInvalidParameters) {
320 return true;
321 }
322 ERRORF(r, "Using a kRestorePrevious frame as fPriorFrame should fail");
323 return false;
324 }
325 if (result != SkCodec::kSuccess) {
326 ERRORF(r, "Failed to decode frame %i from %s when providing prior frame %i, "
327 "error %i", index, rec.fName, cachedIndex, result);
328 }
329 return result == SkCodec::kSuccess;
330 };
331
332 for (int i = 0; i < frameCount; i++) {
333 SkBitmap& cachedFrame = cachedFrames[i];
334 if (!decode(&cachedFrame, i, SkCodec::kNone)) {
335 continue;
336 }
337 const auto reqFrame = frameInfos[i].fRequiredFrame;
338 if (reqFrame == SkCodec::kNone) {
339 // Nothing to compare against.
340 continue;
341 }
342 for (int j = reqFrame; j < i; j++) {
343 SkBitmap frame;
344 if (restore_previous(frameInfos[j])) {
345 (void) decode(&frame, i, j);
346 continue;
347 }
348 if (!decode(&frame, i, j)) {
349 continue;
350 }
351
352 // Now verify they're equal.
353 const size_t rowLen = info.bytesPerPixel() * info.width();
354 for (int y = 0; y < info.height(); y++) {
355 const void* cachedAddr = cachedFrame.getAddr(0, y);
356 SkASSERT(cachedAddr != nullptr);
357 const void* addr = frame.getAddr(0, y);
358 SkASSERT(addr != nullptr);
359 const bool lineMatches = memcmp(cachedAddr, addr, rowLen) == 0;
360 if (!lineMatches) {
361 SkString name = SkStringPrintf("cached_%i", i);
362 write_bm(name.c_str(), cachedFrame);
363 name = SkStringPrintf("frame_%i", i);
364 write_bm(name.c_str(), frame);
365 ERRORF(r, "%s's frame %i is different (starting from line %i) when "
366 "providing prior frame %i!", rec.fName, i, y, j);
367 break;
368 }
369 }
370 }
371 }
372 }
373 }
374 }
375
376 // Verify that a webp image can be animated scaled down. This image has a
377 // kRestoreBG frame, so it is an interesting image to test. After decoding that
378 // frame, we have to erase its rectangle. The rectangle has to be adjusted
379 // based on the scaled size.
DEF_TEST(AndroidCodec_animated,r)380 DEF_TEST(AndroidCodec_animated, r) {
381 if (GetResourcePath().isEmpty()) {
382 return;
383 }
384
385 const char* file = "images/required.webp";
386 sk_sp<SkData> data(GetResourceAsData(file));
387 if (!data) {
388 ERRORF(r, "Missing %s", file);
389 return;
390 }
391
392 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)));
393 if (!codec) {
394 ERRORF(r, "Failed to decode %s", file);
395 return;
396 }
397
398 auto info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
399
400 for (int sampleSize : { 8, 32, 100 }) {
401 auto dimensions = codec->codec()->getScaledDimensions(1.0f / sampleSize);
402 info = info.makeWH(dimensions.width(), dimensions.height());
403 SkBitmap bm;
404 bm.allocPixels(info);
405
406 SkCodec::Options options;
407 for (int i = 0; i < codec->codec()->getFrameCount(); ++i) {
408 SkCodec::FrameInfo frameInfo;
409 REPORTER_ASSERT(r, codec->codec()->getFrameInfo(i, &frameInfo));
410 if (5 == i) {
411 REPORTER_ASSERT(r, frameInfo.fDisposalMethod
412 == SkCodecAnimation::DisposalMethod::kRestoreBGColor);
413 }
414 options.fFrameIndex = i;
415 options.fPriorFrame = i - 1;
416 info = info.makeAlphaType(frameInfo.fAlphaType);
417
418 const auto result = codec->codec()->getPixels(info, bm.getPixels(), bm.rowBytes(),
419 &options);
420 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
421 }
422 }
423 }
424