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 | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 34 | // Size in bytes of the PNG signature. |
| 35 | constexpr size_t kPngSignatureSize = 8u; |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | struct PngOptions { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | int grayscale_tolerance = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | }; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Deprecated. Removing once new PNG crunching code is proved to be correct. |
| 43 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | class Png { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | public: |
| 46 | explicit Png(IDiagnostics* diag) : mDiag(diag) {} |
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 | bool process(const Source& source, std::istream* input, BigBuffer* outBuffer, |
| 49 | const PngOptions& options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | private: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(Png); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 53 | |
| 54 | IDiagnostics* mDiag; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 57 | /** |
| 58 | * An InputStream that filters out unimportant PNG chunks. |
| 59 | */ |
| 60 | class PngChunkFilter : public io::InputStream { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 62 | explicit PngChunkFilter(const android::StringPiece& data); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 63 | virtual ~PngChunkFilter() = default; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 65 | bool Next(const void** buffer, size_t* len) override; |
| 66 | void BackUp(size_t count) override; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 68 | bool CanRewind() const override { return true; } |
| 69 | bool Rewind() override; |
| 70 | size_t ByteCount() const override { return window_start_; } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 72 | bool HadError() const override { |
| 73 | return !error_msg_.empty(); |
| 74 | } |
| 75 | std::string GetError() const override { |
| 76 | return error_msg_; |
| 77 | } |
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 | private: |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 80 | DISALLOW_COPY_AND_ASSIGN(PngChunkFilter); |
| 81 | |
| 82 | bool ConsumeWindow(const void** buffer, size_t* len); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 84 | android::StringPiece data_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | size_t window_start_ = 0; |
| 86 | size_t window_end_ = 0; |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 87 | std::string error_msg_; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * Reads a PNG from the InputStream into memory as an RGBA Image. |
| 92 | */ |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 93 | std::unique_ptr<Image> ReadPng(IAaptContext* context, const Source& source, io::InputStream* in); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | * Writes the RGBA Image, with optional 9-patch meta-data, into the OutputStream |
| 97 | * as a PNG. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 98 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | bool WritePng(IAaptContext* context, const Image* image, |
| 100 | const NinePatch* nine_patch, io::OutputStream* out, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | const PngOptions& options); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | } // namespace aapt |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | #endif // AAPT_PNG_H |