blob: f9038afa5f9f24a844c566abef78b4077ac63c2e [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 {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 int grayScaleTolerance = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035};
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070036
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037class Png {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
39 explicit Png(IDiagnostics* diag) : mDiag(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 bool process(const Source& source, std::istream* input, BigBuffer* outBuffer,
42 const PngOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 private:
45 IDiagnostics* mDiag;
Adam Lesinski21efb682016-09-14 17:35:43 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 DISALLOW_COPY_AND_ASSIGN(Png);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070048};
49
Adam Lesinski21efb682016-09-14 17:35:43 -070050/**
51 * An InputStream that filters out unimportant PNG chunks.
52 */
53class PngChunkFilter : public io::InputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 public:
55 explicit PngChunkFilter(const StringPiece& data);
Adam Lesinski21efb682016-09-14 17:35:43 -070056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 bool Next(const void** buffer, int* len) override;
58 void BackUp(int count) override;
59 bool Skip(int count) override;
Adam Lesinski21efb682016-09-14 17:35:43 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 int64_t ByteCount() const override {
62 return static_cast<int64_t>(mWindowStart);
63 }
Adam Lesinski21efb682016-09-14 17:35:43 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 bool HadError() const override { return mError; }
Adam Lesinski21efb682016-09-14 17:35:43 -070066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 private:
68 bool consumeWindow(const void** buffer, int* len);
Adam Lesinski21efb682016-09-14 17:35:43 -070069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 StringPiece mData;
71 size_t mWindowStart = 0;
72 size_t mWindowEnd = 0;
73 bool mError = false;
Adam Lesinski21efb682016-09-14 17:35:43 -070074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 DISALLOW_COPY_AND_ASSIGN(PngChunkFilter);
Adam Lesinski21efb682016-09-14 17:35:43 -070076};
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/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream
85 * as a PNG.
Adam Lesinski21efb682016-09-14 17:35:43 -070086 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087bool writePng(IAaptContext* context, const Image* image,
88 const NinePatch* ninePatch, io::OutputStream* out,
89 const PngOptions& options);
Adam Lesinski21efb682016-09-14 17:35:43 -070090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091} // namespace aapt
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093#endif // AAPT_PNG_H