The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <linux/fb.h> |
| 28 | #include <linux/kd.h> |
| 29 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 30 | #include <png.h> |
| 31 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | #include "minui.h" |
| 33 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 34 | #ifdef FASTMMI_FEATURE |
| 35 | char *locale = NULL; |
| 36 | #else |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 37 | extern char* locale; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 38 | #endif |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 39 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 40 | #define SURFACE_DATA_ALIGNMENT 8 |
| 41 | |
| 42 | static gr_surface malloc_surface(size_t data_size) { |
| 43 | unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT); |
| 44 | if (temp == NULL) return NULL; |
| 45 | gr_surface surface = (gr_surface) temp; |
| 46 | surface->data = temp + sizeof(GRSurface) + |
| 47 | (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); |
| 48 | return surface; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 49 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 50 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 51 | static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, |
| 52 | png_uint_32* width, png_uint_32* height, png_byte* channels) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | char resPath[256]; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 54 | unsigned char header[8]; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 55 | int result = 0; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 56 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 57 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 58 | resPath[sizeof(resPath)-1] = '\0'; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 59 | FILE* fp = fopen(resPath, "rb"); |
| 60 | if (fp == NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | result = -1; |
| 62 | goto exit; |
| 63 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 64 | |
| 65 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 66 | if (bytesRead != sizeof(header)) { |
| 67 | result = -2; |
| 68 | goto exit; |
| 69 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 70 | |
| 71 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 72 | result = -3; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | goto exit; |
| 74 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 75 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 76 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 77 | if (!*png_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 78 | result = -4; |
| 79 | goto exit; |
| 80 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 81 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 82 | *info_ptr = png_create_info_struct(*png_ptr); |
| 83 | if (!*info_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 | result = -5; |
| 85 | goto exit; |
| 86 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 87 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 88 | if (setjmp(png_jmpbuf(*png_ptr))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 89 | result = -6; |
| 90 | goto exit; |
| 91 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 92 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 93 | png_init_io(*png_ptr, fp); |
| 94 | png_set_sig_bytes(*png_ptr, sizeof(header)); |
| 95 | png_read_info(*png_ptr, *info_ptr); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 96 | |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 97 | int color_type, bit_depth; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 98 | png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 99 | &color_type, NULL, NULL, NULL); |
| 100 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 101 | *channels = png_get_channels(*png_ptr, *info_ptr); |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 102 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 103 | if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { |
| 104 | // 8-bit RGB images: great, nothing to do. |
| 105 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { |
| 106 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 107 | png_set_expand_gray_1_2_4_to_8(*png_ptr); |
Doug Zongker | 577a130 | 2014-03-20 08:27:01 -0700 | [diff] [blame] | 108 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 109 | // paletted images: expand to 8-bit RGB. Note that we DON'T |
| 110 | // currently expand the tRNS chunk (if any) to an alpha |
| 111 | // channel, because minui doesn't support alpha channels in |
| 112 | // general. |
| 113 | png_set_palette_to_rgb(*png_ptr); |
| 114 | *channels = 3; |
| 115 | } else { |
| 116 | fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", |
Doug Zongker | a388a76 | 2014-03-17 16:51:47 -0700 | [diff] [blame] | 117 | bit_depth, *channels, color_type); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 118 | result = -7; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 119 | goto exit; |
| 120 | } |
| 121 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 122 | return result; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 123 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 124 | exit: |
| 125 | if (result < 0) { |
| 126 | png_destroy_read_struct(png_ptr, info_ptr, NULL); |
| 127 | } |
| 128 | if (fp != NULL) { |
| 129 | fclose(fp); |
| 130 | } |
| 131 | |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | // "display" surfaces are transformed into the framebuffer's required |
| 136 | // pixel format (currently only RGBX is supported) at load time, so |
| 137 | // gr_blit() can be nothing more than a memcpy() for each row. The |
| 138 | // next two functions are the only ones that know anything about the |
| 139 | // framebuffer pixel format; they need to be modified if the |
| 140 | // framebuffer format changes (but nothing else should). |
| 141 | |
| 142 | // Allocate and return a gr_surface sufficient for storing an image of |
| 143 | // the indicated size in the framebuffer pixel format. |
| 144 | static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) { |
| 145 | gr_surface surface; |
| 146 | |
| 147 | surface = malloc_surface(width * height * 4); |
| 148 | if (surface == NULL) return NULL; |
| 149 | |
| 150 | surface->width = width; |
| 151 | surface->height = height; |
| 152 | surface->row_bytes = width * 4; |
| 153 | surface->pixel_bytes = 4; |
| 154 | |
| 155 | return surface; |
| 156 | } |
| 157 | |
| 158 | // Copy 'input_row' to 'output_row', transforming it to the |
| 159 | // framebuffer pixel format. The input format depends on the value of |
| 160 | // 'channels': |
| 161 | // |
| 162 | // 1 - input is 8-bit grayscale |
| 163 | // 3 - input is 24-bit RGB |
| 164 | // 4 - input is 32-bit RGBA/RGBX |
| 165 | // |
| 166 | // 'width' is the number of pixels in the row. |
| 167 | static void transform_rgb_to_draw(unsigned char* input_row, |
| 168 | unsigned char* output_row, |
| 169 | int channels, int width) { |
| 170 | int x; |
| 171 | unsigned char* ip = input_row; |
| 172 | unsigned char* op = output_row; |
| 173 | |
| 174 | switch (channels) { |
| 175 | case 1: |
| 176 | // expand gray level to RGBX |
| 177 | for (x = 0; x < width; ++x) { |
| 178 | *op++ = *ip; |
| 179 | *op++ = *ip; |
| 180 | *op++ = *ip; |
| 181 | *op++ = 0xff; |
| 182 | ip++; |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | case 3: |
| 187 | // expand RGBA to RGBX |
| 188 | for (x = 0; x < width; ++x) { |
| 189 | *op++ = *ip++; |
| 190 | *op++ = *ip++; |
| 191 | *op++ = *ip++; |
| 192 | *op++ = 0xff; |
| 193 | } |
| 194 | break; |
| 195 | |
| 196 | case 4: |
| 197 | // copy RGBA to RGBX |
| 198 | memcpy(output_row, input_row, width*4); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int res_create_display_surface(const char* name, gr_surface* pSurface) { |
| 204 | gr_surface surface = NULL; |
| 205 | int result = 0; |
| 206 | png_structp png_ptr = NULL; |
| 207 | png_infop info_ptr = NULL; |
| 208 | png_uint_32 width, height; |
| 209 | png_byte channels; |
| 210 | |
| 211 | *pSurface = NULL; |
| 212 | |
| 213 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 214 | if (result < 0) return result; |
| 215 | |
| 216 | surface = init_display_surface(width, height); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 217 | if (surface == NULL) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 218 | result = -8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 219 | goto exit; |
| 220 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 221 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 222 | unsigned char* p_row = malloc(width * 4); |
Edwin Vane | edc5d17 | 2012-07-27 11:32:23 -0400 | [diff] [blame] | 223 | unsigned int y; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 224 | for (y = 0; y < height; ++y) { |
| 225 | png_read_row(png_ptr, p_row, NULL); |
| 226 | transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 227 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 228 | free(p_row); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 229 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 230 | *pSurface = surface; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 231 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 232 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 233 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 234 | if (result < 0 && surface != NULL) free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 235 | return result; |
| 236 | } |
| 237 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 238 | int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) { |
| 239 | gr_surface* surface = NULL; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 240 | int result = 0; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 241 | png_structp png_ptr = NULL; |
| 242 | png_infop info_ptr = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 243 | png_uint_32 width, height; |
| 244 | png_byte channels; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 245 | int i; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 246 | |
| 247 | *pSurface = NULL; |
| 248 | *frames = -1; |
| 249 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 250 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 251 | if (result < 0) return result; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 252 | |
| 253 | *frames = 1; |
| 254 | png_textp text; |
| 255 | int num_text; |
| 256 | if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { |
| 257 | for (i = 0; i < num_text; ++i) { |
| 258 | if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { |
| 259 | *frames = atoi(text[i].text); |
| 260 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 263 | printf(" found frames = %d\n", *frames); |
| 264 | } |
| 265 | |
| 266 | if (height % *frames != 0) { |
| 267 | printf("bad height (%d) for frame count (%d)\n", height, *frames); |
| 268 | result = -9; |
| 269 | goto exit; |
| 270 | } |
| 271 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 272 | surface = malloc(*frames * sizeof(gr_surface)); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 273 | if (surface == NULL) { |
| 274 | result = -8; |
| 275 | goto exit; |
| 276 | } |
| 277 | for (i = 0; i < *frames; ++i) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 278 | surface[i] = init_display_surface(width, height / *frames); |
| 279 | if (surface[i] == NULL) { |
| 280 | result = -8; |
| 281 | goto exit; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 285 | unsigned char* p_row = malloc(width * 4); |
| 286 | unsigned int y; |
| 287 | for (y = 0; y < height; ++y) { |
| 288 | png_read_row(png_ptr, p_row, NULL); |
| 289 | int frame = y % *frames; |
| 290 | unsigned char* out_row = surface[frame]->data + |
| 291 | (y / *frames) * surface[frame]->row_bytes; |
| 292 | transform_rgb_to_draw(p_row, out_row, channels, width); |
| 293 | } |
| 294 | free(p_row); |
| 295 | |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 296 | *pSurface = (gr_surface*) surface; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 297 | |
| 298 | exit: |
| 299 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 300 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 301 | if (result < 0) { |
| 302 | if (surface) { |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 303 | for (i = 0; i < *frames; ++i) { |
| 304 | if (surface[i]) free(surface[i]); |
| 305 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 306 | free(surface); |
| 307 | } |
| 308 | } |
| 309 | return result; |
| 310 | } |
| 311 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 312 | int res_create_alpha_surface(const char* name, gr_surface* pSurface) { |
| 313 | gr_surface surface = NULL; |
| 314 | int result = 0; |
| 315 | png_structp png_ptr = NULL; |
| 316 | png_infop info_ptr = NULL; |
| 317 | png_uint_32 width, height; |
| 318 | png_byte channels; |
| 319 | |
| 320 | *pSurface = NULL; |
| 321 | |
| 322 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 323 | if (result < 0) return result; |
| 324 | |
| 325 | if (channels != 1) { |
| 326 | result = -7; |
| 327 | goto exit; |
| 328 | } |
| 329 | |
| 330 | surface = malloc_surface(width * height); |
| 331 | if (surface == NULL) { |
| 332 | result = -8; |
| 333 | goto exit; |
| 334 | } |
| 335 | surface->width = width; |
| 336 | surface->height = height; |
| 337 | surface->row_bytes = width; |
| 338 | surface->pixel_bytes = 1; |
| 339 | |
| 340 | unsigned char* p_row; |
| 341 | unsigned int y; |
| 342 | for (y = 0; y < height; ++y) { |
| 343 | p_row = surface->data + y * surface->row_bytes; |
| 344 | png_read_row(png_ptr, p_row, NULL); |
| 345 | } |
| 346 | |
| 347 | *pSurface = surface; |
| 348 | |
| 349 | exit: |
| 350 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 351 | if (result < 0 && surface != NULL) free(surface); |
| 352 | return result; |
| 353 | } |
| 354 | |
| 355 | static int matches_locale(const char* loc, const char* locale) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 356 | if (locale == NULL) return 0; |
| 357 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 358 | if (strcmp(loc, locale) == 0) return 1; |
| 359 | |
| 360 | // if loc does *not* have an underscore, and it matches the start |
| 361 | // of locale, and the next character in locale *is* an underscore, |
| 362 | // that's a match. For instance, loc == "en" matches locale == |
| 363 | // "en_US". |
| 364 | |
| 365 | int i; |
| 366 | for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); |
| 367 | if (loc[i] == '_') return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 368 | |
| 369 | return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); |
| 370 | } |
| 371 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 372 | int res_create_localized_alpha_surface(const char* name, |
| 373 | const char* locale, |
| 374 | gr_surface* pSurface) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 375 | gr_surface surface = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 376 | int result = 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 377 | png_structp png_ptr = NULL; |
| 378 | png_infop info_ptr = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 379 | png_uint_32 width, height; |
| 380 | png_byte channels; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 381 | |
| 382 | *pSurface = NULL; |
| 383 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 384 | if (locale == NULL) { |
| 385 | surface = malloc_surface(0); |
| 386 | surface->width = 0; |
| 387 | surface->height = 0; |
| 388 | surface->row_bytes = 0; |
| 389 | surface->pixel_bytes = 1; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 390 | goto exit; |
| 391 | } |
| 392 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 393 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 394 | if (result < 0) return result; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 395 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 396 | if (channels != 1) { |
| 397 | result = -7; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 398 | goto exit; |
| 399 | } |
| 400 | |
| 401 | unsigned char* row = malloc(width); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 402 | png_uint_32 y; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 403 | for (y = 0; y < height; ++y) { |
| 404 | png_read_row(png_ptr, row, NULL); |
| 405 | int w = (row[1] << 8) | row[0]; |
| 406 | int h = (row[3] << 8) | row[2]; |
| 407 | int len = row[4]; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 408 | char* loc = (char*)row+5; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 409 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 410 | if (y+1+h >= height || matches_locale(loc, locale)) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 411 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 412 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 413 | surface = malloc_surface(w*h); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 414 | if (surface == NULL) { |
| 415 | result = -8; |
| 416 | goto exit; |
| 417 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 418 | surface->width = w; |
| 419 | surface->height = h; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 420 | surface->row_bytes = w; |
| 421 | surface->pixel_bytes = 1; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 422 | |
| 423 | int i; |
| 424 | for (i = 0; i < h; ++i, ++y) { |
| 425 | png_read_row(png_ptr, row, NULL); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 426 | memcpy(surface->data + i*w, row, w); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | *pSurface = (gr_surface) surface; |
| 430 | break; |
| 431 | } else { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 432 | int i; |
| 433 | for (i = 0; i < h; ++i, ++y) { |
| 434 | png_read_row(png_ptr, row, NULL); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | exit: |
| 440 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 441 | if (result < 0 && surface != NULL) free(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 442 | return result; |
| 443 | } |
| 444 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 445 | void res_free_surface(gr_surface surface) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 446 | free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 447 | } |