1 /*
2 * Copyright 2017 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 "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"
14 #include "include/private/SkTemplates.h"
15 #include "tests/Test.h"
16 #include "tools/Resources.h"
17
18 #include <cstring>
19 #include <initializer_list>
20 #include <memory>
21 #include <utility>
22
23 namespace {
24 // This class wraps another SkStream. It does not own the underlying stream, so
25 // that the underlying stream can be reused starting from where the first
26 // client left off. This mimics Android's JavaInputStreamAdaptor.
27 class UnowningStream : public SkStream {
28 public:
UnowningStream(SkStream * stream)29 explicit UnowningStream(SkStream* stream)
30 : fStream(stream)
31 {}
32
read(void * buf,size_t bytes)33 size_t read(void* buf, size_t bytes) override {
34 return fStream->read(buf, bytes);
35 }
36
rewind()37 bool rewind() override {
38 return fStream->rewind();
39 }
40
isAtEnd() const41 bool isAtEnd() const override {
42 return fStream->isAtEnd();
43 }
44 private:
45 SkStream* fStream; // Unowned.
46 };
47 } // namespace
48
49 // Test that some SkCodecs do not attempt to read input beyond the logical
50 // end of the data. Some other SkCodecs do, but some Android apps rely on not
51 // doing so for PNGs. Test on other formats that work.
DEF_TEST(Codec_end,r)52 DEF_TEST(Codec_end, r) {
53 for (const char* path : { "images/plane.png",
54 "images/yellow_rose.png",
55 "images/plane_interlaced.png",
56 "images/mandrill.wbmp",
57 "images/randPixels.bmp",
58 }) {
59 sk_sp<SkData> data = GetResourceAsData(path);
60 if (!data) {
61 continue;
62 }
63
64 const int kNumImages = 2;
65 const size_t size = data->size();
66 sk_sp<SkData> multiData = SkData::MakeUninitialized(size * kNumImages);
67 void* dst = multiData->writable_data();
68 for (int i = 0; i < kNumImages; i++) {
69 memcpy(SkTAddOffset<void>(dst, size * i), data->data(), size);
70 }
71 data.reset();
72
73 SkMemoryStream stream(std::move(multiData));
74 for (int i = 0; i < kNumImages; ++i) {
75 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
76 std::make_unique<UnowningStream>(&stream)));
77 if (!codec) {
78 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
79 continue;
80 }
81
82 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
83 SkBitmap bm;
84 bm.allocPixels(info);
85
86 auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes());
87 if (result != SkCodec::kSuccess) {
88 ERRORF(r, "Failed to getPixels from %s, iteration %i error %i", path, i, result);
89 continue;
90 }
91 }
92 }
93 }
94