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