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