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