]> git.saurik.com Git - android/aapt.git/blame - Images.cpp
merge from donut
[android/aapt.git] / Images.cpp
CommitLineData
a534180c
TAOSP
1//
2// Copyright 2006 The Android Open Source Project
3//
4// Build resource files from raw assets.
5//
6
7#define PNG_INTERNAL
8
9#include "Images.h"
10
11#include <utils/ResourceTypes.h>
12#include <utils/ByteOrder.h>
13
14#include <png.h>
15
16#define NOISY(x) //x
17
18static void
19png_write_aapt_file(png_structp png_ptr, png_bytep data, png_size_t length)
20{
21 status_t err = ((AaptFile*)png_ptr->io_ptr)->writeData(data, length);
22 if (err != NO_ERROR) {
23 png_error(png_ptr, "Write Error");
24 }
25}
26
27
28static void
29png_flush_aapt_file(png_structp png_ptr)
30{
31}
32
33// This holds an image as 8bpp RGBA.
34struct image_info
35{
36 image_info() : rows(NULL), is9Patch(false), allocRows(NULL) { }
37 ~image_info() {
38 if (rows && rows != allocRows) {
39 free(rows);
40 }
41 if (allocRows) {
42 for (int i=0; i<(int)allocHeight; i++) {
43 free(allocRows[i]);
44 }
45 free(allocRows);
46 }
47 }
48
49 png_uint_32 width;
50 png_uint_32 height;
51 png_bytepp rows;
52
53 // 9-patch info.
54 bool is9Patch;
55 Res_png_9patch info9Patch;
56
57 png_uint_32 allocHeight;
58 png_bytepp allocRows;
59};
60
61static void read_png(const char* imageName,
62 png_structp read_ptr, png_infop read_info,
63 image_info* outImageInfo)
64{
65 int color_type;
66 int bit_depth, interlace_type, compression_type;
67 int i;
68
69 png_read_info(read_ptr, read_info);
70
71 png_get_IHDR(read_ptr, read_info, &outImageInfo->width,
72 &outImageInfo->height, &bit_depth, &color_type,
73 &interlace_type, &compression_type, NULL);
74
75 //printf("Image %s:\n", imageName);
76 //printf("color_type=%d, bit_depth=%d, interlace_type=%d, compression_type=%d\n",
77 // color_type, bit_depth, interlace_type, compression_type);
78
79 if (color_type == PNG_COLOR_TYPE_PALETTE)
80 png_set_palette_to_rgb(read_ptr);
81
82 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
83 png_set_gray_1_2_4_to_8(read_ptr);
84
85 if (png_get_valid(read_ptr, read_info, PNG_INFO_tRNS)) {
86 //printf("Has PNG_INFO_tRNS!\n");
87 png_set_tRNS_to_alpha(read_ptr);
88 }
89
90 if (bit_depth == 16)
91 png_set_strip_16(read_ptr);
92
93 if ((color_type&PNG_COLOR_MASK_ALPHA) == 0)
94 png_set_add_alpha(read_ptr, 0xFF, PNG_FILLER_AFTER);
95
96 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
97 png_set_gray_to_rgb(read_ptr);
98
99 png_read_update_info(read_ptr, read_info);
100
101 outImageInfo->rows = (png_bytepp)malloc(
102 outImageInfo->height * png_sizeof(png_bytep));
103 outImageInfo->allocHeight = outImageInfo->height;
104 outImageInfo->allocRows = outImageInfo->rows;
105
106 png_set_rows(read_ptr, read_info, outImageInfo->rows);
107
108 for (i = 0; i < (int)outImageInfo->height; i++)
109 {
110 outImageInfo->rows[i] = (png_bytep)
111 malloc(png_get_rowbytes(read_ptr, read_info));
112 }
113
114 png_read_image(read_ptr, outImageInfo->rows);
115
116 png_read_end(read_ptr, read_info);
117
118 NOISY(printf("Image %s: w=%d, h=%d, d=%d, colors=%d, inter=%d, comp=%d\n",
119 imageName,
120 (int)outImageInfo->width, (int)outImageInfo->height,
121 bit_depth, color_type,
122 interlace_type, compression_type));
123
124 png_get_IHDR(read_ptr, read_info, &outImageInfo->width,
125 &outImageInfo->height, &bit_depth, &color_type,
126 &interlace_type, &compression_type, NULL);
127}
128
129static bool is_tick(png_bytep p, bool transparent, const char** outError)
130{
131 if (transparent) {
132 if (p[3] == 0) {
133 return false;
134 }
135 if (p[3] != 0xff) {
136 *outError = "Frame pixels must be either solid or transparent (not intermediate alphas)";
137 return false;
138 }
139 if (p[0] != 0 || p[1] != 0 || p[2] != 0) {
140 *outError = "Ticks in transparent frame must be black";
141 }
142 return true;
143 }
144
145 if (p[3] != 0xFF) {
146 *outError = "White frame must be a solid color (no alpha)";
147 }
148 if (p[0] == 0xFF && p[1] == 0xFF && p[2] == 0xFF) {
149 return false;
150 }
151 if (p[0] != 0 || p[1] != 0 || p[2] != 0) {
152 *outError = "Ticks in white frame must be black";
153 return false;
154 }
155 return true;
156}
157
158enum {
159 TICK_START,
160 TICK_INSIDE_1,
161 TICK_OUTSIDE_1
162};
163
164static status_t get_horizontal_ticks(
165 png_bytep row, int width, bool transparent, bool required,
166 int32_t* outLeft, int32_t* outRight, const char** outError,
167 uint8_t* outDivs, bool multipleAllowed)
168{
169 int i;
170 *outLeft = *outRight = -1;
171 int state = TICK_START;
172 bool found = false;
173
174 for (i=1; i<width-1; i++) {
175 if (is_tick(row+i*4, transparent, outError)) {
176 if (state == TICK_START ||
177 (state == TICK_OUTSIDE_1 && multipleAllowed)) {
178 *outLeft = i-1;
179 *outRight = width-2;
180 found = true;
181 if (outDivs != NULL) {
182 *outDivs += 2;
183 }
184 state = TICK_INSIDE_1;
185 } else if (state == TICK_OUTSIDE_1) {
186 *outError = "Can't have more than one marked region along edge";
187 *outLeft = i;
188 return UNKNOWN_ERROR;
189 }
190 } else if (*outError == NULL) {
191 if (state == TICK_INSIDE_1) {
192 // We're done with this div. Move on to the next.
193 *outRight = i-1;
194 outRight += 2;
195 outLeft += 2;
196 state = TICK_OUTSIDE_1;
197 }
198 } else {
199 *outLeft = i;
200 return UNKNOWN_ERROR;
201 }
202 }
203
204 if (required && !found) {
205 *outError = "No marked region found along edge";
206 *outLeft = -1;
207 return UNKNOWN_ERROR;
208 }
209
210 return NO_ERROR;
211}
212
213static status_t get_vertical_ticks(
214 png_bytepp rows, int offset, int height, bool transparent, bool required,
215 int32_t* outTop, int32_t* outBottom, const char** outError,
216 uint8_t* outDivs, bool multipleAllowed)
217{
218 int i;
219 *outTop = *outBottom = -1;
220 int state = TICK_START;
221 bool found = false;
222
223 for (i=1; i<height-1; i++) {
224 if (is_tick(rows[i]+offset, transparent, outError)) {
225 if (state == TICK_START ||
226 (state == TICK_OUTSIDE_1 && multipleAllowed)) {
227 *outTop = i-1;
228 *outBottom = height-2;
229 found = true;
230 if (outDivs != NULL) {
231 *outDivs += 2;
232 }
233 state = TICK_INSIDE_1;
234 } else if (state == TICK_OUTSIDE_1) {
235 *outError = "Can't have more than one marked region along edge";
236 *outTop = i;
237 return UNKNOWN_ERROR;
238 }
239 } else if (*outError == NULL) {
240 if (state == TICK_INSIDE_1) {
241 // We're done with this div. Move on to the next.
242 *outBottom = i-1;
243 outTop += 2;
244 outBottom += 2;
245 state = TICK_OUTSIDE_1;
246 }
247 } else {
248 *outTop = i;
249 return UNKNOWN_ERROR;
250 }
251 }
252
253 if (required && !found) {
254 *outError = "No marked region found along edge";
255 *outTop = -1;
256 return UNKNOWN_ERROR;
257 }
258
259 return NO_ERROR;
260}
261
262static uint32_t get_color(
263 png_bytepp rows, int left, int top, int right, int bottom)
264{
265 png_bytep color = rows[top] + left*4;
266
267 if (left > right || top > bottom) {
268 return Res_png_9patch::TRANSPARENT_COLOR;
269 }
270
271 while (top <= bottom) {
272 for (int i = left; i <= right; i++) {
273 png_bytep p = rows[top]+i*4;
274 if (color[3] == 0) {
275 if (p[3] != 0) {
276 return Res_png_9patch::NO_COLOR;
277 }
278 } else if (p[0] != color[0] || p[1] != color[1]
279 || p[2] != color[2] || p[3] != color[3]) {
280 return Res_png_9patch::NO_COLOR;
281 }
282 }
283 top++;
284 }
285
286 if (color[3] == 0) {
287 return Res_png_9patch::TRANSPARENT_COLOR;
288 }
289 return (color[3]<<24) | (color[0]<<16) | (color[1]<<8) | color[2];
290}
291
292static void select_patch(
293 int which, int front, int back, int size, int* start, int* end)
294{
295 switch (which) {
296 case 0:
297 *start = 0;
298 *end = front-1;
299 break;
300 case 1:
301 *start = front;
302 *end = back-1;
303 break;
304 case 2:
305 *start = back;
306 *end = size-1;
307 break;
308 }
309}
310
311static uint32_t get_color(image_info* image, int hpatch, int vpatch)
312{
313 int left, right, top, bottom;
314 select_patch(
315 hpatch, image->info9Patch.xDivs[0], image->info9Patch.xDivs[1],
316 image->width, &left, &right);
317 select_patch(
318 vpatch, image->info9Patch.yDivs[0], image->info9Patch.yDivs[1],
319 image->height, &top, &bottom);
320 //printf("Selecting h=%d v=%d: (%d,%d)-(%d,%d)\n",
321 // hpatch, vpatch, left, top, right, bottom);
322 const uint32_t c = get_color(image->rows, left, top, right, bottom);
323 NOISY(printf("Color in (%d,%d)-(%d,%d): #%08x\n", left, top, right, bottom, c));
324 return c;
325}
326
327static status_t do_9patch(const char* imageName, image_info* image)
328{
329 image->is9Patch = true;
330
331 int W = image->width;
332 int H = image->height;
333 int i, j;
334
12e28f48
TAOSP
335 int maxSizeXDivs = W * sizeof(int32_t);
336 int maxSizeYDivs = H * sizeof(int32_t);
a534180c
TAOSP
337 int32_t* xDivs = (int32_t*) malloc(maxSizeXDivs);
338 int32_t* yDivs = (int32_t*) malloc(maxSizeYDivs);
339 uint8_t numXDivs = 0;
340 uint8_t numYDivs = 0;
341 int8_t numColors;
342 int numRows;
343 int numCols;
344 int top;
345 int left;
346 int right;
347 int bottom;
348 memset(xDivs, -1, maxSizeXDivs);
349 memset(yDivs, -1, maxSizeYDivs);
350 image->info9Patch.paddingLeft = image->info9Patch.paddingRight =
351 image->info9Patch.paddingTop = image->info9Patch.paddingBottom = -1;
352
353 png_bytep p = image->rows[0];
354 bool transparent = p[3] == 0;
355 bool hasColor = false;
356
357 const char* errorMsg = NULL;
358 int errorPixel = -1;
359 const char* errorEdge = "";
360
361 int colorIndex = 0;
362
363 // Validate size...
364 if (W < 3 || H < 3) {
365 errorMsg = "Image must be at least 3x3 (1x1 without frame) pixels";
366 goto getout;
367 }
368
369 // Validate frame...
370 if (!transparent &&
371 (p[0] != 0xFF || p[1] != 0xFF || p[2] != 0xFF || p[3] != 0xFF)) {
372 errorMsg = "Must have one-pixel frame that is either transparent or white";
373 goto getout;
374 }
375
376 // Find left and right of sizing areas...
377 if (get_horizontal_ticks(p, W, transparent, true, &xDivs[0],
378 &xDivs[1], &errorMsg, &numXDivs, true) != NO_ERROR) {
379 errorPixel = xDivs[0];
380 errorEdge = "top";
381 goto getout;
382 }
383
384 // Find top and bottom of sizing areas...
385 if (get_vertical_ticks(image->rows, 0, H, transparent, true, &yDivs[0],
386 &yDivs[1], &errorMsg, &numYDivs, true) != NO_ERROR) {
387 errorPixel = yDivs[0];
388 errorEdge = "left";
389 goto getout;
390 }
391
392 // Find left and right of padding area...
393 if (get_horizontal_ticks(image->rows[H-1], W, transparent, false, &image->info9Patch.paddingLeft,
394 &image->info9Patch.paddingRight, &errorMsg, NULL, false) != NO_ERROR) {
395 errorPixel = image->info9Patch.paddingLeft;
396 errorEdge = "bottom";
397 goto getout;
398 }
399
400 // Find top and bottom of padding area...
401 if (get_vertical_ticks(image->rows, (W-1)*4, H, transparent, false, &image->info9Patch.paddingTop,
402 &image->info9Patch.paddingBottom, &errorMsg, NULL, false) != NO_ERROR) {
403 errorPixel = image->info9Patch.paddingTop;
404 errorEdge = "right";
405 goto getout;
406 }
407
408 // Copy patch data into image
409 image->info9Patch.numXDivs = numXDivs;
410 image->info9Patch.numYDivs = numYDivs;
411 image->info9Patch.xDivs = xDivs;
412 image->info9Patch.yDivs = yDivs;
413
414 // If padding is not yet specified, take values from size.
415 if (image->info9Patch.paddingLeft < 0) {
416 image->info9Patch.paddingLeft = xDivs[0];
417 image->info9Patch.paddingRight = W - 2 - xDivs[1];
418 } else {
419 // Adjust value to be correct!
420 image->info9Patch.paddingRight = W - 2 - image->info9Patch.paddingRight;
421 }
422 if (image->info9Patch.paddingTop < 0) {
423 image->info9Patch.paddingTop = yDivs[0];
424 image->info9Patch.paddingBottom = H - 2 - yDivs[1];
425 } else {
426 // Adjust value to be correct!
427 image->info9Patch.paddingBottom = H - 2 - image->info9Patch.paddingBottom;
428 }
429
430 NOISY(printf("Size ticks for %s: x0=%d, x1=%d, y0=%d, y1=%d\n", imageName,
431 image->info9Patch.xDivs[0], image->info9Patch.xDivs[1],
432 image->info9Patch.yDivs[0], image->info9Patch.yDivs[1]));
433 NOISY(printf("padding ticks for %s: l=%d, r=%d, t=%d, b=%d\n", imageName,
434 image->info9Patch.paddingLeft, image->info9Patch.paddingRight,
435 image->info9Patch.paddingTop, image->info9Patch.paddingBottom));
436
437 // Remove frame from image.
438 image->rows = (png_bytepp)malloc((H-2) * png_sizeof(png_bytep));
439 for (i=0; i<(H-2); i++) {
440 image->rows[i] = image->allocRows[i+1];
441 memmove(image->rows[i], image->rows[i]+4, (W-2)*4);
442 }
443 image->width -= 2;
444 W = image->width;
445 image->height -= 2;
446 H = image->height;
447
448 // Figure out the number of rows and columns in the N-patch
449 numCols = numXDivs + 1;
450 if (xDivs[0] == 0) { // Column 1 is strechable
451 numCols--;
452 }
453 if (xDivs[numXDivs - 1] == W) {
454 numCols--;
455 }
456 numRows = numYDivs + 1;
457 if (yDivs[0] == 0) { // Row 1 is strechable
458 numRows--;
459 }
460 if (yDivs[numYDivs - 1] == H) {
461 numRows--;
462 }
463 numColors = numRows * numCols;
464 image->info9Patch.numColors = numColors;
465 image->info9Patch.colors = (uint32_t*)malloc(numColors * sizeof(uint32_t));
466
467 // Fill in color information for each patch.
468
469 uint32_t c;
470 top = 0;
471
472 // The first row always starts with the top being at y=0 and the bottom
473 // being either yDivs[1] (if yDivs[0]=0) of yDivs[0]. In the former case
474 // the first row is stretchable along the Y axis, otherwise it is fixed.
475 // The last row always ends with the bottom being bitmap.height and the top
476 // being either yDivs[numYDivs-2] (if yDivs[numYDivs-1]=bitmap.height) or
477 // yDivs[numYDivs-1]. In the former case the last row is stretchable along
478 // the Y axis, otherwise it is fixed.
479 //
480 // The first and last columns are similarly treated with respect to the X
481 // axis.
482 //
483 // The above is to help explain some of the special casing that goes on the
484 // code below.
485
486 // The initial yDiv and whether the first row is considered stretchable or
487 // not depends on whether yDiv[0] was zero or not.
488 for (j = (yDivs[0] == 0 ? 1 : 0);
489 j <= numYDivs && top < H;
490 j++) {
491 if (j == numYDivs) {
492 bottom = H;
493 } else {
494 bottom = yDivs[j];
495 }
496 left = 0;
497 // The initial xDiv and whether the first column is considered
498 // stretchable or not depends on whether xDiv[0] was zero or not.
499 for (i = xDivs[0] == 0 ? 1 : 0;
500 i <= numXDivs && left < W;
501 i++) {
502 if (i == numXDivs) {
503 right = W;
504 } else {
505 right = xDivs[i];
506 }
507 c = get_color(image->rows, left, top, right - 1, bottom - 1);
508 image->info9Patch.colors[colorIndex++] = c;
509 NOISY(if (c != Res_png_9patch::NO_COLOR) hasColor = true);
510 left = right;
511 }
512 top = bottom;
513 }
514
515 assert(colorIndex == numColors);
516
517 for (i=0; i<numColors; i++) {
518 if (hasColor) {
519 if (i == 0) printf("Colors in %s:\n ", imageName);
520 printf(" #%08x", image->info9Patch.colors[i]);
521 if (i == numColors - 1) printf("\n");
522 }
523 }
524
525 image->is9Patch = true;
526 image->info9Patch.deviceToFile();
527
528getout:
529 if (errorMsg) {
530 fprintf(stderr,
531 "ERROR: 9-patch image %s malformed.\n"
532 " %s.\n", imageName, errorMsg);
533 if (errorPixel >= 0) {
534 fprintf(stderr,
535 " Found at pixel #%d along %s edge.\n", errorPixel, errorEdge);
536 } else {
537 fprintf(stderr,
538 " Found along %s edge.\n", errorEdge);
539 }
540 return UNKNOWN_ERROR;
541 }
542 return NO_ERROR;
543}
544
545static void checkNinePatchSerialization(Res_png_9patch* inPatch, void * data)
546{
547 if (sizeof(void*) != sizeof(int32_t)) {
548 // can't deserialize on a non-32 bit system
549 return;
550 }
551 size_t patchSize = inPatch->serializedSize();
552 void * newData = malloc(patchSize);
553 memcpy(newData, data, patchSize);
554 Res_png_9patch* outPatch = inPatch->deserialize(newData);
555 // deserialization is done in place, so outPatch == newData
556 assert(outPatch == newData);
557 assert(outPatch->numXDivs == inPatch->numXDivs);
558 assert(outPatch->numYDivs == inPatch->numYDivs);
559 assert(outPatch->paddingLeft == inPatch->paddingLeft);
560 assert(outPatch->paddingRight == inPatch->paddingRight);
561 assert(outPatch->paddingTop == inPatch->paddingTop);
562 assert(outPatch->paddingBottom == inPatch->paddingBottom);
563 for (int i = 0; i < outPatch->numXDivs; i++) {
564 assert(outPatch->xDivs[i] == inPatch->xDivs[i]);
565 }
566 for (int i = 0; i < outPatch->numYDivs; i++) {
567 assert(outPatch->yDivs[i] == inPatch->yDivs[i]);
568 }
569 for (int i = 0; i < outPatch->numColors; i++) {
570 assert(outPatch->colors[i] == inPatch->colors[i]);
571 }
572 free(newData);
573}
574
575static bool patch_equals(Res_png_9patch& patch1, Res_png_9patch& patch2) {
576 if (!(patch1.numXDivs == patch2.numXDivs &&
577 patch1.numYDivs == patch2.numYDivs &&
578 patch1.numColors == patch2.numColors &&
579 patch1.paddingLeft == patch2.paddingLeft &&
580 patch1.paddingRight == patch2.paddingRight &&
581 patch1.paddingTop == patch2.paddingTop &&
582 patch1.paddingBottom == patch2.paddingBottom)) {
583 return false;
584 }
585 for (int i = 0; i < patch1.numColors; i++) {
586 if (patch1.colors[i] != patch2.colors[i]) {
587 return false;
588 }
589 }
590 for (int i = 0; i < patch1.numXDivs; i++) {
591 if (patch1.xDivs[i] != patch2.xDivs[i]) {
592 return false;
593 }
594 }
595 for (int i = 0; i < patch1.numYDivs; i++) {
596 if (patch1.yDivs[i] != patch2.yDivs[i]) {
597 return false;
598 }
599 }
600 return true;
601}
602
603static void dump_image(int w, int h, png_bytepp rows, int color_type)
604{
605 int i, j, rr, gg, bb, aa;
606
607 int bpp;
608 if (color_type == PNG_COLOR_TYPE_PALETTE || color_type == PNG_COLOR_TYPE_GRAY) {
609 bpp = 1;
610 } else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
611 bpp = 2;
612 } else if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
613 // We use a padding byte even when there is no alpha
614 bpp = 4;
615 } else {
616 printf("Unknown color type %d.\n", color_type);
617 }
618
619 for (j = 0; j < h; j++) {
620 png_bytep row = rows[j];
621 for (i = 0; i < w; i++) {
622 rr = row[0];
623 gg = row[1];
624 bb = row[2];
625 aa = row[3];
626 row += bpp;
627
628 if (i == 0) {
629 printf("Row %d:", j);
630 }
631 switch (bpp) {
632 case 1:
633 printf(" (%d)", rr);
634 break;
635 case 2:
636 printf(" (%d %d", rr, gg);
637 break;
638 case 3:
639 printf(" (%d %d %d)", rr, gg, bb);
640 break;
641 case 4:
642 printf(" (%d %d %d %d)", rr, gg, bb, aa);
643 break;
644 }
645 if (i == (w - 1)) {
646 NOISY(printf("\n"));
647 }
648 }
649 }
650}
651
652#define MAX(a,b) ((a)>(b)?(a):(b))
653#define ABS(a) ((a)<0?-(a):(a))
654
655static void analyze_image(const char *imageName, image_info &imageInfo, int grayscaleTolerance,
656 png_colorp rgbPalette, png_bytep alphaPalette,
657 int *paletteEntries, bool *hasTransparency, int *colorType,
658 png_bytepp outRows)
659{
660 int w = imageInfo.width;
661 int h = imageInfo.height;
662 int i, j, rr, gg, bb, aa, idx;
663 uint32_t colors[256], col;
664 int num_colors = 0;
665 int maxGrayDeviation = 0;
666
667 bool isOpaque = true;
668 bool isPalette = true;
669 bool isGrayscale = true;
670
671 // Scan the entire image and determine if:
672 // 1. Every pixel has R == G == B (grayscale)
673 // 2. Every pixel has A == 255 (opaque)
674 // 3. There are no more than 256 distinct RGBA colors
675
676 // NOISY(printf("Initial image data:\n"));
677 // dump_image(w, h, imageInfo.rows, PNG_COLOR_TYPE_RGB_ALPHA);
678
679 for (j = 0; j < h; j++) {
680 png_bytep row = imageInfo.rows[j];
681 png_bytep out = outRows[j];
682 for (i = 0; i < w; i++) {
683 rr = *row++;
684 gg = *row++;
685 bb = *row++;
686 aa = *row++;
687
688 int odev = maxGrayDeviation;
689 maxGrayDeviation = MAX(ABS(rr - gg), maxGrayDeviation);
690 maxGrayDeviation = MAX(ABS(gg - bb), maxGrayDeviation);
691 maxGrayDeviation = MAX(ABS(bb - rr), maxGrayDeviation);
692 if (maxGrayDeviation > odev) {
693 NOISY(printf("New max dev. = %d at pixel (%d, %d) = (%d %d %d %d)\n",
694 maxGrayDeviation, i, j, rr, gg, bb, aa));
695 }
696
697 // Check if image is really grayscale
698 if (isGrayscale) {
699 if (rr != gg || rr != bb) {
700 NOISY(printf("Found a non-gray pixel at %d, %d = (%d %d %d %d)\n",
701 i, j, rr, gg, bb, aa));
702 isGrayscale = false;
703 }
704 }
705
706 // Check if image is really opaque
707 if (isOpaque) {
708 if (aa != 0xff) {
709 NOISY(printf("Found a non-opaque pixel at %d, %d = (%d %d %d %d)\n",
710 i, j, rr, gg, bb, aa));
711 isOpaque = false;
712 }
713 }
714
715 // Check if image is really <= 256 colors
716 if (isPalette) {
717 col = (uint32_t) ((rr << 24) | (gg << 16) | (bb << 8) | aa);
718 bool match = false;
719 for (idx = 0; idx < num_colors; idx++) {
720 if (colors[idx] == col) {
721 match = true;
722 break;
723 }
724 }
725
726 // Write the palette index for the pixel to outRows optimistically
727 // We might overwrite it later if we decide to encode as gray or
728 // gray + alpha
729 *out++ = idx;
730 if (!match) {
731 if (num_colors == 256) {
732 NOISY(printf("Found 257th color at %d, %d\n", i, j));
733 isPalette = false;
734 } else {
735 colors[num_colors++] = col;
736 }
737 }
738 }
739 }
740 }
741
742 *paletteEntries = 0;
743 *hasTransparency = !isOpaque;
744 int bpp = isOpaque ? 3 : 4;
745 int paletteSize = w * h + bpp * num_colors;
746
747 NOISY(printf("isGrayscale = %s\n", isGrayscale ? "true" : "false"));
748 NOISY(printf("isOpaque = %s\n", isOpaque ? "true" : "false"));
749 NOISY(printf("isPalette = %s\n", isPalette ? "true" : "false"));
750 NOISY(printf("Size w/ palette = %d, gray+alpha = %d, rgb(a) = %d\n",
751 paletteSize, 2 * w * h, bpp * w * h));
752 NOISY(printf("Max gray deviation = %d, tolerance = %d\n", maxGrayDeviation, grayscaleTolerance));
753
754 // Choose the best color type for the image.
755 // 1. Opaque gray - use COLOR_TYPE_GRAY at 1 byte/pixel
756 // 2. Gray + alpha - use COLOR_TYPE_PALETTE if the number of distinct combinations
757 // is sufficiently small, otherwise use COLOR_TYPE_GRAY_ALPHA
758 // 3. RGB(A) - use COLOR_TYPE_PALETTE if the number of distinct colors is sufficiently
759 // small, otherwise use COLOR_TYPE_RGB{_ALPHA}
760 if (isGrayscale) {
761 if (isOpaque) {
762 *colorType = PNG_COLOR_TYPE_GRAY; // 1 byte/pixel
763 } else {
764 // Use a simple heuristic to determine whether using a palette will
765 // save space versus using gray + alpha for each pixel.
766 // This doesn't take into account chunk overhead, filtering, LZ
767 // compression, etc.
768 if (isPalette && (paletteSize < 2 * w * h)) {
769 *colorType = PNG_COLOR_TYPE_PALETTE; // 1 byte/pixel + 4 bytes/color
770 } else {
771 *colorType = PNG_COLOR_TYPE_GRAY_ALPHA; // 2 bytes per pixel
772 }
773 }
774 } else if (isPalette && (paletteSize < bpp * w * h)) {
775 *colorType = PNG_COLOR_TYPE_PALETTE;
776 } else {
777 if (maxGrayDeviation <= grayscaleTolerance) {
778 printf("%s: forcing image to gray (max deviation = %d)\n", imageName, maxGrayDeviation);
779 *colorType = isOpaque ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_GRAY_ALPHA;
780 } else {
781 *colorType = isOpaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
782 }
783 }
784
785 // Perform postprocessing of the image or palette data based on the final
786 // color type chosen
787
788 if (*colorType == PNG_COLOR_TYPE_PALETTE) {
789 // Create separate RGB and Alpha palettes and set the number of colors
790 *paletteEntries = num_colors;
791
792 // Create the RGB and alpha palettes
793 for (int idx = 0; idx < num_colors; idx++) {
794 col = colors[idx];
795 rgbPalette[idx].red = (png_byte) ((col >> 24) & 0xff);
796 rgbPalette[idx].green = (png_byte) ((col >> 16) & 0xff);
797 rgbPalette[idx].blue = (png_byte) ((col >> 8) & 0xff);
798 alphaPalette[idx] = (png_byte) (col & 0xff);
799 }
800 } else if (*colorType == PNG_COLOR_TYPE_GRAY || *colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
801 // If the image is gray or gray + alpha, compact the pixels into outRows
802 for (j = 0; j < h; j++) {
803 png_bytep row = imageInfo.rows[j];
804 png_bytep out = outRows[j];
805 for (i = 0; i < w; i++) {
806 rr = *row++;
807 gg = *row++;
808 bb = *row++;
809 aa = *row++;
810
811 if (isGrayscale) {
812 *out++ = rr;
813 } else {
814 *out++ = (png_byte) (rr * 0.2126f + gg * 0.7152f + bb * 0.0722f);
815 }
816 if (!isOpaque) {
817 *out++ = aa;
818 }
819 }
820 }
821 }
822}
823
824
825static void write_png(const char* imageName,
826 png_structp write_ptr, png_infop write_info,
827 image_info& imageInfo, int grayscaleTolerance)
828{
829 bool optimize = true;
830 png_uint_32 width, height;
831 int color_type;
832 int bit_depth, interlace_type, compression_type;
833 int i;
834
835 png_unknown_chunk unknowns[1];
836
837 png_bytepp outRows = (png_bytepp) malloc((int) imageInfo.height * png_sizeof(png_bytep));
838 if (outRows == (png_bytepp) 0) {
839 printf("Can't allocate output buffer!\n");
840 exit(1);
841 }
842 for (i = 0; i < (int) imageInfo.height; i++) {
843 outRows[i] = (png_bytep) malloc(2 * (int) imageInfo.width);
844 if (outRows[i] == (png_bytep) 0) {
845 printf("Can't allocate output buffer!\n");
846 exit(1);
847 }
848 }
849
850 png_set_compression_level(write_ptr, Z_BEST_COMPRESSION);
851
852 NOISY(printf("Writing image %s: w = %d, h = %d\n", imageName,
853 (int) imageInfo.width, (int) imageInfo.height));
854
855 png_color rgbPalette[256];
856 png_byte alphaPalette[256];
857 bool hasTransparency;
858 int paletteEntries;
859
860 analyze_image(imageName, imageInfo, grayscaleTolerance, rgbPalette, alphaPalette,
861 &paletteEntries, &hasTransparency, &color_type, outRows);
862
863 // If the image is a 9-patch, we need to preserve it as a ARGB file to make
864 // sure the pixels will not be pre-dithered/clamped until we decide they are
865 if (imageInfo.is9Patch && (color_type == PNG_COLOR_TYPE_RGB ||
866 color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_PALETTE)) {
867 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
868 }
869
870 switch (color_type) {
871 case PNG_COLOR_TYPE_PALETTE:
872 NOISY(printf("Image %s has %d colors%s, using PNG_COLOR_TYPE_PALETTE\n",
873 imageName, paletteEntries,
874 hasTransparency ? " (with alpha)" : ""));
875 break;
876 case PNG_COLOR_TYPE_GRAY:
877 NOISY(printf("Image %s is opaque gray, using PNG_COLOR_TYPE_GRAY\n", imageName));
878 break;
879 case PNG_COLOR_TYPE_GRAY_ALPHA:
880 NOISY(printf("Image %s is gray + alpha, using PNG_COLOR_TYPE_GRAY_ALPHA\n", imageName));
881 break;
882 case PNG_COLOR_TYPE_RGB:
883 NOISY(printf("Image %s is opaque RGB, using PNG_COLOR_TYPE_RGB\n", imageName));
884 break;
885 case PNG_COLOR_TYPE_RGB_ALPHA:
886 NOISY(printf("Image %s is RGB + alpha, using PNG_COLOR_TYPE_RGB_ALPHA\n", imageName));
887 break;
888 }
889
890 png_set_IHDR(write_ptr, write_info, imageInfo.width, imageInfo.height,
891 8, color_type, PNG_INTERLACE_NONE,
892 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
893
894 if (color_type == PNG_COLOR_TYPE_PALETTE) {
895 png_set_PLTE(write_ptr, write_info, rgbPalette, paletteEntries);
896 if (hasTransparency) {
897 png_set_tRNS(write_ptr, write_info, alphaPalette, paletteEntries, (png_color_16p) 0);
898 }
899 png_set_filter(write_ptr, 0, PNG_NO_FILTERS);
900 } else {
901 png_set_filter(write_ptr, 0, PNG_ALL_FILTERS);
902 }
903
904 if (imageInfo.is9Patch) {
905 NOISY(printf("Adding 9-patch info...\n"));
906 strcpy((char*)unknowns[0].name, "npTc");
907 unknowns[0].data = (png_byte*)imageInfo.info9Patch.serialize();
908 unknowns[0].size = imageInfo.info9Patch.serializedSize();
909 // TODO: remove the check below when everything works
910 checkNinePatchSerialization(&imageInfo.info9Patch, unknowns[0].data);
911 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
912 (png_byte*)"npTc", 1);
913 png_set_unknown_chunks(write_ptr, write_info, unknowns, 1);
914 // XXX I can't get this to work without forcibly changing
915 // the location to what I want... which apparently is supposed
916 // to be a private API, but everything else I have tried results
917 // in the location being set to what I -last- wrote so I never
918 // get written. :p
919 png_set_unknown_chunk_location(write_ptr, write_info, 0, PNG_HAVE_PLTE);
920 }
921
922 png_write_info(write_ptr, write_info);
923
924 png_bytepp rows;
925 if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
926 png_set_filler(write_ptr, 0, PNG_FILLER_AFTER);
927 rows = imageInfo.rows;
928 } else {
929 rows = outRows;
930 }
931 png_write_image(write_ptr, rows);
932
933// NOISY(printf("Final image data:\n"));
934// dump_image(imageInfo.width, imageInfo.height, rows, color_type);
935
936 png_write_end(write_ptr, write_info);
937
938 for (i = 0; i < (int) imageInfo.height; i++) {
939 free(outRows[i]);
940 }
941 free(outRows);
942
943 png_get_IHDR(write_ptr, write_info, &width, &height,
944 &bit_depth, &color_type, &interlace_type,
945 &compression_type, NULL);
946
947 NOISY(printf("Image written: w=%d, h=%d, d=%d, colors=%d, inter=%d, comp=%d\n",
948 (int)width, (int)height, bit_depth, color_type, interlace_type,
949 compression_type));
950}
951
952status_t preProcessImage(Bundle* bundle, const sp<AaptAssets>& assets,
953 const sp<AaptFile>& file, String8* outNewLeafName)
954{
955 String8 ext(file->getPath().getPathExtension());
956
957 // We currently only process PNG images.
958 if (strcmp(ext.string(), ".png") != 0) {
959 return NO_ERROR;
960 }
961
962 // Example of renaming a file:
963 //*outNewLeafName = file->getPath().getBasePath().getFileName();
964 //outNewLeafName->append(".nupng");
965
966 String8 printableName(file->getPrintableSource());
967
968 png_structp read_ptr = NULL;
969 png_infop read_info = NULL;
970 FILE* fp;
971
972 image_info imageInfo;
973
974 png_structp write_ptr = NULL;
975 png_infop write_info = NULL;
976
977 status_t error = UNKNOWN_ERROR;
978
979 const size_t nameLen = file->getPath().length();
980
981 fp = fopen(file->getSourceFile().string(), "rb");
982 if (fp == NULL) {
983 fprintf(stderr, "%s: ERROR: Unable to open PNG file\n", printableName.string());
984 goto bail;
985 }
986
987 read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, (png_error_ptr)NULL,
988 (png_error_ptr)NULL);
989 if (!read_ptr) {
990 goto bail;
991 }
992
993 read_info = png_create_info_struct(read_ptr);
994 if (!read_info) {
995 goto bail;
996 }
997
998 if (setjmp(png_jmpbuf(read_ptr))) {
999 goto bail;
1000 }
1001
1002 png_init_io(read_ptr, fp);
1003
1004 read_png(printableName.string(), read_ptr, read_info, &imageInfo);
1005
1006 if (nameLen > 6) {
1007 const char* name = file->getPath().string();
1008 if (name[nameLen-5] == '9' && name[nameLen-6] == '.') {
1009 if (do_9patch(printableName.string(), &imageInfo) != NO_ERROR) {
1010 goto bail;
1011 }
1012 }
1013 }
1014
1015 write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, (png_error_ptr)NULL,
1016 (png_error_ptr)NULL);
1017 if (!write_ptr)
1018 {
1019 goto bail;
1020 }
1021
1022 write_info = png_create_info_struct(write_ptr);
1023 if (!write_info)
1024 {
1025 goto bail;
1026 }
1027
1028 png_set_write_fn(write_ptr, (void*)file.get(),
1029 png_write_aapt_file, png_flush_aapt_file);
1030
1031 if (setjmp(png_jmpbuf(write_ptr)))
1032 {
1033 goto bail;
1034 }
1035
1036 write_png(printableName.string(), write_ptr, write_info, imageInfo,
1037 bundle->getGrayscaleTolerance());
1038
1039 error = NO_ERROR;
1040
1041 if (bundle->getVerbose()) {
1042 fseek(fp, 0, SEEK_END);
1043 size_t oldSize = (size_t)ftell(fp);
1044 size_t newSize = file->getSize();
1045 float factor = ((float)newSize)/oldSize;
1046 int percent = (int)(factor*100);
1047 printf(" (processed image %s: %d%% size of source)\n", printableName.string(), percent);
1048 }
1049
1050bail:
1051 if (read_ptr) {
1052 png_destroy_read_struct(&read_ptr, &read_info, (png_infopp)NULL);
1053 }
1054 if (fp) {
1055 fclose(fp);
1056 }
1057 if (write_ptr) {
1058 png_destroy_write_struct(&write_ptr, &write_info);
1059 }
1060
1061 if (error != NO_ERROR) {
1062 fprintf(stderr, "ERROR: Failure processing PNG image %s\n",
1063 file->getPrintableSource().string());
1064 }
1065 return error;
1066}
1067
1068
1069
1070status_t postProcessImage(const sp<AaptAssets>& assets,
1071 ResourceTable* table, const sp<AaptFile>& file)
1072{
1073 String8 ext(file->getPath().getPathExtension());
1074
1075 // At this point, now that we have all the resource data, all we need to
1076 // do is compile XML files.
1077 if (strcmp(ext.string(), ".xml") == 0) {
1078 return compileXmlFile(assets, file, table);
1079 }
1080
1081 return NO_ERROR;
1082}