Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "compile/Png.h" |
| 18 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 19 | #include <png.h> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 20 | #include <zlib.h> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 22 | #include <algorithm> |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 23 | #include <unordered_map> |
| 24 | #include <unordered_set> |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "android-base/errors.h" |
| 27 | #include "android-base/logging.h" |
| 28 | #include "android-base/macros.h" |
| 29 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
| 32 | // Size in bytes of the PNG signature. |
| 33 | constexpr size_t kPngSignatureSize = 8u; |
| 34 | |
| 35 | /** |
| 36 | * Custom deleter that destroys libpng read and info structs. |
| 37 | */ |
| 38 | class PngReadStructDeleter { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | PngReadStructDeleter(png_structp read_ptr, png_infop info_ptr) |
| 41 | : read_ptr_(read_ptr), info_ptr_(info_ptr) {} |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | ~PngReadStructDeleter() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | png_destroy_read_struct(&read_ptr_, &info_ptr_, nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | png_structp read_ptr_; |
| 49 | png_infop info_ptr_; |
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(PngReadStructDeleter); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Custom deleter that destroys libpng write and info structs. |
| 56 | */ |
| 57 | class PngWriteStructDeleter { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | PngWriteStructDeleter(png_structp write_ptr, png_infop info_ptr) |
| 60 | : write_ptr_(write_ptr), info_ptr_(info_ptr) {} |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | ~PngWriteStructDeleter() { |
| 63 | png_destroy_write_struct(&write_ptr_, &info_ptr_); |
| 64 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | png_structp write_ptr_; |
| 68 | png_infop info_ptr_; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | DISALLOW_COPY_AND_ASSIGN(PngWriteStructDeleter); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Custom warning logging method that uses IDiagnostics. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | static void LogWarning(png_structp png_ptr, png_const_charp warning_msg) { |
| 75 | IDiagnostics* diag = (IDiagnostics*)png_get_error_ptr(png_ptr); |
| 76 | diag->Warn(DiagMessage() << warning_msg); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Custom error logging method that uses IDiagnostics. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | static void LogError(png_structp png_ptr, png_const_charp error_msg) { |
| 81 | IDiagnostics* diag = (IDiagnostics*)png_get_error_ptr(png_ptr); |
| 82 | diag->Error(DiagMessage() << error_msg); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | static void ReadDataFromStream(png_structp png_ptr, png_bytep buffer, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | png_size_t len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | io::InputStream* in = (io::InputStream*)png_get_io_ptr(png_ptr); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | const void* in_buffer; |
| 90 | int in_len; |
| 91 | if (!in->Next(&in_buffer, &in_len)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | if (in->HadError()) { |
| 93 | std::string err = in->GetError(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | png_error(png_ptr, err.c_str()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 95 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | return; |
| 97 | } |
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 | const size_t bytes_read = std::min(static_cast<size_t>(in_len), len); |
| 100 | memcpy(buffer, in_buffer, bytes_read); |
| 101 | if (bytes_read != static_cast<size_t>(in_len)) { |
| 102 | in->BackUp(in_len - static_cast<int>(bytes_read)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | static void WriteDataToStream(png_structp png_ptr, png_bytep buffer, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | png_size_t len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | io::OutputStream* out = (io::OutputStream*)png_get_io_ptr(png_ptr); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | void* out_buffer; |
| 111 | int out_len; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | while (len > 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | if (!out->Next(&out_buffer, &out_len)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | if (out->HadError()) { |
| 115 | std::string err = out->GetError(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | png_error(png_ptr, err.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | } |
| 118 | return; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | const size_t bytes_written = std::min(static_cast<size_t>(out_len), len); |
| 122 | memcpy(out_buffer, buffer, bytes_written); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | |
| 124 | // Advance the input buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | buffer += bytes_written; |
| 126 | len -= bytes_written; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | |
| 128 | // Advance the output buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | out_len -= static_cast<int>(bytes_written); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // If the entire output buffer wasn't used, backup. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | if (out_len > 0) { |
| 134 | out->BackUp(out_len); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | std::unique_ptr<Image> ReadPng(IAaptContext* context, io::InputStream* in) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | // Read the first 8 bytes of the file looking for the PNG signature. |
| 140 | // Bail early if it does not match. |
| 141 | const png_byte* signature; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | int buffer_size; |
| 143 | if (!in->Next((const void**)&signature, &buffer_size)) { |
| 144 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | DiagMessage() << android::base::SystemErrorCodeToString(errno)); |
| 146 | return {}; |
| 147 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | if (static_cast<size_t>(buffer_size) < kPngSignatureSize || |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | png_sig_cmp(signature, 0, kPngSignatureSize) != 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | DiagMessage() << "file signature does not match PNG signature"); |
| 153 | return {}; |
| 154 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | // Start at the beginning of the first chunk. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | in->BackUp(buffer_size - static_cast<int>(kPngSignatureSize)); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | // Create and initialize the png_struct with the default error and warning |
| 160 | // handlers. |
| 161 | // The header version is also passed in to ensure that this was built against |
| 162 | // the same |
| 163 | // version of libpng. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | png_structp read_ptr = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | if (read_ptr == nullptr) { |
| 167 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | DiagMessage() << "failed to create libpng read png_struct"); |
| 169 | return {}; |
| 170 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 172 | // Create and initialize the memory for image header and data. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | png_infop info_ptr = png_create_info_struct(read_ptr); |
| 174 | if (info_ptr == nullptr) { |
| 175 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | DiagMessage() << "failed to create libpng read png_info"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | png_destroy_read_struct(&read_ptr, nullptr, nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | return {}; |
| 179 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 180 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | // Automatically release PNG resources at end of scope. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | PngReadStructDeleter png_read_deleter(read_ptr, info_ptr); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | // libpng uses longjmp to jump to an error handling routine. |
| 185 | // setjmp will only return true if it was jumped to, aka there was |
| 186 | // an error. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | if (setjmp(png_jmpbuf(read_ptr))) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | return {}; |
| 189 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | // Handle warnings ourselves via IDiagnostics. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 192 | png_set_error_fn(read_ptr, (png_voidp)context->GetDiagnostics(), LogError, |
| 193 | LogWarning); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | // Set up the read functions which read from our custom data sources. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | png_set_read_fn(read_ptr, (png_voidp)in, ReadDataFromStream); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 197 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | // Skip the signature that we already read. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | png_set_sig_bytes(read_ptr, kPngSignatureSize); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 200 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | // Read the chunk headers. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | png_read_info(read_ptr, info_ptr); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | // Extract image meta-data from the various chunk headers. |
| 205 | uint32_t width, height; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | int bit_depth, color_type, interlace_method, compression_method, |
| 207 | filter_method; |
| 208 | png_get_IHDR(read_ptr, info_ptr, &width, &height, &bit_depth, &color_type, |
| 209 | &interlace_method, &compression_method, &filter_method); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 210 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | // When the image is read, expand it so that it is in RGBA 8888 format |
| 212 | // so that image handling is uniform. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 215 | png_set_palette_to_rgb(read_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 217 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) { |
| 219 | png_set_expand_gray_1_2_4_to_8(read_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | if (png_get_valid(read_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 223 | png_set_tRNS_to_alpha(read_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 225 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | if (bit_depth == 16) { |
| 227 | png_set_strip_16(read_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 229 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | if (!(color_type & PNG_COLOR_MASK_ALPHA)) { |
| 231 | png_set_add_alpha(read_ptr, 0xFF, PNG_FILLER_AFTER); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 233 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | if (color_type == PNG_COLOR_TYPE_GRAY || |
| 235 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 236 | png_set_gray_to_rgb(read_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 237 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | if (interlace_method != PNG_INTERLACE_NONE) { |
| 240 | png_set_interlace_handling(read_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 242 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | // Once all the options for reading have been set, we need to flush |
| 244 | // them to libpng. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | png_read_update_info(read_ptr, info_ptr); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 246 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | // 9-patch uses int32_t to index images, so we cap the image dimensions to |
| 248 | // something |
| 249 | // that can always be represented by 9-patch. |
| 250 | if (width > std::numeric_limits<int32_t>::max() || |
| 251 | height > std::numeric_limits<int32_t>::max()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | context->GetDiagnostics()->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | << "PNG image dimensions are too large: " |
| 254 | << width << "x" << height); |
| 255 | return {}; |
| 256 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 257 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | std::unique_ptr<Image> output_image = util::make_unique<Image>(); |
| 259 | output_image->width = static_cast<int32_t>(width); |
| 260 | output_image->height = static_cast<int32_t>(height); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 261 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | const size_t row_bytes = png_get_rowbytes(read_ptr, info_ptr); |
| 263 | CHECK(row_bytes == 4 * width); // RGBA |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 264 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | // Allocate one large block to hold the image. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | output_image->data = |
| 267 | std::unique_ptr<uint8_t[]>(new uint8_t[height * row_bytes]); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 268 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | // Create an array of rows that index into the data block. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | output_image->rows = std::unique_ptr<uint8_t* []>(new uint8_t*[height]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 271 | for (uint32_t h = 0; h < height; h++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | output_image->rows[h] = output_image->data.get() + (h * row_bytes); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 274 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 275 | // Actually read the image pixels. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | png_read_image(read_ptr, output_image->rows.get()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 277 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | // Finish reading. This will read any other chunks after the image data. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | png_read_end(read_ptr, info_ptr); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 280 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | return output_image; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | * Experimentally chosen constant to be added to the overhead of using color |
| 286 | * type |
| 287 | * PNG_COLOR_TYPE_PALETTE to account for the uncompressability of the palette |
| 288 | * chunk. |
| 289 | * Without this, many small PNGs encoded with palettes are larger after |
| 290 | * compression than |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 291 | * the same PNGs encoded as RGBA. |
| 292 | */ |
| 293 | constexpr static const size_t kPaletteOverheadConstant = 1024u * 10u; |
| 294 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 295 | // Pick a color type by which to encode the image, based on which color type |
| 296 | // will take |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 297 | // the least amount of disk space. |
| 298 | // |
| 299 | // 9-patch images traditionally have not been encoded with palettes. |
| 300 | // The original rationale was to avoid dithering until after scaling, |
| 301 | // but I don't think this would be an issue with palettes. Either way, |
| 302 | // our naive size estimation tends to be wrong for small images like 9-patches |
| 303 | // and using palettes balloons the size of the resulting 9-patch. |
| 304 | // In order to not regress in size, restrict 9-patch to not use palettes. |
| 305 | |
| 306 | // The options are: |
| 307 | // |
| 308 | // - RGB |
| 309 | // - RGBA |
| 310 | // - RGB + cheap alpha |
| 311 | // - Color palette |
| 312 | // - Color palette + cheap alpha |
| 313 | // - Color palette + alpha palette |
| 314 | // - Grayscale |
| 315 | // - Grayscale + cheap alpha |
| 316 | // - Grayscale + alpha |
| 317 | // |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | static int PickColorType(int32_t width, int32_t height, bool grayscale, |
| 319 | bool convertible_to_grayscale, bool has_nine_patch, |
| 320 | size_t color_palette_size, size_t alpha_palette_size) { |
| 321 | const size_t palette_chunk_size = 16 + color_palette_size * 3; |
| 322 | const size_t alpha_chunk_size = 16 + alpha_palette_size; |
| 323 | const size_t color_alpha_data_chunk_size = 16 + 4 * width * height; |
| 324 | const size_t color_data_chunk_size = 16 + 3 * width * height; |
| 325 | const size_t grayscale_alpha_data_chunk_size = 16 + 2 * width * height; |
| 326 | const size_t palette_data_chunk_size = 16 + width * height; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | if (grayscale) { |
| 329 | if (alpha_palette_size == 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | // This is the smallest the data can be. |
| 331 | return PNG_COLOR_TYPE_GRAY; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | } else if (color_palette_size <= 256 && !has_nine_patch) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | // This grayscale has alpha and can fit within a palette. |
| 334 | // See if it is worth fitting into a palette. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | const size_t palette_threshold = palette_chunk_size + alpha_chunk_size + |
| 336 | palette_data_chunk_size + |
| 337 | kPaletteOverheadConstant; |
| 338 | if (grayscale_alpha_data_chunk_size > palette_threshold) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 339 | return PNG_COLOR_TYPE_PALETTE; |
| 340 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 341 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 342 | return PNG_COLOR_TYPE_GRAY_ALPHA; |
| 343 | } |
| 344 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | if (color_palette_size <= 256 && !has_nine_patch) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 346 | // This image can fit inside a palette. Let's see if it is worth it. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | size_t total_size_with_palette = |
| 348 | palette_data_chunk_size + palette_chunk_size; |
| 349 | size_t total_size_without_palette = color_data_chunk_size; |
| 350 | if (alpha_palette_size > 0) { |
| 351 | total_size_with_palette += alpha_palette_size; |
| 352 | total_size_without_palette = color_alpha_data_chunk_size; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | if (total_size_without_palette > |
| 356 | total_size_with_palette + kPaletteOverheadConstant) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 357 | return PNG_COLOR_TYPE_PALETTE; |
| 358 | } |
| 359 | } |
| 360 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | if (convertible_to_grayscale) { |
| 362 | if (alpha_palette_size == 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | return PNG_COLOR_TYPE_GRAY; |
| 364 | } else { |
| 365 | return PNG_COLOR_TYPE_GRAY_ALPHA; |
| 366 | } |
| 367 | } |
| 368 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | if (alpha_palette_size == 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | return PNG_COLOR_TYPE_RGB; |
| 371 | } |
| 372 | return PNG_COLOR_TYPE_RGBA; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | // Assigns indices to the color and alpha palettes, encodes them, and then |
| 376 | // invokes |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 377 | // png_set_PLTE/png_set_tRNS. |
| 378 | // This must be done before writing image data. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 379 | // Image data must be transformed to use the indices assigned within the |
| 380 | // palette. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | static void WritePalette(png_structp write_ptr, png_infop write_info_ptr, |
| 382 | std::unordered_map<uint32_t, int>* color_palette, |
| 383 | std::unordered_set<uint32_t>* alpha_palette) { |
| 384 | CHECK(color_palette->size() <= 256); |
| 385 | CHECK(alpha_palette->size() <= 256); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 386 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 387 | // Populate the PNG palette struct and assign indices to the color |
| 388 | // palette. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 389 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | // Colors in the alpha palette should have smaller indices. |
| 391 | // This will ensure that we can truncate the alpha palette if it is |
| 392 | // smaller than the color palette. |
| 393 | int index = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 394 | for (uint32_t color : *alpha_palette) { |
| 395 | (*color_palette)[color] = index++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // Assign the rest of the entries. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | for (auto& entry : *color_palette) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 400 | if (entry.second == -1) { |
| 401 | entry.second = index++; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 402 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 403 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 404 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 405 | // Create the PNG color palette struct. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | auto color_palette_bytes = |
| 407 | std::unique_ptr<png_color[]>(new png_color[color_palette->size()]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 408 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 409 | std::unique_ptr<png_byte[]> alpha_palette_bytes; |
| 410 | if (!alpha_palette->empty()) { |
| 411 | alpha_palette_bytes = |
| 412 | std::unique_ptr<png_byte[]>(new png_byte[alpha_palette->size()]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 415 | for (const auto& entry : *color_palette) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | const uint32_t color = entry.first; |
| 417 | const int index = entry.second; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 418 | CHECK(index >= 0); |
| 419 | CHECK(static_cast<size_t>(index) < color_palette->size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 420 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 421 | png_colorp slot = color_palette_bytes.get() + index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | slot->red = color >> 24; |
| 423 | slot->green = color >> 16; |
| 424 | slot->blue = color >> 8; |
| 425 | |
| 426 | const png_byte alpha = color & 0x000000ff; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 427 | if (alpha != 0xff && alpha_palette_bytes) { |
| 428 | CHECK(static_cast<size_t>(index) < alpha_palette->size()); |
| 429 | alpha_palette_bytes[index] = alpha; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 430 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 431 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 432 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 433 | // The bytes get copied here, so it is safe to release color_palette_bytes at |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 434 | // the end of function |
| 435 | // scope. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 436 | png_set_PLTE(write_ptr, write_info_ptr, color_palette_bytes.get(), |
| 437 | color_palette->size()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 438 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 439 | if (alpha_palette_bytes) { |
| 440 | png_set_tRNS(write_ptr, write_info_ptr, alpha_palette_bytes.get(), |
| 441 | alpha_palette->size(), nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 442 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 445 | // Write the 9-patch custom PNG chunks to write_info_ptr. This must be done |
| 446 | // before |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 447 | // writing image data. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 448 | static void WriteNinePatch(png_structp write_ptr, png_infop write_info_ptr, |
| 449 | const NinePatch* nine_patch) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 450 | // The order of the chunks is important. |
| 451 | // 9-patch code in older platforms expects the 9-patch chunk to |
| 452 | // be last. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 453 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 454 | png_unknown_chunk unknown_chunks[3]; |
| 455 | memset(unknown_chunks, 0, sizeof(unknown_chunks)); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 456 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 457 | size_t index = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 458 | size_t chunk_len = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 459 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 460 | std::unique_ptr<uint8_t[]> serialized_outline = |
| 461 | nine_patch->SerializeRoundedRectOutline(&chunk_len); |
| 462 | strcpy((char*)unknown_chunks[index].name, "npOl"); |
| 463 | unknown_chunks[index].size = chunk_len; |
| 464 | unknown_chunks[index].data = (png_bytep)serialized_outline.get(); |
| 465 | unknown_chunks[index].location = PNG_HAVE_PLTE; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 466 | index++; |
| 467 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 468 | std::unique_ptr<uint8_t[]> serialized_layout_bounds; |
| 469 | if (nine_patch->layout_bounds.nonZero()) { |
| 470 | serialized_layout_bounds = nine_patch->SerializeLayoutBounds(&chunk_len); |
| 471 | strcpy((char*)unknown_chunks[index].name, "npLb"); |
| 472 | unknown_chunks[index].size = chunk_len; |
| 473 | unknown_chunks[index].data = (png_bytep)serialized_layout_bounds.get(); |
| 474 | unknown_chunks[index].location = PNG_HAVE_PLTE; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 475 | index++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 476 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 477 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 478 | std::unique_ptr<uint8_t[]> serialized_nine_patch = |
| 479 | nine_patch->SerializeBase(&chunk_len); |
| 480 | strcpy((char*)unknown_chunks[index].name, "npTc"); |
| 481 | unknown_chunks[index].size = chunk_len; |
| 482 | unknown_chunks[index].data = (png_bytep)serialized_nine_patch.get(); |
| 483 | unknown_chunks[index].location = PNG_HAVE_PLTE; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 484 | index++; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 485 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 486 | // Handle all unknown chunks. We are manually setting the chunks here, |
| 487 | // so we will only ever handle our custom chunks. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 488 | png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS, nullptr, 0); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 489 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 490 | // Set the actual chunks here. The data gets copied, so our buffers can |
| 491 | // safely go out of scope. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | png_set_unknown_chunks(write_ptr, write_info_ptr, unknown_chunks, index); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 495 | bool WritePng(IAaptContext* context, const Image* image, |
| 496 | const NinePatch* nine_patch, io::OutputStream* out, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 497 | const PngOptions& options) { |
| 498 | // Create and initialize the write png_struct with the default error and |
| 499 | // warning handlers. |
| 500 | // The header version is also passed in to ensure that this was built against |
| 501 | // the same |
| 502 | // version of libpng. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 503 | png_structp write_ptr = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 504 | png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 505 | if (write_ptr == nullptr) { |
| 506 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 507 | DiagMessage() << "failed to create libpng write png_struct"); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | // Allocate memory to store image header data. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 512 | png_infop write_info_ptr = png_create_info_struct(write_ptr); |
| 513 | if (write_info_ptr == nullptr) { |
| 514 | context->GetDiagnostics()->Error( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 515 | DiagMessage() << "failed to create libpng write png_info"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 516 | png_destroy_write_struct(&write_ptr, nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
| 520 | // Automatically release PNG resources at end of scope. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 521 | PngWriteStructDeleter png_write_deleter(write_ptr, write_info_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 522 | |
| 523 | // libpng uses longjmp to jump to error handling routines. |
| 524 | // setjmp will return true only if it was jumped to, aka, there was an error. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 525 | if (setjmp(png_jmpbuf(write_ptr))) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | |
| 529 | // Handle warnings with our IDiagnostics. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | png_set_error_fn(write_ptr, (png_voidp)context->GetDiagnostics(), LogError, |
| 531 | LogWarning); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 532 | |
| 533 | // Set up the write functions which write to our custom data sources. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | png_set_write_fn(write_ptr, (png_voidp)out, WriteDataToStream, nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 535 | |
| 536 | // We want small files and can take the performance hit to achieve this goal. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 537 | png_set_compression_level(write_ptr, Z_BEST_COMPRESSION); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 538 | |
| 539 | // Begin analysis of the image data. |
| 540 | // Scan the entire image and determine if: |
| 541 | // 1. Every pixel has R == G == B (grayscale) |
| 542 | // 2. Every pixel has A == 255 (opaque) |
| 543 | // 3. There are no more than 256 distinct RGBA colors (palette). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 544 | std::unordered_map<uint32_t, int> color_palette; |
| 545 | std::unordered_set<uint32_t> alpha_palette; |
| 546 | bool needs_to_zero_rgb_channels_of_transparent_pixels = false; |
| 547 | bool grayscale = true; |
| 548 | int max_gray_deviation = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 549 | |
| 550 | for (int32_t y = 0; y < image->height; y++) { |
| 551 | const uint8_t* row = image->rows[y]; |
| 552 | for (int32_t x = 0; x < image->width; x++) { |
| 553 | int red = *row++; |
| 554 | int green = *row++; |
| 555 | int blue = *row++; |
| 556 | int alpha = *row++; |
| 557 | |
| 558 | if (alpha == 0) { |
| 559 | // The color is completely transparent. |
| 560 | // For purposes of palettes and grayscale optimization, |
| 561 | // treat all channels as 0x00. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 562 | needs_to_zero_rgb_channels_of_transparent_pixels = |
| 563 | needs_to_zero_rgb_channels_of_transparent_pixels || |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 564 | (red != 0 || green != 0 || blue != 0); |
| 565 | red = green = blue = 0; |
| 566 | } |
| 567 | |
| 568 | // Insert the color into the color palette. |
| 569 | const uint32_t color = red << 24 | green << 16 | blue << 8 | alpha; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 570 | color_palette[color] = -1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 571 | |
| 572 | // If the pixel has non-opaque alpha, insert it into the |
| 573 | // alpha palette. |
| 574 | if (alpha != 0xff) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 575 | alpha_palette.insert(color); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // Check if the image is indeed grayscale. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 579 | if (grayscale) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | if (red != green || red != blue) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 581 | grayscale = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | // Calculate the gray scale deviation so that it can be compared |
| 586 | // with the threshold. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 587 | max_gray_deviation = std::max(std::abs(red - green), max_gray_deviation); |
| 588 | max_gray_deviation = std::max(std::abs(green - blue), max_gray_deviation); |
| 589 | max_gray_deviation = std::max(std::abs(blue - red), max_gray_deviation); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 590 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 591 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 592 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 593 | if (context->IsVerbose()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 594 | DiagMessage msg; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 595 | msg << " paletteSize=" << color_palette.size() |
| 596 | << " alphaPaletteSize=" << alpha_palette.size() |
| 597 | << " maxGrayDeviation=" << max_gray_deviation |
| 598 | << " grayScale=" << (grayscale ? "true" : "false"); |
| 599 | context->GetDiagnostics()->Note(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 602 | const bool convertible_to_grayscale = |
| 603 | max_gray_deviation <= options.grayscale_tolerance; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 605 | const int new_color_type = PickColorType( |
| 606 | image->width, image->height, grayscale, convertible_to_grayscale, |
| 607 | nine_patch != nullptr, color_palette.size(), alpha_palette.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 608 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 609 | if (context->IsVerbose()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 610 | DiagMessage msg; |
| 611 | msg << "encoding PNG "; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 612 | if (nine_patch) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | msg << "(with 9-patch) as "; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 614 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 615 | switch (new_color_type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 616 | case PNG_COLOR_TYPE_GRAY: |
| 617 | msg << "GRAY"; |
| 618 | break; |
| 619 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 620 | msg << "GRAY + ALPHA"; |
| 621 | break; |
| 622 | case PNG_COLOR_TYPE_RGB: |
| 623 | msg << "RGB"; |
| 624 | break; |
| 625 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 626 | msg << "RGBA"; |
| 627 | break; |
| 628 | case PNG_COLOR_TYPE_PALETTE: |
| 629 | msg << "PALETTE"; |
| 630 | break; |
| 631 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 632 | msg << "unknown type " << new_color_type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 633 | break; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 634 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 635 | context->GetDiagnostics()->Note(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 636 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 637 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 638 | png_set_IHDR(write_ptr, write_info_ptr, image->width, image->height, 8, |
| 639 | new_color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 640 | PNG_FILTER_TYPE_DEFAULT); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 641 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 642 | if (new_color_type & PNG_COLOR_MASK_PALETTE) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 643 | // Assigns indices to the palette, and writes the encoded palette to the |
| 644 | // libpng writePtr. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 645 | WritePalette(write_ptr, write_info_ptr, &color_palette, &alpha_palette); |
| 646 | png_set_filter(write_ptr, 0, PNG_NO_FILTERS); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 647 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 648 | png_set_filter(write_ptr, 0, PNG_ALL_FILTERS); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 649 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 650 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 651 | if (nine_patch) { |
| 652 | WriteNinePatch(write_ptr, write_info_ptr, nine_patch); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 653 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 654 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 655 | // Flush our updates to the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 656 | png_write_info(write_ptr, write_info_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 657 | |
| 658 | // Write out each row of image data according to its encoding. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 659 | if (new_color_type == PNG_COLOR_TYPE_PALETTE) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 660 | // 1 byte/pixel. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 661 | auto out_row = std::unique_ptr<png_byte[]>(new png_byte[image->width]); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 662 | |
| 663 | for (int32_t y = 0; y < image->height; y++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 664 | png_const_bytep in_row = image->rows[y]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 665 | for (int32_t x = 0; x < image->width; x++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 666 | int rr = *in_row++; |
| 667 | int gg = *in_row++; |
| 668 | int bb = *in_row++; |
| 669 | int aa = *in_row++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 670 | if (aa == 0) { |
| 671 | // Zero out color channels when transparent. |
| 672 | rr = gg = bb = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 673 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 674 | |
| 675 | const uint32_t color = rr << 24 | gg << 16 | bb << 8 | aa; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 676 | const int idx = color_palette[color]; |
| 677 | CHECK(idx != -1); |
| 678 | out_row[x] = static_cast<png_byte>(idx); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 679 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 680 | png_write_row(write_ptr, out_row.get()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 681 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 682 | } else if (new_color_type == PNG_COLOR_TYPE_GRAY || |
| 683 | new_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 684 | const size_t bpp = new_color_type == PNG_COLOR_TYPE_GRAY ? 1 : 2; |
| 685 | auto out_row = |
| 686 | std::unique_ptr<png_byte[]>(new png_byte[image->width * bpp]); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 687 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 688 | for (int32_t y = 0; y < image->height; y++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 689 | png_const_bytep in_row = image->rows[y]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 690 | for (int32_t x = 0; x < image->width; x++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 691 | int rr = in_row[x * 4]; |
| 692 | int gg = in_row[x * 4 + 1]; |
| 693 | int bb = in_row[x * 4 + 2]; |
| 694 | int aa = in_row[x * 4 + 3]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 695 | if (aa == 0) { |
| 696 | // Zero out the gray channel when transparent. |
| 697 | rr = gg = bb = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 698 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 699 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 700 | if (grayscale) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 701 | // The image was already grayscale, red == green == blue. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 702 | out_row[x * bpp] = in_row[x * 4]; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 703 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 704 | // The image is convertible to grayscale, use linear-luminance of |
| 705 | // sRGB colorspace: |
| 706 | // https://en.wikipedia.org/wiki/Grayscale#Colorimetric_.28luminance-preserving.29_conversion_to_grayscale |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 707 | out_row[x * bpp] = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 708 | (png_byte)(rr * 0.2126f + gg * 0.7152f + bb * 0.0722f); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 709 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 710 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 711 | if (bpp == 2) { |
| 712 | // Write out alpha if we have it. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 713 | out_row[x * bpp + 1] = aa; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 716 | png_write_row(write_ptr, out_row.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 717 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 718 | } else if (new_color_type == PNG_COLOR_TYPE_RGB || |
| 719 | new_color_type == PNG_COLOR_TYPE_RGBA) { |
| 720 | const size_t bpp = new_color_type == PNG_COLOR_TYPE_RGB ? 3 : 4; |
| 721 | if (needs_to_zero_rgb_channels_of_transparent_pixels) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 722 | // The source RGBA data can't be used as-is, because we need to zero out |
| 723 | // the RGB |
| 724 | // values of transparent pixels. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 725 | auto out_row = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 726 | std::unique_ptr<png_byte[]>(new png_byte[image->width * bpp]); |
| 727 | |
| 728 | for (int32_t y = 0; y < image->height; y++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 729 | png_const_bytep in_row = image->rows[y]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 730 | for (int32_t x = 0; x < image->width; x++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 731 | int rr = *in_row++; |
| 732 | int gg = *in_row++; |
| 733 | int bb = *in_row++; |
| 734 | int aa = *in_row++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 735 | if (aa == 0) { |
| 736 | // Zero out the RGB channels when transparent. |
| 737 | rr = gg = bb = 0; |
| 738 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 739 | out_row[x * bpp] = rr; |
| 740 | out_row[x * bpp + 1] = gg; |
| 741 | out_row[x * bpp + 2] = bb; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 742 | if (bpp == 4) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 743 | out_row[x * bpp + 3] = aa; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 746 | png_write_row(write_ptr, out_row.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 747 | } |
| 748 | } else { |
| 749 | // The source image can be used as-is, just tell libpng whether or not to |
| 750 | // ignore |
| 751 | // the alpha channel. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 752 | if (new_color_type == PNG_COLOR_TYPE_RGB) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 753 | // Delete the extraneous alpha values that we appended to our buffer |
| 754 | // when reading the original values. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 755 | png_set_filler(write_ptr, 0, PNG_FILLER_AFTER); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 756 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 757 | png_write_image(write_ptr, image->rows.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 758 | } |
| 759 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 760 | LOG(FATAL) << "unreachable"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 763 | png_write_end(write_ptr, write_info_ptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 764 | return true; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 767 | } // namespace aapt |