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