blob: 087f5d89cd1ccb2b97d0cb9a5b5bdbd07479aaa4 [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 III557fbbe2017-05-23 09:37:21 -0400126 { "webp-animated.webp", 3, { 0, 1 }, { kOpaque, kOpaque, kOpaque },
127 { 1000, 500, 1000 }, SkCodec::kRepetitionCountInfinite },
128 { "blendBG.webp", 7, { 0, SkCodec::kNone, SkCodec::kNone, SkCodec::kNone,
129 3, 3 },
130 { 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 IIIfc49b402016-10-31 14:08:56 -0400165 rec.fName, expected, rec.fRequiredFrames.size() + 1);
scroggo19b91532016-10-24 09:03:26 -0700166 continue;
167 }
168
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400169 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700170 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
171 rec.fName, expected, rec.fDurations.size());
172 continue;
173 }
174
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400175 enum class TestMode {
176 kVector,
177 kIndividual,
178 };
179
180 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
181 // Re-create the codec to reset state and test parsing.
182 codec.reset(SkCodec::NewFromData(data));
183
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400184 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400185 std::vector<SkCodec::FrameInfo> frameInfos;
186 switch (mode) {
187 case TestMode::kVector:
188 frameInfos = codec->getFrameInfo();
189 // getFrameInfo returns empty set for non-animated.
190 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
191 break;
192 case TestMode::kIndividual:
193 frameCount = codec->getFrameCount();
194 break;
195 }
196
197 if (frameCount != expected) {
198 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
199 rec.fName, expected, frameCount);
200 continue;
201 }
202
203 // From here on, we are only concerned with animated images.
204 if (1 == frameCount) {
205 continue;
206 }
207
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400208 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400209 SkCodec::FrameInfo frameInfo;
210 switch (mode) {
211 case TestMode::kVector:
212 frameInfo = frameInfos[i];
213 break;
214 case TestMode::kIndividual:
215 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
216 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
217 break;
218 }
219
220 if (rec.fDurations[i] != frameInfo.fDuration) {
221 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
222 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
223 }
224
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400225 auto to_string = [](SkAlphaType type) {
226 switch (type) {
227 case kUnpremul_SkAlphaType:
228 return "unpremul";
229 case kOpaque_SkAlphaType:
230 return "opaque";
231 default:
232 return "other";
233 }
234 };
235
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400236 auto expectedAlpha = 0 == i ? codec->getInfo().alphaType() : rec.fAlphaTypes[i-1];
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400237 auto alpha = frameInfo.fAlphaType;
238 if (expectedAlpha != alpha) {
239 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
240 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
241 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400242
243 if (0 == i) {
244 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
245 } else if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
246 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
247 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
248 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400249 }
250
251 if (TestMode::kIndividual == mode) {
252 // No need to test decoding twice.
253 return;
254 }
255
256 // Compare decoding in two ways:
257 // 1. Provide the frame that a frame depends on, so the codec just has to blend.
258 // (in the array cachedFrames)
259 // 2. Do not provide the frame that a frame depends on, so the codec has to decode
260 // all the way back to a key-frame. (in a local variable uncachedFrame)
261 // The two should look the same.
262 std::vector<SkBitmap> cachedFrames(frameCount);
263 const auto& info = codec->getInfo().makeColorType(kN32_SkColorType);
264
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400265 auto decode = [&](SkBitmap* bm, bool cached, int index) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400266 bm->allocPixels(info);
267 if (cached) {
268 // First copy the pixels from the cached frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400269 const int requiredFrame = frameInfos[index].fRequiredFrame;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400270 if (requiredFrame != SkCodec::kNone) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400271 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
272 cachedFrames[requiredFrame]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400273 REPORTER_ASSERT(r, success);
274 }
275 }
276 SkCodec::Options opts;
277 opts.fFrameIndex = index;
278 opts.fHasPriorFrame = cached;
279 auto result = codec->getPixels(info, bm->getPixels(), bm->rowBytes(),
280 &opts, nullptr, nullptr);
281 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
282 };
283
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400284 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400285 SkBitmap& cachedFrame = cachedFrames[i];
286 decode(&cachedFrame, true, i);
287 SkBitmap uncachedFrame;
288 decode(&uncachedFrame, false, i);
289
290 // Now verify they're equal.
291 const size_t rowLen = info.bytesPerPixel() * info.width();
292 for (int y = 0; y < info.height(); y++) {
293 const void* cachedAddr = cachedFrame.getAddr(0, y);
294 SkASSERT(cachedAddr != nullptr);
295 const void* uncachedAddr = uncachedFrame.getAddr(0, y);
296 SkASSERT(uncachedAddr != nullptr);
297 const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen) == 0;
298 if (!lineMatches) {
299 SkString name = SkStringPrintf("cached_%i", i);
300 write_bm(name.c_str(), cachedFrame);
301 name = SkStringPrintf("uncached_%i", i);
302 write_bm(name.c_str(), uncachedFrame);
303 ERRORF(r, "%s's frame %i is different depending on caching!", rec.fName, i);
304 break;
305 }
306 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500307 }
scroggo19b91532016-10-24 09:03:26 -0700308 }
309 }
310}