Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <iostream> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/macros.h" |
| 24 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "Diagnostics.h" |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 26 | #include "Source.h" |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 27 | #include "compile/Image.h" |
| 28 | #include "io/Io.h" |
| 29 | #include "process/IResourceTableConsumer.h" |
| 30 | #include "util/BigBuffer.h" |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | struct PngOptions { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | int grayscale_tolerance = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | }; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Deprecated. Removing once new PNG crunching code is proved to be correct. |
| 40 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | class Png { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | public: |
| 43 | explicit Png(IDiagnostics* diag) : mDiag(diag) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | bool process(const Source& source, std::istream* input, BigBuffer* outBuffer, |
| 46 | const PngOptions& options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | private: |
| 49 | IDiagnostics* mDiag; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | DISALLOW_COPY_AND_ASSIGN(Png); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 54 | /** |
| 55 | * An InputStream that filters out unimportant PNG chunks. |
| 56 | */ |
| 57 | class PngChunkFilter : public io::InputStream { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | public: |
| 59 | explicit PngChunkFilter(const StringPiece& data); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | bool Next(const void** buffer, int* len) override; |
| 62 | void BackUp(int count) override; |
| 63 | bool Skip(int count) override; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | int64_t ByteCount() const override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | return static_cast<int64_t>(window_start_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | bool HadError() const override { return error_; } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | bool ConsumeWindow(const void** buffer, int* len); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | StringPiece data_; |
| 75 | size_t window_start_ = 0; |
| 76 | size_t window_end_ = 0; |
| 77 | bool error_ = false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(PngChunkFilter); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Reads a PNG from the InputStream into memory as an RGBA Image. |
| 84 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | std::unique_ptr<Image> ReadPng(IAaptContext* context, io::InputStream* in); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream |
| 89 | * as a PNG. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 90 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | bool WritePng(IAaptContext* context, const Image* image, |
| 92 | const NinePatch* nine_patch, io::OutputStream* out, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | const PngOptions& options); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | } // namespace aapt |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | #endif // AAPT_PNG_H |