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 | |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 31 | #include <vector> |
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 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 36 | extern char* locale; |
| 37 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 38 | #define SURFACE_DATA_ALIGNMENT 8 |
| 39 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 40 | static GRSurface* malloc_surface(size_t data_size) { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 41 | size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; |
| 42 | unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size)); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 43 | if (temp == NULL) return NULL; |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 44 | GRSurface* surface = reinterpret_cast<GRSurface*>(temp); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 45 | surface->data = temp + sizeof(GRSurface) + |
| 46 | (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); |
| 47 | return surface; |
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 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 50 | static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, |
| 51 | 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] | 52 | char resPath[256]; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 53 | unsigned char header[8]; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 54 | int result = 0; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 55 | int color_type, bit_depth; |
| 56 | size_t bytesRead; |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 57 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 58 | 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] | 59 | resPath[sizeof(resPath)-1] = '\0'; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 60 | FILE* fp = fopen(resPath, "rb"); |
| 61 | if (fp == NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 62 | result = -1; |
| 63 | goto exit; |
| 64 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 66 | bytesRead = fread(header, 1, sizeof(header), fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 67 | if (bytesRead != sizeof(header)) { |
| 68 | result = -2; |
| 69 | goto exit; |
| 70 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 71 | |
| 72 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 73 | result = -3; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 74 | goto exit; |
| 75 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 76 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 77 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 78 | if (!*png_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 79 | result = -4; |
| 80 | goto exit; |
| 81 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 82 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 83 | *info_ptr = png_create_info_struct(*png_ptr); |
| 84 | if (!*info_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 85 | result = -5; |
| 86 | goto exit; |
| 87 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 88 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 89 | if (setjmp(png_jmpbuf(*png_ptr))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | result = -6; |
| 91 | goto exit; |
| 92 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 93 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 94 | png_init_io(*png_ptr, fp); |
| 95 | png_set_sig_bytes(*png_ptr, sizeof(header)); |
| 96 | png_read_info(*png_ptr, *info_ptr); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 97 | |
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 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 142 | // Allocate and return a GRSurface* sufficient for storing an image of |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 143 | // the indicated size in the framebuffer pixel format. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 144 | static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { |
| 145 | GRSurface* surface = malloc_surface(width * height * 4); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 146 | if (surface == NULL) return NULL; |
| 147 | |
| 148 | surface->width = width; |
| 149 | surface->height = height; |
| 150 | surface->row_bytes = width * 4; |
| 151 | surface->pixel_bytes = 4; |
| 152 | |
| 153 | return surface; |
| 154 | } |
| 155 | |
| 156 | // Copy 'input_row' to 'output_row', transforming it to the |
| 157 | // framebuffer pixel format. The input format depends on the value of |
| 158 | // 'channels': |
| 159 | // |
| 160 | // 1 - input is 8-bit grayscale |
| 161 | // 3 - input is 24-bit RGB |
| 162 | // 4 - input is 32-bit RGBA/RGBX |
| 163 | // |
| 164 | // 'width' is the number of pixels in the row. |
| 165 | static void transform_rgb_to_draw(unsigned char* input_row, |
| 166 | unsigned char* output_row, |
| 167 | int channels, int width) { |
| 168 | int x; |
| 169 | unsigned char* ip = input_row; |
| 170 | unsigned char* op = output_row; |
| 171 | |
| 172 | switch (channels) { |
| 173 | case 1: |
| 174 | // expand gray level to RGBX |
| 175 | for (x = 0; x < width; ++x) { |
| 176 | *op++ = *ip; |
| 177 | *op++ = *ip; |
| 178 | *op++ = *ip; |
| 179 | *op++ = 0xff; |
| 180 | ip++; |
| 181 | } |
| 182 | break; |
| 183 | |
| 184 | case 3: |
| 185 | // expand RGBA to RGBX |
| 186 | for (x = 0; x < width; ++x) { |
| 187 | *op++ = *ip++; |
| 188 | *op++ = *ip++; |
| 189 | *op++ = *ip++; |
| 190 | *op++ = 0xff; |
| 191 | } |
| 192 | break; |
| 193 | |
| 194 | case 4: |
| 195 | // copy RGBA to RGBX |
| 196 | memcpy(output_row, input_row, width*4); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 201 | int res_create_display_surface(const char* name, GRSurface** pSurface) { |
| 202 | GRSurface* surface = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 203 | int result = 0; |
| 204 | png_structp png_ptr = NULL; |
| 205 | png_infop info_ptr = NULL; |
| 206 | png_uint_32 width, height; |
| 207 | png_byte channels; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 208 | unsigned char* p_row; |
| 209 | unsigned int y; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 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 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 222 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 223 | png_set_bgr(png_ptr); |
| 224 | #endif |
| 225 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 226 | p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 227 | for (y = 0; y < height; ++y) { |
| 228 | png_read_row(png_ptr, p_row, NULL); |
| 229 | 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] | 230 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 231 | free(p_row); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 232 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 233 | *pSurface = surface; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 234 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 235 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 236 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 237 | if (result < 0 && surface != NULL) free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 238 | return result; |
| 239 | } |
| 240 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 241 | int res_create_multi_display_surface(const char* name, int* frames, int* fps, |
| 242 | GRSurface*** pSurface) { |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 243 | GRSurface** surface = NULL; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 244 | int result = 0; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 245 | png_structp png_ptr = NULL; |
| 246 | png_infop info_ptr = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 247 | png_uint_32 width, height; |
| 248 | png_byte channels; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 249 | png_textp text; |
| 250 | int num_text; |
| 251 | unsigned char* p_row; |
| 252 | unsigned int y; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 253 | |
| 254 | *pSurface = NULL; |
| 255 | *frames = -1; |
| 256 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 257 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 258 | if (result < 0) return result; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 259 | |
| 260 | *frames = 1; |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 261 | *fps = 20; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 262 | if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 263 | for (int i = 0; i < num_text; ++i) { |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 264 | if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { |
| 265 | *frames = atoi(text[i].text); |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 266 | } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) { |
| 267 | *fps = atoi(text[i].text); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | printf(" found frames = %d\n", *frames); |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 271 | printf(" found fps = %d\n", *fps); |
| 272 | } |
| 273 | |
| 274 | if (frames <= 0 || fps <= 0) { |
| 275 | printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps); |
| 276 | result = -10; |
| 277 | goto exit; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (height % *frames != 0) { |
| 281 | printf("bad height (%d) for frame count (%d)\n", height, *frames); |
| 282 | result = -9; |
| 283 | goto exit; |
| 284 | } |
| 285 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 286 | surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 287 | if (surface == NULL) { |
| 288 | result = -8; |
| 289 | goto exit; |
| 290 | } |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 291 | for (int i = 0; i < *frames; ++i) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 292 | surface[i] = init_display_surface(width, height / *frames); |
| 293 | if (surface[i] == NULL) { |
| 294 | result = -8; |
| 295 | goto exit; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 299 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 300 | png_set_bgr(png_ptr); |
| 301 | #endif |
| 302 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 303 | p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 304 | for (y = 0; y < height; ++y) { |
| 305 | png_read_row(png_ptr, p_row, NULL); |
| 306 | int frame = y % *frames; |
| 307 | unsigned char* out_row = surface[frame]->data + |
| 308 | (y / *frames) * surface[frame]->row_bytes; |
| 309 | transform_rgb_to_draw(p_row, out_row, channels, width); |
| 310 | } |
| 311 | free(p_row); |
| 312 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 313 | *pSurface = reinterpret_cast<GRSurface**>(surface); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 314 | |
| 315 | exit: |
| 316 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 317 | |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 318 | if (result < 0) { |
| 319 | if (surface) { |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 320 | for (int i = 0; i < *frames; ++i) { |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 321 | if (surface[i]) free(surface[i]); |
| 322 | } |
| 323 | free(surface); |
| 324 | } |
| 325 | } |
| 326 | return result; |
| 327 | } |
| 328 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 329 | int res_create_alpha_surface(const char* name, GRSurface** pSurface) { |
| 330 | GRSurface* surface = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 331 | int result = 0; |
| 332 | png_structp png_ptr = NULL; |
| 333 | png_infop info_ptr = NULL; |
| 334 | png_uint_32 width, height; |
| 335 | png_byte channels; |
| 336 | |
| 337 | *pSurface = NULL; |
| 338 | |
| 339 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 340 | if (result < 0) return result; |
| 341 | |
| 342 | if (channels != 1) { |
| 343 | result = -7; |
| 344 | goto exit; |
| 345 | } |
| 346 | |
| 347 | surface = malloc_surface(width * height); |
| 348 | if (surface == NULL) { |
| 349 | result = -8; |
| 350 | goto exit; |
| 351 | } |
| 352 | surface->width = width; |
| 353 | surface->height = height; |
| 354 | surface->row_bytes = width; |
| 355 | surface->pixel_bytes = 1; |
| 356 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 357 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 358 | png_set_bgr(png_ptr); |
| 359 | #endif |
| 360 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 361 | unsigned char* p_row; |
| 362 | unsigned int y; |
| 363 | for (y = 0; y < height; ++y) { |
| 364 | p_row = surface->data + y * surface->row_bytes; |
| 365 | png_read_row(png_ptr, p_row, NULL); |
| 366 | } |
| 367 | |
| 368 | *pSurface = surface; |
| 369 | |
| 370 | exit: |
| 371 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 372 | if (result < 0 && surface != NULL) free(surface); |
| 373 | return result; |
| 374 | } |
| 375 | |
| 376 | static int matches_locale(const char* loc, const char* locale) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 377 | if (locale == NULL) return 0; |
| 378 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 379 | if (strcmp(loc, locale) == 0) return 1; |
| 380 | |
| 381 | // if loc does *not* have an underscore, and it matches the start |
| 382 | // of locale, and the next character in locale *is* an underscore, |
| 383 | // that's a match. For instance, loc == "en" matches locale == |
| 384 | // "en_US". |
| 385 | |
| 386 | int i; |
| 387 | for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); |
| 388 | if (loc[i] == '_') return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 389 | |
| 390 | return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); |
| 391 | } |
| 392 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 393 | int res_create_localized_alpha_surface(const char* name, |
| 394 | const char* locale, |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 395 | GRSurface** pSurface) { |
| 396 | GRSurface* surface = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 397 | int result = 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 398 | png_structp png_ptr = NULL; |
| 399 | png_infop info_ptr = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 400 | png_uint_32 width, height; |
| 401 | png_byte channels; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 402 | png_uint_32 y; |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 403 | std::vector<unsigned char> row; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 404 | |
| 405 | *pSurface = NULL; |
| 406 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 407 | if (locale == NULL) { |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 408 | return result; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 411 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 412 | if (result < 0) return result; |
| 413 | |
| 414 | if (channels != 1) { |
| 415 | result = -7; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 416 | goto exit; |
| 417 | } |
| 418 | |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 419 | row.resize(width); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 420 | for (y = 0; y < height; ++y) { |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 421 | png_read_row(png_ptr, row.data(), NULL); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 422 | int w = (row[1] << 8) | row[0]; |
| 423 | int h = (row[3] << 8) | row[2]; |
| 424 | int len = row[4]; |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 425 | char* loc = reinterpret_cast<char*>(&row[5]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 426 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 427 | if (y+1+h >= height || matches_locale(loc, locale)) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 428 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 429 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 430 | surface = malloc_surface(w*h); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 431 | if (surface == NULL) { |
| 432 | result = -8; |
| 433 | goto exit; |
| 434 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 435 | surface->width = w; |
| 436 | surface->height = h; |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 437 | surface->row_bytes = w; |
| 438 | surface->pixel_bytes = 1; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 439 | |
| 440 | int i; |
| 441 | for (i = 0; i < h; ++i, ++y) { |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 442 | png_read_row(png_ptr, row.data(), NULL); |
| 443 | memcpy(surface->data + i*w, row.data(), w); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 446 | *pSurface = reinterpret_cast<GRSurface*>(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 447 | break; |
| 448 | } else { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 449 | int i; |
| 450 | for (i = 0; i < h; ++i, ++y) { |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 451 | png_read_row(png_ptr, row.data(), NULL); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | exit: |
| 457 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 458 | if (result < 0 && surface != NULL) free(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 459 | return result; |
| 460 | } |
| 461 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 462 | void res_free_surface(GRSurface* surface) { |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 463 | free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 464 | } |