scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 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 | |
scroggo | 38a2cf5 | 2016-10-24 09:56:40 -0700 | [diff] [blame] | 8 | #include "SkBitmap.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 9 | #include "SkCodec.h" |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 10 | #include "SkCommonFlags.h" |
| 11 | #include "SkImageEncoder.h" |
| 12 | #include "SkOSPath.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 13 | #include "SkStream.h" |
| 14 | |
| 15 | #include "Resources.h" |
| 16 | #include "Test.h" |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame^] | 17 | #include "sk_tool_utils.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 18 | |
| 19 | #include <initializer_list> |
| 20 | #include <vector> |
| 21 | |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 22 | static 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 III | 7d22a33 | 2017-04-12 17:01:26 -0400 | [diff] [blame] | 35 | DEF_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 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 42 | DEF_TEST(Codec_frames, r) { |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 43 | #define kOpaque kOpaque_SkAlphaType |
| 44 | #define kUnpremul kUnpremul_SkAlphaType |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 45 | static const struct { |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 46 | const char* fName; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 47 | int fFrameCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 48 | // One less than fFramecount, since the first frame is always |
| 49 | // independent. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 50 | std::vector<int> fRequiredFrames; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 51 | // Same, since the first frame should match getInfo. |
| 52 | std::vector<SkAlphaType> fAlphaTypes; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 53 | // The size of this one should match fFrameCount for animated, empty |
| 54 | // otherwise. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 55 | std::vector<int> fDurations; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 56 | int fRepetitionCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 57 | } gRecs[] = { |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 58 | { "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 III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 74 | { "randPixelsAnim.gif", 13, |
| 75 | // required frames |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 76 | { 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 III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 79 | // durations |
| 80 | { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 }, |
| 81 | // repetition count |
| 82 | 0 }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 83 | { "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 }, |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 88 | SkCodec::kRepetitionCountInfinite }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 89 | { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 90 | |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 91 | { "arrow.png", 1, {}, {}, {}, 0 }, |
| 92 | { "google_chrome.ico", 1, {}, {}, {}, 0 }, |
| 93 | { "brickwork-texture.jpg", 1, {}, {}, {}, 0 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 94 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 95 | { "dng_with_preview.dng", 1, {}, {}, {}, 0 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 96 | #endif |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 97 | { "mandrill.wbmp", 1, {}, {}, {}, 0 }, |
| 98 | { "randPixels.bmp", 1, {}, {}, {}, 0 }, |
| 99 | { "yellow_rose.webp", 1, {}, {}, {}, 0 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 100 | }; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 101 | #undef kOpaque |
| 102 | #undef kUnpremul |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 103 | |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 104 | for (const auto& rec : gRecs) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 105 | sk_sp<SkData> data(GetResourceAsData(rec.fName)); |
| 106 | if (!data) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 107 | // 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 III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 113 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 114 | if (!codec) { |
| 115 | ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName); |
| 116 | continue; |
| 117 | } |
| 118 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 119 | { |
| 120 | SkCodec::FrameInfo frameInfo; |
| 121 | REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo)); |
| 122 | } |
| 123 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 124 | 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 III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 130 | const int expected = rec.fFrameCount; |
| 131 | if (rec.fRequiredFrames.size() + 1 != static_cast<size_t>(expected)) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 132 | ERRORF(r, "'%s' has wrong number entries in fRequiredFrames; expected: %i\tactual: %i", |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 133 | rec.fName, expected, rec.fRequiredFrames.size() + 1); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 134 | continue; |
| 135 | } |
| 136 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 137 | if (rec.fDurations.size() != static_cast<size_t>(expected)) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 138 | 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 III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 143 | 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 III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 152 | int frameCount; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 153 | 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 III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 176 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 177 | 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 III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 237 | auto decode = [&](SkBitmap* bm, bool cached, int index) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 238 | bm->allocPixels(info); |
| 239 | if (cached) { |
| 240 | // First copy the pixels from the cached frame |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 241 | const int requiredFrame = frameInfos[index].fRequiredFrame; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 242 | if (requiredFrame != SkCodec::kNone) { |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame^] | 243 | const bool success = sk_tool_utils::copy_to(bm, kN32_SkColorType, |
| 244 | cachedFrames[requiredFrame]); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 245 | 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 III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 256 | for (int i = 0; i < frameCount; i++) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 257 | 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 III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 279 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |