blob: 026e1dc2bf3f2525bbf04298a485502d23d12a76 [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 Xu0a599562017-03-28 20:11:15 +000028#include <regex>
29#include <string>
Yabin Cui4425c1d2016-02-10 13:47:32 -080030#include <vector>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Ethan Yonker8373cfe2017-09-08 06:50:54 -050032//#include <android-base/strings.h> // does not exist in 6.0
Doug Zongker19faefa2009-03-27 17:06:24 -070033#include <png.h>
34
Tao Bao0ecbd762017-01-16 21:16:58 -080035#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Doug Zongker16f97c32014-03-06 16:16:05 -080037#define SURFACE_DATA_ALIGNMENT 8
38
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070039static GRSurface* malloc_surface(size_t data_size) {
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070040 size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT;
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -080041 unsigned char* temp = static_cast<unsigned char*>(malloc(size));
Doug Zongker16f97c32014-03-06 16:16:05 -080042 if (temp == NULL) return NULL;
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070043 GRSurface* surface = reinterpret_cast<GRSurface*>(temp);
Doug Zongker16f97c32014-03-06 16:16:05 -080044 surface->data = temp + sizeof(GRSurface) +
45 (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
46 return surface;
Doug Zongker19faefa2009-03-27 17:06:24 -070047}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Doug Zongkera418aa72014-03-17 12:10:02 -070049static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
50 png_uint_32* width, png_uint_32* height, png_byte* channels) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051 char resPath[256];
Doug Zongker19faefa2009-03-27 17:06:24 -070052 unsigned char header[8];
Doug Zongkera418aa72014-03-17 12:10:02 -070053 int result = 0;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070054 int color_type, bit_depth;
55 size_t bytesRead;
Doug Zongker6809c512011-03-01 14:04:34 -080056
Doug Zongker19faefa2009-03-27 17:06:24 -070057 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070059 FILE* fp = fopen(resPath, "rb");
60 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 result = -1;
62 goto exit;
63 }
Doug Zongker19faefa2009-03-27 17:06:24 -070064
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070065 bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066 if (bytesRead != sizeof(header)) {
67 result = -2;
68 goto exit;
69 }
Doug Zongker19faefa2009-03-27 17:06:24 -070070
71 if (png_sig_cmp(header, 0, sizeof(header))) {
72 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 goto exit;
74 }
Doug Zongker19faefa2009-03-27 17:06:24 -070075
Doug Zongkera418aa72014-03-17 12:10:02 -070076 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
77 if (!*png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 result = -4;
79 goto exit;
80 }
Doug Zongker19faefa2009-03-27 17:06:24 -070081
Doug Zongkera418aa72014-03-17 12:10:02 -070082 *info_ptr = png_create_info_struct(*png_ptr);
83 if (!*info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 result = -5;
85 goto exit;
86 }
Doug Zongker19faefa2009-03-27 17:06:24 -070087
Doug Zongkera418aa72014-03-17 12:10:02 -070088 if (setjmp(png_jmpbuf(*png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089 result = -6;
90 goto exit;
91 }
Doug Zongker19faefa2009-03-27 17:06:24 -070092
Doug Zongkera418aa72014-03-17 12:10:02 -070093 png_init_io(*png_ptr, fp);
94 png_set_sig_bytes(*png_ptr, sizeof(header));
95 png_read_info(*png_ptr, *info_ptr);
Doug Zongker19faefa2009-03-27 17:06:24 -070096
Doug Zongkera418aa72014-03-17 12:10:02 -070097 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
John Reck94fd07b2013-08-26 16:45:33 -070098 &color_type, NULL, NULL, NULL);
99
Doug Zongkera418aa72014-03-17 12:10:02 -0700100 *channels = png_get_channels(*png_ptr, *info_ptr);
John Reck94fd07b2013-08-26 16:45:33 -0700101
Doug Zongkera418aa72014-03-17 12:10:02 -0700102 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
103 // 8-bit RGB images: great, nothing to do.
104 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
105 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
106 png_set_expand_gray_1_2_4_to_8(*png_ptr);
Doug Zongker577a1302014-03-20 08:27:01 -0700107 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700108 // paletted images: expand to 8-bit RGB. Note that we DON'T
109 // currently expand the tRNS chunk (if any) to an alpha
110 // channel, because minui doesn't support alpha channels in
111 // general.
112 png_set_palette_to_rgb(*png_ptr);
113 *channels = 3;
114 } else {
115 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
Doug Zongkera388a762014-03-17 16:51:47 -0700116 bit_depth, *channels, color_type);
Doug Zongkera418aa72014-03-17 12:10:02 -0700117 result = -7;
Doug Zongker19faefa2009-03-27 17:06:24 -0700118 goto exit;
119 }
120
Doug Zongkera418aa72014-03-17 12:10:02 -0700121 return result;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800122
Doug Zongkera418aa72014-03-17 12:10:02 -0700123 exit:
124 if (result < 0) {
125 png_destroy_read_struct(png_ptr, info_ptr, NULL);
126 }
127 if (fp != NULL) {
128 fclose(fp);
129 }
130
131 return result;
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
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700141// Allocate and return a GRSurface* sufficient for storing an image of
Doug Zongkera418aa72014-03-17 12:10:02 -0700142// the indicated size in the framebuffer pixel format.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700143static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
144 GRSurface* surface = malloc_surface(width * height * 4);
Doug Zongkera418aa72014-03-17 12:10:02 -0700145 if (surface == NULL) return NULL;
146
147 surface->width = width;
148 surface->height = height;
149 surface->row_bytes = width * 4;
150 surface->pixel_bytes = 4;
151
152 return surface;
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.
164static 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 Hughes0a5cb0c2015-04-15 10:58:56 -0700200int res_create_display_surface(const char* name, GRSurface** pSurface) {
201 GRSurface* surface = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700202 int result = 0;
203 png_structp png_ptr = NULL;
204 png_infop info_ptr = NULL;
205 png_uint_32 width, height;
206 png_byte channels;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700207 unsigned char* p_row;
208 unsigned int y;
Doug Zongkera418aa72014-03-17 12:10:02 -0700209
210 *pSurface = NULL;
211
212 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
213 if (result < 0) return result;
214
215 surface = init_display_surface(width, height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700217 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 goto exit;
219 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220
Tony Kuofd778e32015-02-05 21:25:56 +0800221#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
222 png_set_bgr(png_ptr);
223#endif
224
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800225 p_row = static_cast<unsigned char*>(malloc(width * 4));
Doug Zongkera418aa72014-03-17 12:10:02 -0700226 for (y = 0; y < height; ++y) {
227 png_read_row(png_ptr, p_row, NULL);
228 transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700230 free(p_row);
Doug Zongker19faefa2009-03-27 17:06:24 -0700231
Doug Zongkera418aa72014-03-17 12:10:02 -0700232 *pSurface = surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233
Doug Zongkera418aa72014-03-17 12:10:02 -0700234 exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700235 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700236 if (result < 0 && surface != NULL) free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237 return result;
238}
239
Tao Baob723f4f2015-12-11 15:18:51 -0800240int res_create_multi_display_surface(const char* name, int* frames, int* fps,
241 GRSurface*** pSurface) {
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700242 GRSurface** surface = NULL;
Doug Zongker469954f2014-03-07 09:21:25 -0800243 int result = 0;
Doug Zongker469954f2014-03-07 09:21:25 -0800244 png_structp png_ptr = NULL;
245 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700246 png_uint_32 width, height;
247 png_byte channels;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700248 png_textp text;
249 int num_text;
250 unsigned char* p_row;
251 unsigned int y;
Doug Zongker469954f2014-03-07 09:21:25 -0800252
253 *pSurface = NULL;
254 *frames = -1;
255
Doug Zongkera418aa72014-03-17 12:10:02 -0700256 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
257 if (result < 0) return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800258
259 *frames = 1;
Tao Baob723f4f2015-12-11 15:18:51 -0800260 *fps = 20;
Doug Zongker469954f2014-03-07 09:21:25 -0800261 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
Tao Baob723f4f2015-12-11 15:18:51 -0800262 for (int i = 0; i < num_text; ++i) {
Doug Zongker469954f2014-03-07 09:21:25 -0800263 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
264 *frames = atoi(text[i].text);
Tao Baob723f4f2015-12-11 15:18:51 -0800265 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
266 *fps = atoi(text[i].text);
Doug Zongker469954f2014-03-07 09:21:25 -0800267 }
268 }
269 printf(" found frames = %d\n", *frames);
Tao Baob723f4f2015-12-11 15:18:51 -0800270 printf(" found fps = %d\n", *fps);
271 }
272
MinSeong Kim663ad8e2016-11-11 05:26:06 +0000273 if (*frames <= 0 || *fps <= 0) {
Tao Baob723f4f2015-12-11 15:18:51 -0800274 printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
275 result = -10;
276 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800277 }
278
279 if (height % *frames != 0) {
280 printf("bad height (%d) for frame count (%d)\n", height, *frames);
281 result = -9;
282 goto exit;
283 }
284
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800285 surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
Doug Zongker469954f2014-03-07 09:21:25 -0800286 if (surface == NULL) {
287 result = -8;
288 goto exit;
289 }
Tao Baob723f4f2015-12-11 15:18:51 -0800290 for (int i = 0; i < *frames; ++i) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700291 surface[i] = init_display_surface(width, height / *frames);
292 if (surface[i] == NULL) {
293 result = -8;
294 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800295 }
296 }
297
Tony Kuofd778e32015-02-05 21:25:56 +0800298#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
299 png_set_bgr(png_ptr);
300#endif
301
Rahul Chaudhryb29f23f2016-11-09 13:17:01 -0800302 p_row = static_cast<unsigned char*>(malloc(width * 4));
Doug Zongkera418aa72014-03-17 12:10:02 -0700303 for (y = 0; y < height; ++y) {
304 png_read_row(png_ptr, p_row, NULL);
305 int frame = y % *frames;
306 unsigned char* out_row = surface[frame]->data +
307 (y / *frames) * surface[frame]->row_bytes;
308 transform_rgb_to_draw(p_row, out_row, channels, width);
309 }
310 free(p_row);
311
Rahul Chaudhry1cf93f52016-11-08 16:35:22 -0800312 *pSurface = surface;
Doug Zongker469954f2014-03-07 09:21:25 -0800313
314exit:
315 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
316
Doug Zongker469954f2014-03-07 09:21:25 -0800317 if (result < 0) {
318 if (surface) {
Tao Baob723f4f2015-12-11 15:18:51 -0800319 for (int i = 0; i < *frames; ++i) {
Elliott Hughes7d626df2016-02-19 10:33:01 -0800320 free(surface[i]);
Doug Zongker469954f2014-03-07 09:21:25 -0800321 }
322 free(surface);
323 }
324 }
325 return result;
326}
327
Ethan Yonker534d4e02016-08-26 10:05:03 -0500328int res_create_multi_display_surface(const char* name, int* frames,
329 GRSurface*** pSurface) {
330 int fps = 0;
331 return res_create_multi_display_surface(name, frames, &fps, pSurface);
332}
333
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700334int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
335 GRSurface* surface = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700336 int result = 0;
337 png_structp png_ptr = NULL;
338 png_infop info_ptr = NULL;
339 png_uint_32 width, height;
340 png_byte channels;
341
342 *pSurface = NULL;
343
344 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
345 if (result < 0) return result;
346
347 if (channels != 1) {
348 result = -7;
349 goto exit;
350 }
351
352 surface = malloc_surface(width * height);
353 if (surface == NULL) {
354 result = -8;
355 goto exit;
356 }
357 surface->width = width;
358 surface->height = height;
359 surface->row_bytes = width;
360 surface->pixel_bytes = 1;
361
Tony Kuofd778e32015-02-05 21:25:56 +0800362#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
363 png_set_bgr(png_ptr);
364#endif
365
Doug Zongkera418aa72014-03-17 12:10:02 -0700366 unsigned char* p_row;
367 unsigned int y;
368 for (y = 0; y < height; ++y) {
369 p_row = surface->data + y * surface->row_bytes;
370 png_read_row(png_ptr, p_row, NULL);
371 }
372
373 *pSurface = surface;
374
375 exit:
376 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
377 if (result < 0 && surface != NULL) free(surface);
378 return result;
379}
380
Tianjie Xu2430e292016-04-19 15:02:41 -0700381// This function tests if a locale string stored in PNG (prefix) matches
382// the locale string provided by the system (locale).
Tianjie Xu0a599562017-03-28 20:11:15 +0000383bool matches_locale(const std::string& prefix, const std::string& locale) {
384 // According to the BCP 47 format, A locale string may consists of:
385 // language-{extlang}-{script}-{region}-{variant}
386 // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
387 // android's system locale can have the format language-{script}-{region}.
Doug Zongker02ec6b82012-08-22 17:26:40 -0700388
Tianjie Xu0a599562017-03-28 20:11:15 +0000389 // Return true if the whole string of prefix matches the top part of locale. Otherwise try to
390 // match the locale string without the {script} section.
391 // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
392 // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500393 //if (android::base::StartsWith(locale, prefix.c_str())) { // does not exist in 6.0
394 if (strncmp(prefix.c_str(), locale.c_str(), prefix.length()) == 0) {
Tianjie Xu0a599562017-03-28 20:11:15 +0000395 return true;
396 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700397
Tianjie Xu0a599562017-03-28 20:11:15 +0000398 size_t separator = prefix.find('-');
399 if (separator == std::string::npos) {
400 return false;
401 }
402 std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator));
403 return std::regex_match(locale, loc_regex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700404}
405
Doug Zongkera418aa72014-03-17 12:10:02 -0700406int res_create_localized_alpha_surface(const char* name,
407 const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700408 GRSurface** pSurface) {
409 GRSurface* surface = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700410 int result = 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700411 png_structp png_ptr = NULL;
412 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700413 png_uint_32 width, height;
414 png_byte channels;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700415 png_uint_32 y;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800416 std::vector<unsigned char> row;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417
418 *pSurface = NULL;
419
Doug Zongkera418aa72014-03-17 12:10:02 -0700420 if (locale == NULL) {
Yabin Cui4425c1d2016-02-10 13:47:32 -0800421 return result;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700422 }
423
Doug Zongkera418aa72014-03-17 12:10:02 -0700424 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
425 if (result < 0) return result;
426
427 if (channels != 1) {
428 result = -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700429 goto exit;
430 }
431
Yabin Cui4425c1d2016-02-10 13:47:32 -0800432 row.resize(width);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700433 for (y = 0; y < height; ++y) {
Yabin Cui4425c1d2016-02-10 13:47:32 -0800434 png_read_row(png_ptr, row.data(), NULL);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700435 int w = (row[1] << 8) | row[0];
436 int h = (row[3] << 8) | row[2];
Elliott Hughes7d626df2016-02-19 10:33:01 -0800437 __unused int len = row[4];
Yabin Cui4425c1d2016-02-10 13:47:32 -0800438 char* loc = reinterpret_cast<char*>(&row[5]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700439
Doug Zongkera418aa72014-03-17 12:10:02 -0700440 if (y+1+h >= height || matches_locale(loc, locale)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700441 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
442
Doug Zongker16f97c32014-03-06 16:16:05 -0800443 surface = malloc_surface(w*h);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700444 if (surface == NULL) {
445 result = -8;
446 goto exit;
447 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700448 surface->width = w;
449 surface->height = h;
Doug Zongker16f97c32014-03-06 16:16:05 -0800450 surface->row_bytes = w;
451 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700452
453 int i;
454 for (i = 0; i < h; ++i, ++y) {
Yabin Cui4425c1d2016-02-10 13:47:32 -0800455 png_read_row(png_ptr, row.data(), NULL);
456 memcpy(surface->data + i*w, row.data(), w);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700457 }
458
Rahul Chaudhry1cf93f52016-11-08 16:35:22 -0800459 *pSurface = surface;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700460 break;
461 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700462 int i;
463 for (i = 0; i < h; ++i, ++y) {
Yabin Cui4425c1d2016-02-10 13:47:32 -0800464 png_read_row(png_ptr, row.data(), NULL);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700465 }
466 }
467 }
468
469exit:
470 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700471 if (result < 0 && surface != NULL) free(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700472 return result;
473}
474
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700475void res_free_surface(GRSurface* surface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800476 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800477}