Png.h revision cacb28f2d60858106e2819cc7d95a65e8bda890b
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 "Diagnostics.h"
21#include "Source.h"
22#include "compile/Image.h"
23#include "io/Io.h"
24#include "process/IResourceTableConsumer.h"
25#include "util/BigBuffer.h"
26
27#include <android-base/macros.h>
28#include <iostream>
29#include <string>
30
31namespace aapt {
32
33struct PngOptions {
34  int grayScaleTolerance = 0;
35};
36
37class Png {
38 public:
39  explicit Png(IDiagnostics* diag) : mDiag(diag) {}
40
41  bool process(const Source& source, std::istream* input, BigBuffer* outBuffer,
42               const PngOptions& options);
43
44 private:
45  IDiagnostics* mDiag;
46
47  DISALLOW_COPY_AND_ASSIGN(Png);
48};
49
50/**
51 * An InputStream that filters out unimportant PNG chunks.
52 */
53class PngChunkFilter : public io::InputStream {
54 public:
55  explicit PngChunkFilter(const StringPiece& data);
56
57  bool Next(const void** buffer, int* len) override;
58  void BackUp(int count) override;
59  bool Skip(int count) override;
60
61  int64_t ByteCount() const override {
62    return static_cast<int64_t>(mWindowStart);
63  }
64
65  bool HadError() const override { return mError; }
66
67 private:
68  bool consumeWindow(const void** buffer, int* len);
69
70  StringPiece mData;
71  size_t mWindowStart = 0;
72  size_t mWindowEnd = 0;
73  bool mError = false;
74
75  DISALLOW_COPY_AND_ASSIGN(PngChunkFilter);
76};
77
78/**
79 * Reads a PNG from the InputStream into memory as an RGBA Image.
80 */
81std::unique_ptr<Image> readPng(IAaptContext* context, io::InputStream* in);
82
83/**
84 * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream
85 * as a PNG.
86 */
87bool writePng(IAaptContext* context, const Image* image,
88              const NinePatch* ninePatch, io::OutputStream* out,
89              const PngOptions& options);
90
91}  // namespace aapt
92
93#endif  // AAPT_PNG_H
94