blob: 74ee3c63edf4ec6d0ea225d876084e771e8b0ffe [file] [log] [blame]
scroggo19b91532016-10-24 09:03:26 -07001/*
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
scroggo38a2cf52016-10-24 09:56:40 -07008#include "SkBitmap.h"
scroggo19b91532016-10-24 09:03:26 -07009#include "SkCodec.h"
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050010#include "SkCommonFlags.h"
11#include "SkImageEncoder.h"
12#include "SkOSPath.h"
scroggo19b91532016-10-24 09:03:26 -070013#include "SkStream.h"
14
15#include "Resources.h"
16#include "Test.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040017#include "sk_tool_utils.h"
scroggo19b91532016-10-24 09:03:26 -070018
19#include <initializer_list>
20#include <vector>
21
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050022static void write_bm(const char* name, const SkBitmap& bm) {
23 if (FLAGS_writePath.isEmpty()) {
24 return;
25 }
26
27 SkString filename = SkOSPath::Join(FLAGS_writePath[0], name);
28 filename.appendf(".png");
29 SkFILEWStream file(filename.c_str());
30 if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
31 SkDebugf("failed to write '%s'\n", filename.c_str());
32 }
33}
34
Leon Scroggins III7d22a332017-04-12 17:01:26 -040035DEF_TEST(Codec_trunc, r) {
36 sk_sp<SkData> data(GetResourceAsData("box.gif"));
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040037 if (!data) {
38 return;
39 }
Leon Scroggins III7d22a332017-04-12 17:01:26 -040040 data = SkData::MakeSubset(data.get(), 0, 23);
41 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
42 codec->getFrameInfo();
43}
44
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040045// 565 does not support alpha, but there is no reason for it not to support an
46// animated image with a frame that has alpha but then blends onto an opaque
47// frame making the result opaque. Test that we can decode such a frame.
48DEF_TEST(Codec_565, r) {
49 sk_sp<SkData> data(GetResourceAsData("blendBG.webp"));
50 if (!data) {
51 return;
52 }
53 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(std::move(data)));
54 auto info = codec->getInfo().makeColorType(kRGB_565_SkColorType);
55 SkBitmap bm;
56 bm.allocPixels(info);
57
58 SkCodec::Options options;
59 options.fFrameIndex = 1;
60 options.fHasPriorFrame = false;
61
62 const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(),
63 &options, nullptr, nullptr);
64 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
65}
66
67
scroggo19b91532016-10-24 09:03:26 -070068DEF_TEST(Codec_frames, r) {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040069 #define kOpaque kOpaque_SkAlphaType
70 #define kUnpremul kUnpremul_SkAlphaType
scroggo19b91532016-10-24 09:03:26 -070071 static const struct {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040072 const char* fName;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040073 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070074 // One less than fFramecount, since the first frame is always
75 // independent.
Leon Scroggins III249b8e32017-04-17 12:46:33 -040076 std::vector<int> fRequiredFrames;
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040077 // Same, since the first frame should match getInfo.
78 std::vector<SkAlphaType> fAlphaTypes;
scroggo19b91532016-10-24 09:03:26 -070079 // The size of this one should match fFrameCount for animated, empty
80 // otherwise.
Leon Scroggins III249b8e32017-04-17 12:46:33 -040081 std::vector<int> fDurations;
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040082 int fRepetitionCount;
scroggo19b91532016-10-24 09:03:26 -070083 } gRecs[] = {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040084 { "alphabetAnim.gif", 13,
85 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
86 SkCodec::kNone, SkCodec::kNone, 10, 11 },
87 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
88 kUnpremul, kUnpremul, kUnpremul, kOpaque, kOpaque, kUnpremul },
89 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
90 0 },
91 { "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 },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500100 { "randPixelsAnim.gif", 13,
101 // required frames
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400102 { SkCodec::kNone, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
103 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
104 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500105 // durations
106 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
107 // repetition count
108 0 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400109 { "box.gif", 1, {}, {}, {}, 0 },
110 { "color_wheel.gif", 1, {}, {}, {}, 0 },
111 { "test640x479.gif", 4, { 0, 1, 2 },
112 { kOpaque, kOpaque, kOpaque },
113 { 200, 200, 200, 200 },
scroggoe71b1a12016-11-01 08:28:28 -0700114 SkCodec::kRepetitionCountInfinite },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400115 { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5 },
scroggo19b91532016-10-24 09:03:26 -0700116
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400117 { "arrow.png", 1, {}, {}, {}, 0 },
118 { "google_chrome.ico", 1, {}, {}, {}, 0 },
119 { "brickwork-texture.jpg", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -0700120#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400121 { "dng_with_preview.dng", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -0700122#endif
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400123 { "mandrill.wbmp", 1, {}, {}, {}, 0 },
124 { "randPixels.bmp", 1, {}, {}, {}, 0 },
125 { "yellow_rose.webp", 1, {}, {}, {}, 0 },
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400126 { "webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400127 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite },
128 { "blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone,
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400129 4, 4 },
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400130 { kOpaque, kOpaque, kUnpremul, kOpaque, kUnpremul, kUnpremul },
131 { 525, 500, 525, 437, 609, 729, 444 }, 7 },
scroggo19b91532016-10-24 09:03:26 -0700132 };
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400133 #undef kOpaque
134 #undef kUnpremul
scroggo19b91532016-10-24 09:03:26 -0700135
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400136 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400137 sk_sp<SkData> data(GetResourceAsData(rec.fName));
138 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700139 // Useful error statement, but sometimes people run tests without
140 // resources, and they do not want to see these messages.
141 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
142 continue;
143 }
144
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400145 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700146 if (!codec) {
147 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
148 continue;
149 }
150
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400151 {
152 SkCodec::FrameInfo frameInfo;
153 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
154 }
155
scroggoe71b1a12016-11-01 08:28:28 -0700156 const int repetitionCount = codec->getRepetitionCount();
157 if (repetitionCount != rec.fRepetitionCount) {
158 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
159 rec.fName, rec.fRepetitionCount, repetitionCount);
160 }
161
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400162 const int expected = rec.fFrameCount;
163 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700164 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400165 rec.fName, expected - 1, rec.fRequiredFrames.size());
scroggo19b91532016-10-24 09:03:26 -0700166 continue;
167 }
168
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400169 if (expected > 1) {
170 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
171 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
172 rec.fName, expected, rec.fDurations.size());
173 continue;
174 }
175
176 if (rec.fAlphaTypes.size() + 1 != static_cast<size_t>(expected)) {
177 ERRORF(r, "'%s' has wrong number entries in fAlphaTypes; expected: %i\tactual: %i",
178 rec.fName, expected - 1, rec.fAlphaTypes.size());
179 continue;
180 }
scroggo19b91532016-10-24 09:03:26 -0700181 }
182
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400183 enum class TestMode {
184 kVector,
185 kIndividual,
186 };
187
188 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
189 // Re-create the codec to reset state and test parsing.
190 codec.reset(SkCodec::NewFromData(data));
191
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400192 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400193 std::vector<SkCodec::FrameInfo> frameInfos;
194 switch (mode) {
195 case TestMode::kVector:
196 frameInfos = codec->getFrameInfo();
197 // getFrameInfo returns empty set for non-animated.
198 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
199 break;
200 case TestMode::kIndividual:
201 frameCount = codec->getFrameCount();
202 break;
203 }
204
205 if (frameCount != expected) {
206 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
207 rec.fName, expected, frameCount);
208 continue;
209 }
210
211 // From here on, we are only concerned with animated images.
212 if (1 == frameCount) {
213 continue;
214 }
215
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400216 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400217 SkCodec::FrameInfo frameInfo;
218 switch (mode) {
219 case TestMode::kVector:
220 frameInfo = frameInfos[i];
221 break;
222 case TestMode::kIndividual:
223 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
224 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
225 break;
226 }
227
228 if (rec.fDurations[i] != frameInfo.fDuration) {
229 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
230 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
231 }
232
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400233 auto to_string = [](SkAlphaType type) {
234 switch (type) {
235 case kUnpremul_SkAlphaType:
236 return "unpremul";
237 case kOpaque_SkAlphaType:
238 return "opaque";
239 default:
240 return "other";
241 }
242 };
243
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400244 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphaTypes[i-1];
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400245 auto alpha = frameInfo.fAlphaType;
246 if (expectedAlpha != alpha) {
247 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
248 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
249 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400250
251 if (0 == i) {
252 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
253 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
254 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
255 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
256 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400257 }
258
259 if (TestMode::kIndividual == mode) {
260 // No need to test decoding twice.
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400261 continue;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400262 }
263
264 // Compare decoding in two ways:
265 // 1. Provide the frame that a frame depends on, so the codec just has to blend.
266 // (in the array cachedFrames)
267 // 2. Do not provide the frame that a frame depends on, so the codec has to decode
268 // all the way back to a key-frame. (in a local variable uncachedFrame)
269 // The two should look the same.
270 std::vector<SkBitmap> cachedFrames(frameCount);
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400271 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400272
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400273 auto decode = [&](SkBitmap* bm, bool cached, int index) {
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400274 auto decodeInfo = info;
275 if (index > 0) {
276 decodeInfo = info.makeAlphaType(frameInfos[index].fAlphaType);
277 }
278 bm->allocPixels(decodeInfo);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400279 if (cached) {
280 // First copy the pixels from the cached frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400281 const int requiredFrame = frameInfos[index].fRequiredFrame;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400282 if (requiredFrame != SkCodec::kNone) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400283 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
284 cachedFrames[requiredFrame]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400285 REPORTER_ASSERT(r, success);
286 }
287 }
288 SkCodec::Options opts;
289 opts.fFrameIndex = index;
290 opts.fHasPriorFrame = cached;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400291 const auto result = codec->getPixels(decodeInfo, bm->getPixels(), bm->rowBytes(),
292 &opts, nullptr, nullptr);
293 if (result != SkCodec::kSuccess) {
294 ERRORF(r, "Failed to decode frame %i from %s when %scached, error %i",
295 index, rec.fName, (cached ? "" : "not "), result);
296 }
297 return result == SkCodec::kSuccess;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400298 };
299
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400300 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400301 SkBitmap& cachedFrame = cachedFrames[i];
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400302 if (!decode(&cachedFrame, true, i)) {
303 continue;
304 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400305 SkBitmap uncachedFrame;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400306 if (!decode(&uncachedFrame, false, i)) {
307 continue;
308 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400309
310 // Now verify they're equal.
311 const size_t rowLen = info.bytesPerPixel() * info.width();
312 for (int y = 0; y < info.height(); y++) {
313 const void* cachedAddr = cachedFrame.getAddr(0, y);
314 SkASSERT(cachedAddr != nullptr);
315 const void* uncachedAddr = uncachedFrame.getAddr(0, y);
316 SkASSERT(uncachedAddr != nullptr);
317 const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen) == 0;
318 if (!lineMatches) {
319 SkString name = SkStringPrintf("cached_%i", i);
320 write_bm(name.c_str(), cachedFrame);
321 name = SkStringPrintf("uncached_%i", i);
322 write_bm(name.c_str(), uncachedFrame);
323 ERRORF(r, "%s's frame %i is different depending on caching!", rec.fName, i);
324 break;
325 }
326 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500327 }
scroggo19b91532016-10-24 09:03:26 -0700328 }
329 }
330}