blob: 83798856faf132e636dd239d1878cf86f97af20a [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Bao3d0cd1d2018-04-13 13:41:58 -070017#include "private/resources.h"
18
Tao Bao0b1118d2017-01-14 07:46:10 -080019#include <fcntl.h>
20#include <linux/fb.h>
21#include <linux/kd.h>
22#include <stdio.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080024#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <sys/types.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080028#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029
Tao Baodd789822018-11-26 16:28:07 -080030#include <limits>
Tianjie Xuacba38c2017-09-27 17:53:34 -070031#include <memory>
Tianjie Xu0a599562017-03-28 20:11:15 +000032#include <regex>
33#include <string>
Yabin Cui4425c1d2016-02-10 13:47:32 -080034#include <vector>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
bigbiff26d5d5f2020-03-23 09:56:16 -040036<<<<<<< HEAD
Ethan Yonker58f21322018-08-24 11:17:36 -050037//#include <android-base/stringprintf.h> // does not exist in 6.0
Ethan Yonker8373cfe2017-09-08 06:50:54 -050038//#include <android-base/strings.h> // does not exist in 6.0
bigbiff26d5d5f2020-03-23 09:56:16 -040039=======
Tianjie Xu2078b222017-03-22 12:27:26 -070040#include <android-base/strings.h>
bigbiff26d5d5f2020-03-23 09:56:16 -040041>>>>>>> android-10.0.0_r25
Doug Zongker19faefa2009-03-27 17:06:24 -070042#include <png.h>
43
Tao Bao0ecbd762017-01-16 21:16:58 -080044#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tao Bao6cd81682018-05-03 21:53:11 -070046static std::string g_resource_dir{ "/res/images" };
Doug Zongker16f97c32014-03-06 16:16:05 -080047
Tao Baodd789822018-11-26 16:28:07 -080048std::unique_ptr<GRSurface> GRSurface::Create(size_t width, size_t height, size_t row_bytes,
49 size_t pixel_bytes) {
50 if (width == 0 || row_bytes == 0 || height == 0 || pixel_bytes == 0) return nullptr;
51 if (std::numeric_limits<size_t>::max() / row_bytes < height) return nullptr;
52
Tao Bao44820ac2018-10-30 23:34:50 -070053 // Cannot use std::make_unique to access non-public ctor.
54 auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes));
Tao Baodd789822018-11-26 16:28:07 -080055 size_t data_size = row_bytes * height;
Tao Bao63b59dc2018-10-31 15:23:04 -070056 result->data_size_ =
Tao Bao92bdb5a2018-10-21 12:12:37 -070057 (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment;
Tao Bao9cf163e2018-11-07 10:15:50 -080058 result->data_.reset(
59 static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_)));
60 if (!result->data_) return nullptr;
Tao Bao92bdb5a2018-10-21 12:12:37 -070061 return result;
Doug Zongker19faefa2009-03-27 17:06:24 -070062}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063
Tao Bao63b59dc2018-10-31 15:23:04 -070064std::unique_ptr<GRSurface> GRSurface::Clone() const {
Tao Baodd789822018-11-26 16:28:07 -080065 auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes);
Tao Bao9cf163e2018-11-07 10:15:50 -080066 if (!result) return nullptr;
67 memcpy(result->data(), data(), data_size_);
Tao Bao63b59dc2018-10-31 15:23:04 -070068 return result;
69}
Doug Zongker6809c512011-03-01 14:04:34 -080070
bigbiff26d5d5f2020-03-23 09:56:16 -040071<<<<<<< HEAD
Tianjie Xuacba38c2017-09-27 17:53:34 -070072PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullptr, fclose) {
Ethan Yonker58f21322018-08-24 11:17:36 -050073 char res_path[PATH_MAX];
74 sprintf(res_path, "/res/images/%s.png", name.c_str());
75 //std::string res_path = sprintf("/res/images/%s.png", name.c_str());
76 png_fp_.reset(fopen(res_path, "rbe"));
bigbiff26d5d5f2020-03-23 09:56:16 -040077=======
Tao Bao3d0cd1d2018-04-13 13:41:58 -070078PngHandler::PngHandler(const std::string& name) {
Tao Bao6cd81682018-05-03 21:53:11 -070079 std::string res_path = g_resource_dir + "/" + name + ".png";
Tianjie Xuacba38c2017-09-27 17:53:34 -070080 png_fp_.reset(fopen(res_path.c_str(), "rbe"));
Tao Bao3d0cd1d2018-04-13 13:41:58 -070081 // Try to read from |name| if the resource path does not work.
82 if (!png_fp_) {
83 png_fp_.reset(fopen(name.c_str(), "rbe"));
84 }
bigbiff26d5d5f2020-03-23 09:56:16 -040085>>>>>>> android-10.0.0_r25
Tianjie Xuacba38c2017-09-27 17:53:34 -070086 if (!png_fp_) {
87 error_code_ = -1;
88 return;
89 }
Doug Zongker55a36ac2013-03-04 15:49:02 -080090
Tao Bao44820ac2018-10-30 23:34:50 -070091 uint8_t header[8];
Tianjie Xuacba38c2017-09-27 17:53:34 -070092 size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
93 if (bytesRead != sizeof(header)) {
94 error_code_ = -2;
95 return;
96 }
Doug Zongkera418aa72014-03-17 12:10:02 -070097
Tianjie Xuacba38c2017-09-27 17:53:34 -070098 if (png_sig_cmp(header, 0, sizeof(header))) {
99 error_code_ = -3;
100 return;
101 }
102
103 png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
104 if (!png_ptr_) {
105 error_code_ = -4;
106 return;
107 }
108
109 info_ptr_ = png_create_info_struct(png_ptr_);
110 if (!info_ptr_) {
111 error_code_ = -5;
112 return;
113 }
114
115 if (setjmp(png_jmpbuf(png_ptr_))) {
116 error_code_ = -6;
117 return;
118 }
119
120 png_init_io(png_ptr_, png_fp_.get());
121 png_set_sig_bytes(png_ptr_, sizeof(header));
122 png_read_info(png_ptr_, info_ptr_);
123
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700124 png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700125 nullptr);
126
127 channels_ = png_get_channels(png_ptr_, info_ptr_);
128
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700129 if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700130 // 8-bit RGB images: great, nothing to do.
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700131 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700132 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
133 png_set_expand_gray_1_2_4_to_8(png_ptr_);
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700134 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700135 // paletted images: expand to 8-bit RGB. Note that we DON'T
136 // currently expand the tRNS chunk (if any) to an alpha
137 // channel, because minui doesn't support alpha channels in
138 // general.
139 png_set_palette_to_rgb(png_ptr_);
140 channels_ = 3;
141 } else {
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700142 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_,
143 channels_, color_type_);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700144 error_code_ = -7;
145 }
146}
147
148PngHandler::~PngHandler() {
149 if (png_ptr_) {
150 png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr);
151 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700152}
153
Tao Bao44820ac2018-10-30 23:34:50 -0700154// "display" surfaces are transformed into the framebuffer's required pixel format (currently only
155// RGBX is supported) at load time, so gr_blit() can be nothing more than a memcpy() for each row.
Doug Zongkera418aa72014-03-17 12:10:02 -0700156
Tao Bao44820ac2018-10-30 23:34:50 -0700157// Copies 'input_row' to 'output_row', transforming it to the framebuffer pixel format. The input
158// format depends on the value of 'channels':
Doug Zongkera418aa72014-03-17 12:10:02 -0700159//
160// 1 - input is 8-bit grayscale
161// 3 - input is 24-bit RGB
162// 4 - input is 32-bit RGBA/RGBX
163//
164// 'width' is the number of pixels in the row.
Tao Bao44820ac2018-10-30 23:34:50 -0700165static void TransformRgbToDraw(const uint8_t* input_row, uint8_t* output_row, int channels,
166 int width) {
167 const uint8_t* ip = input_row;
168 uint8_t* op = output_row;
Doug Zongkera418aa72014-03-17 12:10:02 -0700169
Tao Bao44820ac2018-10-30 23:34:50 -0700170 switch (channels) {
171 case 1:
172 // expand gray level to RGBX
173 for (int x = 0; x < width; ++x) {
174 *op++ = *ip;
175 *op++ = *ip;
176 *op++ = *ip;
177 *op++ = 0xff;
178 ip++;
179 }
180 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700181
Tao Bao44820ac2018-10-30 23:34:50 -0700182 case 3:
183 // expand RGBA to RGBX
184 for (int x = 0; x < width; ++x) {
185 *op++ = *ip++;
186 *op++ = *ip++;
187 *op++ = *ip++;
188 *op++ = 0xff;
189 }
190 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700191
Tao Bao44820ac2018-10-30 23:34:50 -0700192 case 4:
193 // copy RGBA to RGBX
194 memcpy(output_row, input_row, width * 4);
195 break;
196 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700197}
198
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700199int res_create_display_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700200 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700201
Tianjie Xuacba38c2017-09-27 17:53:34 -0700202 PngHandler png_handler(name);
203 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700204
Tianjie Xuacba38c2017-09-27 17:53:34 -0700205 png_structp png_ptr = png_handler.png_ptr();
206 png_uint_32 width = png_handler.width();
207 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700208
Tao Baodd789822018-11-26 16:28:07 -0800209 auto surface = GRSurface::Create(width, height, width * 4, 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700210 if (!surface) {
211 return -8;
212 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213
Tao Baoed876a72018-07-31 21:32:50 -0700214 PixelFormat pixel_format = gr_pixel_format();
215 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
216 png_set_bgr(png_ptr);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700217 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700218
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800219 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700220 std::vector<uint8_t> p_row(width * 4);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221 png_read_row(png_ptr, p_row.data(), nullptr);
Tao Bao44820ac2018-10-30 23:34:50 -0700222 TransformRgbToDraw(p_row.data(), surface->data() + y * surface->row_bytes,
223 png_handler.channels(), width);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224 }
225
Tao Bao92bdb5a2018-10-21 12:12:37 -0700226 *pSurface = surface.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800227
Tianjie Xuacba38c2017-09-27 17:53:34 -0700228 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229}
230
Tao Baob723f4f2015-12-11 15:18:51 -0800231int res_create_multi_display_surface(const char* name, int* frames, int* fps,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700232 GRSurface*** pSurface) {
233 *pSurface = nullptr;
234 *frames = -1;
Doug Zongker469954f2014-03-07 09:21:25 -0800235
Tianjie Xuacba38c2017-09-27 17:53:34 -0700236 PngHandler png_handler(name);
237 if (!png_handler) return png_handler.error_code();
Doug Zongker469954f2014-03-07 09:21:25 -0800238
Tianjie Xuacba38c2017-09-27 17:53:34 -0700239 png_structp png_ptr = png_handler.png_ptr();
240 png_uint_32 width = png_handler.width();
241 png_uint_32 height = png_handler.height();
Doug Zongker469954f2014-03-07 09:21:25 -0800242
Tianjie Xuacba38c2017-09-27 17:53:34 -0700243 *frames = 1;
244 *fps = 20;
245 png_textp text;
246 int num_text;
247 if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) {
248 for (int i = 0; i < num_text; ++i) {
249 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
250 *frames = atoi(text[i].text);
251 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
252 *fps = atoi(text[i].text);
253 }
Tao Baob723f4f2015-12-11 15:18:51 -0800254 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700255 printf(" found frames = %d\n", *frames);
256 printf(" found fps = %d\n", *fps);
257 }
Tao Baob723f4f2015-12-11 15:18:51 -0800258
Tianjie Xuacba38c2017-09-27 17:53:34 -0700259 int result = 0;
260 GRSurface** surface = nullptr;
261 if (*frames <= 0 || *fps <= 0) {
262 printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
263 result = -10;
264 goto exit;
265 }
Doug Zongker469954f2014-03-07 09:21:25 -0800266
Tianjie Xuacba38c2017-09-27 17:53:34 -0700267 if (height % *frames != 0) {
268 printf("bad height (%d) for frame count (%d)\n", height, *frames);
269 result = -9;
270 goto exit;
271 }
Doug Zongker469954f2014-03-07 09:21:25 -0800272
Tianjie Xuacba38c2017-09-27 17:53:34 -0700273 surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
274 if (!surface) {
275 result = -8;
276 goto exit;
277 }
278 for (int i = 0; i < *frames; ++i) {
Tao Baodd789822018-11-26 16:28:07 -0800279 auto created_surface = GRSurface::Create(width, height / *frames, width * 4, 4);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700280 if (!created_surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700281 result = -8;
282 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800283 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700284 surface[i] = created_surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700285 }
Doug Zongker469954f2014-03-07 09:21:25 -0800286
Tao Baoed876a72018-07-31 21:32:50 -0700287 if (gr_pixel_format() == PixelFormat::ABGR || gr_pixel_format() == PixelFormat::BGRA) {
288 png_set_bgr(png_ptr);
289 }
Tony Kuofd778e32015-02-05 21:25:56 +0800290
Tianjie Xuacba38c2017-09-27 17:53:34 -0700291 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700292 std::vector<uint8_t> p_row(width * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700293 png_read_row(png_ptr, p_row.data(), nullptr);
294 int frame = y % *frames;
Tao Bao44820ac2018-10-30 23:34:50 -0700295 uint8_t* out_row = surface[frame]->data() + (y / *frames) * surface[frame]->row_bytes;
296 TransformRgbToDraw(p_row.data(), out_row, png_handler.channels(), width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700297 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700298
Tianjie Xuacba38c2017-09-27 17:53:34 -0700299 *pSurface = surface;
Doug Zongker469954f2014-03-07 09:21:25 -0800300
301exit:
Tianjie Xuacba38c2017-09-27 17:53:34 -0700302 if (result < 0) {
303 if (surface) {
304 for (int i = 0; i < *frames; ++i) {
305 free(surface[i]);
306 }
307 free(surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800308 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700309 }
310 return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800311}
312
Ethan Yonker534d4e02016-08-26 10:05:03 -0500313int res_create_multi_display_surface(const char* name, int* frames,
314 GRSurface*** pSurface) {
315 int fps = 0;
316 return res_create_multi_display_surface(name, frames, &fps, pSurface);
317}
318
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700319int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700320 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700321
Tianjie Xuacba38c2017-09-27 17:53:34 -0700322 PngHandler png_handler(name);
323 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700324
Tianjie Xuacba38c2017-09-27 17:53:34 -0700325 if (png_handler.channels() != 1) {
326 return -7;
327 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700328
Tianjie Xuacba38c2017-09-27 17:53:34 -0700329 png_structp png_ptr = png_handler.png_ptr();
330 png_uint_32 width = png_handler.width();
331 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700332
Tao Baodd789822018-11-26 16:28:07 -0800333 auto surface = GRSurface::Create(width, height, width, 1);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700334 if (!surface) {
335 return -8;
336 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700337
Tao Baoed876a72018-07-31 21:32:50 -0700338 PixelFormat pixel_format = gr_pixel_format();
339 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
340 png_set_bgr(png_ptr);
341 }
Tony Kuofd778e32015-02-05 21:25:56 +0800342
Tianjie Xuacba38c2017-09-27 17:53:34 -0700343 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700344 uint8_t* p_row = surface->data() + y * surface->row_bytes;
Tianjie Xuacba38c2017-09-27 17:53:34 -0700345 png_read_row(png_ptr, p_row, nullptr);
346 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700347
Tao Bao92bdb5a2018-10-21 12:12:37 -0700348 *pSurface = surface.release();
Doug Zongkera418aa72014-03-17 12:10:02 -0700349
Tianjie Xuacba38c2017-09-27 17:53:34 -0700350 return 0;
Doug Zongkera418aa72014-03-17 12:10:02 -0700351}
352
Tao Bao6cd81682018-05-03 21:53:11 -0700353void res_set_resource_dir(const std::string& dirname) {
354 g_resource_dir = dirname;
355}
356
Tianjie Xu2430e292016-04-19 15:02:41 -0700357// This function tests if a locale string stored in PNG (prefix) matches
358// the locale string provided by the system (locale).
Tianjie Xu0a599562017-03-28 20:11:15 +0000359bool matches_locale(const std::string& prefix, const std::string& locale) {
360 // According to the BCP 47 format, A locale string may consists of:
361 // language-{extlang}-{script}-{region}-{variant}
362 // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
363 // android's system locale can have the format language-{script}-{region}.
Doug Zongker02ec6b82012-08-22 17:26:40 -0700364
Tianjie Xu0a599562017-03-28 20:11:15 +0000365 // Return true if the whole string of prefix matches the top part of locale. Otherwise try to
366 // match the locale string without the {script} section.
367 // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
368 // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
Ethan Yonker58f21322018-08-24 11:17:36 -0500369 //if (android::base::StartsWith(locale, prefix)) { // does not exist in 6.0
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500370 if (strncmp(prefix.c_str(), locale.c_str(), prefix.length()) == 0) {
Tianjie Xu0a599562017-03-28 20:11:15 +0000371 return true;
372 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700373
Tianjie Xu0a599562017-03-28 20:11:15 +0000374 size_t separator = prefix.find('-');
375 if (separator == std::string::npos) {
376 return false;
377 }
378 std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator));
379 return std::regex_match(locale, loc_regex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700380}
381
Tianjie Xu29d55752017-09-20 17:53:46 -0700382std::vector<std::string> get_locales_in_png(const std::string& png_name) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700383 PngHandler png_handler(png_name);
384 if (!png_handler) {
385 printf("Failed to open %s, error: %d\n", png_name.c_str(), png_handler.error_code());
Tianjie Xu29d55752017-09-20 17:53:46 -0700386 return {};
387 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700388 if (png_handler.channels() != 1) {
389 printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels());
Tianjie Xu29d55752017-09-20 17:53:46 -0700390 return {};
391 }
392
393 std::vector<std::string> result;
Tao Bao44820ac2018-10-30 23:34:50 -0700394 std::vector<uint8_t> row(png_handler.width());
Tianjie Xuacba38c2017-09-27 17:53:34 -0700395 for (png_uint_32 y = 0; y < png_handler.height(); ++y) {
396 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700397 int h = (row[3] << 8) | row[2];
398 std::string loc(reinterpret_cast<char*>(&row[5]));
399 if (!loc.empty()) {
400 result.push_back(loc);
401 }
402 for (int i = 0; i < h; ++i, ++y) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700403 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700404 }
405 }
406
Tianjie Xu29d55752017-09-20 17:53:46 -0700407 return result;
408}
409
Doug Zongkera418aa72014-03-17 12:10:02 -0700410int res_create_localized_alpha_surface(const char* name,
411 const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700412 GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700413 *pSurface = nullptr;
414 if (locale == nullptr) {
415 return 0;
416 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417
Tianjie Xuacba38c2017-09-27 17:53:34 -0700418 PngHandler png_handler(name);
419 if (!png_handler) return png_handler.error_code();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700420
Tianjie Xuacba38c2017-09-27 17:53:34 -0700421 if (png_handler.channels() != 1) {
422 return -7;
423 }
424
425 png_structp png_ptr = png_handler.png_ptr();
426 png_uint_32 width = png_handler.width();
427 png_uint_32 height = png_handler.height();
428
429 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700430 std::vector<uint8_t> row(width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700431 png_read_row(png_ptr, row.data(), nullptr);
432 int w = (row[1] << 8) | row[0];
433 int h = (row[3] << 8) | row[2];
434 __unused int len = row[4];
435 char* loc = reinterpret_cast<char*>(&row[5]);
436
437 if (y + 1 + h >= height || matches_locale(loc, locale)) {
438 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
439
Tao Baodd789822018-11-26 16:28:07 -0800440 auto surface = GRSurface::Create(w, h, w, 1);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700441 if (!surface) {
442 return -8;
443 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700444
445 for (int i = 0; i < h; ++i, ++y) {
446 png_read_row(png_ptr, row.data(), nullptr);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700447 memcpy(surface->data() + i * w, row.data(), w);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700448 }
449
Tao Bao92bdb5a2018-10-21 12:12:37 -0700450 *pSurface = surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700451 break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700452 }
453
Tianjie Xuacba38c2017-09-27 17:53:34 -0700454 for (int i = 0; i < h; ++i, ++y) {
455 png_read_row(png_ptr, row.data(), nullptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700456 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700457 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700458
Tianjie Xuacba38c2017-09-27 17:53:34 -0700459 return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700460}
461
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700462void res_free_surface(GRSurface* surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700463 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464}