blob: 24b9c13ae901c3549a6d6be797d31663e1e1e52b [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"));
37 data = SkData::MakeSubset(data.get(), 0, 23);
38 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
39 codec->getFrameInfo();
40}
41
scroggo19b91532016-10-24 09:03:26 -070042DEF_TEST(Codec_frames, r) {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040043 #define kOpaque kOpaque_SkAlphaType
44 #define kUnpremul kUnpremul_SkAlphaType
scroggo19b91532016-10-24 09:03:26 -070045 static const struct {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040046 const char* fName;
Leon Scroggins III249b8e32017-04-17 12:46:33 -040047 int fFrameCount;
scroggo19b91532016-10-24 09:03:26 -070048 // One less than fFramecount, since the first frame is always
49 // independent.
Leon Scroggins III249b8e32017-04-17 12:46:33 -040050 std::vector<int> fRequiredFrames;
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040051 // Same, since the first frame should match getInfo.
52 std::vector<SkAlphaType> fAlphaTypes;
scroggo19b91532016-10-24 09:03:26 -070053 // The size of this one should match fFrameCount for animated, empty
54 // otherwise.
Leon Scroggins III249b8e32017-04-17 12:46:33 -040055 std::vector<int> fDurations;
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040056 int fRepetitionCount;
scroggo19b91532016-10-24 09:03:26 -070057 } gRecs[] = {
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040058 { "alphabetAnim.gif", 13,
59 { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone,
60 SkCodec::kNone, SkCodec::kNone, 10, 11 },
61 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
62 kUnpremul, kUnpremul, kUnpremul, kOpaque, kOpaque, kUnpremul },
63 { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 },
64 0 },
65 { "randPixelsAnim2.gif", 4,
66 // required frames
67 { 0, 0, 1 },
68 // alphas
69 { kOpaque, kOpaque, kOpaque },
70 // durations
71 { 0, 1000, 170, 40 },
72 // repetition count
73 0 },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050074 { "randPixelsAnim.gif", 13,
75 // required frames
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040076 { SkCodec::kNone, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 },
77 { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul,
78 kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul },
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -050079 // durations
80 { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 },
81 // repetition count
82 0 },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040083 { "box.gif", 1, {}, {}, {}, 0 },
84 { "color_wheel.gif", 1, {}, {}, {}, 0 },
85 { "test640x479.gif", 4, { 0, 1, 2 },
86 { kOpaque, kOpaque, kOpaque },
87 { 200, 200, 200, 200 },
scroggoe71b1a12016-11-01 08:28:28 -070088 SkCodec::kRepetitionCountInfinite },
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040089 { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5 },
scroggo19b91532016-10-24 09:03:26 -070090
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040091 { "arrow.png", 1, {}, {}, {}, 0 },
92 { "google_chrome.ico", 1, {}, {}, {}, 0 },
93 { "brickwork-texture.jpg", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -070094#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040095 { "dng_with_preview.dng", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -070096#endif
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -040097 { "mandrill.wbmp", 1, {}, {}, {}, 0 },
98 { "randPixels.bmp", 1, {}, {}, {}, 0 },
99 { "yellow_rose.webp", 1, {}, {}, {}, 0 },
scroggo19b91532016-10-24 09:03:26 -0700100 };
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400101 #undef kOpaque
102 #undef kUnpremul
scroggo19b91532016-10-24 09:03:26 -0700103
Leon Scroggins IIIa4db9be2017-04-11 10:32:02 -0400104 for (const auto& rec : gRecs) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400105 sk_sp<SkData> data(GetResourceAsData(rec.fName));
106 if (!data) {
scroggo19b91532016-10-24 09:03:26 -0700107 // Useful error statement, but sometimes people run tests without
108 // resources, and they do not want to see these messages.
109 //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName);
110 continue;
111 }
112
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400113 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggo19b91532016-10-24 09:03:26 -0700114 if (!codec) {
115 ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName);
116 continue;
117 }
118
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400119 {
120 SkCodec::FrameInfo frameInfo;
121 REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo));
122 }
123
scroggoe71b1a12016-11-01 08:28:28 -0700124 const int repetitionCount = codec->getRepetitionCount();
125 if (repetitionCount != rec.fRepetitionCount) {
126 ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i",
127 rec.fName, rec.fRepetitionCount, repetitionCount);
128 }
129
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400130 const int expected = rec.fFrameCount;
131 if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700132 ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i",
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400133 rec.fName, expected, rec.fRequiredFrames.size() + 1);
scroggo19b91532016-10-24 09:03:26 -0700134 continue;
135 }
136
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400137 if (rec.fDurations.size() != static_cast<size_t>(expected)) {
scroggo19b91532016-10-24 09:03:26 -0700138 ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i",
139 rec.fName, expected, rec.fDurations.size());
140 continue;
141 }
142
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400143 enum class TestMode {
144 kVector,
145 kIndividual,
146 };
147
148 for (auto mode : { TestMode::kVector, TestMode::kIndividual }) {
149 // Re-create the codec to reset state and test parsing.
150 codec.reset(SkCodec::NewFromData(data));
151
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400152 int frameCount;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400153 std::vector<SkCodec::FrameInfo> frameInfos;
154 switch (mode) {
155 case TestMode::kVector:
156 frameInfos = codec->getFrameInfo();
157 // getFrameInfo returns empty set for non-animated.
158 frameCount = frameInfos.empty() ? 1 : frameInfos.size();
159 break;
160 case TestMode::kIndividual:
161 frameCount = codec->getFrameCount();
162 break;
163 }
164
165 if (frameCount != expected) {
166 ERRORF(r, "'%s' expected frame count: %i\tactual: %i",
167 rec.fName, expected, frameCount);
168 continue;
169 }
170
171 // From here on, we are only concerned with animated images.
172 if (1 == frameCount) {
173 continue;
174 }
175
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400176 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400177 SkCodec::FrameInfo frameInfo;
178 switch (mode) {
179 case TestMode::kVector:
180 frameInfo = frameInfos[i];
181 break;
182 case TestMode::kIndividual:
183 REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr));
184 REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo));
185 break;
186 }
187
188 if (rec.fDurations[i] != frameInfo.fDuration) {
189 ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i",
190 rec.fName, i, rec.fDurations[i], frameInfo.fDuration);
191 }
192
193 if (0 == i) {
194 REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone);
195 REPORTER_ASSERT(r, frameInfo.fAlphaType == codec->getInfo().alphaType());
196 continue;
197 }
198
199 if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) {
200 ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i",
201 rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame);
202 }
203
204 auto to_string = [](SkAlphaType type) {
205 switch (type) {
206 case kUnpremul_SkAlphaType:
207 return "unpremul";
208 case kOpaque_SkAlphaType:
209 return "opaque";
210 default:
211 return "other";
212 }
213 };
214
215 auto expectedAlpha = rec.fAlphaTypes[i-1];
216 auto alpha = frameInfo.fAlphaType;
217 if (expectedAlpha != alpha) {
218 ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s",
219 rec.fName, i, to_string(expectedAlpha), to_string(alpha));
220 }
221 }
222
223 if (TestMode::kIndividual == mode) {
224 // No need to test decoding twice.
225 return;
226 }
227
228 // Compare decoding in two ways:
229 // 1. Provide the frame that a frame depends on, so the codec just has to blend.
230 // (in the array cachedFrames)
231 // 2. Do not provide the frame that a frame depends on, so the codec has to decode
232 // all the way back to a key-frame. (in a local variable uncachedFrame)
233 // The two should look the same.
234 std::vector<SkBitmap> cachedFrames(frameCount);
235 const auto& info = codec->getInfo().makeColorType(kN32_SkColorType);
236
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400237 auto decode = [&](SkBitmap* bm, bool cached, int index) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400238 bm->allocPixels(info);
239 if (cached) {
240 // First copy the pixels from the cached frame
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400241 const int requiredFrame = frameInfos[index].fRequiredFrame;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400242 if (requiredFrame != SkCodec::kNone) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400243 const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType,
244 cachedFrames[requiredFrame]);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400245 REPORTER_ASSERT(r, success);
246 }
247 }
248 SkCodec::Options opts;
249 opts.fFrameIndex = index;
250 opts.fHasPriorFrame = cached;
251 auto result = codec->getPixels(info, bm->getPixels(), bm->rowBytes(),
252 &opts, nullptr, nullptr);
253 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
254 };
255
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400256 for (int i = 0; i < frameCount; i++) {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400257 SkBitmap& cachedFrame = cachedFrames[i];
258 decode(&cachedFrame, true, i);
259 SkBitmap uncachedFrame;
260 decode(&uncachedFrame, false, i);
261
262 // Now verify they're equal.
263 const size_t rowLen = info.bytesPerPixel() * info.width();
264 for (int y = 0; y < info.height(); y++) {
265 const void* cachedAddr = cachedFrame.getAddr(0, y);
266 SkASSERT(cachedAddr != nullptr);
267 const void* uncachedAddr = uncachedFrame.getAddr(0, y);
268 SkASSERT(uncachedAddr != nullptr);
269 const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen) == 0;
270 if (!lineMatches) {
271 SkString name = SkStringPrintf("cached_%i", i);
272 write_bm(name.c_str(), cachedFrame);
273 name = SkStringPrintf("uncached_%i", i);
274 write_bm(name.c_str(), uncachedFrame);
275 ERRORF(r, "%s's frame %i is different depending on caching!", rec.fName, i);
276 break;
277 }
278 }
Leon Scroggins IIIb0b625b2016-12-22 16:40:24 -0500279 }
scroggo19b91532016-10-24 09:03:26 -0700280 }
281 }
282}