blob: 7ca1f0ec78004fe5d2a12f08445b249faff1254a [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 Lesinski06460ef2017-03-14 18:52:13 -070034// Size in bytes of the PNG signature.
35constexpr size_t kPngSignatureSize = 8u;
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037struct PngOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 int grayscale_tolerance = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039};
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041/**
42 * Deprecated. Removing once new PNG crunching code is proved to be correct.
43 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044class Png {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 public:
46 explicit Png(IDiagnostics* diag) : mDiag(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 bool process(const Source& source, std::istream* input, BigBuffer* outBuffer,
49 const PngOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 private:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 DISALLOW_COPY_AND_ASSIGN(Png);
Adam Lesinski06460ef2017-03-14 18:52:13 -070053
54 IDiagnostics* mDiag;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070055};
56
Adam Lesinski21efb682016-09-14 17:35:43 -070057/**
58 * An InputStream that filters out unimportant PNG chunks.
59 */
60class PngChunkFilter : public io::InputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080062 explicit PngChunkFilter(const android::StringPiece& data);
Adam Lesinski06460ef2017-03-14 18:52:13 -070063 virtual ~PngChunkFilter() = default;
Adam Lesinski21efb682016-09-14 17:35:43 -070064
Adam Lesinski06460ef2017-03-14 18:52:13 -070065 bool Next(const void** buffer, size_t* len) override;
66 void BackUp(size_t count) override;
Adam Lesinski21efb682016-09-14 17:35:43 -070067
Adam Lesinski06460ef2017-03-14 18:52:13 -070068 bool CanRewind() const override { return true; }
69 bool Rewind() override;
70 size_t ByteCount() const override { return window_start_; }
Adam Lesinski21efb682016-09-14 17:35:43 -070071
Adam Lesinskicc73e992017-05-12 18:16:44 -070072 bool HadError() const override {
73 return !error_msg_.empty();
74 }
75 std::string GetError() const override {
76 return error_msg_;
77 }
Adam Lesinski21efb682016-09-14 17:35:43 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 private:
Adam Lesinski06460ef2017-03-14 18:52:13 -070080 DISALLOW_COPY_AND_ASSIGN(PngChunkFilter);
81
82 bool ConsumeWindow(const void** buffer, size_t* len);
Adam Lesinski21efb682016-09-14 17:35:43 -070083
Adam Lesinskid5083f62017-01-16 15:07:21 -080084 android::StringPiece data_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 size_t window_start_ = 0;
86 size_t window_end_ = 0;
Adam Lesinskicc73e992017-05-12 18:16:44 -070087 std::string error_msg_;
Adam Lesinski21efb682016-09-14 17:35:43 -070088};
89
90/**
91 * Reads a PNG from the InputStream into memory as an RGBA Image.
92 */
Adam Lesinskicc73e992017-05-12 18:16:44 -070093std::unique_ptr<Image> ReadPng(IAaptContext* context, const Source& source, io::InputStream* in);
Adam Lesinski21efb682016-09-14 17:35:43 -070094
95/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream
97 * as a PNG.
Adam Lesinski21efb682016-09-14 17:35:43 -070098 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099bool WritePng(IAaptContext* context, const Image* image,
100 const NinePatch* nine_patch, io::OutputStream* out,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 const PngOptions& options);
Adam Lesinski21efb682016-09-14 17:35:43 -0700102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103} // namespace aapt
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105#endif // AAPT_PNG_H