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