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