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 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <linux/fb.h> |
| 19 | #include <linux/kd.h> |
| 20 | #include <stdio.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/types.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 28 | #include <vector> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 29 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 30 | #include <png.h> |
| 31 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 32 | #include "minui/minui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 34 | #define SURFACE_DATA_ALIGNMENT 8 |
| 35 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 36 | static GRSurface* malloc_surface(size_t data_size) { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 37 | size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 38 | unsigned char* temp = static_cast<unsigned char*>(malloc(size)); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 39 | if (temp == NULL) return NULL; |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 40 | GRSurface* surface = reinterpret_cast<GRSurface*>(temp); |
Doug Zongker | 16f97c3 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 41 | surface->data = temp + sizeof(GRSurface) + |
| 42 | (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); |
| 43 | return surface; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 44 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 46 | static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, |
| 47 | 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] | 48 | char resPath[256]; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 49 | unsigned char header[8]; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 50 | int result = 0; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 51 | int color_type, bit_depth; |
| 52 | size_t bytesRead; |
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 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 62 | 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 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 94 | png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 95 | &color_type, NULL, NULL, NULL); |
| 96 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 97 | *channels = png_get_channels(*png_ptr, *info_ptr); |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 98 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 99 | if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { |
| 100 | // 8-bit RGB images: great, nothing to do. |
| 101 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { |
| 102 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 103 | png_set_expand_gray_1_2_4_to_8(*png_ptr); |
Doug Zongker | 577a130 | 2014-03-20 08:27:01 -0700 | [diff] [blame] | 104 | } 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] | 105 | // paletted images: expand to 8-bit RGB. Note that we DON'T |
| 106 | // currently expand the tRNS chunk (if any) to an alpha |
| 107 | // channel, because minui doesn't support alpha channels in |
| 108 | // general. |
| 109 | png_set_palette_to_rgb(*png_ptr); |
| 110 | *channels = 3; |
| 111 | } else { |
| 112 | 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] | 113 | bit_depth, *channels, color_type); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 114 | result = -7; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
| 117 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 118 | return result; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 119 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 120 | exit: |
| 121 | if (result < 0) { |
| 122 | png_destroy_read_struct(png_ptr, info_ptr, NULL); |
| 123 | } |
| 124 | if (fp != NULL) { |
| 125 | fclose(fp); |
| 126 | } |
| 127 | |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | // "display" surfaces are transformed into the framebuffer's required |
| 132 | // pixel format (currently only RGBX is supported) at load time, so |
| 133 | // gr_blit() can be nothing more than a memcpy() for each row. The |
| 134 | // next two functions are the only ones that know anything about the |
| 135 | // framebuffer pixel format; they need to be modified if the |
| 136 | // framebuffer format changes (but nothing else should). |
| 137 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 138 | // Allocate and return a GRSurface* sufficient for storing an image of |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 139 | // the indicated size in the framebuffer pixel format. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 140 | static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { |
| 141 | GRSurface* surface = malloc_surface(width * height * 4); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 142 | if (surface == NULL) return NULL; |
| 143 | |
| 144 | surface->width = width; |
| 145 | surface->height = height; |
| 146 | surface->row_bytes = width * 4; |
| 147 | surface->pixel_bytes = 4; |
| 148 | |
| 149 | return surface; |
| 150 | } |
| 151 | |
| 152 | // Copy 'input_row' to 'output_row', transforming it to the |
| 153 | // framebuffer pixel format. The input format depends on the value of |
| 154 | // 'channels': |
| 155 | // |
| 156 | // 1 - input is 8-bit grayscale |
| 157 | // 3 - input is 24-bit RGB |
| 158 | // 4 - input is 32-bit RGBA/RGBX |
| 159 | // |
| 160 | // 'width' is the number of pixels in the row. |
| 161 | static void transform_rgb_to_draw(unsigned char* input_row, |
| 162 | unsigned char* output_row, |
| 163 | int channels, int width) { |
| 164 | int x; |
| 165 | unsigned char* ip = input_row; |
| 166 | unsigned char* op = output_row; |
| 167 | |
| 168 | switch (channels) { |
| 169 | case 1: |
| 170 | // expand gray level to RGBX |
| 171 | for (x = 0; x < width; ++x) { |
| 172 | *op++ = *ip; |
| 173 | *op++ = *ip; |
| 174 | *op++ = *ip; |
| 175 | *op++ = 0xff; |
| 176 | ip++; |
| 177 | } |
| 178 | break; |
| 179 | |
| 180 | case 3: |
| 181 | // expand RGBA to RGBX |
| 182 | for (x = 0; x < width; ++x) { |
| 183 | *op++ = *ip++; |
| 184 | *op++ = *ip++; |
| 185 | *op++ = *ip++; |
| 186 | *op++ = 0xff; |
| 187 | } |
| 188 | break; |
| 189 | |
| 190 | case 4: |
| 191 | // copy RGBA to RGBX |
| 192 | memcpy(output_row, input_row, width*4); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 197 | int res_create_display_surface(const char* name, GRSurface** pSurface) { |
| 198 | GRSurface* surface = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 199 | int result = 0; |
| 200 | png_structp png_ptr = NULL; |
| 201 | png_infop info_ptr = NULL; |
| 202 | png_uint_32 width, height; |
| 203 | png_byte channels; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 204 | unsigned char* p_row; |
| 205 | unsigned int y; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 206 | |
| 207 | *pSurface = NULL; |
| 208 | |
| 209 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 210 | if (result < 0) return result; |
| 211 | |
| 212 | surface = init_display_surface(width, height); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 213 | if (surface == NULL) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 214 | result = -8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 215 | goto exit; |
| 216 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 217 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 218 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 219 | png_set_bgr(png_ptr); |
| 220 | #endif |
| 221 | |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 222 | p_row = static_cast<unsigned char*>(malloc(width * 4)); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 223 | for (y = 0; y < height; ++y) { |
| 224 | png_read_row(png_ptr, p_row, NULL); |
| 225 | 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] | 226 | } |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 227 | free(p_row); |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 228 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 229 | *pSurface = surface; |
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 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 232 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 233 | if (result < 0 && surface != NULL) free(surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 234 | return result; |
| 235 | } |
| 236 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 237 | int res_create_multi_display_surface(const char* name, int* frames, int* fps, |
| 238 | GRSurface*** pSurface) { |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 239 | GRSurface** surface = NULL; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 240 | int result = 0; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 241 | png_structp png_ptr = NULL; |
| 242 | png_infop info_ptr = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 243 | png_uint_32 width, height; |
| 244 | png_byte channels; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 245 | png_textp text; |
| 246 | int num_text; |
| 247 | unsigned char* p_row; |
| 248 | unsigned int y; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 249 | |
| 250 | *pSurface = NULL; |
| 251 | *frames = -1; |
| 252 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 253 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 254 | if (result < 0) return result; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 255 | |
| 256 | *frames = 1; |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 257 | *fps = 20; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 258 | if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 259 | for (int i = 0; i < num_text; ++i) { |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 260 | if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { |
| 261 | *frames = atoi(text[i].text); |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 262 | } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) { |
| 263 | *fps = atoi(text[i].text); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | printf(" found frames = %d\n", *frames); |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 267 | printf(" found fps = %d\n", *fps); |
| 268 | } |
| 269 | |
MinSeong Kim | 126cf8c | 2016-11-11 05:26:06 +0000 | [diff] [blame] | 270 | if (*frames <= 0 || *fps <= 0) { |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 271 | printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps); |
| 272 | result = -10; |
| 273 | goto exit; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (height % *frames != 0) { |
| 277 | printf("bad height (%d) for frame count (%d)\n", height, *frames); |
| 278 | result = -9; |
| 279 | goto exit; |
| 280 | } |
| 281 | |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 282 | surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*))); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 283 | if (surface == NULL) { |
| 284 | result = -8; |
| 285 | goto exit; |
| 286 | } |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 287 | for (int i = 0; i < *frames; ++i) { |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 288 | surface[i] = init_display_surface(width, height / *frames); |
| 289 | if (surface[i] == NULL) { |
| 290 | result = -8; |
| 291 | goto exit; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 295 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 296 | png_set_bgr(png_ptr); |
| 297 | #endif |
| 298 | |
Rahul Chaudhry | b29f23f | 2016-11-09 13:17:01 -0800 | [diff] [blame] | 299 | p_row = static_cast<unsigned char*>(malloc(width * 4)); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 300 | for (y = 0; y < height; ++y) { |
| 301 | png_read_row(png_ptr, p_row, NULL); |
| 302 | int frame = y % *frames; |
| 303 | unsigned char* out_row = surface[frame]->data + |
| 304 | (y / *frames) * surface[frame]->row_bytes; |
| 305 | transform_rgb_to_draw(p_row, out_row, channels, width); |
| 306 | } |
| 307 | free(p_row); |
| 308 | |
Rahul Chaudhry | 1cf93f5 | 2016-11-08 16:35:22 -0800 | [diff] [blame] | 309 | *pSurface = surface; |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 310 | |
| 311 | exit: |
| 312 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 313 | |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 314 | if (result < 0) { |
| 315 | if (surface) { |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 316 | for (int i = 0; i < *frames; ++i) { |
Elliott Hughes | 7d626df | 2016-02-19 10:33:01 -0800 | [diff] [blame] | 317 | free(surface[i]); |
Doug Zongker | 469954f | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 318 | } |
| 319 | free(surface); |
| 320 | } |
| 321 | } |
| 322 | return result; |
| 323 | } |
| 324 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 325 | int res_create_alpha_surface(const char* name, GRSurface** pSurface) { |
| 326 | GRSurface* surface = NULL; |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 327 | int result = 0; |
| 328 | png_structp png_ptr = NULL; |
| 329 | png_infop info_ptr = NULL; |
| 330 | png_uint_32 width, height; |
| 331 | png_byte channels; |
| 332 | |
| 333 | *pSurface = NULL; |
| 334 | |
| 335 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 336 | if (result < 0) return result; |
| 337 | |
| 338 | if (channels != 1) { |
| 339 | result = -7; |
| 340 | goto exit; |
| 341 | } |
| 342 | |
| 343 | surface = malloc_surface(width * height); |
| 344 | if (surface == NULL) { |
| 345 | result = -8; |
| 346 | goto exit; |
| 347 | } |
| 348 | surface->width = width; |
| 349 | surface->height = height; |
| 350 | surface->row_bytes = width; |
| 351 | surface->pixel_bytes = 1; |
| 352 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 353 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 354 | png_set_bgr(png_ptr); |
| 355 | #endif |
| 356 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 357 | unsigned char* p_row; |
| 358 | unsigned int y; |
| 359 | for (y = 0; y < height; ++y) { |
| 360 | p_row = surface->data + y * surface->row_bytes; |
| 361 | png_read_row(png_ptr, p_row, NULL); |
| 362 | } |
| 363 | |
| 364 | *pSurface = surface; |
| 365 | |
| 366 | exit: |
| 367 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 368 | if (result < 0 && surface != NULL) free(surface); |
| 369 | return result; |
| 370 | } |
| 371 | |
Tianjie Xu | 2430e29 | 2016-04-19 15:02:41 -0700 | [diff] [blame] | 372 | // This function tests if a locale string stored in PNG (prefix) matches |
| 373 | // the locale string provided by the system (locale). |
| 374 | bool matches_locale(const char* prefix, const char* locale) { |
Tianjie Xu | d17a688 | 2017-01-09 11:18:29 -0800 | [diff] [blame] | 375 | if (locale == nullptr) { |
| 376 | return false; |
| 377 | } |
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 | |
Rahul Chaudhry | 1cf93f5 | 2016-11-08 16:35:22 -0800 | [diff] [blame] | 439 | *pSurface = 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 | } |