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