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 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 30 | #include <pixelflinger/pixelflinger.h> |
| 31 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 32 | #include <png.h> |
| 33 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "minui.h" |
| 35 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 36 | #ifdef FASTMMI_FEATURE |
| 37 | char *locale = NULL; |
| 38 | #else |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 39 | extern char* locale; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 40 | #endif |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 41 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 42 | // libpng gives "undefined reference to 'pow'" errors, and I have no |
| 43 | // idea how to convince the build system to link with -lm. We don't |
| 44 | // need this functionality (it's used for gamma adjustment) so provide |
| 45 | // a dummy implementation to satisfy the linker. |
| 46 | double pow(double x, double y) { |
| 47 | return x * y; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 48 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 50 | int res_create_surface(const char* name, gr_surface* pSurface) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 51 | char resPath[256]; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 52 | GGLSurface* surface = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 53 | int result = 0; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 54 | unsigned char header[8]; |
| 55 | png_structp png_ptr = NULL; |
| 56 | png_infop info_ptr = NULL; |
| 57 | |
| 58 | *pSurface = NULL; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 59 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 60 | 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] | 61 | resPath[sizeof(resPath)-1] = '\0'; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 62 | FILE* fp = fopen(resPath, "rb"); |
| 63 | if (fp == NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | result = -1; |
| 65 | goto exit; |
| 66 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 67 | |
| 68 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | if (bytesRead != sizeof(header)) { |
| 70 | result = -2; |
| 71 | goto exit; |
| 72 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 73 | |
| 74 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 75 | result = -3; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | goto exit; |
| 77 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 78 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 79 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 80 | if (!png_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 81 | result = -4; |
| 82 | goto exit; |
| 83 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 84 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 85 | info_ptr = png_create_info_struct(png_ptr); |
| 86 | if (!info_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 87 | result = -5; |
| 88 | goto exit; |
| 89 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 90 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 91 | if (setjmp(png_jmpbuf(png_ptr))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 92 | result = -6; |
| 93 | goto exit; |
| 94 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 95 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 96 | png_init_io(png_ptr, fp); |
| 97 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 98 | png_read_info(png_ptr, info_ptr); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 99 | |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 100 | int color_type, bit_depth; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 101 | size_t width, height; |
| 102 | |
Ethan Yonker | 5a95c3f | 2014-11-07 13:32:55 -0600 | [diff] [blame] | 103 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 104 | &color_type, NULL, NULL, NULL); |
| 105 | |
Ethan Yonker | 5a95c3f | 2014-11-07 13:32:55 -0600 | [diff] [blame] | 106 | png_byte channels = png_get_channels(png_ptr, info_ptr); |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 107 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 108 | if (!(bit_depth == 8 && |
| 109 | ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) || |
| 110 | (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) || |
| 111 | (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE || |
| 112 | color_type == PNG_COLOR_TYPE_GRAY))))) { |
| 113 | return -7; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 114 | goto exit; |
| 115 | } |
| 116 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 117 | size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width; |
| 118 | size_t pixelSize = stride * height; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 119 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 120 | surface = malloc(sizeof(GGLSurface) + pixelSize); |
| 121 | if (surface == NULL) { |
| 122 | result = -8; |
| 123 | goto exit; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 124 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 125 | unsigned char* pData = (unsigned char*) (surface + 1); |
| 126 | surface->version = sizeof(GGLSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 127 | surface->width = width; |
| 128 | surface->height = height; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 129 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 130 | surface->data = pData; |
| 131 | surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 : |
| 132 | ((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8)); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 133 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 134 | int alpha = 0; |
| 135 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 136 | png_set_palette_to_rgb(png_ptr); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 137 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 138 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 139 | png_set_tRNS_to_alpha(png_ptr); |
| 140 | alpha = 1; |
| 141 | } |
| 142 | if (color_type == PNG_COLOR_TYPE_GRAY) { |
| 143 | alpha = 1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 144 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 145 | |
Edwin Vane | edc5d17 | 2012-07-27 11:32:23 -0400 | [diff] [blame] | 146 | unsigned int y; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 147 | if (channels == 3 || (channels == 1 && !alpha)) { |
| 148 | for (y = 0; y < height; ++y) { |
| 149 | unsigned char* pRow = pData + y * stride; |
| 150 | png_read_row(png_ptr, pRow, NULL); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 151 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 152 | int x; |
| 153 | for(x = width - 1; x >= 0; x--) { |
| 154 | int sx = x * 3; |
| 155 | int dx = x * 4; |
| 156 | unsigned char r = pRow[sx]; |
| 157 | unsigned char g = pRow[sx + 1]; |
| 158 | unsigned char b = pRow[sx + 2]; |
| 159 | unsigned char a = 0xff; |
| 160 | pRow[dx ] = r; // r |
| 161 | pRow[dx + 1] = g; // g |
| 162 | pRow[dx + 2] = b; // b |
| 163 | pRow[dx + 3] = a; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 166 | } else { |
| 167 | for (y = 0; y < height; ++y) { |
| 168 | unsigned char* pRow = pData + y * stride; |
| 169 | png_read_row(png_ptr, pRow, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 172 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 173 | *pSurface = (gr_surface) surface; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 174 | |
| 175 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 176 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 177 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 178 | if (fp != NULL) { |
| 179 | fclose(fp); |
| 180 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 181 | if (result < 0) { |
| 182 | if (surface) { |
| 183 | free(surface); |
| 184 | } |
| 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 189 | static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, |
| 190 | png_uint_32* width, png_uint_32* height, png_byte* channels) { |
| 191 | char resPath[256]; |
| 192 | unsigned char header[8]; |
| 193 | int result = 0; |
| 194 | |
| 195 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
| 196 | resPath[sizeof(resPath)-1] = '\0'; |
| 197 | FILE* fp = fopen(resPath, "rb"); |
| 198 | if (fp == NULL) { |
| 199 | result = -1; |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
| 204 | if (bytesRead != sizeof(header)) { |
| 205 | result = -2; |
| 206 | goto exit; |
| 207 | } |
| 208 | |
| 209 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 210 | result = -3; |
| 211 | goto exit; |
| 212 | } |
| 213 | |
| 214 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 215 | if (!*png_ptr) { |
| 216 | result = -4; |
| 217 | goto exit; |
| 218 | } |
| 219 | |
| 220 | *info_ptr = png_create_info_struct(*png_ptr); |
| 221 | if (!*info_ptr) { |
| 222 | result = -5; |
| 223 | goto exit; |
| 224 | } |
| 225 | |
| 226 | if (setjmp(png_jmpbuf(*png_ptr))) { |
| 227 | result = -6; |
| 228 | goto exit; |
| 229 | } |
| 230 | |
| 231 | png_init_io(*png_ptr, fp); |
| 232 | png_set_sig_bytes(*png_ptr, sizeof(header)); |
| 233 | png_read_info(*png_ptr, *info_ptr); |
| 234 | |
| 235 | int color_type, bit_depth; |
| 236 | png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, |
| 237 | &color_type, NULL, NULL, NULL); |
| 238 | |
| 239 | *channels = png_get_channels(*png_ptr, *info_ptr); |
| 240 | |
| 241 | if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { |
| 242 | // 8-bit RGB images: great, nothing to do. |
| 243 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { |
| 244 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 245 | png_set_expand_gray_1_2_4_to_8(*png_ptr); |
| 246 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { |
| 247 | // paletted images: expand to 8-bit RGB. Note that we DON'T |
| 248 | // currently expand the tRNS chunk (if any) to an alpha |
| 249 | // channel, because minui doesn't support alpha channels in |
| 250 | // general. |
| 251 | png_set_palette_to_rgb(*png_ptr); |
| 252 | *channels = 3; |
| 253 | } else { |
| 254 | fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", |
| 255 | bit_depth, *channels, color_type); |
| 256 | result = -7; |
| 257 | goto exit; |
| 258 | } |
| 259 | |
| 260 | return result; |
| 261 | |
| 262 | exit: |
| 263 | if (result < 0) { |
| 264 | png_destroy_read_struct(png_ptr, info_ptr, NULL); |
| 265 | } |
| 266 | if (fp != NULL) { |
| 267 | fclose(fp); |
| 268 | } |
| 269 | |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) { |
| 274 | gr_surface* surface = NULL; |
| 275 | int result = 0; |
| 276 | png_structp png_ptr = NULL; |
| 277 | png_infop info_ptr = NULL; |
| 278 | png_uint_32 width, height; |
| 279 | png_byte channels; |
| 280 | int i; |
| 281 | |
| 282 | *pSurface = NULL; |
| 283 | *frames = -1; |
| 284 | |
| 285 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 286 | if (result < 0) return result; |
| 287 | |
| 288 | *frames = 1; |
| 289 | png_textp text; |
| 290 | int num_text; |
| 291 | if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { |
| 292 | for (i = 0; i < num_text; ++i) { |
| 293 | if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { |
| 294 | *frames = atoi(text[i].text); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | printf(" found frames = %d\n", *frames); |
| 299 | } |
| 300 | |
| 301 | if (height % *frames != 0) { |
| 302 | printf("bad height (%d) for frame count (%d)\n", height, *frames); |
| 303 | result = -9; |
| 304 | goto exit; |
| 305 | } |
| 306 | |
| 307 | surface = malloc(*frames * sizeof(gr_surface)); |
| 308 | if (surface == NULL) { |
| 309 | result = -8; |
| 310 | goto exit; |
| 311 | } |
| 312 | for (i = 0; i < *frames; ++i) { |
| 313 | surface[i] = NULL;//init_display_surface(width, height / *frames); |
| 314 | if (surface[i] == NULL) { |
| 315 | result = -8; |
| 316 | goto exit; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /*unsigned char* p_row = malloc(width * 4); |
| 321 | unsigned int y; |
| 322 | for (y = 0; y < height; ++y) { |
| 323 | png_read_row(png_ptr, p_row, NULL); |
| 324 | int frame = y % *frames; |
| 325 | unsigned char* out_row = surface[frame]->data + |
| 326 | (y / *frames) * surface[frame]->row_bytes; |
| 327 | transform_rgb_to_draw(p_row, out_row, channels, width); |
| 328 | } |
| 329 | free(p_row); this will need to be brought in line with the older resources and graphics code */ |
| 330 | |
| 331 | *pSurface = (gr_surface*) surface; |
| 332 | |
| 333 | exit: |
| 334 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 335 | |
| 336 | if (result < 0) { |
| 337 | if (surface) { |
| 338 | for (i = 0; i < *frames; ++i) { |
| 339 | if (surface[i]) free(surface[i]); |
| 340 | } |
| 341 | free(surface); |
| 342 | } |
| 343 | } |
| 344 | return result; |
| 345 | } |
| 346 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 347 | static int matches_locale(const char* loc) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 348 | if (locale == NULL) return 0; |
| 349 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 350 | if (strcmp(loc, locale) == 0) return 1; |
| 351 | |
| 352 | // if loc does *not* have an underscore, and it matches the start |
| 353 | // of locale, and the next character in locale *is* an underscore, |
| 354 | // that's a match. For instance, loc == "en" matches locale == |
| 355 | // "en_US". |
| 356 | |
| 357 | int i; |
| 358 | for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); |
| 359 | if (loc[i] == '_') return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 360 | |
| 361 | return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); |
| 362 | } |
| 363 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 364 | int res_create_localized_surface(const char* name, gr_surface* pSurface) { |
| 365 | char resPath[256]; |
| 366 | GGLSurface* surface = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 367 | int result = 0; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 368 | unsigned char header[8]; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 369 | png_structp png_ptr = NULL; |
| 370 | png_infop info_ptr = NULL; |
| 371 | |
| 372 | *pSurface = NULL; |
| 373 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 374 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
| 375 | resPath[sizeof(resPath)-1] = '\0'; |
| 376 | FILE* fp = fopen(resPath, "rb"); |
| 377 | if (fp == NULL) { |
| 378 | result = -1; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 379 | goto exit; |
| 380 | } |
| 381 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 382 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
| 383 | if (bytesRead != sizeof(header)) { |
| 384 | result = -2; |
| 385 | goto exit; |
| 386 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 387 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 388 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 389 | result = -3; |
| 390 | goto exit; |
| 391 | } |
| 392 | |
| 393 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 394 | if (!png_ptr) { |
| 395 | result = -4; |
| 396 | goto exit; |
| 397 | } |
| 398 | |
| 399 | info_ptr = png_create_info_struct(png_ptr); |
| 400 | if (!info_ptr) { |
| 401 | result = -5; |
| 402 | goto exit; |
| 403 | } |
| 404 | |
| 405 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 406 | result = -6; |
| 407 | goto exit; |
| 408 | } |
| 409 | |
| 410 | png_init_io(png_ptr, fp); |
| 411 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 412 | png_read_info(png_ptr, info_ptr); |
| 413 | |
| 414 | int color_type, bit_depth; |
| 415 | size_t width, height; |
| 416 | |
| 417 | png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth, |
| 418 | &color_type, NULL, NULL, NULL); |
| 419 | |
| 420 | png_byte* channels = png_get_channels(png_ptr, info_ptr); |
| 421 | size_t stride = 4 * width; |
| 422 | |
| 423 | if (!(bit_depth == 8 && |
| 424 | (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) { |
| 425 | return -7; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 426 | goto exit; |
| 427 | } |
| 428 | |
| 429 | unsigned char* row = malloc(width); |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 430 | int y; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 431 | for (y = 0; y < height; ++y) { |
| 432 | png_read_row(png_ptr, row, NULL); |
| 433 | int w = (row[1] << 8) | row[0]; |
| 434 | int h = (row[3] << 8) | row[2]; |
| 435 | int len = row[4]; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 436 | char* loc = row+5; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 437 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 438 | if (y+1+h >= height || matches_locale(loc)) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 439 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 440 | |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 441 | surface = malloc(sizeof(GGLSurface)); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 442 | if (surface == NULL) { |
| 443 | result = -8; |
| 444 | goto exit; |
| 445 | } |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 446 | unsigned char* pData = malloc(w*h); |
| 447 | |
| 448 | surface->version = sizeof(GGLSurface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 449 | surface->width = w; |
| 450 | surface->height = h; |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 451 | surface->stride = w; /* Yes, pixels, not bytes */ |
| 452 | surface->data = pData; |
| 453 | surface->format = GGL_PIXEL_FORMAT_A_8; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 454 | |
| 455 | int i; |
| 456 | for (i = 0; i < h; ++i, ++y) { |
| 457 | png_read_row(png_ptr, row, NULL); |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 458 | memcpy(pData + i*w, row, w); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | *pSurface = (gr_surface) surface; |
| 462 | break; |
| 463 | } else { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 464 | int i; |
| 465 | for (i = 0; i < h; ++i, ++y) { |
| 466 | png_read_row(png_ptr, row, NULL); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | exit: |
| 472 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 473 | |
| 474 | if (fp != NULL) { |
| 475 | fclose(fp); |
| 476 | } |
| 477 | if (result < 0) { |
| 478 | if (surface) { |
| 479 | free(surface); |
| 480 | } |
| 481 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 482 | return result; |
| 483 | } |
| 484 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 485 | void res_free_surface(gr_surface surface) { |
Ethan Yonker | a33161b | 2014-11-06 15:11:20 -0600 | [diff] [blame] | 486 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 487 | if (pSurface) { |
| 488 | free(pSurface); |
| 489 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 490 | } |