Png.h revision 383db5ebcc3a4a615faf249bf4f126f42e80b82e
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_PNG_H
18#define AAPT_PNG_H
19
20#include <iostream>
21#include <string>
22
23#include "android-base/macros.h"
24
25#include "Diagnostics.h"
26#include "Source.h"
27#include "compile/Image.h"
28#include "io/Io.h"
29#include "process/IResourceTableConsumer.h"
30#include "util/BigBuffer.h"
31
32namespace aapt {
33
34struct PngOptions {
35  int grayscale_tolerance = 0;
36};
37
38/**
39 * Deprecated. Removing once new PNG crunching code is proved to be correct.
40 */
41class Png {
42 public:
43  explicit Png(IDiagnostics* diag) : mDiag(diag) {}
44
45  bool process(const Source& source, std::istream* input, BigBuffer* outBuffer,
46               const PngOptions& options);
47
48 private:
49  IDiagnostics* mDiag;
50
51  DISALLOW_COPY_AND_ASSIGN(Png);
52};
53
54/**
55 * An InputStream that filters out unimportant PNG chunks.
56 */
57class PngChunkFilter : public io::InputStream {
58 public:
59  explicit PngChunkFilter(const StringPiece& data);
60
61  bool Next(const void** buffer, int* len) override;
62  void BackUp(int count) override;
63  bool Skip(int count) override;
64
65  google::protobuf::int64 ByteCount() const override {
66    return static_cast<google::protobuf::int64>(window_start_);
67  }
68
69  bool HadError() const override { return error_; }
70
71 private:
72  bool ConsumeWindow(const void** buffer, int* len);
73
74  StringPiece data_;
75  size_t window_start_ = 0;
76  size_t window_end_ = 0;
77  bool error_ = false;
78
79  DISALLOW_COPY_AND_ASSIGN(PngChunkFilter);
80};
81
82/**
83 * Reads a PNG from the InputStream into memory as an RGBA Image.
84 */
85std::unique_ptr<Image> ReadPng(IAaptContext* context, io::InputStream* in);
86
87/**
88 * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream
89 * as a PNG.
90 */
91bool WritePng(IAaptContext* context, const Image* image,
92              const NinePatch* nine_patch, io::OutputStream* out,
93              const PngOptions& options);
94
95}  // namespace aapt
96
97#endif  // AAPT_PNG_H
98