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