blob: f1bc53ee9cf7581810cbed20804c75444fed5750 [file] [log] [blame]
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001/*
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 Lesinski98aa3ad2015-04-06 11:46:52 -070017#include "Png.h"
18#include "Source.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070019#include "util/BigBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Util.h"
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070021
22#include <androidfw/ResourceTypes.h>
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070023#include <png.h>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024#include <zlib.h>
25#include <iostream>
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070026#include <sstream>
27#include <string>
28#include <vector>
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070029
30namespace aapt {
31
32constexpr bool kDebug = false;
33constexpr size_t kPngSignatureSize = 8u;
34
35struct PngInfo {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 ~PngInfo() {
37 for (png_bytep row : rows) {
38 if (row != nullptr) {
39 delete[] row;
40 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070041 }
42
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 delete[] xDivs;
44 delete[] yDivs;
45 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 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 Lesinski98aa3ad2015-04-06 11:46:52 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 uint32_t width = 0;
55 uint32_t height = 0;
56 std::vector<png_bytep> rows;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 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 Lesinski98aa3ad2015-04-06 11:46:52 -070063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -070078};
79
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080static 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 Lesinski98aa3ad2015-04-06 11:46:52 -070087}
88
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089static 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 Lesinskice5e56e2016-10-21 17:56:45 -070092 png_bytep buf = outBuffer->NextBlock<png_byte>(length);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 memcpy(buf, data, length);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070094}
95
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096static void flushDataToStream(png_structp /*writePtr*/) {}
Adam Lesinski98aa3ad2015-04-06 11:46:52 -070097
98static void logWarning(png_structp readPtr, png_const_charp warningMessage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 IDiagnostics* diag =
100 reinterpret_cast<IDiagnostics*>(png_get_error_ptr(readPtr));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 diag->Warn(DiagMessage() << warningMessage);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700102}
103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104static bool readPng(IDiagnostics* diag, png_structp readPtr, png_infop infoPtr,
105 PngInfo* outInfo) {
106 if (setjmp(png_jmpbuf(readPtr))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 diag->Error(DiagMessage() << "failed reading png");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 return false;
109 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 png_set_sig_bytes(readPtr, kPngSignatureSize);
112 png_read_info(readPtr, infoPtr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 int colorType, bitDepth, interlaceType, compressionType;
115 png_get_IHDR(readPtr, infoPtr, &outInfo->width, &outInfo->height, &bitDepth,
116 &colorType, &interlaceType, &compressionType, nullptr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 if (colorType == PNG_COLOR_TYPE_PALETTE) {
119 png_set_palette_to_rgb(readPtr);
120 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8) {
123 png_set_expand_gray_1_2_4_to_8(readPtr);
124 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 if (png_get_valid(readPtr, infoPtr, PNG_INFO_tRNS)) {
127 png_set_tRNS_to_alpha(readPtr);
128 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 if (bitDepth == 16) {
131 png_set_strip_16(readPtr);
132 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 if (!(colorType & PNG_COLOR_MASK_ALPHA)) {
135 png_set_add_alpha(readPtr, 0xFF, PNG_FILLER_AFTER);
136 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 if (colorType == PNG_COLOR_TYPE_GRAY ||
139 colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
140 png_set_gray_to_rgb(readPtr);
141 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 png_set_interlace_handling(readPtr);
144 png_read_update_info(readPtr, infoPtr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 png_read_image(readPtr, outInfo->rows.data());
153 png_read_end(readPtr, infoPtr);
154 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700155}
156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157static 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700182}
183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184/*static void dump_image(int w, int h, const png_byte* const* rows, int
185color_type) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700186 int i, j, rr, gg, bb, aa;
187
188 int bpp;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 if (color_type == PNG_COLOR_TYPE_PALETTE || color_type ==
190PNG_COLOR_TYPE_GRAY) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700191 bpp = 1;
192 } else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
193 bpp = 2;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 } else if (color_type == PNG_COLOR_TYPE_RGB || color_type ==
195PNG_COLOR_TYPE_RGB_ALPHA) {
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700196 // 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 Lesinskicacb28f2016-10-19 12:18:14 -0700235#define MAX(a, b) ((a) > (b) ? (a) : (b))
236#define ABS(a) ((a) < 0 ? -(a) : (a))
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238static 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700242 png_bytepp outRows) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 bool isOpaque = true;
251 bool isPalette = true;
252 bool isGrayscale = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700253
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 if (kDebug) {
260 printf("Initial image data:\n");
261 // dump_image(w, h, imageInfo.rows.data(), PNG_COLOR_TYPE_RGB_ALPHA);
262 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700281 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700283
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700292 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 }
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 Lesinski98aa3ad2015-04-06 11:46:52 -0700361 } else {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 // 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 Lesinskice5e56e2016-10-21 17:56:45 -0700376 diag->Note(DiagMessage() << "forcing image to gray (max deviation = "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 << 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700413 } else {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 *out++ = (png_byte)(rr * 0.2126f + gg * 0.7152f + bb * 0.0722f);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700415 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 if (!isOpaque) {
417 *out++ = aa;
418 }
419 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700420 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700422}
423
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424static bool writePng(IDiagnostics* diag, png_structp writePtr,
425 png_infop infoPtr, PngInfo* info, int grayScaleTolerance) {
426 if (setjmp(png_jmpbuf(writePtr))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 diag->Error(DiagMessage() << "failed to write png");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700450 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700452
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 png_set_compression_level(writePtr, Z_BEST_COMPRESSION);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700454
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 if (kDebug) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 diag->Note(DiagMessage() << "writing image: w = " << info->width
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 << ", h = " << info->height);
458 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700459
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 png_color rgbPalette[256];
461 png_byte alphaPalette[256];
462 bool hasTransparency;
463 int paletteEntries;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700464
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 analyze_image(diag, *info, grayScaleTolerance, rgbPalette, alphaPalette,
466 &paletteEntries, &hasTransparency, &colorType, outRows);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700467
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 // 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 Lesinskice5e56e2016-10-21 17:56:45 -0700479 diag->Note(DiagMessage() << "has " << paletteEntries << " colors"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 << (hasTransparency ? " (with alpha)" : "")
481 << ", using PNG_COLOR_TYPE_PALLETTE");
482 break;
483 case PNG_COLOR_TYPE_GRAY:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 diag->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 << "is opaque gray, using PNG_COLOR_TYPE_GRAY");
486 break;
487 case PNG_COLOR_TYPE_GRAY_ALPHA:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 diag->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 << "is gray + alpha, using PNG_COLOR_TYPE_GRAY_ALPHA");
490 break;
491 case PNG_COLOR_TYPE_RGB:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 diag->Note(DiagMessage() << "is opaque RGB, using PNG_COLOR_TYPE_RGB");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 break;
494 case PNG_COLOR_TYPE_RGB_ALPHA:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 diag->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 << "is RGB + alpha, using PNG_COLOR_TYPE_RGB_ALPHA");
497 break;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700498 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700500
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700504
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700510 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 png_set_filter(writePtr, 0, PNG_NO_FILTERS);
512 } else {
513 png_set_filter(writePtr, 0, PNG_ALL_FILTERS);
514 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700515
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700529 if (kDebug) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 diag->Note(DiagMessage() << "adding 9-patch info..");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 }
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 Lesinski98aa3ad2015-04-06 11:46:52 -0700556 }
557
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 for (int i = 0; i < chunkCount; i++) {
559 unknowns[i].location = PNG_HAVE_PLTE;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700560 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 png_set_keep_unknown_chunks(writePtr, PNG_HANDLE_CHUNK_ALWAYS, chunkNames,
562 chunkCount);
563 png_set_unknown_chunks(writePtr, infoPtr, unknowns, chunkCount);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700564
565#if PNG_LIBPNG_VER < 10600
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700571#endif
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 }
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 Lesinski98aa3ad2015-04-06 11:46:52 -0700581 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 rows = info->rows.data();
583 } else {
584 rows = outRows;
585 }
586 png_write_image(writePtr, rows);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700587
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 if (kDebug) {
589 printf("Final image data:\n");
590 // dump_image(info->width, info->height, rows, colorType);
591 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700592
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 png_write_end(writePtr, infoPtr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700594
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700602
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 png_get_IHDR(writePtr, infoPtr, &width, &height, &bitDepth, &colorType,
604 &interlaceType, &compressionType, nullptr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700605
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 if (kDebug) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 diag->Note(DiagMessage() << "image written: w = " << width
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 << ", h = " << height << ", d = " << bitDepth
609 << ", colors = " << colorType
610 << ", inter = " << interlaceType
611 << ", comp = " << compressionType);
612 }
613 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700614}
615
616constexpr uint32_t kColorWhite = 0xffffffffu;
617constexpr uint32_t kColorTick = 0xff000000u;
618constexpr uint32_t kColorLayoutBoundsTick = 0xff0000ffu;
619
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620enum class TickType { kNone, kTick, kLayoutBounds, kBoth };
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700621
622static TickType tickType(png_bytep p, bool transparent, const char** outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 png_uint_32 color = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700624
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 if (transparent) {
626 if (p[3] == 0) {
627 return TickType::kNone;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700628 }
629 if (color == kColorLayoutBoundsTick) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700642 }
643
644 if (p[0] != 0 || p[1] != 0 || p[2] != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 *outError = "Ticks in transparent frame must be black or red";
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700646 }
647 return TickType::kTick;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
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 Lesinski98aa3ad2015-04-06 11:46:52 -0700668}
669
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670enum class TickState { kStart, kInside1, kOutside1 };
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700671
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672static bool getHorizontalTicks(png_bytep row, int width, bool transparent,
673 bool required, int32_t* outLeft,
674 int32_t* outRight, const char** outError,
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700675 uint8_t* outDivs, bool multipleAllowed) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 *outLeft = *outRight = -1;
677 TickState state = TickState::kStart;
678 bool found = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700679
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700689 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700694 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 }
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 Lesinski98aa3ad2015-04-06 11:46:52 -0700707 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 }
709
710 if (required && !found) {
711 *outError = "No marked region found along edge";
712 *outLeft = -1;
713 return false;
714 }
715 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700716}
717
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718static 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700725
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700735 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700740 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 }
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 Lesinski98aa3ad2015-04-06 11:46:52 -0700753 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 }
755
756 if (required && !found) {
757 *outError = "No marked region found along edge";
758 *outTop = -1;
759 return false;
760 }
761 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700762}
763
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764static 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700770
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700782 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700784
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700797 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 }
799 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700800}
801
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802static bool getVerticalLayoutBoundsTicks(png_bytepp rows, int offset,
803 int height, bool transparent,
804 bool /* required */, int32_t* outTop,
805 int32_t* outBottom,
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700806 const char** outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 *outTop = *outBottom = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700808
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700821 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700823
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700836 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 }
838 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700839}
840
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841static 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700853 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 if (opacity == 0xff) return;
855 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700856}
857
858static uint8_t maxAlphaOverRow(png_bytep row, int startX, int endX) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700865}
866
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867static 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700875}
876
877static void getOutline(PngInfo* image) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 int midX = image->width / 2;
879 int midY = image->height / 2;
880 int endX = image->width - 2;
881 int endY = image->height - 2;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700882
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700893
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700894 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700904
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700911
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700917
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 int diagonalInset = 0;
919 findMaxOpacity(image->rows.data(), innerStartX, innerStartY, innerMidX,
920 innerMidY, 1, 1, &diagonalInset);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700921
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 /* 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700929
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700936}
937
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700938static uint32_t getColor(png_bytepp rows, int left, int top, int right,
939 int bottom) {
940 png_bytep color = rows[top] + left * 4;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700941
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700942 if (left > right || top > bottom) {
943 return android::Res_png_9patch::TRANSPARENT_COLOR;
944 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700945
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700946 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700952 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 } 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700957 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958 top++;
959 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700960
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700965}
966
967static bool do9Patch(PngInfo* image, std::string* outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 image->is9Patch = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700969
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970 int W = image->width;
971 int H = image->height;
972 int i, j;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700973
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700980
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 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 Lesinski98aa3ad2015-04-06 11:46:52 -0700994
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 png_bytep p = image->rows[0];
996 bool transparent = p[3] == 0;
997 bool hasColor = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700998
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 const char* errorMsg = nullptr;
1000 int errorPixel = -1;
1001 const char* errorEdge = nullptr;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001002
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 int colorIndex = 0;
1004 std::vector<png_bytep> newRows;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001005
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001077 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001079
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 // use opacity of pixels to estimate the round rect outline
1081 getOutline(image);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001082
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001098
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 /* 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001108
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001116
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001117 image->width -= 2;
1118 W = image->width;
1119 image->height -= 2;
1120 H = image->height;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001121
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001137
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001144
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001145 numColors = numRows * numCols;
1146 image->info9Patch.numColors = numColors;
1147 image->colors.resize(numColors);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001148
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001149 // Fill in color information for each patch.
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001150
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001151 uint32_t c;
1152 top = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001153
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001154 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001167
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 // 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001173 } else {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 bottom = yDivs[j];
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001175 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001190 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001191 }
1192 left = right;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001193 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 top = bottom;
1195 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001196
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197 assert(colorIndex == numColors);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001198
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001199 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001204 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001206getout:
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 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 Lesinski98aa3ad2015-04-06 11:46:52 -07001218 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 *outError = err.str();
1220 return false;
1221 }
1222 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001223}
1224
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001225bool Png::process(const Source& source, std::istream* input,
1226 BigBuffer* outBuffer, const PngOptions& options) {
1227 png_byte signature[kPngSignatureSize];
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001228
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001229 // Read the PNG signature first.
1230 if (!input->read(reinterpret_cast<char*>(signature), kPngSignatureSize)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 mDiag->Error(DiagMessage() << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 return false;
1233 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001234
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 // If the PNG signature doesn't match, bail early.
1236 if (png_sig_cmp(signature, 0, kPngSignatureSize) != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001237 mDiag->Error(DiagMessage() << "not a valid png file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 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 Lesinskice5e56e2016-10-21 17:56:45 -07001250 mDiag->Error(DiagMessage() << "failed to allocate read ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001251 goto bail;
1252 }
1253
1254 infoPtr = png_create_info_struct(readPtr);
1255 if (!infoPtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001256 mDiag->Error(DiagMessage() << "failed to allocate info ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001257 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 Lesinskice5e56e2016-10-21 17:56:45 -07001270 if (util::EndsWith(source.path, ".9.png")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271 std::string errorMsg;
1272 if (!do9Patch(&pngInfo, &errorMsg)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001273 mDiag->Error(DiagMessage() << errorMsg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001274 goto bail;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001275 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001277
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 writePtr =
1279 png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, nullptr, nullptr);
1280 if (!writePtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 mDiag->Error(DiagMessage() << "failed to allocate write ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001282 goto bail;
1283 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001284
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001285 writeInfoPtr = png_create_info_struct(writePtr);
1286 if (!writeInfoPtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001287 mDiag->Error(DiagMessage() << "failed to allocate write info ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001288 goto bail;
1289 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001290
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 png_set_error_fn(writePtr, nullptr, nullptr, logWarning);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001292
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293 // Set the write function to write to std::ostream.
1294 png_set_write_fn(writePtr, (png_voidp)outBuffer, writeDataToStream,
1295 flushDataToStream);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001296
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297 if (!writePng(mDiag, writePtr, writeInfoPtr, &pngInfo,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 options.grayscale_tolerance)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299 goto bail;
1300 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001301
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 result = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001303bail:
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001304 if (readPtr) {
1305 png_destroy_read_struct(&readPtr, &infoPtr, nullptr);
1306 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001307
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 if (writePtr) {
1309 png_destroy_write_struct(&writePtr, &writeInfoPtr);
1310 }
1311 return result;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001312}
1313
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001314} // namespace aapt