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" |
| 17 | |
| 18 | #include <initializer_list> |
| 19 | #include <vector> |
| 20 | |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 21 | static void write_bm(const char* name, const SkBitmap& bm) { |
| 22 | if (FLAGS_writePath.isEmpty()) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | SkString filename = SkOSPath::Join(FLAGS_writePath[0], name); |
| 27 | filename.appendf(".png"); |
| 28 | SkFILEWStream file(filename.c_str()); |
| 29 | if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) { |
| 30 | SkDebugf("failed to write '%s'\n", filename.c_str()); |
| 31 | } |
| 32 | } |
| 33 | |
Leon Scroggins III | 7d22a33 | 2017-04-12 17:01:26 -0400 | [diff] [blame^] | 34 | DEF_TEST(Codec_trunc, r) { |
| 35 | sk_sp<SkData> data(GetResourceAsData("box.gif")); |
| 36 | data = SkData::MakeSubset(data.get(), 0, 23); |
| 37 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); |
| 38 | codec->getFrameInfo(); |
| 39 | } |
| 40 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 41 | DEF_TEST(Codec_frames, r) { |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 42 | #define kOpaque kOpaque_SkAlphaType |
| 43 | #define kUnpremul kUnpremul_SkAlphaType |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 44 | static const struct { |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 45 | const char* fName; |
| 46 | size_t fFrameCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 47 | // One less than fFramecount, since the first frame is always |
| 48 | // independent. |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 49 | std::vector<size_t> fRequiredFrames; |
| 50 | // Same, since the first frame should match getInfo. |
| 51 | std::vector<SkAlphaType> fAlphaTypes; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 52 | // The size of this one should match fFrameCount for animated, empty |
| 53 | // otherwise. |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 54 | std::vector<size_t> fDurations; |
| 55 | int fRepetitionCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 56 | } gRecs[] = { |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 57 | { "alphabetAnim.gif", 13, |
| 58 | { SkCodec::kNone, 0, 0, 0, 0, 5, 6, SkCodec::kNone, |
| 59 | SkCodec::kNone, SkCodec::kNone, 10, 11 }, |
| 60 | { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, |
| 61 | kUnpremul, kUnpremul, kUnpremul, kOpaque, kOpaque, kUnpremul }, |
| 62 | { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }, |
| 63 | 0 }, |
| 64 | { "randPixelsAnim2.gif", 4, |
| 65 | // required frames |
| 66 | { 0, 0, 1 }, |
| 67 | // alphas |
| 68 | { kOpaque, kOpaque, kOpaque }, |
| 69 | // durations |
| 70 | { 0, 1000, 170, 40 }, |
| 71 | // repetition count |
| 72 | 0 }, |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 73 | { "randPixelsAnim.gif", 13, |
| 74 | // required frames |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 75 | { SkCodec::kNone, 1, 2, 3, 4, 3, 6, 7, 7, 7, 9, 9 }, |
| 76 | { kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, |
| 77 | kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul, kUnpremul }, |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 78 | // durations |
| 79 | { 0, 1000, 170, 40, 220, 7770, 90, 90, 90, 90, 90, 90, 90 }, |
| 80 | // repetition count |
| 81 | 0 }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 82 | { "box.gif", 1, {}, {}, {}, 0 }, |
| 83 | { "color_wheel.gif", 1, {}, {}, {}, 0 }, |
| 84 | { "test640x479.gif", 4, { 0, 1, 2 }, |
| 85 | { kOpaque, kOpaque, kOpaque }, |
| 86 | { 200, 200, 200, 200 }, |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 87 | SkCodec::kRepetitionCountInfinite }, |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 88 | { "colorTables.gif", 2, { 0 }, { kOpaque }, { 1000, 1000 }, 5 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 89 | |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 90 | { "arrow.png", 1, {}, {}, {}, 0 }, |
| 91 | { "google_chrome.ico", 1, {}, {}, {}, 0 }, |
| 92 | { "brickwork-texture.jpg", 1, {}, {}, {}, 0 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 93 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 94 | { "dng_with_preview.dng", 1, {}, {}, {}, 0 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 95 | #endif |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 96 | { "mandrill.wbmp", 1, {}, {}, {}, 0 }, |
| 97 | { "randPixels.bmp", 1, {}, {}, {}, 0 }, |
| 98 | { "yellow_rose.webp", 1, {}, {}, {}, 0 }, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 99 | }; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 100 | #undef kOpaque |
| 101 | #undef kUnpremul |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 102 | |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 103 | for (const auto& rec : gRecs) { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 104 | sk_sp<SkData> data(GetResourceAsData(rec.fName)); |
| 105 | if (!data) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 106 | // Useful error statement, but sometimes people run tests without |
| 107 | // resources, and they do not want to see these messages. |
| 108 | //ERRORF(r, "Missing resources? Could not find '%s'", rec.fName); |
| 109 | continue; |
| 110 | } |
| 111 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 112 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 113 | if (!codec) { |
| 114 | ERRORF(r, "Failed to create an SkCodec from '%s'", rec.fName); |
| 115 | continue; |
| 116 | } |
| 117 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 118 | { |
| 119 | SkCodec::FrameInfo frameInfo; |
| 120 | REPORTER_ASSERT(r, !codec->getFrameInfo(0, &frameInfo)); |
| 121 | } |
| 122 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 123 | const int repetitionCount = codec->getRepetitionCount(); |
| 124 | if (repetitionCount != rec.fRepetitionCount) { |
| 125 | ERRORF(r, "%s repetition count does not match! expected: %i\tactual: %i", |
| 126 | rec.fName, rec.fRepetitionCount, repetitionCount); |
| 127 | } |
| 128 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 129 | const size_t expected = rec.fFrameCount; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 130 | if (rec.fRequiredFrames.size() + 1 != expected) { |
| 131 | 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] | 132 | rec.fName, expected, rec.fRequiredFrames.size() + 1); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 133 | continue; |
| 134 | } |
| 135 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 136 | if (rec.fDurations.size() != expected) { |
| 137 | ERRORF(r, "'%s' has wrong number entries in fDurations; expected: %i\tactual: %i", |
| 138 | rec.fName, expected, rec.fDurations.size()); |
| 139 | continue; |
| 140 | } |
| 141 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 142 | enum class TestMode { |
| 143 | kVector, |
| 144 | kIndividual, |
| 145 | }; |
| 146 | |
| 147 | for (auto mode : { TestMode::kVector, TestMode::kIndividual }) { |
| 148 | // Re-create the codec to reset state and test parsing. |
| 149 | codec.reset(SkCodec::NewFromData(data)); |
| 150 | |
| 151 | size_t frameCount; |
| 152 | std::vector<SkCodec::FrameInfo> frameInfos; |
| 153 | switch (mode) { |
| 154 | case TestMode::kVector: |
| 155 | frameInfos = codec->getFrameInfo(); |
| 156 | // getFrameInfo returns empty set for non-animated. |
| 157 | frameCount = frameInfos.empty() ? 1 : frameInfos.size(); |
| 158 | break; |
| 159 | case TestMode::kIndividual: |
| 160 | frameCount = codec->getFrameCount(); |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (frameCount != expected) { |
| 165 | ERRORF(r, "'%s' expected frame count: %i\tactual: %i", |
| 166 | rec.fName, expected, frameCount); |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | // From here on, we are only concerned with animated images. |
| 171 | if (1 == frameCount) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | for (size_t i = 0; i < frameCount; i++) { |
| 176 | SkCodec::FrameInfo frameInfo; |
| 177 | switch (mode) { |
| 178 | case TestMode::kVector: |
| 179 | frameInfo = frameInfos[i]; |
| 180 | break; |
| 181 | case TestMode::kIndividual: |
| 182 | REPORTER_ASSERT(r, codec->getFrameInfo(i, nullptr)); |
| 183 | REPORTER_ASSERT(r, codec->getFrameInfo(i, &frameInfo)); |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | if (rec.fDurations[i] != frameInfo.fDuration) { |
| 188 | ERRORF(r, "%s frame %i's durations do not match! expected: %i\tactual: %i", |
| 189 | rec.fName, i, rec.fDurations[i], frameInfo.fDuration); |
| 190 | } |
| 191 | |
| 192 | if (0 == i) { |
| 193 | REPORTER_ASSERT(r, frameInfo.fRequiredFrame == SkCodec::kNone); |
| 194 | REPORTER_ASSERT(r, frameInfo.fAlphaType == codec->getInfo().alphaType()); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | if (rec.fRequiredFrames[i-1] != frameInfo.fRequiredFrame) { |
| 199 | ERRORF(r, "%s's frame %i has wrong dependency! expected: %i\tactual: %i", |
| 200 | rec.fName, i, rec.fRequiredFrames[i-1], frameInfo.fRequiredFrame); |
| 201 | } |
| 202 | |
| 203 | auto to_string = [](SkAlphaType type) { |
| 204 | switch (type) { |
| 205 | case kUnpremul_SkAlphaType: |
| 206 | return "unpremul"; |
| 207 | case kOpaque_SkAlphaType: |
| 208 | return "opaque"; |
| 209 | default: |
| 210 | return "other"; |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | auto expectedAlpha = rec.fAlphaTypes[i-1]; |
| 215 | auto alpha = frameInfo.fAlphaType; |
| 216 | if (expectedAlpha != alpha) { |
| 217 | ERRORF(r, "%s's frame %i has wrong alpha type! expected: %s\tactual: %s", |
| 218 | rec.fName, i, to_string(expectedAlpha), to_string(alpha)); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (TestMode::kIndividual == mode) { |
| 223 | // No need to test decoding twice. |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Compare decoding in two ways: |
| 228 | // 1. Provide the frame that a frame depends on, so the codec just has to blend. |
| 229 | // (in the array cachedFrames) |
| 230 | // 2. Do not provide the frame that a frame depends on, so the codec has to decode |
| 231 | // all the way back to a key-frame. (in a local variable uncachedFrame) |
| 232 | // The two should look the same. |
| 233 | std::vector<SkBitmap> cachedFrames(frameCount); |
| 234 | const auto& info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 235 | |
| 236 | auto decode = [&](SkBitmap* bm, bool cached, size_t index) { |
| 237 | bm->allocPixels(info); |
| 238 | if (cached) { |
| 239 | // First copy the pixels from the cached frame |
| 240 | const size_t requiredFrame = frameInfos[index].fRequiredFrame; |
| 241 | if (requiredFrame != SkCodec::kNone) { |
| 242 | const bool success = cachedFrames[requiredFrame].copyTo(bm); |
| 243 | REPORTER_ASSERT(r, success); |
| 244 | } |
| 245 | } |
| 246 | SkCodec::Options opts; |
| 247 | opts.fFrameIndex = index; |
| 248 | opts.fHasPriorFrame = cached; |
| 249 | auto result = codec->getPixels(info, bm->getPixels(), bm->rowBytes(), |
| 250 | &opts, nullptr, nullptr); |
| 251 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 252 | }; |
| 253 | |
| 254 | for (size_t i = 0; i < frameCount; i++) { |
| 255 | SkBitmap& cachedFrame = cachedFrames[i]; |
| 256 | decode(&cachedFrame, true, i); |
| 257 | SkBitmap uncachedFrame; |
| 258 | decode(&uncachedFrame, false, i); |
| 259 | |
| 260 | // Now verify they're equal. |
| 261 | const size_t rowLen = info.bytesPerPixel() * info.width(); |
| 262 | for (int y = 0; y < info.height(); y++) { |
| 263 | const void* cachedAddr = cachedFrame.getAddr(0, y); |
| 264 | SkASSERT(cachedAddr != nullptr); |
| 265 | const void* uncachedAddr = uncachedFrame.getAddr(0, y); |
| 266 | SkASSERT(uncachedAddr != nullptr); |
| 267 | const bool lineMatches = memcmp(cachedAddr, uncachedAddr, rowLen) == 0; |
| 268 | if (!lineMatches) { |
| 269 | SkString name = SkStringPrintf("cached_%i", i); |
| 270 | write_bm(name.c_str(), cachedFrame); |
| 271 | name = SkStringPrintf("uncached_%i", i); |
| 272 | write_bm(name.c_str(), uncachedFrame); |
| 273 | ERRORF(r, "%s's frame %i is different depending on caching!", rec.fName, i); |
| 274 | break; |
| 275 | } |
| 276 | } |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 277 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |