| OLD | NEW |
| (Empty) |
| 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 "SkCodec.h" | |
| 9 #include "SkData.h" | |
| 10 #include "SkImageInfo.h" | |
| 11 #include "SkRWBuffer.h" | |
| 12 #include "SkString.h" | |
| 13 | |
| 14 #include "Resources.h" | |
| 15 #include "Test.h" | |
| 16 | |
| 17 static sk_sp<SkData> make_from_resource(const char* name) { | |
| 18 SkString fullPath = GetResourcePath(name); | |
| 19 return SkData::MakeFromFileName(fullPath.c_str()); | |
| 20 } | |
| 21 | |
| 22 static SkImageInfo standardize_info(SkCodec* codec) { | |
| 23 SkImageInfo defaultInfo = codec->getInfo(); | |
| 24 return SkImageInfo::MakeN32Premul(defaultInfo.width(), defaultInfo.height())
; | |
| 25 } | |
| 26 | |
| 27 static bool create_truth(sk_sp<SkData> data, SkBitmap* dst) { | |
| 28 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get())); | |
| 29 if (!codec) { | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 const SkImageInfo info = standardize_info(codec); | |
| 34 dst->allocPixels(info); | |
| 35 return SkCodec::kSuccess == codec->getPixels(info, dst->getPixels(), dst->ro
wBytes()); | |
| 36 } | |
| 37 | |
| 38 /* | |
| 39 * Represents a stream without all of its data. | |
| 40 */ | |
| 41 class HaltingStream : public SkStream { | |
| 42 public: | |
| 43 HaltingStream(sk_sp<SkData> data) | |
| 44 : fTotalSize(data->size()) | |
| 45 , fLimit(fTotalSize / 2) | |
| 46 , fStream(std::move(data)) | |
| 47 {} | |
| 48 | |
| 49 void addNewData() { | |
| 50 // Arbitrary size, but deliberately different from | |
| 51 // the buffer size used by SkPngCodec. | |
| 52 fLimit = SkTMin(fTotalSize, fLimit + 1000); | |
| 53 } | |
| 54 | |
| 55 size_t read(void* buffer, size_t size) override { | |
| 56 if (fStream.getPosition() + size > fLimit) { | |
| 57 size = fLimit - fStream.getPosition(); | |
| 58 } | |
| 59 | |
| 60 return fStream.read(buffer, size); | |
| 61 } | |
| 62 | |
| 63 bool isAtEnd() const override { | |
| 64 return fStream.isAtEnd(); | |
| 65 } | |
| 66 | |
| 67 bool hasPosition() const override { return true; } | |
| 68 size_t getPosition() const override { return fStream.getPosition(); } | |
| 69 bool rewind() override { return fStream.rewind(); } | |
| 70 | |
| 71 private: | |
| 72 const size_t fTotalSize; | |
| 73 size_t fLimit; | |
| 74 SkMemoryStream fStream; | |
| 75 }; | |
| 76 | |
| 77 static void test_partial(skiatest::Reporter* r, const char* name) { | |
| 78 sk_sp<SkData> file = make_from_resource(name); | |
| 79 if (!file) { | |
| 80 SkDebugf("missing resource %s\n", name); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 SkBitmap truth; | |
| 85 if (!create_truth(file, &truth)) { | |
| 86 ERRORF(r, "Failed to decode %s\n", name); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 const size_t fileSize = file->size(); | |
| 91 | |
| 92 // Now decode part of the file | |
| 93 HaltingStream* stream = new HaltingStream(file); | |
| 94 | |
| 95 // Note that we cheat and hold on to a pointer to stream, though it is owned
by | |
| 96 // partialCodec. | |
| 97 SkAutoTDelete<SkCodec> partialCodec(SkCodec::NewFromStream(stream)); | |
| 98 if (!partialCodec) { | |
| 99 // Technically, this could be a small file where half the file is not | |
| 100 // enough. | |
| 101 ERRORF(r, "Failed to create codec for %s", name); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 const SkImageInfo info = standardize_info(partialCodec); | |
| 106 SkASSERT(info == truth.info()); | |
| 107 SkBitmap incremental; | |
| 108 incremental.allocPixels(info); | |
| 109 | |
| 110 const SkCodec::Result startResult = partialCodec->startIncrementalDecode(inf
o, | |
| 111 incremental.getPixels(), incremental.rowBytes()); | |
| 112 if (startResult != SkCodec::kSuccess) { | |
| 113 ERRORF(r, "Failed to start incremental decode\n"); | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 while (true) { | |
| 118 const SkCodec::Result result = partialCodec->incrementalDecode(); | |
| 119 | |
| 120 if (stream->getPosition() == fileSize) { | |
| 121 REPORTER_ASSERT(r, result == SkCodec::kSuccess); | |
| 122 break; | |
| 123 } | |
| 124 | |
| 125 SkASSERT(stream->getPosition() < fileSize); | |
| 126 | |
| 127 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput); | |
| 128 | |
| 129 // Append an arbitrary amount of data. | |
| 130 stream->addNewData(); | |
| 131 } | |
| 132 | |
| 133 // compare to original | |
| 134 for (int i = 0; i < info.height(); i++) { | |
| 135 REPORTER_ASSERT(r, !memcmp(truth.getAddr(0, 0), incremental.getAddr(0, 0
), | |
| 136 info.minRowBytes())); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 DEF_TEST(Codec_partial, r) { | |
| 141 test_partial(r, "plane.png"); | |
| 142 test_partial(r, "plane_interlaced.png"); | |
| 143 test_partial(r, "yellow_rose.png"); | |
| 144 test_partial(r, "index8.png"); | |
| 145 test_partial(r, "color_wheel.png"); | |
| 146 test_partial(r, "mandrill_256.png"); | |
| 147 test_partial(r, "mandrill_32.png"); | |
| 148 test_partial(r, "arrow.png"); | |
| 149 test_partial(r, "randPixels.png"); | |
| 150 test_partial(r, "baby_tux.png"); | |
| 151 } | |
| OLD | NEW |