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