2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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"
19 #include <initializer_list>
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 {
29 explicit UnowningStream(SkStream* stream)
33 size_t read(void* buf, size_t bytes) override {
34 return fStream->read(buf, bytes);
37 bool rewind() override {
38 return fStream->rewind();
41 bool isAtEnd() const override {
42 return fStream->isAtEnd();
45 SkStream* fStream; // Unowned.
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.
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",
59 sk_sp<SkData> data = GetResourceAsData(path);
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);
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)));
78 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
82 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
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);