blob: 7ab05b58b8b9438319a59373f56d25870f2bf422 [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
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100235#ifdef MAX
236#undef MAX
237#endif
238#ifdef ABS
239#undef ABS
240#endif
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242#define MAX(a, b) ((a) > (b) ? (a) : (b))
243#define ABS(a) ((a) < 0 ? -(a) : (a))
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245static void analyze_image(IDiagnostics* diag, const PngInfo& imageInfo,
246 int grayscaleTolerance, png_colorp rgbPalette,
247 png_bytep alphaPalette, int* paletteEntries,
248 bool* hasTransparency, int* colorType,
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700249 png_bytepp outRows) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 int w = imageInfo.width;
251 int h = imageInfo.height;
252 int i, j, rr, gg, bb, aa, idx;
253 uint32_t colors[256], col;
254 int num_colors = 0;
255 int maxGrayDeviation = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 bool isOpaque = true;
258 bool isPalette = true;
259 bool isGrayscale = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 // Scan the entire image and determine if:
262 // 1. Every pixel has R == G == B (grayscale)
263 // 2. Every pixel has A == 255 (opaque)
264 // 3. There are no more than 256 distinct RGBA colors
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700265
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 if (kDebug) {
267 printf("Initial image data:\n");
268 // dump_image(w, h, imageInfo.rows.data(), PNG_COLOR_TYPE_RGB_ALPHA);
269 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 for (j = 0; j < h; j++) {
272 const png_byte* row = imageInfo.rows[j];
273 png_bytep out = outRows[j];
274 for (i = 0; i < w; i++) {
275 rr = *row++;
276 gg = *row++;
277 bb = *row++;
278 aa = *row++;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 int odev = maxGrayDeviation;
281 maxGrayDeviation = MAX(ABS(rr - gg), maxGrayDeviation);
282 maxGrayDeviation = MAX(ABS(gg - bb), maxGrayDeviation);
283 maxGrayDeviation = MAX(ABS(bb - rr), maxGrayDeviation);
284 if (maxGrayDeviation > odev) {
285 if (kDebug) {
286 printf("New max dev. = %d at pixel (%d, %d) = (%d %d %d %d)\n",
287 maxGrayDeviation, i, j, rr, gg, bb, aa);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700288 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700290
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 // Check if image is really grayscale
292 if (isGrayscale) {
293 if (rr != gg || rr != bb) {
294 if (kDebug) {
295 printf("Found a non-gray pixel at %d, %d = (%d %d %d %d)\n", i, j,
296 rr, gg, bb, aa);
297 }
298 isGrayscale = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700299 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 }
301
302 // Check if image is really opaque
303 if (isOpaque) {
304 if (aa != 0xff) {
305 if (kDebug) {
306 printf("Found a non-opaque pixel at %d, %d = (%d %d %d %d)\n", i, j,
307 rr, gg, bb, aa);
308 }
309 isOpaque = false;
310 }
311 }
312
313 // Check if image is really <= 256 colors
314 if (isPalette) {
315 col = (uint32_t)((rr << 24) | (gg << 16) | (bb << 8) | aa);
316 bool match = false;
317 for (idx = 0; idx < num_colors; idx++) {
318 if (colors[idx] == col) {
319 match = true;
320 break;
321 }
322 }
323
324 // Write the palette index for the pixel to outRows optimistically
325 // We might overwrite it later if we decide to encode as gray or
326 // gray + alpha
327 *out++ = idx;
328 if (!match) {
329 if (num_colors == 256) {
330 if (kDebug) {
331 printf("Found 257th color at %d, %d\n", i, j);
332 }
333 isPalette = false;
334 } else {
335 colors[num_colors++] = col;
336 }
337 }
338 }
339 }
340 }
341
342 *paletteEntries = 0;
343 *hasTransparency = !isOpaque;
344 int bpp = isOpaque ? 3 : 4;
345 int paletteSize = w * h + bpp * num_colors;
346
347 if (kDebug) {
348 printf("isGrayscale = %s\n", isGrayscale ? "true" : "false");
349 printf("isOpaque = %s\n", isOpaque ? "true" : "false");
350 printf("isPalette = %s\n", isPalette ? "true" : "false");
351 printf("Size w/ palette = %d, gray+alpha = %d, rgb(a) = %d\n", paletteSize,
352 2 * w * h, bpp * w * h);
353 printf("Max gray deviation = %d, tolerance = %d\n", maxGrayDeviation,
354 grayscaleTolerance);
355 }
356
357 // Choose the best color type for the image.
358 // 1. Opaque gray - use COLOR_TYPE_GRAY at 1 byte/pixel
359 // 2. Gray + alpha - use COLOR_TYPE_PALETTE if the number of distinct
360 // combinations
361 // is sufficiently small, otherwise use COLOR_TYPE_GRAY_ALPHA
362 // 3. RGB(A) - use COLOR_TYPE_PALETTE if the number of distinct colors is
363 // sufficiently
364 // small, otherwise use COLOR_TYPE_RGB{_ALPHA}
365 if (isGrayscale) {
366 if (isOpaque) {
367 *colorType = PNG_COLOR_TYPE_GRAY; // 1 byte/pixel
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700368 } else {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 // Use a simple heuristic to determine whether using a palette will
370 // save space versus using gray + alpha for each pixel.
371 // This doesn't take into account chunk overhead, filtering, LZ
372 // compression, etc.
373 if (isPalette && (paletteSize < 2 * w * h)) {
374 *colorType = PNG_COLOR_TYPE_PALETTE; // 1 byte/pixel + 4 bytes/color
375 } else {
376 *colorType = PNG_COLOR_TYPE_GRAY_ALPHA; // 2 bytes per pixel
377 }
378 }
379 } else if (isPalette && (paletteSize < bpp * w * h)) {
380 *colorType = PNG_COLOR_TYPE_PALETTE;
381 } else {
382 if (maxGrayDeviation <= grayscaleTolerance) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 diag->Note(DiagMessage() << "forcing image to gray (max deviation = "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 << maxGrayDeviation << ")");
385 *colorType = isOpaque ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_GRAY_ALPHA;
386 } else {
387 *colorType = isOpaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
388 }
389 }
390
391 // Perform postprocessing of the image or palette data based on the final
392 // color type chosen
393
394 if (*colorType == PNG_COLOR_TYPE_PALETTE) {
395 // Create separate RGB and Alpha palettes and set the number of colors
396 *paletteEntries = num_colors;
397
398 // Create the RGB and alpha palettes
399 for (int idx = 0; idx < num_colors; idx++) {
400 col = colors[idx];
401 rgbPalette[idx].red = (png_byte)((col >> 24) & 0xff);
402 rgbPalette[idx].green = (png_byte)((col >> 16) & 0xff);
403 rgbPalette[idx].blue = (png_byte)((col >> 8) & 0xff);
404 alphaPalette[idx] = (png_byte)(col & 0xff);
405 }
406 } else if (*colorType == PNG_COLOR_TYPE_GRAY ||
407 *colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
408 // If the image is gray or gray + alpha, compact the pixels into outRows
409 for (j = 0; j < h; j++) {
410 const png_byte* row = imageInfo.rows[j];
411 png_bytep out = outRows[j];
412 for (i = 0; i < w; i++) {
413 rr = *row++;
414 gg = *row++;
415 bb = *row++;
416 aa = *row++;
417
418 if (isGrayscale) {
419 *out++ = rr;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700420 } else {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 *out++ = (png_byte)(rr * 0.2126f + gg * 0.7152f + bb * 0.0722f);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700422 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 if (!isOpaque) {
424 *out++ = aa;
425 }
426 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700427 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700429}
430
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431static bool writePng(IDiagnostics* diag, png_structp writePtr,
432 png_infop infoPtr, PngInfo* info, int grayScaleTolerance) {
433 if (setjmp(png_jmpbuf(writePtr))) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 diag->Error(DiagMessage() << "failed to write png");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 return false;
436 }
437
438 uint32_t width, height;
439 int colorType, bitDepth, interlaceType, compressionType;
440
441 png_unknown_chunk unknowns[3];
442 unknowns[0].data = nullptr;
443 unknowns[1].data = nullptr;
444 unknowns[2].data = nullptr;
445
446 png_bytepp outRows =
447 (png_bytepp)malloc((int)info->height * sizeof(png_bytep));
448 if (outRows == (png_bytepp)0) {
449 printf("Can't allocate output buffer!\n");
450 exit(1);
451 }
452 for (uint32_t i = 0; i < info->height; i++) {
453 outRows[i] = (png_bytep)malloc(2 * (int)info->width);
454 if (outRows[i] == (png_bytep)0) {
455 printf("Can't allocate output buffer!\n");
456 exit(1);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700457 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700459
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 png_set_compression_level(writePtr, Z_BEST_COMPRESSION);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700461
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 if (kDebug) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 diag->Note(DiagMessage() << "writing image: w = " << info->width
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 << ", h = " << info->height);
465 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700466
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 png_color rgbPalette[256];
468 png_byte alphaPalette[256];
469 bool hasTransparency;
470 int paletteEntries;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700471
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 analyze_image(diag, *info, grayScaleTolerance, rgbPalette, alphaPalette,
473 &paletteEntries, &hasTransparency, &colorType, outRows);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700474
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 // If the image is a 9-patch, we need to preserve it as a ARGB file to make
476 // sure the pixels will not be pre-dithered/clamped until we decide they are
477 if (info->is9Patch &&
478 (colorType == PNG_COLOR_TYPE_RGB || colorType == PNG_COLOR_TYPE_GRAY ||
479 colorType == PNG_COLOR_TYPE_PALETTE)) {
480 colorType = PNG_COLOR_TYPE_RGB_ALPHA;
481 }
482
483 if (kDebug) {
484 switch (colorType) {
485 case PNG_COLOR_TYPE_PALETTE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 diag->Note(DiagMessage() << "has " << paletteEntries << " colors"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 << (hasTransparency ? " (with alpha)" : "")
488 << ", using PNG_COLOR_TYPE_PALLETTE");
489 break;
490 case PNG_COLOR_TYPE_GRAY:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 diag->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 << "is opaque gray, using PNG_COLOR_TYPE_GRAY");
493 break;
494 case PNG_COLOR_TYPE_GRAY_ALPHA:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 diag->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 << "is gray + alpha, using PNG_COLOR_TYPE_GRAY_ALPHA");
497 break;
498 case PNG_COLOR_TYPE_RGB:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 diag->Note(DiagMessage() << "is opaque RGB, using PNG_COLOR_TYPE_RGB");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 break;
501 case PNG_COLOR_TYPE_RGB_ALPHA:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 diag->Note(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 << "is RGB + alpha, using PNG_COLOR_TYPE_RGB_ALPHA");
504 break;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700505 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700507
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 png_set_IHDR(writePtr, infoPtr, info->width, info->height, 8, colorType,
509 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
510 PNG_FILTER_TYPE_DEFAULT);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700511
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 if (colorType == PNG_COLOR_TYPE_PALETTE) {
513 png_set_PLTE(writePtr, infoPtr, rgbPalette, paletteEntries);
514 if (hasTransparency) {
515 png_set_tRNS(writePtr, infoPtr, alphaPalette, paletteEntries,
516 (png_color_16p)0);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700517 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 png_set_filter(writePtr, 0, PNG_NO_FILTERS);
519 } else {
520 png_set_filter(writePtr, 0, PNG_ALL_FILTERS);
521 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700522
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 if (info->is9Patch) {
524 int chunkCount = 2 + (info->haveLayoutBounds ? 1 : 0);
525 int pIndex = info->haveLayoutBounds ? 2 : 1;
526 int bIndex = 1;
527 int oIndex = 0;
528
529 // Chunks ordered thusly because older platforms depend on the base 9 patch
530 // data being last
531 png_bytep chunkNames = info->haveLayoutBounds
532 ? (png_bytep) "npOl\0npLb\0npTc\0"
533 : (png_bytep) "npOl\0npTc";
534
535 // base 9 patch data
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700536 if (kDebug) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 diag->Note(DiagMessage() << "adding 9-patch info..");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
539 strcpy((char*)unknowns[pIndex].name, "npTc");
540 unknowns[pIndex].data = (png_byte*)info->serialize9Patch();
541 unknowns[pIndex].size = info->info9Patch.serializedSize();
542 // TODO: remove the check below when everything works
543 checkNinePatchSerialization(&info->info9Patch, unknowns[pIndex].data);
544
545 // automatically generated 9 patch outline data
546 int chunkSize = sizeof(png_uint_32) * 6;
547 strcpy((char*)unknowns[oIndex].name, "npOl");
548 unknowns[oIndex].data = (png_byte*)calloc(chunkSize, 1);
549 png_byte outputData[chunkSize];
550 memcpy(&outputData, &info->outlineInsetsLeft, 4 * sizeof(png_uint_32));
551 ((float*)outputData)[4] = info->outlineRadius;
552 ((png_uint_32*)outputData)[5] = info->outlineAlpha;
553 memcpy(unknowns[oIndex].data, &outputData, chunkSize);
554 unknowns[oIndex].size = chunkSize;
555
556 // optional optical inset / layout bounds data
557 if (info->haveLayoutBounds) {
558 int chunkSize = sizeof(png_uint_32) * 4;
559 strcpy((char*)unknowns[bIndex].name, "npLb");
560 unknowns[bIndex].data = (png_byte*)calloc(chunkSize, 1);
561 memcpy(unknowns[bIndex].data, &info->layoutBoundsLeft, chunkSize);
562 unknowns[bIndex].size = chunkSize;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700563 }
564
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 for (int i = 0; i < chunkCount; i++) {
566 unknowns[i].location = PNG_HAVE_PLTE;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700567 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 png_set_keep_unknown_chunks(writePtr, PNG_HANDLE_CHUNK_ALWAYS, chunkNames,
569 chunkCount);
570 png_set_unknown_chunks(writePtr, infoPtr, unknowns, chunkCount);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700571
572#if PNG_LIBPNG_VER < 10600
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 // Deal with unknown chunk location bug in 1.5.x and earlier.
574 png_set_unknown_chunk_location(writePtr, infoPtr, 0, PNG_HAVE_PLTE);
575 if (info->haveLayoutBounds) {
576 png_set_unknown_chunk_location(writePtr, infoPtr, 1, PNG_HAVE_PLTE);
577 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700578#endif
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 }
580
581 png_write_info(writePtr, infoPtr);
582
583 png_bytepp rows;
584 if (colorType == PNG_COLOR_TYPE_RGB ||
585 colorType == PNG_COLOR_TYPE_RGB_ALPHA) {
586 if (colorType == PNG_COLOR_TYPE_RGB) {
587 png_set_filler(writePtr, 0, PNG_FILLER_AFTER);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700588 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 rows = info->rows.data();
590 } else {
591 rows = outRows;
592 }
593 png_write_image(writePtr, rows);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700594
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 if (kDebug) {
596 printf("Final image data:\n");
597 // dump_image(info->width, info->height, rows, colorType);
598 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700599
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 png_write_end(writePtr, infoPtr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700601
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 for (uint32_t i = 0; i < info->height; i++) {
603 free(outRows[i]);
604 }
605 free(outRows);
606 free(unknowns[0].data);
607 free(unknowns[1].data);
608 free(unknowns[2].data);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700609
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 png_get_IHDR(writePtr, infoPtr, &width, &height, &bitDepth, &colorType,
611 &interlaceType, &compressionType, nullptr);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700612
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 if (kDebug) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 diag->Note(DiagMessage() << "image written: w = " << width
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 << ", h = " << height << ", d = " << bitDepth
616 << ", colors = " << colorType
617 << ", inter = " << interlaceType
618 << ", comp = " << compressionType);
619 }
620 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700621}
622
623constexpr uint32_t kColorWhite = 0xffffffffu;
624constexpr uint32_t kColorTick = 0xff000000u;
625constexpr uint32_t kColorLayoutBoundsTick = 0xff0000ffu;
626
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627enum class TickType { kNone, kTick, kLayoutBounds, kBoth };
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700628
629static TickType tickType(png_bytep p, bool transparent, const char** outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 png_uint_32 color = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700631
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 if (transparent) {
633 if (p[3] == 0) {
634 return TickType::kNone;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700635 }
636 if (color == kColorLayoutBoundsTick) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 return TickType::kLayoutBounds;
638 }
639 if (color == kColorTick) {
640 return TickType::kTick;
641 }
642
643 // Error cases
644 if (p[3] != 0xff) {
645 *outError =
646 "Frame pixels must be either solid or transparent "
647 "(not intermediate alphas)";
648 return TickType::kNone;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700649 }
650
651 if (p[0] != 0 || p[1] != 0 || p[2] != 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 *outError = "Ticks in transparent frame must be black or red";
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700653 }
654 return TickType::kTick;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 }
656
657 if (p[3] != 0xFF) {
658 *outError = "White frame must be a solid color (no alpha)";
659 }
660 if (color == kColorWhite) {
661 return TickType::kNone;
662 }
663 if (color == kColorTick) {
664 return TickType::kTick;
665 }
666 if (color == kColorLayoutBoundsTick) {
667 return TickType::kLayoutBounds;
668 }
669
670 if (p[0] != 0 || p[1] != 0 || p[2] != 0) {
671 *outError = "Ticks in white frame must be black or red";
672 return TickType::kNone;
673 }
674 return TickType::kTick;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700675}
676
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677enum class TickState { kStart, kInside1, kOutside1 };
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700678
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679static bool getHorizontalTicks(png_bytep row, int width, bool transparent,
680 bool required, int32_t* outLeft,
681 int32_t* outRight, const char** outError,
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700682 uint8_t* outDivs, bool multipleAllowed) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 *outLeft = *outRight = -1;
684 TickState state = TickState::kStart;
685 bool found = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700686
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 for (int i = 1; i < width - 1; i++) {
688 if (tickType(row + i * 4, transparent, outError) == TickType::kTick) {
689 if (state == TickState::kStart ||
690 (state == TickState::kOutside1 && multipleAllowed)) {
691 *outLeft = i - 1;
692 *outRight = width - 2;
693 found = true;
694 if (outDivs != NULL) {
695 *outDivs += 2;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700696 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 state = TickState::kInside1;
698 } else if (state == TickState::kOutside1) {
699 *outError = "Can't have more than one marked region along edge";
700 *outLeft = i;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700701 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 }
703 } else if (!*outError) {
704 if (state == TickState::kInside1) {
705 // We're done with this div. Move on to the next.
706 *outRight = i - 1;
707 outRight += 2;
708 outLeft += 2;
709 state = TickState::kOutside1;
710 }
711 } else {
712 *outLeft = i;
713 return false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700714 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
716
717 if (required && !found) {
718 *outError = "No marked region found along edge";
719 *outLeft = -1;
720 return false;
721 }
722 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700723}
724
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725static bool getVerticalTicks(png_bytepp rows, int offset, int height,
726 bool transparent, bool required, int32_t* outTop,
727 int32_t* outBottom, const char** outError,
728 uint8_t* outDivs, bool multipleAllowed) {
729 *outTop = *outBottom = -1;
730 TickState state = TickState::kStart;
731 bool found = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700732
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 for (int i = 1; i < height - 1; i++) {
734 if (tickType(rows[i] + offset, transparent, outError) == TickType::kTick) {
735 if (state == TickState::kStart ||
736 (state == TickState::kOutside1 && multipleAllowed)) {
737 *outTop = i - 1;
738 *outBottom = height - 2;
739 found = true;
740 if (outDivs != NULL) {
741 *outDivs += 2;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700742 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 state = TickState::kInside1;
744 } else if (state == TickState::kOutside1) {
745 *outError = "Can't have more than one marked region along edge";
746 *outTop = i;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700747 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 }
749 } else if (!*outError) {
750 if (state == TickState::kInside1) {
751 // We're done with this div. Move on to the next.
752 *outBottom = i - 1;
753 outTop += 2;
754 outBottom += 2;
755 state = TickState::kOutside1;
756 }
757 } else {
758 *outTop = i;
759 return false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700760 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
762
763 if (required && !found) {
764 *outError = "No marked region found along edge";
765 *outTop = -1;
766 return false;
767 }
768 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700769}
770
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771static bool getHorizontalLayoutBoundsTicks(png_bytep row, int width,
772 bool transparent,
773 bool /* required */,
774 int32_t* outLeft, int32_t* outRight,
775 const char** outError) {
776 *outLeft = *outRight = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700777
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // Look for left tick
779 if (tickType(row + 4, transparent, outError) == TickType::kLayoutBounds) {
780 // Starting with a layout padding tick
781 int i = 1;
782 while (i < width - 1) {
783 (*outLeft)++;
784 i++;
785 if (tickType(row + i * 4, transparent, outError) !=
786 TickType::kLayoutBounds) {
787 break;
788 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700789 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700791
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 // Look for right tick
793 if (tickType(row + (width - 2) * 4, transparent, outError) ==
794 TickType::kLayoutBounds) {
795 // Ending with a layout padding tick
796 int i = width - 2;
797 while (i > 1) {
798 (*outRight)++;
799 i--;
800 if (tickType(row + i * 4, transparent, outError) !=
801 TickType::kLayoutBounds) {
802 break;
803 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700804 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 }
806 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700807}
808
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809static bool getVerticalLayoutBoundsTicks(png_bytepp rows, int offset,
810 int height, bool transparent,
811 bool /* required */, int32_t* outTop,
812 int32_t* outBottom,
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700813 const char** outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 *outTop = *outBottom = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700815
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 // Look for top tick
817 if (tickType(rows[1] + offset, transparent, outError) ==
818 TickType::kLayoutBounds) {
819 // Starting with a layout padding tick
820 int i = 1;
821 while (i < height - 1) {
822 (*outTop)++;
823 i++;
824 if (tickType(rows[i] + offset, transparent, outError) !=
825 TickType::kLayoutBounds) {
826 break;
827 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700828 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700830
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 // Look for bottom tick
832 if (tickType(rows[height - 2] + offset, transparent, outError) ==
833 TickType::kLayoutBounds) {
834 // Ending with a layout padding tick
835 int i = height - 2;
836 while (i > 1) {
837 (*outBottom)++;
838 i--;
839 if (tickType(rows[i] + offset, transparent, outError) !=
840 TickType::kLayoutBounds) {
841 break;
842 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700843 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 }
845 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700846}
847
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848static void findMaxOpacity(png_bytepp rows, int startX, int startY, int endX,
849 int endY, int dX, int dY, int* outInset) {
850 uint8_t maxOpacity = 0;
851 int inset = 0;
852 *outInset = 0;
853 for (int x = startX, y = startY; x != endX && y != endY;
854 x += dX, y += dY, inset++) {
855 png_byte* color = rows[y] + x * 4;
856 uint8_t opacity = color[3];
857 if (opacity > maxOpacity) {
858 maxOpacity = opacity;
859 *outInset = inset;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700860 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 if (opacity == 0xff) return;
862 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700863}
864
865static uint8_t maxAlphaOverRow(png_bytep row, int startX, int endX) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 uint8_t maxAlpha = 0;
867 for (int x = startX; x < endX; x++) {
868 uint8_t alpha = (row + x * 4)[3];
869 if (alpha > maxAlpha) maxAlpha = alpha;
870 }
871 return maxAlpha;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700872}
873
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874static uint8_t maxAlphaOverCol(png_bytepp rows, int offsetX, int startY,
875 int endY) {
876 uint8_t maxAlpha = 0;
877 for (int y = startY; y < endY; y++) {
878 uint8_t alpha = (rows[y] + offsetX * 4)[3];
879 if (alpha > maxAlpha) maxAlpha = alpha;
880 }
881 return maxAlpha;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700882}
883
884static void getOutline(PngInfo* image) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 int midX = image->width / 2;
886 int midY = image->height / 2;
887 int endX = image->width - 2;
888 int endY = image->height - 2;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700889
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 // find left and right extent of nine patch content on center row
891 if (image->width > 4) {
892 findMaxOpacity(image->rows.data(), 1, midY, midX, -1, 1, 0,
893 &image->outlineInsetsLeft);
894 findMaxOpacity(image->rows.data(), endX, midY, midX, -1, -1, 0,
895 &image->outlineInsetsRight);
896 } else {
897 image->outlineInsetsLeft = 0;
898 image->outlineInsetsRight = 0;
899 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700900
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 // find top and bottom extent of nine patch content on center column
902 if (image->height > 4) {
903 findMaxOpacity(image->rows.data(), midX, 1, -1, midY, 0, 1,
904 &image->outlineInsetsTop);
905 findMaxOpacity(image->rows.data(), midX, endY, -1, midY, 0, -1,
906 &image->outlineInsetsBottom);
907 } else {
908 image->outlineInsetsTop = 0;
909 image->outlineInsetsBottom = 0;
910 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700911
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 int innerStartX = 1 + image->outlineInsetsLeft;
913 int innerStartY = 1 + image->outlineInsetsTop;
914 int innerEndX = endX - image->outlineInsetsRight;
915 int innerEndY = endY - image->outlineInsetsBottom;
916 int innerMidX = (innerEndX + innerStartX) / 2;
917 int innerMidY = (innerEndY + innerStartY) / 2;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700918
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700919 // assuming the image is a round rect, compute the radius by marching
920 // diagonally from the top left corner towards the center
921 image->outlineAlpha = std::max(
922 maxAlphaOverRow(image->rows[innerMidY], innerStartX, innerEndX),
923 maxAlphaOverCol(image->rows.data(), innerMidX, innerStartY, innerStartY));
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700924
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700925 int diagonalInset = 0;
926 findMaxOpacity(image->rows.data(), innerStartX, innerStartY, innerMidX,
927 innerMidY, 1, 1, &diagonalInset);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700928
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 /* Determine source radius based upon inset:
930 * sqrt(r^2 + r^2) = sqrt(i^2 + i^2) + r
931 * sqrt(2) * r = sqrt(2) * i + r
932 * (sqrt(2) - 1) * r = sqrt(2) * i
933 * r = sqrt(2) / (sqrt(2) - 1) * i
934 */
935 image->outlineRadius = 3.4142f * diagonalInset;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700936
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 if (kDebug) {
938 printf("outline insets %d %d %d %d, rad %f, alpha %x\n",
939 image->outlineInsetsLeft, image->outlineInsetsTop,
940 image->outlineInsetsRight, image->outlineInsetsBottom,
941 image->outlineRadius, image->outlineAlpha);
942 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700943}
944
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945static uint32_t getColor(png_bytepp rows, int left, int top, int right,
946 int bottom) {
947 png_bytep color = rows[top] + left * 4;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700948
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 if (left > right || top > bottom) {
950 return android::Res_png_9patch::TRANSPARENT_COLOR;
951 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700952
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 while (top <= bottom) {
954 for (int i = left; i <= right; i++) {
955 png_bytep p = rows[top] + i * 4;
956 if (color[3] == 0) {
957 if (p[3] != 0) {
958 return android::Res_png_9patch::NO_COLOR;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700959 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 } else if (p[0] != color[0] || p[1] != color[1] || p[2] != color[2] ||
961 p[3] != color[3]) {
962 return android::Res_png_9patch::NO_COLOR;
963 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700964 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 top++;
966 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700967
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 if (color[3] == 0) {
969 return android::Res_png_9patch::TRANSPARENT_COLOR;
970 }
971 return (color[3] << 24) | (color[0] << 16) | (color[1] << 8) | color[2];
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700972}
973
974static bool do9Patch(PngInfo* image, std::string* outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975 image->is9Patch = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700976
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 int W = image->width;
978 int H = image->height;
979 int i, j;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700980
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 const int maxSizeXDivs = W * sizeof(int32_t);
982 const int maxSizeYDivs = H * sizeof(int32_t);
983 int32_t* xDivs = image->xDivs = new int32_t[W];
984 int32_t* yDivs = image->yDivs = new int32_t[H];
985 uint8_t numXDivs = 0;
986 uint8_t numYDivs = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -0700987
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 int8_t numColors;
989 int numRows;
990 int numCols;
991 int top;
992 int left;
993 int right;
994 int bottom;
995 memset(xDivs, -1, maxSizeXDivs);
996 memset(yDivs, -1, maxSizeYDivs);
997 image->info9Patch.paddingLeft = image->info9Patch.paddingRight = -1;
998 image->info9Patch.paddingTop = image->info9Patch.paddingBottom = -1;
999 image->layoutBoundsLeft = image->layoutBoundsRight = 0;
1000 image->layoutBoundsTop = image->layoutBoundsBottom = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001001
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 png_bytep p = image->rows[0];
1003 bool transparent = p[3] == 0;
1004 bool hasColor = false;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001005
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006 const char* errorMsg = nullptr;
1007 int errorPixel = -1;
1008 const char* errorEdge = nullptr;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001009
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 int colorIndex = 0;
1011 std::vector<png_bytep> newRows;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001012
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013 // Validate size...
1014 if (W < 3 || H < 3) {
1015 errorMsg = "Image must be at least 3x3 (1x1 without frame) pixels";
1016 goto getout;
1017 }
1018
1019 // Validate frame...
1020 if (!transparent &&
1021 (p[0] != 0xFF || p[1] != 0xFF || p[2] != 0xFF || p[3] != 0xFF)) {
1022 errorMsg = "Must have one-pixel frame that is either transparent or white";
1023 goto getout;
1024 }
1025
1026 // Find left and right of sizing areas...
1027 if (!getHorizontalTicks(p, W, transparent, true, &xDivs[0], &xDivs[1],
1028 &errorMsg, &numXDivs, true)) {
1029 errorPixel = xDivs[0];
1030 errorEdge = "top";
1031 goto getout;
1032 }
1033
1034 // Find top and bottom of sizing areas...
1035 if (!getVerticalTicks(image->rows.data(), 0, H, transparent, true, &yDivs[0],
1036 &yDivs[1], &errorMsg, &numYDivs, true)) {
1037 errorPixel = yDivs[0];
1038 errorEdge = "left";
1039 goto getout;
1040 }
1041
1042 // Copy patch size data into image...
1043 image->info9Patch.numXDivs = numXDivs;
1044 image->info9Patch.numYDivs = numYDivs;
1045
1046 // Find left and right of padding area...
1047 if (!getHorizontalTicks(image->rows[H - 1], W, transparent, false,
1048 &image->info9Patch.paddingLeft,
1049 &image->info9Patch.paddingRight, &errorMsg, nullptr,
1050 false)) {
1051 errorPixel = image->info9Patch.paddingLeft;
1052 errorEdge = "bottom";
1053 goto getout;
1054 }
1055
1056 // Find top and bottom of padding area...
1057 if (!getVerticalTicks(image->rows.data(), (W - 1) * 4, H, transparent, false,
1058 &image->info9Patch.paddingTop,
1059 &image->info9Patch.paddingBottom, &errorMsg, nullptr,
1060 false)) {
1061 errorPixel = image->info9Patch.paddingTop;
1062 errorEdge = "right";
1063 goto getout;
1064 }
1065
1066 // Find left and right of layout padding...
1067 getHorizontalLayoutBoundsTicks(image->rows[H - 1], W, transparent, false,
1068 &image->layoutBoundsLeft,
1069 &image->layoutBoundsRight, &errorMsg);
1070
1071 getVerticalLayoutBoundsTicks(image->rows.data(), (W - 1) * 4, H, transparent,
1072 false, &image->layoutBoundsTop,
1073 &image->layoutBoundsBottom, &errorMsg);
1074
1075 image->haveLayoutBounds =
1076 image->layoutBoundsLeft != 0 || image->layoutBoundsRight != 0 ||
1077 image->layoutBoundsTop != 0 || image->layoutBoundsBottom != 0;
1078
1079 if (image->haveLayoutBounds) {
1080 if (kDebug) {
1081 printf("layoutBounds=%d %d %d %d\n", image->layoutBoundsLeft,
1082 image->layoutBoundsTop, image->layoutBoundsRight,
1083 image->layoutBoundsBottom);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001084 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001085 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001086
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 // use opacity of pixels to estimate the round rect outline
1088 getOutline(image);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001089
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001090 // If padding is not yet specified, take values from size.
1091 if (image->info9Patch.paddingLeft < 0) {
1092 image->info9Patch.paddingLeft = xDivs[0];
1093 image->info9Patch.paddingRight = W - 2 - xDivs[1];
1094 } else {
1095 // Adjust value to be correct!
1096 image->info9Patch.paddingRight = W - 2 - image->info9Patch.paddingRight;
1097 }
1098 if (image->info9Patch.paddingTop < 0) {
1099 image->info9Patch.paddingTop = yDivs[0];
1100 image->info9Patch.paddingBottom = H - 2 - yDivs[1];
1101 } else {
1102 // Adjust value to be correct!
1103 image->info9Patch.paddingBottom = H - 2 - image->info9Patch.paddingBottom;
1104 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001105
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 /* if (kDebug) {
1107 printf("Size ticks for %s: x0=%d, x1=%d, y0=%d, y1=%d\n", imageName,
1108 xDivs[0], xDivs[1],
1109 yDivs[0], yDivs[1]);
1110 printf("padding ticks for %s: l=%d, r=%d, t=%d, b=%d\n", imageName,
1111 image->info9Patch.paddingLeft, image->info9Patch.paddingRight,
1112 image->info9Patch.paddingTop,
1113 image->info9Patch.paddingBottom);
1114 }*/
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001115
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 // Remove frame from image.
1117 newRows.resize(H - 2);
1118 for (i = 0; i < H - 2; i++) {
1119 newRows[i] = image->rows[i + 1];
1120 memmove(newRows[i], newRows[i] + 4, (W - 2) * 4);
1121 }
1122 image->rows.swap(newRows);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001123
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001124 image->width -= 2;
1125 W = image->width;
1126 image->height -= 2;
1127 H = image->height;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001128
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001129 // Figure out the number of rows and columns in the N-patch
1130 numCols = numXDivs + 1;
1131 if (xDivs[0] == 0) { // Column 1 is strechable
1132 numCols--;
1133 }
1134 if (xDivs[numXDivs - 1] == W) {
1135 numCols--;
1136 }
1137 numRows = numYDivs + 1;
1138 if (yDivs[0] == 0) { // Row 1 is strechable
1139 numRows--;
1140 }
1141 if (yDivs[numYDivs - 1] == H) {
1142 numRows--;
1143 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001144
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001145 // Make sure the amount of rows and columns will fit in the number of
1146 // colors we can use in the 9-patch format.
1147 if (numRows * numCols > 0x7F) {
1148 errorMsg = "Too many rows and columns in 9-patch perimeter";
1149 goto getout;
1150 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001151
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 numColors = numRows * numCols;
1153 image->info9Patch.numColors = numColors;
1154 image->colors.resize(numColors);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001155
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 // Fill in color information for each patch.
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001157
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001158 uint32_t c;
1159 top = 0;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001160
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001161 // The first row always starts with the top being at y=0 and the bottom
1162 // being either yDivs[1] (if yDivs[0]=0) of yDivs[0]. In the former case
1163 // the first row is stretchable along the Y axis, otherwise it is fixed.
1164 // The last row always ends with the bottom being bitmap.height and the top
1165 // being either yDivs[numYDivs-2] (if yDivs[numYDivs-1]=bitmap.height) or
1166 // yDivs[numYDivs-1]. In the former case the last row is stretchable along
1167 // the Y axis, otherwise it is fixed.
1168 //
1169 // The first and last columns are similarly treated with respect to the X
1170 // axis.
1171 //
1172 // The above is to help explain some of the special casing that goes on the
1173 // code below.
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001174
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 // The initial yDiv and whether the first row is considered stretchable or
1176 // not depends on whether yDiv[0] was zero or not.
1177 for (j = (yDivs[0] == 0 ? 1 : 0); j <= numYDivs && top < H; j++) {
1178 if (j == numYDivs) {
1179 bottom = H;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001180 } else {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 bottom = yDivs[j];
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001182 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001183 left = 0;
1184 // The initial xDiv and whether the first column is considered
1185 // stretchable or not depends on whether xDiv[0] was zero or not.
1186 for (i = xDivs[0] == 0 ? 1 : 0; i <= numXDivs && left < W; i++) {
1187 if (i == numXDivs) {
1188 right = W;
1189 } else {
1190 right = xDivs[i];
1191 }
1192 c = getColor(image->rows.data(), left, top, right - 1, bottom - 1);
1193 image->colors[colorIndex++] = c;
1194 if (kDebug) {
1195 if (c != android::Res_png_9patch::NO_COLOR) {
1196 hasColor = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001197 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001198 }
1199 left = right;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001200 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201 top = bottom;
1202 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001203
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001204 assert(colorIndex == numColors);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001205
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001206 if (kDebug && hasColor) {
1207 for (i = 0; i < numColors; i++) {
1208 if (i == 0) printf("Colors:\n");
1209 printf(" #%08x", image->colors[i]);
1210 if (i == numColors - 1) printf("\n");
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001211 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001213getout:
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001214 if (errorMsg) {
1215 std::stringstream err;
1216 err << "9-patch malformed: " << errorMsg;
1217 if (errorEdge) {
1218 err << "." << std::endl;
1219 if (errorPixel >= 0) {
1220 err << "Found at pixel #" << errorPixel << " along " << errorEdge
1221 << " edge";
1222 } else {
1223 err << "Found along " << errorEdge << " edge";
1224 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001225 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 *outError = err.str();
1227 return false;
1228 }
1229 return true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001230}
1231
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232bool Png::process(const Source& source, std::istream* input,
1233 BigBuffer* outBuffer, const PngOptions& options) {
1234 png_byte signature[kPngSignatureSize];
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001235
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 // Read the PNG signature first.
1237 if (!input->read(reinterpret_cast<char*>(signature), kPngSignatureSize)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001238 mDiag->Error(DiagMessage() << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 return false;
1240 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001241
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 // If the PNG signature doesn't match, bail early.
1243 if (png_sig_cmp(signature, 0, kPngSignatureSize) != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 mDiag->Error(DiagMessage() << "not a valid png file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001245 return false;
1246 }
1247
1248 bool result = false;
1249 png_structp readPtr = nullptr;
1250 png_infop infoPtr = nullptr;
1251 png_structp writePtr = nullptr;
1252 png_infop writeInfoPtr = nullptr;
1253 PngInfo pngInfo = {};
1254
1255 readPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, nullptr, nullptr);
1256 if (!readPtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 mDiag->Error(DiagMessage() << "failed to allocate read ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 goto bail;
1259 }
1260
1261 infoPtr = png_create_info_struct(readPtr);
1262 if (!infoPtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001263 mDiag->Error(DiagMessage() << "failed to allocate info ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 goto bail;
1265 }
1266
1267 png_set_error_fn(readPtr, reinterpret_cast<png_voidp>(mDiag), nullptr,
1268 logWarning);
1269
1270 // Set the read function to read from std::istream.
1271 png_set_read_fn(readPtr, (png_voidp)input, readDataFromStream);
1272
1273 if (!readPng(mDiag, readPtr, infoPtr, &pngInfo)) {
1274 goto bail;
1275 }
1276
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 if (util::EndsWith(source.path, ".9.png")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 std::string errorMsg;
1279 if (!do9Patch(&pngInfo, &errorMsg)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001280 mDiag->Error(DiagMessage() << errorMsg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001281 goto bail;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001282 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001284
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001285 writePtr =
1286 png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, nullptr, nullptr);
1287 if (!writePtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 mDiag->Error(DiagMessage() << "failed to allocate write ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 goto bail;
1290 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001291
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292 writeInfoPtr = png_create_info_struct(writePtr);
1293 if (!writeInfoPtr) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 mDiag->Error(DiagMessage() << "failed to allocate write info ptr");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001295 goto bail;
1296 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001297
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001298 png_set_error_fn(writePtr, nullptr, nullptr, logWarning);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001299
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001300 // Set the write function to write to std::ostream.
1301 png_set_write_fn(writePtr, (png_voidp)outBuffer, writeDataToStream,
1302 flushDataToStream);
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001303
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001304 if (!writePng(mDiag, writePtr, writeInfoPtr, &pngInfo,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 options.grayscale_tolerance)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 goto bail;
1307 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001308
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 result = true;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001310bail:
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 if (readPtr) {
1312 png_destroy_read_struct(&readPtr, &infoPtr, nullptr);
1313 }
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001314
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 if (writePtr) {
1316 png_destroy_write_struct(&writePtr, &writeInfoPtr);
1317 }
1318 return result;
Adam Lesinski98aa3ad2015-04-06 11:46:52 -07001319}
1320
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321} // namespace aapt