blob: 01c9adbc909429bf0aa8fb2a04ed21c4a9ec28a6 [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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <iostream>
21#include <string>
22
23#include "android-base/macros.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "Diagnostics.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070026#include "Source.h"
Adam Lesinski21efb682016-09-14 17:35:43 -070027#include "compile/Image.h"
28#include "io/Io.h"
29#include "process/IResourceTableConsumer.h"
30#include "util/BigBuffer.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070031
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070032namespace aapt {
33
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034struct PngOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 int grayscale_tolerance = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036};
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038/**
39 * Deprecated. Removing once new PNG crunching code is proved to be correct.
40 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041class Png {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 explicit Png(IDiagnostics* diag) : mDiag(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 bool process(const Source& source, std::istream* input, BigBuffer* outBuffer,
46 const PngOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 private:
49 IDiagnostics* mDiag;
Adam Lesinski21efb682016-09-14 17:35:43 -070050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 DISALLOW_COPY_AND_ASSIGN(Png);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070052};
53
Adam Lesinski21efb682016-09-14 17:35:43 -070054/**
55 * An InputStream that filters out unimportant PNG chunks.
56 */
57class PngChunkFilter : public io::InputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 public:
59 explicit PngChunkFilter(const StringPiece& data);
Adam Lesinski21efb682016-09-14 17:35:43 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 bool Next(const void** buffer, int* len) override;
62 void BackUp(int count) override;
63 bool Skip(int count) override;
Adam Lesinski21efb682016-09-14 17:35:43 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 int64_t ByteCount() const override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 return static_cast<int64_t>(window_start_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 }
Adam Lesinski21efb682016-09-14 17:35:43 -070068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 bool HadError() const override { return error_; }
Adam Lesinski21efb682016-09-14 17:35:43 -070070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 bool ConsumeWindow(const void** buffer, int* len);
Adam Lesinski21efb682016-09-14 17:35:43 -070073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 StringPiece data_;
75 size_t window_start_ = 0;
76 size_t window_end_ = 0;
77 bool error_ = false;
Adam Lesinski21efb682016-09-14 17:35:43 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 DISALLOW_COPY_AND_ASSIGN(PngChunkFilter);
Adam Lesinski21efb682016-09-14 17:35:43 -070080};
81
82/**
83 * Reads a PNG from the InputStream into memory as an RGBA Image.
84 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085std::unique_ptr<Image> ReadPng(IAaptContext* context, io::InputStream* in);
Adam Lesinski21efb682016-09-14 17:35:43 -070086
87/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream
89 * as a PNG.
Adam Lesinski21efb682016-09-14 17:35:43 -070090 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091bool WritePng(IAaptContext* context, const Image* image,
92 const NinePatch* nine_patch, io::OutputStream* out,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 const PngOptions& options);
Adam Lesinski21efb682016-09-14 17:35:43 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095} // namespace aapt
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097#endif // AAPT_PNG_H