scroggo | 8e6c7ad | 2016-09-16 08:20:38 -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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/codec/SkCodec.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkData.h" |
| 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/core/SkStream.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 14 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tests/CodecPriv.h" |
| 17 | #include "tests/FakeStreams.h" |
| 18 | #include "tests/Test.h" |
| 19 | #include "tools/Resources.h" |
Ben Wagner | 1a462bd | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 20 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 21 | #include <cstring> |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 22 | #include <initializer_list> |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <utility> |
| 25 | #include <vector> |
| 26 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 27 | static SkImageInfo standardize_info(SkCodec* codec) { |
| 28 | SkImageInfo defaultInfo = codec->getInfo(); |
| 29 | // Note: This drops the SkColorSpace, allowing the equality check between two |
| 30 | // different codecs created from the same file to have the same SkImageInfo. |
| 31 | return SkImageInfo::MakeN32Premul(defaultInfo.width(), defaultInfo.height()); |
| 32 | } |
| 33 | |
| 34 | static bool create_truth(sk_sp<SkData> data, SkBitmap* dst) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 35 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(data))); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 36 | if (!codec) { |
| 37 | return false; |
| 38 | } |
| 39 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 40 | const SkImageInfo info = standardize_info(codec.get()); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 41 | dst->allocPixels(info); |
| 42 | return SkCodec::kSuccess == codec->getPixels(info, dst->getPixels(), dst->rowBytes()); |
| 43 | } |
| 44 | |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 45 | static bool compare_bitmaps(skiatest::Reporter* r, const SkBitmap& bm1, const SkBitmap& bm2) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 46 | const SkImageInfo& info = bm1.info(); |
| 47 | if (info != bm2.info()) { |
| 48 | ERRORF(r, "Bitmaps have different image infos!"); |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 49 | return false; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 50 | } |
| 51 | const size_t rowBytes = info.minRowBytes(); |
| 52 | for (int i = 0; i < info.height(); i++) { |
nagarajan.n | 0ec0bf0 | 2017-10-11 10:41:27 +0530 | [diff] [blame] | 53 | if (memcmp(bm1.getAddr(0, i), bm2.getAddr(0, i), rowBytes)) { |
| 54 | ERRORF(r, "Bitmaps have different pixels, starting on line %i!", i); |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 55 | return false; |
nagarajan.n | 0ec0bf0 | 2017-10-11 10:41:27 +0530 | [diff] [blame] | 56 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 57 | } |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 58 | |
| 59 | return true; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 62 | static void test_partial(skiatest::Reporter* r, const char* name, const sk_sp<SkData>& file, |
| 63 | size_t minBytes, size_t increment) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 64 | SkBitmap truth; |
| 65 | if (!create_truth(file, &truth)) { |
| 66 | ERRORF(r, "Failed to decode %s\n", name); |
| 67 | return; |
| 68 | } |
| 69 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 70 | // Now decode part of the file |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 71 | HaltingStream* stream = new HaltingStream(file, minBytes); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 72 | |
| 73 | // Note that we cheat and hold on to a pointer to stream, though it is owned by |
| 74 | // partialCodec. |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 75 | auto partialCodec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream)); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 76 | if (!partialCodec) { |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 77 | ERRORF(r, "Failed to create codec for %s with %zu bytes", name, minBytes); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 81 | const SkImageInfo info = standardize_info(partialCodec.get()); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 82 | SkASSERT(info == truth.info()); |
| 83 | SkBitmap incremental; |
| 84 | incremental.allocPixels(info); |
| 85 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 86 | while (true) { |
| 87 | const SkCodec::Result startResult = partialCodec->startIncrementalDecode(info, |
| 88 | incremental.getPixels(), incremental.rowBytes()); |
| 89 | if (startResult == SkCodec::kSuccess) { |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | if (stream->isAllDataReceived()) { |
| 94 | ERRORF(r, "Failed to start incremental decode\n"); |
| 95 | return; |
| 96 | } |
| 97 | |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 98 | stream->addNewData(increment); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | while (true) { |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 102 | // This imitates how Chromium calls getFrameCount before resuming a decode. |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 103 | partialCodec->getFrameCount(); |
Nigel Tao | 1f1cd1f | 2019-06-22 17:35:38 +1000 | [diff] [blame] | 104 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 105 | const SkCodec::Result result = partialCodec->incrementalDecode(); |
| 106 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 107 | if (result == SkCodec::kSuccess) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 111 | REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); |
| 112 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 113 | if (stream->isAllDataReceived()) { |
| 114 | ERRORF(r, "Failed to completely decode %s", name); |
| 115 | return; |
| 116 | } |
| 117 | |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 118 | stream->addNewData(increment); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // compare to original |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 122 | compare_bitmaps(r, truth, incremental); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 125 | static void test_partial(skiatest::Reporter* r, const char* name, size_t minBytes = 0) { |
| 126 | sk_sp<SkData> file = GetResourceAsData(name); |
| 127 | if (!file) { |
| 128 | SkDebugf("missing resource %s\n", name); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // This size is arbitrary, but deliberately different from the buffer size used by SkPngCodec. |
| 133 | constexpr size_t kIncrement = 1000; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 134 | test_partial(r, name, file, std::max(file->size() / 2, minBytes), kIncrement); |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 135 | } |
| 136 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 137 | DEF_TEST(Codec_partial, r) { |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 138 | #if 0 |
| 139 | // FIXME (scroggo): SkPngCodec needs to use SkStreamBuffer in order to |
| 140 | // support incremental decoding. |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 141 | test_partial(r, "images/plane.png"); |
| 142 | test_partial(r, "images/plane_interlaced.png"); |
| 143 | test_partial(r, "images/yellow_rose.png"); |
| 144 | test_partial(r, "images/index8.png"); |
| 145 | test_partial(r, "images/color_wheel.png"); |
| 146 | test_partial(r, "images/mandrill_256.png"); |
| 147 | test_partial(r, "images/mandrill_32.png"); |
| 148 | test_partial(r, "images/arrow.png"); |
| 149 | test_partial(r, "images/randPixels.png"); |
| 150 | test_partial(r, "images/baby_tux.png"); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 151 | #endif |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 152 | test_partial(r, "images/box.gif"); |
| 153 | test_partial(r, "images/randPixels.gif", 215); |
| 154 | test_partial(r, "images/color_wheel.gif"); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Leon Scroggins III | 762ff97 | 2018-12-13 10:34:38 -0500 | [diff] [blame] | 157 | DEF_TEST(Codec_partialWuffs, r) { |
| 158 | const char* path = "images/alphabetAnim.gif"; |
| 159 | auto file = GetResourceAsData(path); |
| 160 | if (!file) { |
| 161 | ERRORF(r, "missing %s", path); |
| 162 | } else { |
| 163 | // This is the end of the first frame. SkCodec will treat this as a |
| 164 | // single frame gif. |
| 165 | file = SkData::MakeSubset(file.get(), 0, 153); |
| 166 | // Start with 100 to get a partial decode, then add the rest of the |
| 167 | // first frame to decode a full image. |
| 168 | test_partial(r, path, file, 100, 53); |
| 169 | } |
| 170 | } |
| 171 | |
Nigel Tao | a1cc9f6 | 2019-10-24 15:02:05 +1100 | [diff] [blame] | 172 | DEF_TEST(Codec_frameCountUpdatesInIncrementalDecode, r) { |
| 173 | sk_sp<SkData> file = GetResourceAsData("images/colorTables.gif"); |
| 174 | size_t fileSize = file->size(); |
| 175 | REPORTER_ASSERT(r, fileSize == 2829); |
| 176 | std::unique_ptr<SkCodec> fullCodec(SkCodec::MakeFromData(file)); |
| 177 | REPORTER_ASSERT(r, fullCodec->getFrameCount() == 2); |
| 178 | const SkImageInfo info = standardize_info(fullCodec.get()); |
| 179 | |
| 180 | static const size_t n = 1000; |
| 181 | HaltingStream* stream = new HaltingStream(file, n); |
| 182 | // Note that we cheat and hold on to a pointer to stream, though it is owned by |
| 183 | // partialCodec. |
| 184 | auto partialCodec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream)); |
| 185 | REPORTER_ASSERT(r, partialCodec->getFrameCount() == 1); |
| 186 | |
| 187 | SkBitmap bitmap; |
| 188 | bitmap.allocPixels(info); |
| 189 | REPORTER_ASSERT(r, SkCodec::kSuccess == |
| 190 | partialCodec->startIncrementalDecode( |
| 191 | info, bitmap.getPixels(), bitmap.rowBytes())); |
| 192 | REPORTER_ASSERT(r, SkCodec::kIncompleteInput == |
| 193 | partialCodec->incrementalDecode()); |
| 194 | |
| 195 | REPORTER_ASSERT(r, partialCodec->getFrameCount() == 1); |
| 196 | stream->addNewData(fileSize - n); |
| 197 | REPORTER_ASSERT(r, partialCodec->getFrameCount() == 2); |
| 198 | } |
Nigel Tao | a1cc9f6 | 2019-10-24 15:02:05 +1100 | [diff] [blame] | 199 | |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 200 | // Verify that when decoding an animated gif byte by byte we report the correct |
| 201 | // fRequiredFrame as soon as getFrameInfo reports the frame. |
| 202 | DEF_TEST(Codec_requiredFrame, r) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 203 | auto path = "images/colorTables.gif"; |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 204 | sk_sp<SkData> file = GetResourceAsData(path); |
| 205 | if (!file) { |
| 206 | return; |
| 207 | } |
| 208 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 209 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file)); |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 210 | if (!codec) { |
| 211 | ERRORF(r, "Failed to create codec from %s", path); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | auto frameInfo = codec->getFrameInfo(); |
| 216 | if (frameInfo.size() <= 1) { |
| 217 | ERRORF(r, "Test is uninteresting with 0 or 1 frames"); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | HaltingStream* stream(nullptr); |
| 222 | std::unique_ptr<SkCodec> partialCodec(nullptr); |
| 223 | for (size_t i = 0; !partialCodec; i++) { |
| 224 | if (file->size() == i) { |
| 225 | ERRORF(r, "Should have created a partial codec for %s", path); |
| 226 | return; |
| 227 | } |
| 228 | stream = new HaltingStream(file, i); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 229 | partialCodec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream)); |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | std::vector<SkCodec::FrameInfo> partialInfo; |
| 233 | size_t frameToCompare = 0; |
Nigel Tao | afea9c3 | 2018-08-01 15:00:32 +1000 | [diff] [blame] | 234 | while (true) { |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 235 | partialInfo = partialCodec->getFrameInfo(); |
| 236 | for (; frameToCompare < partialInfo.size(); frameToCompare++) { |
| 237 | REPORTER_ASSERT(r, partialInfo[frameToCompare].fRequiredFrame |
| 238 | == frameInfo[frameToCompare].fRequiredFrame); |
| 239 | } |
| 240 | |
| 241 | if (frameToCompare == frameInfo.size()) { |
| 242 | break; |
| 243 | } |
Nigel Tao | afea9c3 | 2018-08-01 15:00:32 +1000 | [diff] [blame] | 244 | |
| 245 | if (stream->getLength() == file->size()) { |
| 246 | ERRORF(r, "Should have found all frames for %s", path); |
| 247 | return; |
| 248 | } |
| 249 | stream->addNewData(1); |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 253 | DEF_TEST(Codec_partialAnim, r) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 254 | auto path = "images/test640x479.gif"; |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 255 | sk_sp<SkData> file = GetResourceAsData(path); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 256 | if (!file) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // This stream will be owned by fullCodec, but we hang on to the pointer |
| 261 | // to determine frame offsets. |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 262 | std::unique_ptr<SkCodec> fullCodec(SkCodec::MakeFromStream(std::make_unique<SkMemoryStream>(file))); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 263 | const auto info = standardize_info(fullCodec.get()); |
| 264 | |
| 265 | // frameByteCounts stores the number of bytes to decode a particular frame. |
| 266 | // - [0] is the number of bytes for the header |
| 267 | // - frames[i] requires frameByteCounts[i+1] bytes to decode |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 268 | const std::vector<size_t> frameByteCounts = { 455, 69350, 1344, 1346, 1327 }; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 269 | std::vector<SkBitmap> frames; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 270 | for (size_t i = 0; true; i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 271 | SkBitmap frame; |
| 272 | frame.allocPixels(info); |
| 273 | |
| 274 | SkCodec::Options opts; |
| 275 | opts.fFrameIndex = i; |
| 276 | const SkCodec::Result result = fullCodec->getPixels(info, frame.getPixels(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 277 | frame.rowBytes(), &opts); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 278 | |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 279 | if (result == SkCodec::kIncompleteInput || result == SkCodec::kInvalidInput) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 280 | // We need to distinguish between a partial frame and no more frames. |
| 281 | // getFrameInfo lets us do this, since it tells the number of frames |
| 282 | // not considering whether they are complete. |
| 283 | // FIXME: Should we use a different Result? |
| 284 | if (fullCodec->getFrameInfo().size() > i) { |
| 285 | // This is a partial frame. |
| 286 | frames.push_back(frame); |
| 287 | } |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | if (result != SkCodec::kSuccess) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 292 | ERRORF(r, "Failed to decode frame %zu from %s", i, path); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
| 296 | frames.push_back(frame); |
| 297 | } |
| 298 | |
| 299 | // Now decode frames partially, then completely, and compare to the original. |
| 300 | HaltingStream* haltingStream = new HaltingStream(file, frameByteCounts[0]); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 301 | std::unique_ptr<SkCodec> partialCodec(SkCodec::MakeFromStream( |
| 302 | std::unique_ptr<SkStream>(haltingStream))); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 303 | if (!partialCodec) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 304 | ERRORF(r, "Failed to create a partial codec from %s with %zu bytes out of %zu", |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 305 | path, frameByteCounts[0], file->size()); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | SkASSERT(frameByteCounts.size() > frames.size()); |
| 310 | for (size_t i = 0; i < frames.size(); i++) { |
| 311 | const size_t fullFrameBytes = frameByteCounts[i + 1]; |
| 312 | const size_t firstHalf = fullFrameBytes / 2; |
| 313 | const size_t secondHalf = fullFrameBytes - firstHalf; |
| 314 | |
| 315 | haltingStream->addNewData(firstHalf); |
Leon Scroggins III | 3639faa | 2016-12-08 11:38:58 -0500 | [diff] [blame] | 316 | auto frameInfo = partialCodec->getFrameInfo(); |
| 317 | REPORTER_ASSERT(r, frameInfo.size() == i + 1); |
| 318 | REPORTER_ASSERT(r, !frameInfo[i].fFullyReceived); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 319 | |
| 320 | SkBitmap frame; |
| 321 | frame.allocPixels(info); |
| 322 | |
| 323 | SkCodec::Options opts; |
| 324 | opts.fFrameIndex = i; |
| 325 | SkCodec::Result result = partialCodec->startIncrementalDecode(info, |
| 326 | frame.getPixels(), frame.rowBytes(), &opts); |
| 327 | if (result != SkCodec::kSuccess) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 328 | ERRORF(r, "Failed to start incremental decode for %s on frame %zu", |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 329 | path, i); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | result = partialCodec->incrementalDecode(); |
| 334 | REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result); |
| 335 | |
| 336 | haltingStream->addNewData(secondHalf); |
| 337 | result = partialCodec->incrementalDecode(); |
| 338 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 339 | |
Leon Scroggins III | 3639faa | 2016-12-08 11:38:58 -0500 | [diff] [blame] | 340 | frameInfo = partialCodec->getFrameInfo(); |
| 341 | REPORTER_ASSERT(r, frameInfo.size() == i + 1); |
| 342 | REPORTER_ASSERT(r, frameInfo[i].fFullyReceived); |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 343 | if (!compare_bitmaps(r, frames[i], frame)) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 344 | ERRORF(r, "\tfailure was on frame %zu", i); |
| 345 | SkString name = SkStringPrintf("expected_%zu", i); |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 346 | write_bm(name.c_str(), frames[i]); |
| 347 | |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 348 | name = SkStringPrintf("actual_%zu", i); |
Leon Scroggins III | cb6b884 | 2018-12-04 13:55:13 -0500 | [diff] [blame] | 349 | write_bm(name.c_str(), frame); |
| 350 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 351 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Test that calling getPixels when an incremental decode has been |
| 355 | // started (but not finished) makes the next call to incrementalDecode |
| 356 | // require a call to startIncrementalDecode. |
| 357 | static void test_interleaved(skiatest::Reporter* r, const char* name) { |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 358 | sk_sp<SkData> file = GetResourceAsData(name); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 359 | if (!file) { |
| 360 | return; |
| 361 | } |
| 362 | const size_t halfSize = file->size() / 2; |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 363 | std::unique_ptr<SkCodec> partialCodec(SkCodec::MakeFromStream( |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 364 | std::make_unique<HaltingStream>(std::move(file), halfSize))); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 365 | if (!partialCodec) { |
| 366 | ERRORF(r, "Failed to create codec for %s", name); |
| 367 | return; |
| 368 | } |
| 369 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 370 | const SkImageInfo info = standardize_info(partialCodec.get()); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 371 | SkBitmap incremental; |
| 372 | incremental.allocPixels(info); |
| 373 | |
| 374 | const SkCodec::Result startResult = partialCodec->startIncrementalDecode(info, |
| 375 | incremental.getPixels(), incremental.rowBytes()); |
| 376 | if (startResult != SkCodec::kSuccess) { |
| 377 | ERRORF(r, "Failed to start incremental decode\n"); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | SkCodec::Result result = partialCodec->incrementalDecode(); |
| 382 | REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); |
| 383 | |
| 384 | SkBitmap full; |
| 385 | full.allocPixels(info); |
| 386 | result = partialCodec->getPixels(info, full.getPixels(), full.rowBytes()); |
| 387 | REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); |
| 388 | |
| 389 | // Now incremental decode will fail |
| 390 | result = partialCodec->incrementalDecode(); |
| 391 | REPORTER_ASSERT(r, result == SkCodec::kInvalidParameters); |
| 392 | } |
| 393 | |
| 394 | DEF_TEST(Codec_rewind, r) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 395 | test_interleaved(r, "images/plane.png"); |
| 396 | test_interleaved(r, "images/plane_interlaced.png"); |
| 397 | test_interleaved(r, "images/box.gif"); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 398 | } |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 399 | |
| 400 | // Modified version of the giflib logo, from |
| 401 | // http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html |
| 402 | // The global color map has been replaced with a local color map. |
| 403 | static unsigned char gNoGlobalColorMap[] = { |
| 404 | // Header |
| 405 | 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, |
| 406 | |
| 407 | // Logical screen descriptor |
| 408 | 0x0A, 0x00, 0x0A, 0x00, 0x11, 0x00, 0x00, |
| 409 | |
| 410 | // Image descriptor |
| 411 | 0x2C, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x0A, 0x00, 0x81, |
| 412 | |
| 413 | // Local color table |
| 414 | 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, |
| 415 | |
| 416 | // Image data |
| 417 | 0x02, 0x16, 0x8C, 0x2D, 0x99, 0x87, 0x2A, 0x1C, 0xDC, 0x33, 0xA0, 0x02, 0x75, |
| 418 | 0xEC, 0x95, 0xFA, 0xA8, 0xDE, 0x60, 0x8C, 0x04, 0x91, 0x4C, 0x01, 0x00, |
| 419 | |
| 420 | // Trailer |
| 421 | 0x3B, |
| 422 | }; |
| 423 | |
| 424 | // Test that a gif file truncated before its local color map behaves as expected. |
| 425 | DEF_TEST(Codec_GifPreMap, r) { |
| 426 | sk_sp<SkData> data = SkData::MakeWithoutCopy(gNoGlobalColorMap, sizeof(gNoGlobalColorMap)); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 427 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data)); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 428 | if (!codec) { |
| 429 | ERRORF(r, "failed to create codec"); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | SkBitmap truth; |
| 434 | auto info = standardize_info(codec.get()); |
| 435 | truth.allocPixels(info); |
| 436 | |
| 437 | auto result = codec->getPixels(info, truth.getPixels(), truth.rowBytes()); |
| 438 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 439 | |
| 440 | // Truncate to 23 bytes, just before the color map. This should fail to decode. |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 441 | // |
| 442 | // See also Codec_GifTruncated2 in GifTest.cpp for this magic 23. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 443 | codec = SkCodec::MakeFromData(SkData::MakeWithoutCopy(gNoGlobalColorMap, 23)); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 444 | REPORTER_ASSERT(r, codec); |
| 445 | if (codec) { |
| 446 | SkBitmap bm; |
| 447 | bm.allocPixels(info); |
| 448 | result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 449 | |
| 450 | // See the comments in Codec_GifTruncated2. |
| 451 | #ifdef SK_HAS_WUFFS_LIBRARY |
| 452 | REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); |
| 453 | #else |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 454 | REPORTER_ASSERT(r, result == SkCodec::kInvalidInput); |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 455 | #endif |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Again, truncate to 23 bytes, this time for an incremental decode. We |
| 459 | // cannot start an incremental decode until we have more data. If we did, |
| 460 | // we would be using the wrong color table. |
| 461 | HaltingStream* stream = new HaltingStream(data, 23); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 462 | codec = SkCodec::MakeFromStream(std::unique_ptr<SkStream>(stream)); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 463 | REPORTER_ASSERT(r, codec); |
| 464 | if (codec) { |
| 465 | SkBitmap bm; |
| 466 | bm.allocPixels(info); |
| 467 | result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes()); |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 468 | |
| 469 | // See the comments in Codec_GifTruncated2. |
| 470 | #ifdef SK_HAS_WUFFS_LIBRARY |
| 471 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 472 | |
| 473 | // Note that this is incrementalDecode, not startIncrementalDecode. |
| 474 | result = codec->incrementalDecode(); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 475 | REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); |
| 476 | |
| 477 | stream->addNewData(data->size()); |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 478 | #else |
| 479 | REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); |
| 480 | |
| 481 | // Note that this is startIncrementalDecode, not incrementalDecode. |
| 482 | stream->addNewData(data->size()); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 483 | result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes()); |
| 484 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 485 | #endif |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 486 | |
| 487 | result = codec->incrementalDecode(); |
| 488 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 489 | compare_bitmaps(r, truth, bm); |
| 490 | } |
| 491 | } |
Leon Scroggins III | b644650 | 2017-04-24 09:32:50 -0400 | [diff] [blame] | 492 | |
| 493 | DEF_TEST(Codec_emptyIDAT, r) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 494 | const char* name = "images/baby_tux.png"; |
Leon Scroggins III | b644650 | 2017-04-24 09:32:50 -0400 | [diff] [blame] | 495 | sk_sp<SkData> file = GetResourceAsData(name); |
| 496 | if (!file) { |
Leon Scroggins III | b644650 | 2017-04-24 09:32:50 -0400 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
| 500 | // Truncate to the beginning of the IDAT, immediately after the IDAT tag. |
| 501 | file = SkData::MakeSubset(file.get(), 0, 80); |
| 502 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 503 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(std::move(file))); |
Leon Scroggins III | b644650 | 2017-04-24 09:32:50 -0400 | [diff] [blame] | 504 | if (!codec) { |
| 505 | ERRORF(r, "Failed to create a codec for %s", name); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | SkBitmap bm; |
| 510 | const auto info = standardize_info(codec.get()); |
| 511 | bm.allocPixels(info); |
| 512 | |
| 513 | const auto result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 514 | REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result); |
| 515 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 516 | |
| 517 | DEF_TEST(Codec_incomplete, r) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 518 | for (const char* name : { "images/baby_tux.png", |
| 519 | "images/baby_tux.webp", |
| 520 | "images/CMYK.jpg", |
| 521 | "images/color_wheel.gif", |
| 522 | "images/google_chrome.ico", |
| 523 | "images/rle.bmp", |
| 524 | "images/mandrill.wbmp", |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 525 | }) { |
| 526 | sk_sp<SkData> file = GetResourceAsData(name); |
Bruce Wang | 61f36d3 | 2018-05-29 17:29:24 -0400 | [diff] [blame] | 527 | if (!file) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 528 | continue; |
| 529 | } |
| 530 | |
| 531 | for (size_t len = 14; len <= file->size(); len += 5) { |
| 532 | SkCodec::Result result; |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 533 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream( |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 534 | std::make_unique<SkMemoryStream>(file->data(), len), &result)); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 535 | if (codec) { |
| 536 | if (result != SkCodec::kSuccess) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 537 | ERRORF(r, "Created an SkCodec for %s with %zu bytes, but " |
| 538 | "reported an error %i", name, len, (int)result); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 539 | } |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | if (SkCodec::kIncompleteInput != result) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 544 | ERRORF(r, "Reported error %i for %s with %zu bytes", |
| 545 | (int)result, name, len); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |