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