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 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 17 | #include "Png.h" |
| 18 | #include "Source.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 19 | #include "util/BigBuffer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Util.h" |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 21 | |
| 22 | #include <androidfw/ResourceTypes.h> |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 23 | #include <png.h> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 24 | #include <zlib.h> |
| 25 | #include <iostream> |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | #include <vector> |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 29 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | constexpr bool kDebug = false; |
| 33 | constexpr size_t kPngSignatureSize = 8u; |
| 34 | |
| 35 | struct PngInfo { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | ~PngInfo() { |
| 37 | for (png_bytep row : rows) { |
| 38 | if (row != nullptr) { |
| 39 | delete[] row; |
| 40 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | delete[] xDivs; |
| 44 | delete[] yDivs; |
| 45 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | void* serialize9Patch() { |
| 48 | void* serialized = android::Res_png_9patch::serialize(info9Patch, xDivs, |
| 49 | yDivs, colors.data()); |
| 50 | reinterpret_cast<android::Res_png_9patch*>(serialized)->deviceToFile(); |
| 51 | return serialized; |
| 52 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | uint32_t width = 0; |
| 55 | uint32_t height = 0; |
| 56 | std::vector<png_bytep> rows; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | bool is9Patch = false; |
| 59 | android::Res_png_9patch info9Patch; |
| 60 | int32_t* xDivs = nullptr; |
| 61 | int32_t* yDivs = nullptr; |
| 62 | std::vector<uint32_t> colors; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | // Layout padding. |
| 65 | bool haveLayoutBounds = false; |
| 66 | int32_t layoutBoundsLeft; |
| 67 | int32_t layoutBoundsTop; |
| 68 | int32_t layoutBoundsRight; |
| 69 | int32_t layoutBoundsBottom; |
| 70 | |
| 71 | // Round rect outline description. |
| 72 | int32_t outlineInsetsLeft; |
| 73 | int32_t outlineInsetsTop; |
| 74 | int32_t outlineInsetsRight; |
| 75 | int32_t outlineInsetsBottom; |
| 76 | float outlineRadius; |
| 77 | uint8_t outlineAlpha; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | static void readDataFromStream(png_structp readPtr, png_bytep data, |
| 81 | png_size_t length) { |
| 82 | std::istream* input = |
| 83 | reinterpret_cast<std::istream*>(png_get_io_ptr(readPtr)); |
| 84 | if (!input->read(reinterpret_cast<char*>(data), length)) { |
| 85 | png_error(readPtr, strerror(errno)); |
| 86 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | static void writeDataToStream(png_structp writePtr, png_bytep data, |
| 90 | png_size_t length) { |
| 91 | BigBuffer* outBuffer = reinterpret_cast<BigBuffer*>(png_get_io_ptr(writePtr)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | png_bytep buf = outBuffer->NextBlock<png_byte>(length); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | memcpy(buf, data, length); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | static void flushDataToStream(png_structp /*writePtr*/) {} |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 97 | |
| 98 | static void logWarning(png_structp readPtr, png_const_charp warningMessage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | IDiagnostics* diag = |
| 100 | reinterpret_cast<IDiagnostics*>(png_get_error_ptr(readPtr)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | diag->Warn(DiagMessage() << warningMessage); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | static bool readPng(IDiagnostics* diag, png_structp readPtr, png_infop infoPtr, |
| 105 | PngInfo* outInfo) { |
| 106 | if (setjmp(png_jmpbuf(readPtr))) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | diag->Error(DiagMessage() << "failed reading png"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | return false; |
| 109 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 110 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | png_set_sig_bytes(readPtr, kPngSignatureSize); |
| 112 | png_read_info(readPtr, infoPtr); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | int colorType, bitDepth, interlaceType, compressionType; |
| 115 | png_get_IHDR(readPtr, infoPtr, &outInfo->width, &outInfo->height, &bitDepth, |
| 116 | &colorType, &interlaceType, &compressionType, nullptr); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 117 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | if (colorType == PNG_COLOR_TYPE_PALETTE) { |
| 119 | png_set_palette_to_rgb(readPtr); |
| 120 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8) { |
| 123 | png_set_expand_gray_1_2_4_to_8(readPtr); |
| 124 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | if (png_get_valid(readPtr, infoPtr, PNG_INFO_tRNS)) { |
| 127 | png_set_tRNS_to_alpha(readPtr); |
| 128 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 129 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | if (bitDepth == 16) { |
| 131 | png_set_strip_16(readPtr); |
| 132 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 133 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | if (!(colorType & PNG_COLOR_MASK_ALPHA)) { |
| 135 | png_set_add_alpha(readPtr, 0xFF, PNG_FILLER_AFTER); |
| 136 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | if (colorType == PNG_COLOR_TYPE_GRAY || |
| 139 | colorType == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 140 | png_set_gray_to_rgb(readPtr); |
| 141 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 142 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | png_set_interlace_handling(readPtr); |
| 144 | png_read_update_info(readPtr, infoPtr); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 145 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | const uint32_t rowBytes = png_get_rowbytes(readPtr, infoPtr); |
| 147 | outInfo->rows.resize(outInfo->height); |
| 148 | for (size_t i = 0; i < outInfo->height; i++) { |
| 149 | outInfo->rows[i] = new png_byte[rowBytes]; |
| 150 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | png_read_image(readPtr, outInfo->rows.data()); |
| 153 | png_read_end(readPtr, infoPtr); |
| 154 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | static void checkNinePatchSerialization(android::Res_png_9patch* inPatch, |
| 158 | void* data) { |
| 159 | size_t patchSize = inPatch->serializedSize(); |
| 160 | void* newData = malloc(patchSize); |
| 161 | memcpy(newData, data, patchSize); |
| 162 | android::Res_png_9patch* outPatch = inPatch->deserialize(newData); |
| 163 | outPatch->fileToDevice(); |
| 164 | // deserialization is done in place, so outPatch == newData |
| 165 | assert(outPatch == newData); |
| 166 | assert(outPatch->numXDivs == inPatch->numXDivs); |
| 167 | assert(outPatch->numYDivs == inPatch->numYDivs); |
| 168 | assert(outPatch->paddingLeft == inPatch->paddingLeft); |
| 169 | assert(outPatch->paddingRight == inPatch->paddingRight); |
| 170 | assert(outPatch->paddingTop == inPatch->paddingTop); |
| 171 | assert(outPatch->paddingBottom == inPatch->paddingBottom); |
| 172 | /* for (int i = 0; i < outPatch->numXDivs; i++) { |
| 173 | assert(outPatch->getXDivs()[i] == inPatch->getXDivs()[i]); |
| 174 | } |
| 175 | for (int i = 0; i < outPatch->numYDivs; i++) { |
| 176 | assert(outPatch->getYDivs()[i] == inPatch->getYDivs()[i]); |
| 177 | } |
| 178 | for (int i = 0; i < outPatch->numColors; i++) { |
| 179 | assert(outPatch->getColors()[i] == inPatch->getColors()[i]); |
| 180 | }*/ |
| 181 | free(newData); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | /*static void dump_image(int w, int h, const png_byte* const* rows, int |
| 185 | color_type) { |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 186 | int i, j, rr, gg, bb, aa; |
| 187 | |
| 188 | int bpp; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | if (color_type == PNG_COLOR_TYPE_PALETTE || color_type == |
| 190 | PNG_COLOR_TYPE_GRAY) { |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 191 | bpp = 1; |
| 192 | } else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 193 | bpp = 2; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | } else if (color_type == PNG_COLOR_TYPE_RGB || color_type == |
| 195 | PNG_COLOR_TYPE_RGB_ALPHA) { |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 196 | // We use a padding byte even when there is no alpha |
| 197 | bpp = 4; |
| 198 | } else { |
| 199 | printf("Unknown color type %d.\n", color_type); |
| 200 | } |
| 201 | |
| 202 | for (j = 0; j < h; j++) { |
| 203 | const png_byte* row = rows[j]; |
| 204 | for (i = 0; i < w; i++) { |
| 205 | rr = row[0]; |
| 206 | gg = row[1]; |
| 207 | bb = row[2]; |
| 208 | aa = row[3]; |
| 209 | row += bpp; |
| 210 | |
| 211 | if (i == 0) { |
| 212 | printf("Row %d:", j); |
| 213 | } |
| 214 | switch (bpp) { |
| 215 | case 1: |
| 216 | printf(" (%d)", rr); |
| 217 | break; |
| 218 | case 2: |
| 219 | printf(" (%d %d", rr, gg); |
| 220 | break; |
| 221 | case 3: |
| 222 | printf(" (%d %d %d)", rr, gg, bb); |
| 223 | break; |
| 224 | case 4: |
| 225 | printf(" (%d %d %d %d)", rr, gg, bb, aa); |
| 226 | break; |
| 227 | } |
| 228 | if (i == (w - 1)) { |
| 229 | printf("\n"); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | }*/ |
| 234 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 235 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 236 | #define ABS(a) ((a) < 0 ? -(a) : (a)) |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 237 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | static void analyze_image(IDiagnostics* diag, const PngInfo& imageInfo, |
| 239 | int grayscaleTolerance, png_colorp rgbPalette, |
| 240 | png_bytep alphaPalette, int* paletteEntries, |
| 241 | bool* hasTransparency, int* colorType, |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 242 | png_bytepp outRows) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | int w = imageInfo.width; |
| 244 | int h = imageInfo.height; |
| 245 | int i, j, rr, gg, bb, aa, idx; |
| 246 | uint32_t colors[256], col; |
| 247 | int num_colors = 0; |
| 248 | int maxGrayDeviation = 0; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 249 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | bool isOpaque = true; |
| 251 | bool isPalette = true; |
| 252 | bool isGrayscale = true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 253 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | // Scan the entire image and determine if: |
| 255 | // 1. Every pixel has R == G == B (grayscale) |
| 256 | // 2. Every pixel has A == 255 (opaque) |
| 257 | // 3. There are no more than 256 distinct RGBA colors |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 258 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | if (kDebug) { |
| 260 | printf("Initial image data:\n"); |
| 261 | // dump_image(w, h, imageInfo.rows.data(), PNG_COLOR_TYPE_RGB_ALPHA); |
| 262 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 263 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | for (j = 0; j < h; j++) { |
| 265 | const png_byte* row = imageInfo.rows[j]; |
| 266 | png_bytep out = outRows[j]; |
| 267 | for (i = 0; i < w; i++) { |
| 268 | rr = *row++; |
| 269 | gg = *row++; |
| 270 | bb = *row++; |
| 271 | aa = *row++; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 272 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | int odev = maxGrayDeviation; |
| 274 | maxGrayDeviation = MAX(ABS(rr - gg), maxGrayDeviation); |
| 275 | maxGrayDeviation = MAX(ABS(gg - bb), maxGrayDeviation); |
| 276 | maxGrayDeviation = MAX(ABS(bb - rr), maxGrayDeviation); |
| 277 | if (maxGrayDeviation > odev) { |
| 278 | if (kDebug) { |
| 279 | printf("New max dev. = %d at pixel (%d, %d) = (%d %d %d %d)\n", |
| 280 | maxGrayDeviation, i, j, rr, gg, bb, aa); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 281 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 283 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 284 | // Check if image is really grayscale |
| 285 | if (isGrayscale) { |
| 286 | if (rr != gg || rr != bb) { |
| 287 | if (kDebug) { |
| 288 | printf("Found a non-gray pixel at %d, %d = (%d %d %d %d)\n", i, j, |
| 289 | rr, gg, bb, aa); |
| 290 | } |
| 291 | isGrayscale = false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 292 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Check if image is really opaque |
| 296 | if (isOpaque) { |
| 297 | if (aa != 0xff) { |
| 298 | if (kDebug) { |
| 299 | printf("Found a non-opaque pixel at %d, %d = (%d %d %d %d)\n", i, j, |
| 300 | rr, gg, bb, aa); |
| 301 | } |
| 302 | isOpaque = false; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Check if image is really <= 256 colors |
| 307 | if (isPalette) { |
| 308 | col = (uint32_t)((rr << 24) | (gg << 16) | (bb << 8) | aa); |
| 309 | bool match = false; |
| 310 | for (idx = 0; idx < num_colors; idx++) { |
| 311 | if (colors[idx] == col) { |
| 312 | match = true; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Write the palette index for the pixel to outRows optimistically |
| 318 | // We might overwrite it later if we decide to encode as gray or |
| 319 | // gray + alpha |
| 320 | *out++ = idx; |
| 321 | if (!match) { |
| 322 | if (num_colors == 256) { |
| 323 | if (kDebug) { |
| 324 | printf("Found 257th color at %d, %d\n", i, j); |
| 325 | } |
| 326 | isPalette = false; |
| 327 | } else { |
| 328 | colors[num_colors++] = col; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | *paletteEntries = 0; |
| 336 | *hasTransparency = !isOpaque; |
| 337 | int bpp = isOpaque ? 3 : 4; |
| 338 | int paletteSize = w * h + bpp * num_colors; |
| 339 | |
| 340 | if (kDebug) { |
| 341 | printf("isGrayscale = %s\n", isGrayscale ? "true" : "false"); |
| 342 | printf("isOpaque = %s\n", isOpaque ? "true" : "false"); |
| 343 | printf("isPalette = %s\n", isPalette ? "true" : "false"); |
| 344 | printf("Size w/ palette = %d, gray+alpha = %d, rgb(a) = %d\n", paletteSize, |
| 345 | 2 * w * h, bpp * w * h); |
| 346 | printf("Max gray deviation = %d, tolerance = %d\n", maxGrayDeviation, |
| 347 | grayscaleTolerance); |
| 348 | } |
| 349 | |
| 350 | // Choose the best color type for the image. |
| 351 | // 1. Opaque gray - use COLOR_TYPE_GRAY at 1 byte/pixel |
| 352 | // 2. Gray + alpha - use COLOR_TYPE_PALETTE if the number of distinct |
| 353 | // combinations |
| 354 | // is sufficiently small, otherwise use COLOR_TYPE_GRAY_ALPHA |
| 355 | // 3. RGB(A) - use COLOR_TYPE_PALETTE if the number of distinct colors is |
| 356 | // sufficiently |
| 357 | // small, otherwise use COLOR_TYPE_RGB{_ALPHA} |
| 358 | if (isGrayscale) { |
| 359 | if (isOpaque) { |
| 360 | *colorType = PNG_COLOR_TYPE_GRAY; // 1 byte/pixel |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 361 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 362 | // Use a simple heuristic to determine whether using a palette will |
| 363 | // save space versus using gray + alpha for each pixel. |
| 364 | // This doesn't take into account chunk overhead, filtering, LZ |
| 365 | // compression, etc. |
| 366 | if (isPalette && (paletteSize < 2 * w * h)) { |
| 367 | *colorType = PNG_COLOR_TYPE_PALETTE; // 1 byte/pixel + 4 bytes/color |
| 368 | } else { |
| 369 | *colorType = PNG_COLOR_TYPE_GRAY_ALPHA; // 2 bytes per pixel |
| 370 | } |
| 371 | } |
| 372 | } else if (isPalette && (paletteSize < bpp * w * h)) { |
| 373 | *colorType = PNG_COLOR_TYPE_PALETTE; |
| 374 | } else { |
| 375 | if (maxGrayDeviation <= grayscaleTolerance) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | diag->Note(DiagMessage() << "forcing image to gray (max deviation = " |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | << maxGrayDeviation << ")"); |
| 378 | *colorType = isOpaque ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_GRAY_ALPHA; |
| 379 | } else { |
| 380 | *colorType = isOpaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Perform postprocessing of the image or palette data based on the final |
| 385 | // color type chosen |
| 386 | |
| 387 | if (*colorType == PNG_COLOR_TYPE_PALETTE) { |
| 388 | // Create separate RGB and Alpha palettes and set the number of colors |
| 389 | *paletteEntries = num_colors; |
| 390 | |
| 391 | // Create the RGB and alpha palettes |
| 392 | for (int idx = 0; idx < num_colors; idx++) { |
| 393 | col = colors[idx]; |
| 394 | rgbPalette[idx].red = (png_byte)((col >> 24) & 0xff); |
| 395 | rgbPalette[idx].green = (png_byte)((col >> 16) & 0xff); |
| 396 | rgbPalette[idx].blue = (png_byte)((col >> 8) & 0xff); |
| 397 | alphaPalette[idx] = (png_byte)(col & 0xff); |
| 398 | } |
| 399 | } else if (*colorType == PNG_COLOR_TYPE_GRAY || |
| 400 | *colorType == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 401 | // If the image is gray or gray + alpha, compact the pixels into outRows |
| 402 | for (j = 0; j < h; j++) { |
| 403 | const png_byte* row = imageInfo.rows[j]; |
| 404 | png_bytep out = outRows[j]; |
| 405 | for (i = 0; i < w; i++) { |
| 406 | rr = *row++; |
| 407 | gg = *row++; |
| 408 | bb = *row++; |
| 409 | aa = *row++; |
| 410 | |
| 411 | if (isGrayscale) { |
| 412 | *out++ = rr; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 413 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 414 | *out++ = (png_byte)(rr * 0.2126f + gg * 0.7152f + bb * 0.0722f); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 415 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | if (!isOpaque) { |
| 417 | *out++ = aa; |
| 418 | } |
| 419 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 420 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 424 | static bool writePng(IDiagnostics* diag, png_structp writePtr, |
| 425 | png_infop infoPtr, PngInfo* info, int grayScaleTolerance) { |
| 426 | if (setjmp(png_jmpbuf(writePtr))) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 427 | diag->Error(DiagMessage() << "failed to write png"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
| 431 | uint32_t width, height; |
| 432 | int colorType, bitDepth, interlaceType, compressionType; |
| 433 | |
| 434 | png_unknown_chunk unknowns[3]; |
| 435 | unknowns[0].data = nullptr; |
| 436 | unknowns[1].data = nullptr; |
| 437 | unknowns[2].data = nullptr; |
| 438 | |
| 439 | png_bytepp outRows = |
| 440 | (png_bytepp)malloc((int)info->height * sizeof(png_bytep)); |
| 441 | if (outRows == (png_bytepp)0) { |
| 442 | printf("Can't allocate output buffer!\n"); |
| 443 | exit(1); |
| 444 | } |
| 445 | for (uint32_t i = 0; i < info->height; i++) { |
| 446 | outRows[i] = (png_bytep)malloc(2 * (int)info->width); |
| 447 | if (outRows[i] == (png_bytep)0) { |
| 448 | printf("Can't allocate output buffer!\n"); |
| 449 | exit(1); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 450 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 452 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 453 | png_set_compression_level(writePtr, Z_BEST_COMPRESSION); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 454 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 455 | if (kDebug) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 456 | diag->Note(DiagMessage() << "writing image: w = " << info->width |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 457 | << ", h = " << info->height); |
| 458 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 459 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 460 | png_color rgbPalette[256]; |
| 461 | png_byte alphaPalette[256]; |
| 462 | bool hasTransparency; |
| 463 | int paletteEntries; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 464 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 465 | analyze_image(diag, *info, grayScaleTolerance, rgbPalette, alphaPalette, |
| 466 | &paletteEntries, &hasTransparency, &colorType, outRows); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 467 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 468 | // If the image is a 9-patch, we need to preserve it as a ARGB file to make |
| 469 | // sure the pixels will not be pre-dithered/clamped until we decide they are |
| 470 | if (info->is9Patch && |
| 471 | (colorType == PNG_COLOR_TYPE_RGB || colorType == PNG_COLOR_TYPE_GRAY || |
| 472 | colorType == PNG_COLOR_TYPE_PALETTE)) { |
| 473 | colorType = PNG_COLOR_TYPE_RGB_ALPHA; |
| 474 | } |
| 475 | |
| 476 | if (kDebug) { |
| 477 | switch (colorType) { |
| 478 | case PNG_COLOR_TYPE_PALETTE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 479 | diag->Note(DiagMessage() << "has " << paletteEntries << " colors" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | << (hasTransparency ? " (with alpha)" : "") |
| 481 | << ", using PNG_COLOR_TYPE_PALLETTE"); |
| 482 | break; |
| 483 | case PNG_COLOR_TYPE_GRAY: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 484 | diag->Note(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 485 | << "is opaque gray, using PNG_COLOR_TYPE_GRAY"); |
| 486 | break; |
| 487 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 488 | diag->Note(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 489 | << "is gray + alpha, using PNG_COLOR_TYPE_GRAY_ALPHA"); |
| 490 | break; |
| 491 | case PNG_COLOR_TYPE_RGB: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | diag->Note(DiagMessage() << "is opaque RGB, using PNG_COLOR_TYPE_RGB"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 493 | break; |
| 494 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 495 | diag->Note(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 496 | << "is RGB + alpha, using PNG_COLOR_TYPE_RGB_ALPHA"); |
| 497 | break; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 498 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 499 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 500 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 501 | png_set_IHDR(writePtr, infoPtr, info->width, info->height, 8, colorType, |
| 502 | PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, |
| 503 | PNG_FILTER_TYPE_DEFAULT); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 504 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 505 | if (colorType == PNG_COLOR_TYPE_PALETTE) { |
| 506 | png_set_PLTE(writePtr, infoPtr, rgbPalette, paletteEntries); |
| 507 | if (hasTransparency) { |
| 508 | png_set_tRNS(writePtr, infoPtr, alphaPalette, paletteEntries, |
| 509 | (png_color_16p)0); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 510 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 511 | png_set_filter(writePtr, 0, PNG_NO_FILTERS); |
| 512 | } else { |
| 513 | png_set_filter(writePtr, 0, PNG_ALL_FILTERS); |
| 514 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 515 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 516 | if (info->is9Patch) { |
| 517 | int chunkCount = 2 + (info->haveLayoutBounds ? 1 : 0); |
| 518 | int pIndex = info->haveLayoutBounds ? 2 : 1; |
| 519 | int bIndex = 1; |
| 520 | int oIndex = 0; |
| 521 | |
| 522 | // Chunks ordered thusly because older platforms depend on the base 9 patch |
| 523 | // data being last |
| 524 | png_bytep chunkNames = info->haveLayoutBounds |
| 525 | ? (png_bytep) "npOl\0npLb\0npTc\0" |
| 526 | : (png_bytep) "npOl\0npTc"; |
| 527 | |
| 528 | // base 9 patch data |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 529 | if (kDebug) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | diag->Note(DiagMessage() << "adding 9-patch info.."); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 531 | } |
| 532 | strcpy((char*)unknowns[pIndex].name, "npTc"); |
| 533 | unknowns[pIndex].data = (png_byte*)info->serialize9Patch(); |
| 534 | unknowns[pIndex].size = info->info9Patch.serializedSize(); |
| 535 | // TODO: remove the check below when everything works |
| 536 | checkNinePatchSerialization(&info->info9Patch, unknowns[pIndex].data); |
| 537 | |
| 538 | // automatically generated 9 patch outline data |
| 539 | int chunkSize = sizeof(png_uint_32) * 6; |
| 540 | strcpy((char*)unknowns[oIndex].name, "npOl"); |
| 541 | unknowns[oIndex].data = (png_byte*)calloc(chunkSize, 1); |
| 542 | png_byte outputData[chunkSize]; |
| 543 | memcpy(&outputData, &info->outlineInsetsLeft, 4 * sizeof(png_uint_32)); |
| 544 | ((float*)outputData)[4] = info->outlineRadius; |
| 545 | ((png_uint_32*)outputData)[5] = info->outlineAlpha; |
| 546 | memcpy(unknowns[oIndex].data, &outputData, chunkSize); |
| 547 | unknowns[oIndex].size = chunkSize; |
| 548 | |
| 549 | // optional optical inset / layout bounds data |
| 550 | if (info->haveLayoutBounds) { |
| 551 | int chunkSize = sizeof(png_uint_32) * 4; |
| 552 | strcpy((char*)unknowns[bIndex].name, "npLb"); |
| 553 | unknowns[bIndex].data = (png_byte*)calloc(chunkSize, 1); |
| 554 | memcpy(unknowns[bIndex].data, &info->layoutBoundsLeft, chunkSize); |
| 555 | unknowns[bIndex].size = chunkSize; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 558 | for (int i = 0; i < chunkCount; i++) { |
| 559 | unknowns[i].location = PNG_HAVE_PLTE; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 560 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 561 | png_set_keep_unknown_chunks(writePtr, PNG_HANDLE_CHUNK_ALWAYS, chunkNames, |
| 562 | chunkCount); |
| 563 | png_set_unknown_chunks(writePtr, infoPtr, unknowns, chunkCount); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 564 | |
| 565 | #if PNG_LIBPNG_VER < 10600 |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 566 | // Deal with unknown chunk location bug in 1.5.x and earlier. |
| 567 | png_set_unknown_chunk_location(writePtr, infoPtr, 0, PNG_HAVE_PLTE); |
| 568 | if (info->haveLayoutBounds) { |
| 569 | png_set_unknown_chunk_location(writePtr, infoPtr, 1, PNG_HAVE_PLTE); |
| 570 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 571 | #endif |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | png_write_info(writePtr, infoPtr); |
| 575 | |
| 576 | png_bytepp rows; |
| 577 | if (colorType == PNG_COLOR_TYPE_RGB || |
| 578 | colorType == PNG_COLOR_TYPE_RGB_ALPHA) { |
| 579 | if (colorType == PNG_COLOR_TYPE_RGB) { |
| 580 | png_set_filler(writePtr, 0, PNG_FILLER_AFTER); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 581 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 582 | rows = info->rows.data(); |
| 583 | } else { |
| 584 | rows = outRows; |
| 585 | } |
| 586 | png_write_image(writePtr, rows); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 587 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 588 | if (kDebug) { |
| 589 | printf("Final image data:\n"); |
| 590 | // dump_image(info->width, info->height, rows, colorType); |
| 591 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 592 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 593 | png_write_end(writePtr, infoPtr); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 594 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 595 | for (uint32_t i = 0; i < info->height; i++) { |
| 596 | free(outRows[i]); |
| 597 | } |
| 598 | free(outRows); |
| 599 | free(unknowns[0].data); |
| 600 | free(unknowns[1].data); |
| 601 | free(unknowns[2].data); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 602 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 603 | png_get_IHDR(writePtr, infoPtr, &width, &height, &bitDepth, &colorType, |
| 604 | &interlaceType, &compressionType, nullptr); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 605 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 606 | if (kDebug) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 607 | diag->Note(DiagMessage() << "image written: w = " << width |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 608 | << ", h = " << height << ", d = " << bitDepth |
| 609 | << ", colors = " << colorType |
| 610 | << ", inter = " << interlaceType |
| 611 | << ", comp = " << compressionType); |
| 612 | } |
| 613 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | constexpr uint32_t kColorWhite = 0xffffffffu; |
| 617 | constexpr uint32_t kColorTick = 0xff000000u; |
| 618 | constexpr uint32_t kColorLayoutBoundsTick = 0xff0000ffu; |
| 619 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 620 | enum class TickType { kNone, kTick, kLayoutBounds, kBoth }; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 621 | |
| 622 | static TickType tickType(png_bytep p, bool transparent, const char** outError) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 623 | png_uint_32 color = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 624 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 625 | if (transparent) { |
| 626 | if (p[3] == 0) { |
| 627 | return TickType::kNone; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 628 | } |
| 629 | if (color == kColorLayoutBoundsTick) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 630 | return TickType::kLayoutBounds; |
| 631 | } |
| 632 | if (color == kColorTick) { |
| 633 | return TickType::kTick; |
| 634 | } |
| 635 | |
| 636 | // Error cases |
| 637 | if (p[3] != 0xff) { |
| 638 | *outError = |
| 639 | "Frame pixels must be either solid or transparent " |
| 640 | "(not intermediate alphas)"; |
| 641 | return TickType::kNone; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | if (p[0] != 0 || p[1] != 0 || p[2] != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 645 | *outError = "Ticks in transparent frame must be black or red"; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 646 | } |
| 647 | return TickType::kTick; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (p[3] != 0xFF) { |
| 651 | *outError = "White frame must be a solid color (no alpha)"; |
| 652 | } |
| 653 | if (color == kColorWhite) { |
| 654 | return TickType::kNone; |
| 655 | } |
| 656 | if (color == kColorTick) { |
| 657 | return TickType::kTick; |
| 658 | } |
| 659 | if (color == kColorLayoutBoundsTick) { |
| 660 | return TickType::kLayoutBounds; |
| 661 | } |
| 662 | |
| 663 | if (p[0] != 0 || p[1] != 0 || p[2] != 0) { |
| 664 | *outError = "Ticks in white frame must be black or red"; |
| 665 | return TickType::kNone; |
| 666 | } |
| 667 | return TickType::kTick; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 670 | enum class TickState { kStart, kInside1, kOutside1 }; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 671 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 672 | static bool getHorizontalTicks(png_bytep row, int width, bool transparent, |
| 673 | bool required, int32_t* outLeft, |
| 674 | int32_t* outRight, const char** outError, |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 675 | uint8_t* outDivs, bool multipleAllowed) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 676 | *outLeft = *outRight = -1; |
| 677 | TickState state = TickState::kStart; |
| 678 | bool found = false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 679 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 680 | for (int i = 1; i < width - 1; i++) { |
| 681 | if (tickType(row + i * 4, transparent, outError) == TickType::kTick) { |
| 682 | if (state == TickState::kStart || |
| 683 | (state == TickState::kOutside1 && multipleAllowed)) { |
| 684 | *outLeft = i - 1; |
| 685 | *outRight = width - 2; |
| 686 | found = true; |
| 687 | if (outDivs != NULL) { |
| 688 | *outDivs += 2; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 689 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 690 | state = TickState::kInside1; |
| 691 | } else if (state == TickState::kOutside1) { |
| 692 | *outError = "Can't have more than one marked region along edge"; |
| 693 | *outLeft = i; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 694 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 695 | } |
| 696 | } else if (!*outError) { |
| 697 | if (state == TickState::kInside1) { |
| 698 | // We're done with this div. Move on to the next. |
| 699 | *outRight = i - 1; |
| 700 | outRight += 2; |
| 701 | outLeft += 2; |
| 702 | state = TickState::kOutside1; |
| 703 | } |
| 704 | } else { |
| 705 | *outLeft = i; |
| 706 | return false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 707 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | if (required && !found) { |
| 711 | *outError = "No marked region found along edge"; |
| 712 | *outLeft = -1; |
| 713 | return false; |
| 714 | } |
| 715 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 718 | static bool getVerticalTicks(png_bytepp rows, int offset, int height, |
| 719 | bool transparent, bool required, int32_t* outTop, |
| 720 | int32_t* outBottom, const char** outError, |
| 721 | uint8_t* outDivs, bool multipleAllowed) { |
| 722 | *outTop = *outBottom = -1; |
| 723 | TickState state = TickState::kStart; |
| 724 | bool found = false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 725 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 726 | for (int i = 1; i < height - 1; i++) { |
| 727 | if (tickType(rows[i] + offset, transparent, outError) == TickType::kTick) { |
| 728 | if (state == TickState::kStart || |
| 729 | (state == TickState::kOutside1 && multipleAllowed)) { |
| 730 | *outTop = i - 1; |
| 731 | *outBottom = height - 2; |
| 732 | found = true; |
| 733 | if (outDivs != NULL) { |
| 734 | *outDivs += 2; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 735 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 736 | state = TickState::kInside1; |
| 737 | } else if (state == TickState::kOutside1) { |
| 738 | *outError = "Can't have more than one marked region along edge"; |
| 739 | *outTop = i; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 740 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 741 | } |
| 742 | } else if (!*outError) { |
| 743 | if (state == TickState::kInside1) { |
| 744 | // We're done with this div. Move on to the next. |
| 745 | *outBottom = i - 1; |
| 746 | outTop += 2; |
| 747 | outBottom += 2; |
| 748 | state = TickState::kOutside1; |
| 749 | } |
| 750 | } else { |
| 751 | *outTop = i; |
| 752 | return false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 753 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (required && !found) { |
| 757 | *outError = "No marked region found along edge"; |
| 758 | *outTop = -1; |
| 759 | return false; |
| 760 | } |
| 761 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 764 | static bool getHorizontalLayoutBoundsTicks(png_bytep row, int width, |
| 765 | bool transparent, |
| 766 | bool /* required */, |
| 767 | int32_t* outLeft, int32_t* outRight, |
| 768 | const char** outError) { |
| 769 | *outLeft = *outRight = 0; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 770 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 771 | // Look for left tick |
| 772 | if (tickType(row + 4, transparent, outError) == TickType::kLayoutBounds) { |
| 773 | // Starting with a layout padding tick |
| 774 | int i = 1; |
| 775 | while (i < width - 1) { |
| 776 | (*outLeft)++; |
| 777 | i++; |
| 778 | if (tickType(row + i * 4, transparent, outError) != |
| 779 | TickType::kLayoutBounds) { |
| 780 | break; |
| 781 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 782 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 783 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 784 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 785 | // Look for right tick |
| 786 | if (tickType(row + (width - 2) * 4, transparent, outError) == |
| 787 | TickType::kLayoutBounds) { |
| 788 | // Ending with a layout padding tick |
| 789 | int i = width - 2; |
| 790 | while (i > 1) { |
| 791 | (*outRight)++; |
| 792 | i--; |
| 793 | if (tickType(row + i * 4, transparent, outError) != |
| 794 | TickType::kLayoutBounds) { |
| 795 | break; |
| 796 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 797 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 798 | } |
| 799 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 802 | static bool getVerticalLayoutBoundsTicks(png_bytepp rows, int offset, |
| 803 | int height, bool transparent, |
| 804 | bool /* required */, int32_t* outTop, |
| 805 | int32_t* outBottom, |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 806 | const char** outError) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 807 | *outTop = *outBottom = 0; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 808 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 809 | // Look for top tick |
| 810 | if (tickType(rows[1] + offset, transparent, outError) == |
| 811 | TickType::kLayoutBounds) { |
| 812 | // Starting with a layout padding tick |
| 813 | int i = 1; |
| 814 | while (i < height - 1) { |
| 815 | (*outTop)++; |
| 816 | i++; |
| 817 | if (tickType(rows[i] + offset, transparent, outError) != |
| 818 | TickType::kLayoutBounds) { |
| 819 | break; |
| 820 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 821 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 822 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 823 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 824 | // Look for bottom tick |
| 825 | if (tickType(rows[height - 2] + offset, transparent, outError) == |
| 826 | TickType::kLayoutBounds) { |
| 827 | // Ending with a layout padding tick |
| 828 | int i = height - 2; |
| 829 | while (i > 1) { |
| 830 | (*outBottom)++; |
| 831 | i--; |
| 832 | if (tickType(rows[i] + offset, transparent, outError) != |
| 833 | TickType::kLayoutBounds) { |
| 834 | break; |
| 835 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 836 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 837 | } |
| 838 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 841 | static void findMaxOpacity(png_bytepp rows, int startX, int startY, int endX, |
| 842 | int endY, int dX, int dY, int* outInset) { |
| 843 | uint8_t maxOpacity = 0; |
| 844 | int inset = 0; |
| 845 | *outInset = 0; |
| 846 | for (int x = startX, y = startY; x != endX && y != endY; |
| 847 | x += dX, y += dY, inset++) { |
| 848 | png_byte* color = rows[y] + x * 4; |
| 849 | uint8_t opacity = color[3]; |
| 850 | if (opacity > maxOpacity) { |
| 851 | maxOpacity = opacity; |
| 852 | *outInset = inset; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 853 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 854 | if (opacity == 0xff) return; |
| 855 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static uint8_t maxAlphaOverRow(png_bytep row, int startX, int endX) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 859 | uint8_t maxAlpha = 0; |
| 860 | for (int x = startX; x < endX; x++) { |
| 861 | uint8_t alpha = (row + x * 4)[3]; |
| 862 | if (alpha > maxAlpha) maxAlpha = alpha; |
| 863 | } |
| 864 | return maxAlpha; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 867 | static uint8_t maxAlphaOverCol(png_bytepp rows, int offsetX, int startY, |
| 868 | int endY) { |
| 869 | uint8_t maxAlpha = 0; |
| 870 | for (int y = startY; y < endY; y++) { |
| 871 | uint8_t alpha = (rows[y] + offsetX * 4)[3]; |
| 872 | if (alpha > maxAlpha) maxAlpha = alpha; |
| 873 | } |
| 874 | return maxAlpha; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static void getOutline(PngInfo* image) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 878 | int midX = image->width / 2; |
| 879 | int midY = image->height / 2; |
| 880 | int endX = image->width - 2; |
| 881 | int endY = image->height - 2; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 882 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 883 | // find left and right extent of nine patch content on center row |
| 884 | if (image->width > 4) { |
| 885 | findMaxOpacity(image->rows.data(), 1, midY, midX, -1, 1, 0, |
| 886 | &image->outlineInsetsLeft); |
| 887 | findMaxOpacity(image->rows.data(), endX, midY, midX, -1, -1, 0, |
| 888 | &image->outlineInsetsRight); |
| 889 | } else { |
| 890 | image->outlineInsetsLeft = 0; |
| 891 | image->outlineInsetsRight = 0; |
| 892 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 893 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 894 | // find top and bottom extent of nine patch content on center column |
| 895 | if (image->height > 4) { |
| 896 | findMaxOpacity(image->rows.data(), midX, 1, -1, midY, 0, 1, |
| 897 | &image->outlineInsetsTop); |
| 898 | findMaxOpacity(image->rows.data(), midX, endY, -1, midY, 0, -1, |
| 899 | &image->outlineInsetsBottom); |
| 900 | } else { |
| 901 | image->outlineInsetsTop = 0; |
| 902 | image->outlineInsetsBottom = 0; |
| 903 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 904 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 905 | int innerStartX = 1 + image->outlineInsetsLeft; |
| 906 | int innerStartY = 1 + image->outlineInsetsTop; |
| 907 | int innerEndX = endX - image->outlineInsetsRight; |
| 908 | int innerEndY = endY - image->outlineInsetsBottom; |
| 909 | int innerMidX = (innerEndX + innerStartX) / 2; |
| 910 | int innerMidY = (innerEndY + innerStartY) / 2; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 911 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 912 | // assuming the image is a round rect, compute the radius by marching |
| 913 | // diagonally from the top left corner towards the center |
| 914 | image->outlineAlpha = std::max( |
| 915 | maxAlphaOverRow(image->rows[innerMidY], innerStartX, innerEndX), |
| 916 | maxAlphaOverCol(image->rows.data(), innerMidX, innerStartY, innerStartY)); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 917 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 918 | int diagonalInset = 0; |
| 919 | findMaxOpacity(image->rows.data(), innerStartX, innerStartY, innerMidX, |
| 920 | innerMidY, 1, 1, &diagonalInset); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 921 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 922 | /* Determine source radius based upon inset: |
| 923 | * sqrt(r^2 + r^2) = sqrt(i^2 + i^2) + r |
| 924 | * sqrt(2) * r = sqrt(2) * i + r |
| 925 | * (sqrt(2) - 1) * r = sqrt(2) * i |
| 926 | * r = sqrt(2) / (sqrt(2) - 1) * i |
| 927 | */ |
| 928 | image->outlineRadius = 3.4142f * diagonalInset; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 929 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 930 | if (kDebug) { |
| 931 | printf("outline insets %d %d %d %d, rad %f, alpha %x\n", |
| 932 | image->outlineInsetsLeft, image->outlineInsetsTop, |
| 933 | image->outlineInsetsRight, image->outlineInsetsBottom, |
| 934 | image->outlineRadius, image->outlineAlpha); |
| 935 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 938 | static uint32_t getColor(png_bytepp rows, int left, int top, int right, |
| 939 | int bottom) { |
| 940 | png_bytep color = rows[top] + left * 4; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 941 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 942 | if (left > right || top > bottom) { |
| 943 | return android::Res_png_9patch::TRANSPARENT_COLOR; |
| 944 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 945 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 946 | while (top <= bottom) { |
| 947 | for (int i = left; i <= right; i++) { |
| 948 | png_bytep p = rows[top] + i * 4; |
| 949 | if (color[3] == 0) { |
| 950 | if (p[3] != 0) { |
| 951 | return android::Res_png_9patch::NO_COLOR; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 952 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 953 | } else if (p[0] != color[0] || p[1] != color[1] || p[2] != color[2] || |
| 954 | p[3] != color[3]) { |
| 955 | return android::Res_png_9patch::NO_COLOR; |
| 956 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 957 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 958 | top++; |
| 959 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 960 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 961 | if (color[3] == 0) { |
| 962 | return android::Res_png_9patch::TRANSPARENT_COLOR; |
| 963 | } |
| 964 | return (color[3] << 24) | (color[0] << 16) | (color[1] << 8) | color[2]; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static bool do9Patch(PngInfo* image, std::string* outError) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 968 | image->is9Patch = true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 969 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 970 | int W = image->width; |
| 971 | int H = image->height; |
| 972 | int i, j; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 973 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 974 | const int maxSizeXDivs = W * sizeof(int32_t); |
| 975 | const int maxSizeYDivs = H * sizeof(int32_t); |
| 976 | int32_t* xDivs = image->xDivs = new int32_t[W]; |
| 977 | int32_t* yDivs = image->yDivs = new int32_t[H]; |
| 978 | uint8_t numXDivs = 0; |
| 979 | uint8_t numYDivs = 0; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 980 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 981 | int8_t numColors; |
| 982 | int numRows; |
| 983 | int numCols; |
| 984 | int top; |
| 985 | int left; |
| 986 | int right; |
| 987 | int bottom; |
| 988 | memset(xDivs, -1, maxSizeXDivs); |
| 989 | memset(yDivs, -1, maxSizeYDivs); |
| 990 | image->info9Patch.paddingLeft = image->info9Patch.paddingRight = -1; |
| 991 | image->info9Patch.paddingTop = image->info9Patch.paddingBottom = -1; |
| 992 | image->layoutBoundsLeft = image->layoutBoundsRight = 0; |
| 993 | image->layoutBoundsTop = image->layoutBoundsBottom = 0; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 994 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 995 | png_bytep p = image->rows[0]; |
| 996 | bool transparent = p[3] == 0; |
| 997 | bool hasColor = false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 998 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 999 | const char* errorMsg = nullptr; |
| 1000 | int errorPixel = -1; |
| 1001 | const char* errorEdge = nullptr; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1002 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1003 | int colorIndex = 0; |
| 1004 | std::vector<png_bytep> newRows; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1005 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1006 | // Validate size... |
| 1007 | if (W < 3 || H < 3) { |
| 1008 | errorMsg = "Image must be at least 3x3 (1x1 without frame) pixels"; |
| 1009 | goto getout; |
| 1010 | } |
| 1011 | |
| 1012 | // Validate frame... |
| 1013 | if (!transparent && |
| 1014 | (p[0] != 0xFF || p[1] != 0xFF || p[2] != 0xFF || p[3] != 0xFF)) { |
| 1015 | errorMsg = "Must have one-pixel frame that is either transparent or white"; |
| 1016 | goto getout; |
| 1017 | } |
| 1018 | |
| 1019 | // Find left and right of sizing areas... |
| 1020 | if (!getHorizontalTicks(p, W, transparent, true, &xDivs[0], &xDivs[1], |
| 1021 | &errorMsg, &numXDivs, true)) { |
| 1022 | errorPixel = xDivs[0]; |
| 1023 | errorEdge = "top"; |
| 1024 | goto getout; |
| 1025 | } |
| 1026 | |
| 1027 | // Find top and bottom of sizing areas... |
| 1028 | if (!getVerticalTicks(image->rows.data(), 0, H, transparent, true, &yDivs[0], |
| 1029 | &yDivs[1], &errorMsg, &numYDivs, true)) { |
| 1030 | errorPixel = yDivs[0]; |
| 1031 | errorEdge = "left"; |
| 1032 | goto getout; |
| 1033 | } |
| 1034 | |
| 1035 | // Copy patch size data into image... |
| 1036 | image->info9Patch.numXDivs = numXDivs; |
| 1037 | image->info9Patch.numYDivs = numYDivs; |
| 1038 | |
| 1039 | // Find left and right of padding area... |
| 1040 | if (!getHorizontalTicks(image->rows[H - 1], W, transparent, false, |
| 1041 | &image->info9Patch.paddingLeft, |
| 1042 | &image->info9Patch.paddingRight, &errorMsg, nullptr, |
| 1043 | false)) { |
| 1044 | errorPixel = image->info9Patch.paddingLeft; |
| 1045 | errorEdge = "bottom"; |
| 1046 | goto getout; |
| 1047 | } |
| 1048 | |
| 1049 | // Find top and bottom of padding area... |
| 1050 | if (!getVerticalTicks(image->rows.data(), (W - 1) * 4, H, transparent, false, |
| 1051 | &image->info9Patch.paddingTop, |
| 1052 | &image->info9Patch.paddingBottom, &errorMsg, nullptr, |
| 1053 | false)) { |
| 1054 | errorPixel = image->info9Patch.paddingTop; |
| 1055 | errorEdge = "right"; |
| 1056 | goto getout; |
| 1057 | } |
| 1058 | |
| 1059 | // Find left and right of layout padding... |
| 1060 | getHorizontalLayoutBoundsTicks(image->rows[H - 1], W, transparent, false, |
| 1061 | &image->layoutBoundsLeft, |
| 1062 | &image->layoutBoundsRight, &errorMsg); |
| 1063 | |
| 1064 | getVerticalLayoutBoundsTicks(image->rows.data(), (W - 1) * 4, H, transparent, |
| 1065 | false, &image->layoutBoundsTop, |
| 1066 | &image->layoutBoundsBottom, &errorMsg); |
| 1067 | |
| 1068 | image->haveLayoutBounds = |
| 1069 | image->layoutBoundsLeft != 0 || image->layoutBoundsRight != 0 || |
| 1070 | image->layoutBoundsTop != 0 || image->layoutBoundsBottom != 0; |
| 1071 | |
| 1072 | if (image->haveLayoutBounds) { |
| 1073 | if (kDebug) { |
| 1074 | printf("layoutBounds=%d %d %d %d\n", image->layoutBoundsLeft, |
| 1075 | image->layoutBoundsTop, image->layoutBoundsRight, |
| 1076 | image->layoutBoundsBottom); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1077 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1078 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1079 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1080 | // use opacity of pixels to estimate the round rect outline |
| 1081 | getOutline(image); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1082 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1083 | // If padding is not yet specified, take values from size. |
| 1084 | if (image->info9Patch.paddingLeft < 0) { |
| 1085 | image->info9Patch.paddingLeft = xDivs[0]; |
| 1086 | image->info9Patch.paddingRight = W - 2 - xDivs[1]; |
| 1087 | } else { |
| 1088 | // Adjust value to be correct! |
| 1089 | image->info9Patch.paddingRight = W - 2 - image->info9Patch.paddingRight; |
| 1090 | } |
| 1091 | if (image->info9Patch.paddingTop < 0) { |
| 1092 | image->info9Patch.paddingTop = yDivs[0]; |
| 1093 | image->info9Patch.paddingBottom = H - 2 - yDivs[1]; |
| 1094 | } else { |
| 1095 | // Adjust value to be correct! |
| 1096 | image->info9Patch.paddingBottom = H - 2 - image->info9Patch.paddingBottom; |
| 1097 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1098 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1099 | /* if (kDebug) { |
| 1100 | printf("Size ticks for %s: x0=%d, x1=%d, y0=%d, y1=%d\n", imageName, |
| 1101 | xDivs[0], xDivs[1], |
| 1102 | yDivs[0], yDivs[1]); |
| 1103 | printf("padding ticks for %s: l=%d, r=%d, t=%d, b=%d\n", imageName, |
| 1104 | image->info9Patch.paddingLeft, image->info9Patch.paddingRight, |
| 1105 | image->info9Patch.paddingTop, |
| 1106 | image->info9Patch.paddingBottom); |
| 1107 | }*/ |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1108 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1109 | // Remove frame from image. |
| 1110 | newRows.resize(H - 2); |
| 1111 | for (i = 0; i < H - 2; i++) { |
| 1112 | newRows[i] = image->rows[i + 1]; |
| 1113 | memmove(newRows[i], newRows[i] + 4, (W - 2) * 4); |
| 1114 | } |
| 1115 | image->rows.swap(newRows); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1116 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1117 | image->width -= 2; |
| 1118 | W = image->width; |
| 1119 | image->height -= 2; |
| 1120 | H = image->height; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1122 | // Figure out the number of rows and columns in the N-patch |
| 1123 | numCols = numXDivs + 1; |
| 1124 | if (xDivs[0] == 0) { // Column 1 is strechable |
| 1125 | numCols--; |
| 1126 | } |
| 1127 | if (xDivs[numXDivs - 1] == W) { |
| 1128 | numCols--; |
| 1129 | } |
| 1130 | numRows = numYDivs + 1; |
| 1131 | if (yDivs[0] == 0) { // Row 1 is strechable |
| 1132 | numRows--; |
| 1133 | } |
| 1134 | if (yDivs[numYDivs - 1] == H) { |
| 1135 | numRows--; |
| 1136 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1137 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1138 | // Make sure the amount of rows and columns will fit in the number of |
| 1139 | // colors we can use in the 9-patch format. |
| 1140 | if (numRows * numCols > 0x7F) { |
| 1141 | errorMsg = "Too many rows and columns in 9-patch perimeter"; |
| 1142 | goto getout; |
| 1143 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1144 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1145 | numColors = numRows * numCols; |
| 1146 | image->info9Patch.numColors = numColors; |
| 1147 | image->colors.resize(numColors); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1148 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1149 | // Fill in color information for each patch. |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1150 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1151 | uint32_t c; |
| 1152 | top = 0; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1153 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1154 | // The first row always starts with the top being at y=0 and the bottom |
| 1155 | // being either yDivs[1] (if yDivs[0]=0) of yDivs[0]. In the former case |
| 1156 | // the first row is stretchable along the Y axis, otherwise it is fixed. |
| 1157 | // The last row always ends with the bottom being bitmap.height and the top |
| 1158 | // being either yDivs[numYDivs-2] (if yDivs[numYDivs-1]=bitmap.height) or |
| 1159 | // yDivs[numYDivs-1]. In the former case the last row is stretchable along |
| 1160 | // the Y axis, otherwise it is fixed. |
| 1161 | // |
| 1162 | // The first and last columns are similarly treated with respect to the X |
| 1163 | // axis. |
| 1164 | // |
| 1165 | // The above is to help explain some of the special casing that goes on the |
| 1166 | // code below. |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1168 | // The initial yDiv and whether the first row is considered stretchable or |
| 1169 | // not depends on whether yDiv[0] was zero or not. |
| 1170 | for (j = (yDivs[0] == 0 ? 1 : 0); j <= numYDivs && top < H; j++) { |
| 1171 | if (j == numYDivs) { |
| 1172 | bottom = H; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1173 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1174 | bottom = yDivs[j]; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1175 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1176 | left = 0; |
| 1177 | // The initial xDiv and whether the first column is considered |
| 1178 | // stretchable or not depends on whether xDiv[0] was zero or not. |
| 1179 | for (i = xDivs[0] == 0 ? 1 : 0; i <= numXDivs && left < W; i++) { |
| 1180 | if (i == numXDivs) { |
| 1181 | right = W; |
| 1182 | } else { |
| 1183 | right = xDivs[i]; |
| 1184 | } |
| 1185 | c = getColor(image->rows.data(), left, top, right - 1, bottom - 1); |
| 1186 | image->colors[colorIndex++] = c; |
| 1187 | if (kDebug) { |
| 1188 | if (c != android::Res_png_9patch::NO_COLOR) { |
| 1189 | hasColor = true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1190 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1191 | } |
| 1192 | left = right; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1193 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1194 | top = bottom; |
| 1195 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1196 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1197 | assert(colorIndex == numColors); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1198 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1199 | if (kDebug && hasColor) { |
| 1200 | for (i = 0; i < numColors; i++) { |
| 1201 | if (i == 0) printf("Colors:\n"); |
| 1202 | printf(" #%08x", image->colors[i]); |
| 1203 | if (i == numColors - 1) printf("\n"); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1204 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1205 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1206 | getout: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1207 | if (errorMsg) { |
| 1208 | std::stringstream err; |
| 1209 | err << "9-patch malformed: " << errorMsg; |
| 1210 | if (errorEdge) { |
| 1211 | err << "." << std::endl; |
| 1212 | if (errorPixel >= 0) { |
| 1213 | err << "Found at pixel #" << errorPixel << " along " << errorEdge |
| 1214 | << " edge"; |
| 1215 | } else { |
| 1216 | err << "Found along " << errorEdge << " edge"; |
| 1217 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1218 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1219 | *outError = err.str(); |
| 1220 | return false; |
| 1221 | } |
| 1222 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1225 | bool Png::process(const Source& source, std::istream* input, |
| 1226 | BigBuffer* outBuffer, const PngOptions& options) { |
| 1227 | png_byte signature[kPngSignatureSize]; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1228 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1229 | // Read the PNG signature first. |
| 1230 | if (!input->read(reinterpret_cast<char*>(signature), kPngSignatureSize)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1231 | mDiag->Error(DiagMessage() << strerror(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1232 | return false; |
| 1233 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1234 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1235 | // If the PNG signature doesn't match, bail early. |
| 1236 | if (png_sig_cmp(signature, 0, kPngSignatureSize) != 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1237 | mDiag->Error(DiagMessage() << "not a valid png file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | bool result = false; |
| 1242 | png_structp readPtr = nullptr; |
| 1243 | png_infop infoPtr = nullptr; |
| 1244 | png_structp writePtr = nullptr; |
| 1245 | png_infop writeInfoPtr = nullptr; |
| 1246 | PngInfo pngInfo = {}; |
| 1247 | |
| 1248 | readPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, nullptr, nullptr); |
| 1249 | if (!readPtr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1250 | mDiag->Error(DiagMessage() << "failed to allocate read ptr"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1251 | goto bail; |
| 1252 | } |
| 1253 | |
| 1254 | infoPtr = png_create_info_struct(readPtr); |
| 1255 | if (!infoPtr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1256 | mDiag->Error(DiagMessage() << "failed to allocate info ptr"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1257 | goto bail; |
| 1258 | } |
| 1259 | |
| 1260 | png_set_error_fn(readPtr, reinterpret_cast<png_voidp>(mDiag), nullptr, |
| 1261 | logWarning); |
| 1262 | |
| 1263 | // Set the read function to read from std::istream. |
| 1264 | png_set_read_fn(readPtr, (png_voidp)input, readDataFromStream); |
| 1265 | |
| 1266 | if (!readPng(mDiag, readPtr, infoPtr, &pngInfo)) { |
| 1267 | goto bail; |
| 1268 | } |
| 1269 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1270 | if (util::EndsWith(source.path, ".9.png")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1271 | std::string errorMsg; |
| 1272 | if (!do9Patch(&pngInfo, &errorMsg)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1273 | mDiag->Error(DiagMessage() << errorMsg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1274 | goto bail; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1275 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1276 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1277 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1278 | writePtr = |
| 1279 | png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, nullptr, nullptr); |
| 1280 | if (!writePtr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1281 | mDiag->Error(DiagMessage() << "failed to allocate write ptr"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1282 | goto bail; |
| 1283 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1284 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1285 | writeInfoPtr = png_create_info_struct(writePtr); |
| 1286 | if (!writeInfoPtr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1287 | mDiag->Error(DiagMessage() << "failed to allocate write info ptr"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1288 | goto bail; |
| 1289 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1290 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1291 | png_set_error_fn(writePtr, nullptr, nullptr, logWarning); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1292 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1293 | // Set the write function to write to std::ostream. |
| 1294 | png_set_write_fn(writePtr, (png_voidp)outBuffer, writeDataToStream, |
| 1295 | flushDataToStream); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1296 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1297 | if (!writePng(mDiag, writePtr, writeInfoPtr, &pngInfo, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1298 | options.grayscale_tolerance)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1299 | goto bail; |
| 1300 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1301 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1302 | result = true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1303 | bail: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1304 | if (readPtr) { |
| 1305 | png_destroy_read_struct(&readPtr, &infoPtr, nullptr); |
| 1306 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1307 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1308 | if (writePtr) { |
| 1309 | png_destroy_write_struct(&writePtr, &writeInfoPtr); |
| 1310 | } |
| 1311 | return result; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1314 | } // namespace aapt |