blob: fd5789374459a7cb24691890111c8b23eb8603c5 [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 Bao0b1118d2017-01-14 07:46:10 -080017#include <fcntl.h>
18#include <linux/fb.h>
19#include <linux/kd.h>
20#include <stdio.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <sys/ioctl.h>
24#include <sys/mman.h>
25#include <sys/types.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080026#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027
Tianjie Xuacba38c2017-09-27 17:53:34 -070028#include <memory>
Tianjie Xu0a599562017-03-28 20:11:15 +000029#include <regex>
30#include <string>
Yabin Cui4425c1d2016-02-10 13:47:32 -080031#include <vector>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Ethan Yonker58f21322018-08-24 11:17:36 -050033//#include <android-base/stringprintf.h> // does not exist in 6.0
Ethan Yonker8373cfe2017-09-08 06:50:54 -050034//#include <android-base/strings.h> // does not exist in 6.0
Doug Zongker19faefa2009-03-27 17:06:24 -070035#include <png.h>
36
Tao Bao0ecbd762017-01-16 21:16:58 -080037#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Doug Zongker16f97c32014-03-06 16:16:05 -080039#define SURFACE_DATA_ALIGNMENT 8
40
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070041static GRSurface* malloc_surface(size_t data_size) {
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070042 size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -080043 unsigned char* temp = static_cast<unsigned char*>(malloc(size));
Doug Zongker16f97c32014-03-06 16:16:05 -080044 if (temp == NULL) return NULL;
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070045 GRSurface* surface = reinterpret_cast<GRSurface*>(temp);
Doug Zongker16f97c32014-03-06 16:16:05 -080046 surface->data = temp + sizeof(GRSurface) +
47 (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
48 return surface;
Doug Zongker19faefa2009-03-27 17:06:24 -070049}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Tianjie Xuacba38c2017-09-27 17:53:34 -070051// This class handles the png file parsing. It also holds the ownership of the png pointer and the
52// opened file pointer. Both will be destroyed/closed when this object goes out of scope.
53class PngHandler {
54 public:
55 PngHandler(const std::string& name);
Doug Zongker6809c512011-03-01 14:04:34 -080056
Tianjie Xuacba38c2017-09-27 17:53:34 -070057 ~PngHandler();
Doug Zongker19faefa2009-03-27 17:06:24 -070058
Tianjie Xuacba38c2017-09-27 17:53:34 -070059 png_uint_32 width() const {
60 return width_;
61 }
Doug Zongker19faefa2009-03-27 17:06:24 -070062
Tianjie Xuacba38c2017-09-27 17:53:34 -070063 png_uint_32 height() const {
64 return height_;
65 }
Doug Zongker19faefa2009-03-27 17:06:24 -070066
Tianjie Xuacba38c2017-09-27 17:53:34 -070067 png_byte channels() const {
68 return channels_;
69 }
Doug Zongker19faefa2009-03-27 17:06:24 -070070
Tianjie Xuacba38c2017-09-27 17:53:34 -070071 png_structp png_ptr() const {
72 return png_ptr_;
73 }
Doug Zongker19faefa2009-03-27 17:06:24 -070074
Tianjie Xuacba38c2017-09-27 17:53:34 -070075 png_infop info_ptr() const {
76 return info_ptr_;
77 }
Doug Zongker19faefa2009-03-27 17:06:24 -070078
Tianjie Xuacba38c2017-09-27 17:53:34 -070079 int error_code() const {
80 return error_code_;
81 };
Doug Zongker19faefa2009-03-27 17:06:24 -070082
Tianjie Xuacba38c2017-09-27 17:53:34 -070083 operator bool() const {
84 return error_code_ == 0;
85 }
John Reck94fd07b2013-08-26 16:45:33 -070086
Tianjie Xuacba38c2017-09-27 17:53:34 -070087 private:
88 png_structp png_ptr_{ nullptr };
89 png_infop info_ptr_{ nullptr };
90 png_uint_32 width_;
91 png_uint_32 height_;
92 png_byte channels_;
John Reck94fd07b2013-08-26 16:45:33 -070093
Tianjie Xuacba38c2017-09-27 17:53:34 -070094 // The |error_code_| is set to a negative value if an error occurs when opening the png file.
95 int error_code_;
96 // After initialization, we'll keep the file pointer open before destruction of PngHandler.
97 std::unique_ptr<FILE, decltype(&fclose)> png_fp_;
98};
Doug Zongker19faefa2009-03-27 17:06:24 -070099
Tianjie Xuacba38c2017-09-27 17:53:34 -0700100PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullptr, fclose) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500101 char res_path[PATH_MAX];
102 sprintf(res_path, "/res/images/%s.png", name.c_str());
103 //std::string res_path = sprintf("/res/images/%s.png", name.c_str());
104 png_fp_.reset(fopen(res_path, "rbe"));
Tianjie Xuacba38c2017-09-27 17:53:34 -0700105 if (!png_fp_) {
106 error_code_ = -1;
107 return;
108 }
Doug Zongker55a36ac2013-03-04 15:49:02 -0800109
Tianjie Xuacba38c2017-09-27 17:53:34 -0700110 unsigned char header[8];
111 size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
112 if (bytesRead != sizeof(header)) {
113 error_code_ = -2;
114 return;
115 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700116
Tianjie Xuacba38c2017-09-27 17:53:34 -0700117 if (png_sig_cmp(header, 0, sizeof(header))) {
118 error_code_ = -3;
119 return;
120 }
121
122 png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
123 if (!png_ptr_) {
124 error_code_ = -4;
125 return;
126 }
127
128 info_ptr_ = png_create_info_struct(png_ptr_);
129 if (!info_ptr_) {
130 error_code_ = -5;
131 return;
132 }
133
134 if (setjmp(png_jmpbuf(png_ptr_))) {
135 error_code_ = -6;
136 return;
137 }
138
139 png_init_io(png_ptr_, png_fp_.get());
140 png_set_sig_bytes(png_ptr_, sizeof(header));
141 png_read_info(png_ptr_, info_ptr_);
142
143 int color_type;
144 int bit_depth;
145 png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth, &color_type, nullptr, nullptr,
146 nullptr);
147
148 channels_ = png_get_channels(png_ptr_, info_ptr_);
149
150 if (bit_depth == 8 && channels_ == 3 && color_type == PNG_COLOR_TYPE_RGB) {
151 // 8-bit RGB images: great, nothing to do.
152 } else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
153 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
154 png_set_expand_gray_1_2_4_to_8(png_ptr_);
155 } else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
156 // paletted images: expand to 8-bit RGB. Note that we DON'T
157 // currently expand the tRNS chunk (if any) to an alpha
158 // channel, because minui doesn't support alpha channels in
159 // general.
160 png_set_palette_to_rgb(png_ptr_);
161 channels_ = 3;
162 } else {
163 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth,
164 channels_, color_type);
165 error_code_ = -7;
166 }
167}
168
169PngHandler::~PngHandler() {
170 if (png_ptr_) {
171 png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr);
172 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700173}
174
175// "display" surfaces are transformed into the framebuffer's required
176// pixel format (currently only RGBX is supported) at load time, so
177// gr_blit() can be nothing more than a memcpy() for each row. The
178// next two functions are the only ones that know anything about the
179// framebuffer pixel format; they need to be modified if the
180// framebuffer format changes (but nothing else should).
181
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700182// Allocate and return a GRSurface* sufficient for storing an image of
Doug Zongkera418aa72014-03-17 12:10:02 -0700183// the indicated size in the framebuffer pixel format.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700184static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
185 GRSurface* surface = malloc_surface(width * height * 4);
Doug Zongkera418aa72014-03-17 12:10:02 -0700186 if (surface == NULL) return NULL;
187
188 surface->width = width;
189 surface->height = height;
190 surface->row_bytes = width * 4;
191 surface->pixel_bytes = 4;
192
193 return surface;
194}
195
196// Copy 'input_row' to 'output_row', transforming it to the
197// framebuffer pixel format. The input format depends on the value of
198// 'channels':
199//
200// 1 - input is 8-bit grayscale
201// 3 - input is 24-bit RGB
202// 4 - input is 32-bit RGBA/RGBX
203//
204// 'width' is the number of pixels in the row.
205static void transform_rgb_to_draw(unsigned char* input_row,
206 unsigned char* output_row,
207 int channels, int width) {
208 int x;
209 unsigned char* ip = input_row;
210 unsigned char* op = output_row;
211
212 switch (channels) {
213 case 1:
214 // expand gray level to RGBX
215 for (x = 0; x < width; ++x) {
216 *op++ = *ip;
217 *op++ = *ip;
218 *op++ = *ip;
219 *op++ = 0xff;
220 ip++;
221 }
222 break;
223
224 case 3:
225 // expand RGBA to RGBX
226 for (x = 0; x < width; ++x) {
227 *op++ = *ip++;
228 *op++ = *ip++;
229 *op++ = *ip++;
230 *op++ = 0xff;
231 }
232 break;
233
234 case 4:
235 // copy RGBA to RGBX
236 memcpy(output_row, input_row, width*4);
237 break;
238 }
239}
240
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700241int res_create_display_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700242 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700243
Tianjie Xuacba38c2017-09-27 17:53:34 -0700244 PngHandler png_handler(name);
245 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700246
Tianjie Xuacba38c2017-09-27 17:53:34 -0700247 png_structp png_ptr = png_handler.png_ptr();
248 png_uint_32 width = png_handler.width();
249 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700250
Tianjie Xuacba38c2017-09-27 17:53:34 -0700251 GRSurface* surface = init_display_surface(width, height);
252 if (!surface) {
253 return -8;
254 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255
Tony Kuofd778e32015-02-05 21:25:56 +0800256#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
Tianjie Xuacba38c2017-09-27 17:53:34 -0700257 png_set_bgr(png_ptr);
Tony Kuofd778e32015-02-05 21:25:56 +0800258#endif
259
Tianjie Xuacba38c2017-09-27 17:53:34 -0700260 for (png_uint_32 y = 0; y < height; ++y) {
261 std::vector<unsigned char> p_row(width * 4);
262 png_read_row(png_ptr, p_row.data(), nullptr);
263 transform_rgb_to_draw(p_row.data(), surface->data + y * surface->row_bytes,
264 png_handler.channels(), width);
265 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700266
Tianjie Xuacba38c2017-09-27 17:53:34 -0700267 *pSurface = surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268
Tianjie Xuacba38c2017-09-27 17:53:34 -0700269 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270}
271
Tao Baob723f4f2015-12-11 15:18:51 -0800272int res_create_multi_display_surface(const char* name, int* frames, int* fps,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700273 GRSurface*** pSurface) {
274 *pSurface = nullptr;
275 *frames = -1;
Doug Zongker469954f2014-03-07 09:21:25 -0800276
Tianjie Xuacba38c2017-09-27 17:53:34 -0700277 PngHandler png_handler(name);
278 if (!png_handler) return png_handler.error_code();
Doug Zongker469954f2014-03-07 09:21:25 -0800279
Tianjie Xuacba38c2017-09-27 17:53:34 -0700280 png_structp png_ptr = png_handler.png_ptr();
281 png_uint_32 width = png_handler.width();
282 png_uint_32 height = png_handler.height();
Doug Zongker469954f2014-03-07 09:21:25 -0800283
Tianjie Xuacba38c2017-09-27 17:53:34 -0700284 *frames = 1;
285 *fps = 20;
286 png_textp text;
287 int num_text;
288 if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) {
289 for (int i = 0; i < num_text; ++i) {
290 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
291 *frames = atoi(text[i].text);
292 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
293 *fps = atoi(text[i].text);
294 }
Tao Baob723f4f2015-12-11 15:18:51 -0800295 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700296 printf(" found frames = %d\n", *frames);
297 printf(" found fps = %d\n", *fps);
298 }
Tao Baob723f4f2015-12-11 15:18:51 -0800299
Tianjie Xuacba38c2017-09-27 17:53:34 -0700300 int result = 0;
301 GRSurface** surface = nullptr;
302 if (*frames <= 0 || *fps <= 0) {
303 printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
304 result = -10;
305 goto exit;
306 }
Doug Zongker469954f2014-03-07 09:21:25 -0800307
Tianjie Xuacba38c2017-09-27 17:53:34 -0700308 if (height % *frames != 0) {
309 printf("bad height (%d) for frame count (%d)\n", height, *frames);
310 result = -9;
311 goto exit;
312 }
Doug Zongker469954f2014-03-07 09:21:25 -0800313
Tianjie Xuacba38c2017-09-27 17:53:34 -0700314 surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
315 if (!surface) {
316 result = -8;
317 goto exit;
318 }
319 for (int i = 0; i < *frames; ++i) {
320 surface[i] = init_display_surface(width, height / *frames);
321 if (!surface[i]) {
322 result = -8;
323 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800324 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700325 }
Doug Zongker469954f2014-03-07 09:21:25 -0800326
Tony Kuofd778e32015-02-05 21:25:56 +0800327#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
Tianjie Xuacba38c2017-09-27 17:53:34 -0700328 png_set_bgr(png_ptr);
Tony Kuofd778e32015-02-05 21:25:56 +0800329#endif
330
Tianjie Xuacba38c2017-09-27 17:53:34 -0700331 for (png_uint_32 y = 0; y < height; ++y) {
332 std::vector<unsigned char> p_row(width * 4);
333 png_read_row(png_ptr, p_row.data(), nullptr);
334 int frame = y % *frames;
335 unsigned char* out_row = surface[frame]->data + (y / *frames) * surface[frame]->row_bytes;
336 transform_rgb_to_draw(p_row.data(), out_row, png_handler.channels(), width);
337 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700338
Tianjie Xuacba38c2017-09-27 17:53:34 -0700339 *pSurface = surface;
Doug Zongker469954f2014-03-07 09:21:25 -0800340
341exit:
Tianjie Xuacba38c2017-09-27 17:53:34 -0700342 if (result < 0) {
343 if (surface) {
344 for (int i = 0; i < *frames; ++i) {
345 free(surface[i]);
346 }
347 free(surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800348 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700349 }
350 return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800351}
352
Ethan Yonker534d4e02016-08-26 10:05:03 -0500353int res_create_multi_display_surface(const char* name, int* frames,
354 GRSurface*** pSurface) {
355 int fps = 0;
356 return res_create_multi_display_surface(name, frames, &fps, pSurface);
357}
358
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700359int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700360 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700361
Tianjie Xuacba38c2017-09-27 17:53:34 -0700362 PngHandler png_handler(name);
363 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700364
Tianjie Xuacba38c2017-09-27 17:53:34 -0700365 if (png_handler.channels() != 1) {
366 return -7;
367 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700368
Tianjie Xuacba38c2017-09-27 17:53:34 -0700369 png_structp png_ptr = png_handler.png_ptr();
370 png_uint_32 width = png_handler.width();
371 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700372
Tianjie Xuacba38c2017-09-27 17:53:34 -0700373 GRSurface* surface = malloc_surface(width * height);
374 if (!surface) {
375 return -8;
376 }
377 surface->width = width;
378 surface->height = height;
379 surface->row_bytes = width;
380 surface->pixel_bytes = 1;
Doug Zongkera418aa72014-03-17 12:10:02 -0700381
Tony Kuofd778e32015-02-05 21:25:56 +0800382#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
Tianjie Xuacba38c2017-09-27 17:53:34 -0700383 png_set_bgr(png_ptr);
Tony Kuofd778e32015-02-05 21:25:56 +0800384#endif
385
Tianjie Xuacba38c2017-09-27 17:53:34 -0700386 for (png_uint_32 y = 0; y < height; ++y) {
387 unsigned char* p_row = surface->data + y * surface->row_bytes;
388 png_read_row(png_ptr, p_row, nullptr);
389 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700390
Tianjie Xuacba38c2017-09-27 17:53:34 -0700391 *pSurface = surface;
Doug Zongkera418aa72014-03-17 12:10:02 -0700392
Tianjie Xuacba38c2017-09-27 17:53:34 -0700393 return 0;
Doug Zongkera418aa72014-03-17 12:10:02 -0700394}
395
Tianjie Xu2430e292016-04-19 15:02:41 -0700396// This function tests if a locale string stored in PNG (prefix) matches
397// the locale string provided by the system (locale).
Tianjie Xu0a599562017-03-28 20:11:15 +0000398bool matches_locale(const std::string& prefix, const std::string& locale) {
399 // According to the BCP 47 format, A locale string may consists of:
400 // language-{extlang}-{script}-{region}-{variant}
401 // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
402 // android's system locale can have the format language-{script}-{region}.
Doug Zongker02ec6b82012-08-22 17:26:40 -0700403
Tianjie Xu0a599562017-03-28 20:11:15 +0000404 // Return true if the whole string of prefix matches the top part of locale. Otherwise try to
405 // match the locale string without the {script} section.
406 // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
407 // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
Ethan Yonker58f21322018-08-24 11:17:36 -0500408 //if (android::base::StartsWith(locale, prefix)) { // does not exist in 6.0
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500409 if (strncmp(prefix.c_str(), locale.c_str(), prefix.length()) == 0) {
Tianjie Xu0a599562017-03-28 20:11:15 +0000410 return true;
411 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700412
Tianjie Xu0a599562017-03-28 20:11:15 +0000413 size_t separator = prefix.find('-');
414 if (separator == std::string::npos) {
415 return false;
416 }
417 std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator));
418 return std::regex_match(locale, loc_regex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700419}
420
Tianjie Xu29d55752017-09-20 17:53:46 -0700421std::vector<std::string> get_locales_in_png(const std::string& png_name) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700422 PngHandler png_handler(png_name);
423 if (!png_handler) {
424 printf("Failed to open %s, error: %d\n", png_name.c_str(), png_handler.error_code());
Tianjie Xu29d55752017-09-20 17:53:46 -0700425 return {};
426 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700427 if (png_handler.channels() != 1) {
428 printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels());
Tianjie Xu29d55752017-09-20 17:53:46 -0700429 return {};
430 }
431
432 std::vector<std::string> result;
Tianjie Xuacba38c2017-09-27 17:53:34 -0700433 std::vector<unsigned char> row(png_handler.width());
434 for (png_uint_32 y = 0; y < png_handler.height(); ++y) {
435 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700436 int h = (row[3] << 8) | row[2];
437 std::string loc(reinterpret_cast<char*>(&row[5]));
438 if (!loc.empty()) {
439 result.push_back(loc);
440 }
441 for (int i = 0; i < h; ++i, ++y) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700442 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700443 }
444 }
445
Tianjie Xu29d55752017-09-20 17:53:46 -0700446 return result;
447}
448
Doug Zongkera418aa72014-03-17 12:10:02 -0700449int res_create_localized_alpha_surface(const char* name,
450 const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700451 GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700452 *pSurface = nullptr;
453 if (locale == nullptr) {
454 return 0;
455 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700456
Tianjie Xuacba38c2017-09-27 17:53:34 -0700457 PngHandler png_handler(name);
458 if (!png_handler) return png_handler.error_code();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700459
Tianjie Xuacba38c2017-09-27 17:53:34 -0700460 if (png_handler.channels() != 1) {
461 return -7;
462 }
463
464 png_structp png_ptr = png_handler.png_ptr();
465 png_uint_32 width = png_handler.width();
466 png_uint_32 height = png_handler.height();
467
468 for (png_uint_32 y = 0; y < height; ++y) {
469 std::vector<unsigned char> row(width);
470 png_read_row(png_ptr, row.data(), nullptr);
471 int w = (row[1] << 8) | row[0];
472 int h = (row[3] << 8) | row[2];
473 __unused int len = row[4];
474 char* loc = reinterpret_cast<char*>(&row[5]);
475
476 if (y + 1 + h >= height || matches_locale(loc, locale)) {
477 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
478
479 GRSurface* surface = malloc_surface(w * h);
480 if (!surface) {
481 return -8;
482 }
483 surface->width = w;
484 surface->height = h;
485 surface->row_bytes = w;
486 surface->pixel_bytes = 1;
487
488 for (int i = 0; i < h; ++i, ++y) {
489 png_read_row(png_ptr, row.data(), nullptr);
490 memcpy(surface->data + i * w, row.data(), w);
491 }
492
493 *pSurface = surface;
494 break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700495 }
496
Tianjie Xuacba38c2017-09-27 17:53:34 -0700497 for (int i = 0; i < h; ++i, ++y) {
498 png_read_row(png_ptr, row.data(), nullptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700499 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700500 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700501
Tianjie Xuacba38c2017-09-27 17:53:34 -0700502 return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700503}
504
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700505void res_free_surface(GRSurface* surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700506 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800507}