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