blob: 4a15d95f36ccc2bda7051f9a6434a57a6a3c8c56 [file] [log] [blame]
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001/*
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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "Diagnostics.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070021#include "Source.h"
Adam Lesinski21efb682016-09-14 17:35:43 -070022#include "compile/Image.h"
23#include "io/Io.h"
24#include "process/IResourceTableConsumer.h"
25#include "util/BigBuffer.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070026
Adam Lesinski21efb682016-09-14 17:35:43 -070027#include <android-base/macros.h>
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070028#include <iostream>
29#include <string>
30
31namespace aapt {
32
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033struct PngOptions {
34 int grayScaleTolerance = 0;
35};
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070036
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037class Png {
38public:
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070039 explicit Png(IDiagnostics* diag) : mDiag(diag) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040 }
41
42 bool process(const Source& source, std::istream* input, BigBuffer* outBuffer,
43 const PngOptions& options);
44
45private:
46 IDiagnostics* mDiag;
Adam Lesinski21efb682016-09-14 17:35:43 -070047
48 DISALLOW_COPY_AND_ASSIGN(Png);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070049};
50
Adam Lesinski21efb682016-09-14 17:35:43 -070051/**
52 * An InputStream that filters out unimportant PNG chunks.
53 */
54class PngChunkFilter : public io::InputStream {
55public:
56 explicit PngChunkFilter(const StringPiece& data);
57
58 bool Next(const void** buffer, int* len) override;
59 void BackUp(int count) override;
60 bool Skip(int count) override;
61
62 int64_t ByteCount() const override {
63 return static_cast<int64_t>(mWindowStart);
64 }
65
66 bool HadError() const override {
67 return mError;
68 }
69
70private:
71 bool consumeWindow(const void** buffer, int* len);
72
73 StringPiece mData;
74 size_t mWindowStart = 0;
75 size_t mWindowEnd = 0;
76 bool mError = false;
77
78 DISALLOW_COPY_AND_ASSIGN(PngChunkFilter);
79};
80
81/**
82 * Reads a PNG from the InputStream into memory as an RGBA Image.
83 */
84std::unique_ptr<Image> readPng(IAaptContext* context, io::InputStream* in);
85
86/**
87 * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream as a PNG.
88 */
89bool writePng(IAaptContext* context, const Image* image, const NinePatch* ninePatch,
90 io::OutputStream* out, const PngOptions& options);
91
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070092} // namespace aapt
93
94#endif // AAPT_PNG_H