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