blob: fe47df15315b644c81867b20dae4be25390cd7be [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
Tianjie Xu2078b222017-03-22 12:27:26 -070036#include <android-base/strings.h>
Doug Zongker19faefa2009-03-27 17:06:24 -070037#include <png.h>
38
Tao Bao0ecbd762017-01-16 21:16:58 -080039#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Tao Bao6cd81682018-05-03 21:53:11 -070041static std::string g_resource_dir{ "/res/images" };
Doug Zongker16f97c32014-03-06 16:16:05 -080042
Tao Baodd789822018-11-26 16:28:07 -080043std::unique_ptr<GRSurface> GRSurface::Create(size_t width, size_t height, size_t row_bytes,
44 size_t pixel_bytes) {
45 if (width == 0 || row_bytes == 0 || height == 0 || pixel_bytes == 0) return nullptr;
46 if (std::numeric_limits<size_t>::max() / row_bytes < height) return nullptr;
47
Tao Bao44820ac2018-10-30 23:34:50 -070048 // Cannot use std::make_unique to access non-public ctor.
49 auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes));
Tao Baodd789822018-11-26 16:28:07 -080050 size_t data_size = row_bytes * height;
Tao Bao63b59dc2018-10-31 15:23:04 -070051 result->data_size_ =
Tao Bao92bdb5a2018-10-21 12:12:37 -070052 (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment;
Tao Bao9cf163e2018-11-07 10:15:50 -080053 result->data_.reset(
54 static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_)));
55 if (!result->data_) return nullptr;
Tao Bao92bdb5a2018-10-21 12:12:37 -070056 return result;
Doug Zongker19faefa2009-03-27 17:06:24 -070057}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
Tao Bao63b59dc2018-10-31 15:23:04 -070059std::unique_ptr<GRSurface> GRSurface::Clone() const {
Tao Baodd789822018-11-26 16:28:07 -080060 auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes);
Tao Bao9cf163e2018-11-07 10:15:50 -080061 if (!result) return nullptr;
62 memcpy(result->data(), data(), data_size_);
Tao Bao63b59dc2018-10-31 15:23:04 -070063 return result;
64}
Doug Zongker6809c512011-03-01 14:04:34 -080065
Tao Bao3d0cd1d2018-04-13 13:41:58 -070066PngHandler::PngHandler(const std::string& name) {
Tao Bao6cd81682018-05-03 21:53:11 -070067 std::string res_path = g_resource_dir + "/" + name + ".png";
Tianjie Xuacba38c2017-09-27 17:53:34 -070068 png_fp_.reset(fopen(res_path.c_str(), "rbe"));
Tao Bao3d0cd1d2018-04-13 13:41:58 -070069 // Try to read from |name| if the resource path does not work.
70 if (!png_fp_) {
71 png_fp_.reset(fopen(name.c_str(), "rbe"));
72 }
Tianjie Xuacba38c2017-09-27 17:53:34 -070073 if (!png_fp_) {
74 error_code_ = -1;
75 return;
76 }
Doug Zongker55a36ac2013-03-04 15:49:02 -080077
Tao Bao44820ac2018-10-30 23:34:50 -070078 uint8_t header[8];
Tianjie Xuacba38c2017-09-27 17:53:34 -070079 size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
80 if (bytesRead != sizeof(header)) {
81 error_code_ = -2;
82 return;
83 }
Doug Zongkera418aa72014-03-17 12:10:02 -070084
Tianjie Xuacba38c2017-09-27 17:53:34 -070085 if (png_sig_cmp(header, 0, sizeof(header))) {
86 error_code_ = -3;
87 return;
88 }
89
90 png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
91 if (!png_ptr_) {
92 error_code_ = -4;
93 return;
94 }
95
96 info_ptr_ = png_create_info_struct(png_ptr_);
97 if (!info_ptr_) {
98 error_code_ = -5;
99 return;
100 }
101
102 if (setjmp(png_jmpbuf(png_ptr_))) {
103 error_code_ = -6;
104 return;
105 }
106
107 png_init_io(png_ptr_, png_fp_.get());
108 png_set_sig_bytes(png_ptr_, sizeof(header));
109 png_read_info(png_ptr_, info_ptr_);
110
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700111 png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700112 nullptr);
113
114 channels_ = png_get_channels(png_ptr_, info_ptr_);
115
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700116 if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700117 // 8-bit RGB images: great, nothing to do.
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700118 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700119 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
120 png_set_expand_gray_1_2_4_to_8(png_ptr_);
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700121 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700122 // paletted images: expand to 8-bit RGB. Note that we DON'T
123 // currently expand the tRNS chunk (if any) to an alpha
124 // channel, because minui doesn't support alpha channels in
125 // general.
126 png_set_palette_to_rgb(png_ptr_);
127 channels_ = 3;
128 } else {
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700129 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_,
130 channels_, color_type_);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700131 error_code_ = -7;
132 }
133}
134
135PngHandler::~PngHandler() {
136 if (png_ptr_) {
137 png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr);
138 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700139}
140
Tao Bao44820ac2018-10-30 23:34:50 -0700141// "display" surfaces are transformed into the framebuffer's required pixel format (currently only
142// 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 -0700143
Tao Bao44820ac2018-10-30 23:34:50 -0700144// Copies 'input_row' to 'output_row', transforming it to the framebuffer pixel format. The input
145// format depends on the value of 'channels':
Doug Zongkera418aa72014-03-17 12:10:02 -0700146//
147// 1 - input is 8-bit grayscale
148// 3 - input is 24-bit RGB
149// 4 - input is 32-bit RGBA/RGBX
150//
151// 'width' is the number of pixels in the row.
Tao Bao44820ac2018-10-30 23:34:50 -0700152static void TransformRgbToDraw(const uint8_t* input_row, uint8_t* output_row, int channels,
153 int width) {
154 const uint8_t* ip = input_row;
155 uint8_t* op = output_row;
Doug Zongkera418aa72014-03-17 12:10:02 -0700156
Tao Bao44820ac2018-10-30 23:34:50 -0700157 switch (channels) {
158 case 1:
159 // expand gray level to RGBX
160 for (int x = 0; x < width; ++x) {
161 *op++ = *ip;
162 *op++ = *ip;
163 *op++ = *ip;
164 *op++ = 0xff;
165 ip++;
166 }
167 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700168
Tao Bao44820ac2018-10-30 23:34:50 -0700169 case 3:
170 // expand RGBA to RGBX
171 for (int x = 0; x < width; ++x) {
172 *op++ = *ip++;
173 *op++ = *ip++;
174 *op++ = *ip++;
175 *op++ = 0xff;
176 }
177 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700178
Tao Bao44820ac2018-10-30 23:34:50 -0700179 case 4:
180 // copy RGBA to RGBX
181 memcpy(output_row, input_row, width * 4);
182 break;
183 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700184}
185
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700186int res_create_display_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700187 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700188
Tianjie Xuacba38c2017-09-27 17:53:34 -0700189 PngHandler png_handler(name);
190 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700191
Tianjie Xuacba38c2017-09-27 17:53:34 -0700192 png_structp png_ptr = png_handler.png_ptr();
193 png_uint_32 width = png_handler.width();
194 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700195
Tao Baodd789822018-11-26 16:28:07 -0800196 auto surface = GRSurface::Create(width, height, width * 4, 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700197 if (!surface) {
198 return -8;
199 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200
Tao Baoed876a72018-07-31 21:32:50 -0700201 PixelFormat pixel_format = gr_pixel_format();
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800202 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700203 png_set_bgr(png_ptr);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700204 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700205
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800206 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700207 std::vector<uint8_t> p_row(width * 4);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 png_read_row(png_ptr, p_row.data(), nullptr);
Tao Bao44820ac2018-10-30 23:34:50 -0700209 TransformRgbToDraw(p_row.data(), surface->data() + y * surface->row_bytes,
210 png_handler.channels(), width);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211 }
212
Tao Bao92bdb5a2018-10-21 12:12:37 -0700213 *pSurface = surface.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214
Tianjie Xuacba38c2017-09-27 17:53:34 -0700215 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216}
217
Tao Baob723f4f2015-12-11 15:18:51 -0800218int res_create_multi_display_surface(const char* name, int* frames, int* fps,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700219 GRSurface*** pSurface) {
220 *pSurface = nullptr;
221 *frames = -1;
Doug Zongker469954f2014-03-07 09:21:25 -0800222
Tianjie Xuacba38c2017-09-27 17:53:34 -0700223 PngHandler png_handler(name);
224 if (!png_handler) return png_handler.error_code();
Doug Zongker469954f2014-03-07 09:21:25 -0800225
Tianjie Xuacba38c2017-09-27 17:53:34 -0700226 png_structp png_ptr = png_handler.png_ptr();
227 png_uint_32 width = png_handler.width();
228 png_uint_32 height = png_handler.height();
Doug Zongker469954f2014-03-07 09:21:25 -0800229
Tianjie Xuacba38c2017-09-27 17:53:34 -0700230 *frames = 1;
231 *fps = 20;
232 png_textp text;
233 int num_text;
234 if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) {
235 for (int i = 0; i < num_text; ++i) {
236 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
237 *frames = atoi(text[i].text);
238 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
239 *fps = atoi(text[i].text);
240 }
Tao Baob723f4f2015-12-11 15:18:51 -0800241 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700242 printf(" found frames = %d\n", *frames);
243 printf(" found fps = %d\n", *fps);
244 }
Tao Baob723f4f2015-12-11 15:18:51 -0800245
Tianjie Xuacba38c2017-09-27 17:53:34 -0700246 int result = 0;
247 GRSurface** surface = nullptr;
248 if (*frames <= 0 || *fps <= 0) {
249 printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
250 result = -10;
251 goto exit;
252 }
Doug Zongker469954f2014-03-07 09:21:25 -0800253
Tianjie Xuacba38c2017-09-27 17:53:34 -0700254 if (height % *frames != 0) {
255 printf("bad height (%d) for frame count (%d)\n", height, *frames);
256 result = -9;
257 goto exit;
258 }
Doug Zongker469954f2014-03-07 09:21:25 -0800259
Tianjie Xuacba38c2017-09-27 17:53:34 -0700260 surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
261 if (!surface) {
262 result = -8;
263 goto exit;
264 }
265 for (int i = 0; i < *frames; ++i) {
Tao Baodd789822018-11-26 16:28:07 -0800266 auto created_surface = GRSurface::Create(width, height / *frames, width * 4, 4);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700267 if (!created_surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700268 result = -8;
269 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800270 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700271 surface[i] = created_surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700272 }
Doug Zongker469954f2014-03-07 09:21:25 -0800273
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800274 if (gr_pixel_format() == PixelFormat::ARGB || gr_pixel_format() == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700275 png_set_bgr(png_ptr);
276 }
Tony Kuofd778e32015-02-05 21:25:56 +0800277
Tianjie Xuacba38c2017-09-27 17:53:34 -0700278 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700279 std::vector<uint8_t> p_row(width * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700280 png_read_row(png_ptr, p_row.data(), nullptr);
281 int frame = y % *frames;
Tao Bao44820ac2018-10-30 23:34:50 -0700282 uint8_t* out_row = surface[frame]->data() + (y / *frames) * surface[frame]->row_bytes;
283 TransformRgbToDraw(p_row.data(), out_row, png_handler.channels(), width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700284 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700285
Tianjie Xuacba38c2017-09-27 17:53:34 -0700286 *pSurface = surface;
Doug Zongker469954f2014-03-07 09:21:25 -0800287
288exit:
Tianjie Xuacba38c2017-09-27 17:53:34 -0700289 if (result < 0) {
290 if (surface) {
291 for (int i = 0; i < *frames; ++i) {
292 free(surface[i]);
293 }
294 free(surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800295 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700296 }
297 return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800298}
299
Ethan Yonker534d4e02016-08-26 10:05:03 -0500300int res_create_multi_display_surface(const char* name, int* frames,
301 GRSurface*** pSurface) {
302 int fps = 0;
303 return res_create_multi_display_surface(name, frames, &fps, pSurface);
304}
305
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700306int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700307 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700308
Tianjie Xuacba38c2017-09-27 17:53:34 -0700309 PngHandler png_handler(name);
310 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700311
Tianjie Xuacba38c2017-09-27 17:53:34 -0700312 if (png_handler.channels() != 1) {
313 return -7;
314 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700315
Tianjie Xuacba38c2017-09-27 17:53:34 -0700316 png_structp png_ptr = png_handler.png_ptr();
317 png_uint_32 width = png_handler.width();
318 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700319
Tao Baodd789822018-11-26 16:28:07 -0800320 auto surface = GRSurface::Create(width, height, width, 1);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700321 if (!surface) {
322 return -8;
323 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700324
Tao Baoed876a72018-07-31 21:32:50 -0700325 PixelFormat pixel_format = gr_pixel_format();
Adrian Salidocc7b7eb2019-12-12 20:18:09 -0800326 if (pixel_format == PixelFormat::ARGB || pixel_format == PixelFormat::BGRA) {
Tao Baoed876a72018-07-31 21:32:50 -0700327 png_set_bgr(png_ptr);
328 }
Tony Kuofd778e32015-02-05 21:25:56 +0800329
Tianjie Xuacba38c2017-09-27 17:53:34 -0700330 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700331 uint8_t* p_row = surface->data() + y * surface->row_bytes;
Tianjie Xuacba38c2017-09-27 17:53:34 -0700332 png_read_row(png_ptr, p_row, nullptr);
333 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700334
Tao Bao92bdb5a2018-10-21 12:12:37 -0700335 *pSurface = surface.release();
Doug Zongkera418aa72014-03-17 12:10:02 -0700336
Tianjie Xuacba38c2017-09-27 17:53:34 -0700337 return 0;
Doug Zongkera418aa72014-03-17 12:10:02 -0700338}
339
Tao Bao6cd81682018-05-03 21:53:11 -0700340void res_set_resource_dir(const std::string& dirname) {
341 g_resource_dir = dirname;
342}
343
Tianjie Xu2430e292016-04-19 15:02:41 -0700344// This function tests if a locale string stored in PNG (prefix) matches
345// the locale string provided by the system (locale).
Tianjie Xu0a599562017-03-28 20:11:15 +0000346bool matches_locale(const std::string& prefix, const std::string& locale) {
347 // According to the BCP 47 format, A locale string may consists of:
348 // language-{extlang}-{script}-{region}-{variant}
349 // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
350 // android's system locale can have the format language-{script}-{region}.
Doug Zongker02ec6b82012-08-22 17:26:40 -0700351
Tianjie Xu0a599562017-03-28 20:11:15 +0000352 // Return true if the whole string of prefix matches the top part of locale. Otherwise try to
353 // match the locale string without the {script} section.
354 // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
355 // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
xunchang34723082019-04-22 15:32:17 -0700356 if (prefix.empty()) {
357 return false;
358 }
359
Elliott Hughes1d65c952017-12-20 12:36:31 -0800360 if (android::base::StartsWith(locale, prefix)) {
Tianjie Xu0a599562017-03-28 20:11:15 +0000361 return true;
362 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700363
Tianjie Xu0a599562017-03-28 20:11:15 +0000364 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 Zongker02ec6b82012-08-22 17:26:40 -0700370}
371
Tianjie Xu29d55752017-09-20 17:53:46 -0700372std::vector<std::string> get_locales_in_png(const std::string& png_name) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700373 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 Xu29d55752017-09-20 17:53:46 -0700376 return {};
377 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700378 if (png_handler.channels() != 1) {
379 printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels());
Tianjie Xu29d55752017-09-20 17:53:46 -0700380 return {};
381 }
382
383 std::vector<std::string> result;
Tao Bao44820ac2018-10-30 23:34:50 -0700384 std::vector<uint8_t> row(png_handler.width());
Tianjie Xuacba38c2017-09-27 17:53:34 -0700385 for (png_uint_32 y = 0; y < png_handler.height(); ++y) {
386 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700387 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 Xuacba38c2017-09-27 17:53:34 -0700393 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700394 }
395 }
396
Tianjie Xu29d55752017-09-20 17:53:46 -0700397 return result;
398}
399
Doug Zongkera418aa72014-03-17 12:10:02 -0700400int res_create_localized_alpha_surface(const char* name,
401 const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700402 GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700403 *pSurface = nullptr;
404 if (locale == nullptr) {
405 return 0;
406 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700407
Tianjie Xuacba38c2017-09-27 17:53:34 -0700408 PngHandler png_handler(name);
409 if (!png_handler) return png_handler.error_code();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700410
Tianjie Xuacba38c2017-09-27 17:53:34 -0700411 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) {
Tao Bao44820ac2018-10-30 23:34:50 -0700420 std::vector<uint8_t> row(width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700421 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
xunchang9d05c8a2019-04-16 12:07:42 -0700427 // We need to include one additional line for the metadata of the localized image.
428 if (y + 1 + h > height) {
429 printf("Read exceeds the image boundary, y %u, h %d, height %u\n", y, h, height);
430 return -8;
431 }
432
433 if (matches_locale(loc, locale)) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700434 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
435
Tao Baodd789822018-11-26 16:28:07 -0800436 auto surface = GRSurface::Create(w, h, w, 1);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700437 if (!surface) {
xunchang9d05c8a2019-04-16 12:07:42 -0700438 return -9;
Tianjie Xuacba38c2017-09-27 17:53:34 -0700439 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700440
441 for (int i = 0; i < h; ++i, ++y) {
442 png_read_row(png_ptr, row.data(), nullptr);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700443 memcpy(surface->data() + i * w, row.data(), w);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700444 }
445
Tao Bao92bdb5a2018-10-21 12:12:37 -0700446 *pSurface = surface.release();
xunchang9d05c8a2019-04-16 12:07:42 -0700447 return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700448 }
449
Tianjie Xuacba38c2017-09-27 17:53:34 -0700450 for (int i = 0; i < h; ++i, ++y) {
451 png_read_row(png_ptr, row.data(), nullptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700452 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700453 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700454
xunchang9d05c8a2019-04-16 12:07:42 -0700455 return -10;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700456}
457
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700458void res_free_surface(GRSurface* surface) {
lijiazi7d306fb2020-12-23 19:21:06 +0800459 delete(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800460}