blob: 6b771209135071b28a4f79e778795af6aeeea9a2 [file] [log] [blame]
Leon Scroggins III83239652017-04-21 13:47:12 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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 "src/core/SkMakeUnique.h"
16#include "tests/Test.h"
17#include "tools/Resources.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040018
19#include <cstring>
Ben Wagner9707a7e2019-05-06 17:17:19 -040020#include <initializer_list>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040021#include <memory>
22#include <utility>
Leon Scroggins III83239652017-04-21 13:47:12 -040023
24namespace {
Leon Scroggins III600effb2017-04-24 15:44:45 -040025// 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.
28class UnowningStream : public SkStream {
Leon Scroggins III83239652017-04-21 13:47:12 -040029public:
Leon Scroggins III600effb2017-04-24 15:44:45 -040030 explicit UnowningStream(SkStream* stream)
31 : fStream(stream)
32 {}
Leon Scroggins III83239652017-04-21 13:47:12 -040033
34 size_t read(void* buf, size_t bytes) override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040035 return fStream->read(buf, bytes);
Leon Scroggins III83239652017-04-21 13:47:12 -040036 }
37
38 bool rewind() override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040039 return fStream->rewind();
Leon Scroggins III83239652017-04-21 13:47:12 -040040 }
41
42 bool isAtEnd() const override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040043 return fStream->isAtEnd();
Leon Scroggins III83239652017-04-21 13:47:12 -040044 }
45private:
Leon Scroggins III600effb2017-04-24 15:44:45 -040046 SkStream* fStream; // Unowned.
Leon Scroggins III83239652017-04-21 13:47:12 -040047};
48} // namespace
49
Leon Scroggins III600effb2017-04-24 15:44:45 -040050// 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.
Leon Scroggins III83239652017-04-21 13:47:12 -040053DEF_TEST(Codec_end, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050054 for (const char* path : { "images/plane.png",
55 "images/yellow_rose.png",
56 "images/plane_interlaced.png",
57 "images/google_chrome.ico",
58 "images/color_wheel.ico",
59 "images/mandrill.wbmp",
60 "images/randPixels.bmp",
Leon Scroggins III600effb2017-04-24 15:44:45 -040061 }) {
62 sk_sp<SkData> data = GetResourceAsData(path);
63 if (!data) {
Leon Scroggins III83239652017-04-21 13:47:12 -040064 continue;
65 }
66
Leon Scroggins III600effb2017-04-24 15:44:45 -040067 const int kNumImages = 2;
68 const size_t size = data->size();
69 sk_sp<SkData> multiData = SkData::MakeUninitialized(size * kNumImages);
70 void* dst = multiData->writable_data();
71 for (int i = 0; i < kNumImages; i++) {
72 memcpy(SkTAddOffset<void>(dst, size * i), data->data(), size);
Leon Scroggins III83239652017-04-21 13:47:12 -040073 }
Leon Scroggins III600effb2017-04-24 15:44:45 -040074 data.reset();
Leon Scroggins III83239652017-04-21 13:47:12 -040075
Leon Scroggins III600effb2017-04-24 15:44:45 -040076 SkMemoryStream stream(std::move(multiData));
77 for (int i = 0; i < kNumImages; ++i) {
Mike Reedede7bac2017-07-23 15:30:02 -040078 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
79 skstd::make_unique<UnowningStream>(&stream)));
Leon Scroggins III600effb2017-04-24 15:44:45 -040080 if (!codec) {
81 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
82 continue;
83 }
Leon Scroggins III83239652017-04-21 13:47:12 -040084
Leon Scroggins III600effb2017-04-24 15:44:45 -040085 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
86 SkBitmap bm;
87 bm.allocPixels(info);
Leon Scroggins III83239652017-04-21 13:47:12 -040088
Leon Scroggins III600effb2017-04-24 15:44:45 -040089 auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes());
90 if (result != SkCodec::kSuccess) {
91 ERRORF(r, "Failed to getPixels from %s, iteration %i error %i", path, i, result);
92 continue;
93 }
Leon Scroggins III83239652017-04-21 13:47:12 -040094 }
95 }
96}