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