blob: c7af1904d865cb58b884330896990e17ffb3b8e2 [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
Tianjie Xuacba38c2017-09-27 17:53:34 -070030#include <memory>
Tianjie Xu2078b222017-03-22 12:27:26 -070031#include <regex>
32#include <string>
Yabin Cui4425c1d2016-02-10 13:47:32 -080033#include <vector>
Tao Bao0b1118d2017-01-14 07:46:10 -080034
Tianjie Xu2078b222017-03-22 12:27:26 -070035#include <android-base/strings.h>
Doug Zongker19faefa2009-03-27 17:06:24 -070036#include <png.h>
37
Tao Bao0ecbd762017-01-16 21:16:58 -080038#include "minui/minui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Tao Bao6cd81682018-05-03 21:53:11 -070040static std::string g_resource_dir{ "/res/images" };
41
Tao Bao44820ac2018-10-30 23:34:50 -070042std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_bytes, int pixel_bytes,
43 size_t data_size) {
Tao Bao92bdb5a2018-10-21 12:12:37 -070044 static constexpr size_t kSurfaceDataAlignment = 8;
Tao Bao44820ac2018-10-30 23:34:50 -070045 // Cannot use std::make_unique to access non-public ctor.
46 auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes));
Tao Bao63b59dc2018-10-31 15:23:04 -070047 result->data_size_ =
Tao Bao92bdb5a2018-10-21 12:12:37 -070048 (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment;
Tao Bao9cf163e2018-11-07 10:15:50 -080049 result->data_.reset(
50 static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_)));
51 if (!result->data_) return nullptr;
Tao Bao92bdb5a2018-10-21 12:12:37 -070052 return result;
53}
54
Tao Bao63b59dc2018-10-31 15:23:04 -070055std::unique_ptr<GRSurface> GRSurface::Clone() const {
56 auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes, data_size_);
Tao Bao9cf163e2018-11-07 10:15:50 -080057 if (!result) return nullptr;
58 memcpy(result->data(), data(), data_size_);
Tao Bao63b59dc2018-10-31 15:23:04 -070059 return result;
60}
61
Tao Bao3d0cd1d2018-04-13 13:41:58 -070062PngHandler::PngHandler(const std::string& name) {
Tao Bao6cd81682018-05-03 21:53:11 -070063 std::string res_path = g_resource_dir + "/" + name + ".png";
Tianjie Xuacba38c2017-09-27 17:53:34 -070064 png_fp_.reset(fopen(res_path.c_str(), "rbe"));
Tao Bao3d0cd1d2018-04-13 13:41:58 -070065 // Try to read from |name| if the resource path does not work.
66 if (!png_fp_) {
67 png_fp_.reset(fopen(name.c_str(), "rbe"));
68 }
Tianjie Xuacba38c2017-09-27 17:53:34 -070069 if (!png_fp_) {
70 error_code_ = -1;
71 return;
72 }
Doug Zongker55a36ac2013-03-04 15:49:02 -080073
Tao Bao44820ac2018-10-30 23:34:50 -070074 uint8_t header[8];
Tianjie Xuacba38c2017-09-27 17:53:34 -070075 size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
76 if (bytesRead != sizeof(header)) {
77 error_code_ = -2;
78 return;
79 }
Doug Zongkera418aa72014-03-17 12:10:02 -070080
Tianjie Xuacba38c2017-09-27 17:53:34 -070081 if (png_sig_cmp(header, 0, sizeof(header))) {
82 error_code_ = -3;
83 return;
84 }
85
86 png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
87 if (!png_ptr_) {
88 error_code_ = -4;
89 return;
90 }
91
92 info_ptr_ = png_create_info_struct(png_ptr_);
93 if (!info_ptr_) {
94 error_code_ = -5;
95 return;
96 }
97
98 if (setjmp(png_jmpbuf(png_ptr_))) {
99 error_code_ = -6;
100 return;
101 }
102
103 png_init_io(png_ptr_, png_fp_.get());
104 png_set_sig_bytes(png_ptr_, sizeof(header));
105 png_read_info(png_ptr_, info_ptr_);
106
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700107 png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700108 nullptr);
109
110 channels_ = png_get_channels(png_ptr_, info_ptr_);
111
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700112 if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700113 // 8-bit RGB images: great, nothing to do.
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700114 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700115 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
116 png_set_expand_gray_1_2_4_to_8(png_ptr_);
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700117 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700118 // paletted images: expand to 8-bit RGB. Note that we DON'T
119 // currently expand the tRNS chunk (if any) to an alpha
120 // channel, because minui doesn't support alpha channels in
121 // general.
122 png_set_palette_to_rgb(png_ptr_);
123 channels_ = 3;
124 } else {
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700125 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_,
126 channels_, color_type_);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700127 error_code_ = -7;
128 }
129}
130
131PngHandler::~PngHandler() {
132 if (png_ptr_) {
133 png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr);
134 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700135}
136
Tao Bao44820ac2018-10-30 23:34:50 -0700137// "display" surfaces are transformed into the framebuffer's required pixel format (currently only
138// 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 -0700139
Tao Bao44820ac2018-10-30 23:34:50 -0700140// Copies 'input_row' to 'output_row', transforming it to the framebuffer pixel format. The input
141// format depends on the value of 'channels':
Doug Zongkera418aa72014-03-17 12:10:02 -0700142//
143// 1 - input is 8-bit grayscale
144// 3 - input is 24-bit RGB
145// 4 - input is 32-bit RGBA/RGBX
146//
147// 'width' is the number of pixels in the row.
Tao Bao44820ac2018-10-30 23:34:50 -0700148static void TransformRgbToDraw(const uint8_t* input_row, uint8_t* output_row, int channels,
149 int width) {
150 const uint8_t* ip = input_row;
151 uint8_t* op = output_row;
Doug Zongkera418aa72014-03-17 12:10:02 -0700152
Tao Bao44820ac2018-10-30 23:34:50 -0700153 switch (channels) {
154 case 1:
155 // expand gray level to RGBX
156 for (int x = 0; x < width; ++x) {
157 *op++ = *ip;
158 *op++ = *ip;
159 *op++ = *ip;
160 *op++ = 0xff;
161 ip++;
162 }
163 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700164
Tao Bao44820ac2018-10-30 23:34:50 -0700165 case 3:
166 // expand RGBA to RGBX
167 for (int x = 0; x < width; ++x) {
168 *op++ = *ip++;
169 *op++ = *ip++;
170 *op++ = *ip++;
171 *op++ = 0xff;
172 }
173 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700174
Tao Bao44820ac2018-10-30 23:34:50 -0700175 case 4:
176 // copy RGBA to RGBX
177 memcpy(output_row, input_row, width * 4);
178 break;
179 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700180}
181
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700182int res_create_display_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700183 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700184
Tianjie Xuacba38c2017-09-27 17:53:34 -0700185 PngHandler png_handler(name);
186 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700187
Tianjie Xuacba38c2017-09-27 17:53:34 -0700188 png_structp png_ptr = png_handler.png_ptr();
189 png_uint_32 width = png_handler.width();
190 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700191
Tao Bao44820ac2018-10-30 23:34:50 -0700192 auto surface = GRSurface::Create(width, height, width * 4, 4, width * height * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700193 if (!surface) {
194 return -8;
195 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800196
Tao Baoed876a72018-07-31 21:32:50 -0700197 PixelFormat pixel_format = gr_pixel_format();
198 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
199 png_set_bgr(png_ptr);
200 }
Tony Kuofd778e32015-02-05 21:25:56 +0800201
Tianjie Xuacba38c2017-09-27 17:53:34 -0700202 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700203 std::vector<uint8_t> p_row(width * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700204 png_read_row(png_ptr, p_row.data(), nullptr);
Tao Bao44820ac2018-10-30 23:34:50 -0700205 TransformRgbToDraw(p_row.data(), surface->data() + y * surface->row_bytes,
206 png_handler.channels(), width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700207 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700208
Tao Bao92bdb5a2018-10-21 12:12:37 -0700209 *pSurface = surface.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210
Tianjie Xuacba38c2017-09-27 17:53:34 -0700211 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212}
213
Tao Baob723f4f2015-12-11 15:18:51 -0800214int res_create_multi_display_surface(const char* name, int* frames, int* fps,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700215 GRSurface*** pSurface) {
216 *pSurface = nullptr;
217 *frames = -1;
Doug Zongker469954f2014-03-07 09:21:25 -0800218
Tianjie Xuacba38c2017-09-27 17:53:34 -0700219 PngHandler png_handler(name);
220 if (!png_handler) return png_handler.error_code();
Doug Zongker469954f2014-03-07 09:21:25 -0800221
Tianjie Xuacba38c2017-09-27 17:53:34 -0700222 png_structp png_ptr = png_handler.png_ptr();
223 png_uint_32 width = png_handler.width();
224 png_uint_32 height = png_handler.height();
Doug Zongker469954f2014-03-07 09:21:25 -0800225
Tianjie Xuacba38c2017-09-27 17:53:34 -0700226 *frames = 1;
227 *fps = 20;
228 png_textp text;
229 int num_text;
230 if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) {
231 for (int i = 0; i < num_text; ++i) {
232 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
233 *frames = atoi(text[i].text);
234 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
235 *fps = atoi(text[i].text);
236 }
Tao Baob723f4f2015-12-11 15:18:51 -0800237 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700238 printf(" found frames = %d\n", *frames);
239 printf(" found fps = %d\n", *fps);
240 }
Tao Baob723f4f2015-12-11 15:18:51 -0800241
Tianjie Xuacba38c2017-09-27 17:53:34 -0700242 int result = 0;
243 GRSurface** surface = nullptr;
244 if (*frames <= 0 || *fps <= 0) {
245 printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
246 result = -10;
247 goto exit;
248 }
Doug Zongker469954f2014-03-07 09:21:25 -0800249
Tianjie Xuacba38c2017-09-27 17:53:34 -0700250 if (height % *frames != 0) {
251 printf("bad height (%d) for frame count (%d)\n", height, *frames);
252 result = -9;
253 goto exit;
254 }
Doug Zongker469954f2014-03-07 09:21:25 -0800255
Tianjie Xuacba38c2017-09-27 17:53:34 -0700256 surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
257 if (!surface) {
258 result = -8;
259 goto exit;
260 }
261 for (int i = 0; i < *frames; ++i) {
Tao Bao44820ac2018-10-30 23:34:50 -0700262 auto height_per_frame = height / *frames;
263 auto created_surface =
264 GRSurface::Create(width, height_per_frame, width * 4, 4, width * height_per_frame);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700265 if (!created_surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700266 result = -8;
267 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800268 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700269 surface[i] = created_surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700270 }
Doug Zongker469954f2014-03-07 09:21:25 -0800271
Tao Baoed876a72018-07-31 21:32:50 -0700272 if (gr_pixel_format() == PixelFormat::ABGR || gr_pixel_format() == PixelFormat::BGRA) {
273 png_set_bgr(png_ptr);
274 }
Tony Kuofd778e32015-02-05 21:25:56 +0800275
Tianjie Xuacba38c2017-09-27 17:53:34 -0700276 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700277 std::vector<uint8_t> p_row(width * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700278 png_read_row(png_ptr, p_row.data(), nullptr);
279 int frame = y % *frames;
Tao Bao44820ac2018-10-30 23:34:50 -0700280 uint8_t* out_row = surface[frame]->data() + (y / *frames) * surface[frame]->row_bytes;
281 TransformRgbToDraw(p_row.data(), out_row, png_handler.channels(), width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700282 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700283
Tianjie Xuacba38c2017-09-27 17:53:34 -0700284 *pSurface = surface;
Doug Zongker469954f2014-03-07 09:21:25 -0800285
286exit:
Tianjie Xuacba38c2017-09-27 17:53:34 -0700287 if (result < 0) {
288 if (surface) {
289 for (int i = 0; i < *frames; ++i) {
290 free(surface[i]);
291 }
292 free(surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800293 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700294 }
295 return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800296}
297
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700298int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700299 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700300
Tianjie Xuacba38c2017-09-27 17:53:34 -0700301 PngHandler png_handler(name);
302 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700303
Tianjie Xuacba38c2017-09-27 17:53:34 -0700304 if (png_handler.channels() != 1) {
305 return -7;
306 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700307
Tianjie Xuacba38c2017-09-27 17:53:34 -0700308 png_structp png_ptr = png_handler.png_ptr();
309 png_uint_32 width = png_handler.width();
310 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700311
Tao Bao44820ac2018-10-30 23:34:50 -0700312 auto surface = GRSurface::Create(width, height, width, 1, width * height);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700313 if (!surface) {
314 return -8;
315 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700316
Tao Baoed876a72018-07-31 21:32:50 -0700317 PixelFormat pixel_format = gr_pixel_format();
318 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
319 png_set_bgr(png_ptr);
320 }
Tony Kuofd778e32015-02-05 21:25:56 +0800321
Tianjie Xuacba38c2017-09-27 17:53:34 -0700322 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700323 uint8_t* p_row = surface->data() + y * surface->row_bytes;
Tianjie Xuacba38c2017-09-27 17:53:34 -0700324 png_read_row(png_ptr, p_row, nullptr);
325 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700326
Tao Bao92bdb5a2018-10-21 12:12:37 -0700327 *pSurface = surface.release();
Doug Zongkera418aa72014-03-17 12:10:02 -0700328
Tianjie Xuacba38c2017-09-27 17:53:34 -0700329 return 0;
Doug Zongkera418aa72014-03-17 12:10:02 -0700330}
331
Tao Bao6cd81682018-05-03 21:53:11 -0700332void res_set_resource_dir(const std::string& dirname) {
333 g_resource_dir = dirname;
334}
335
Tianjie Xu2430e292016-04-19 15:02:41 -0700336// This function tests if a locale string stored in PNG (prefix) matches
337// the locale string provided by the system (locale).
Tianjie Xu2078b222017-03-22 12:27:26 -0700338bool matches_locale(const std::string& prefix, const std::string& locale) {
339 // According to the BCP 47 format, A locale string may consists of:
340 // language-{extlang}-{script}-{region}-{variant}
341 // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
342 // android's system locale can have the format language-{script}-{region}.
Doug Zongker02ec6b82012-08-22 17:26:40 -0700343
Tianjie Xu2078b222017-03-22 12:27:26 -0700344 // Return true if the whole string of prefix matches the top part of locale. Otherwise try to
345 // match the locale string without the {script} section.
346 // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
347 // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
Elliott Hughes1d65c952017-12-20 12:36:31 -0800348 if (android::base::StartsWith(locale, prefix)) {
Tianjie Xu2078b222017-03-22 12:27:26 -0700349 return true;
350 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700351
Tianjie Xu2078b222017-03-22 12:27:26 -0700352 size_t separator = prefix.find('-');
353 if (separator == std::string::npos) {
354 return false;
355 }
356 std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator));
357 return std::regex_match(locale, loc_regex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700358}
359
Tianjie Xu29d55752017-09-20 17:53:46 -0700360std::vector<std::string> get_locales_in_png(const std::string& png_name) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700361 PngHandler png_handler(png_name);
362 if (!png_handler) {
363 printf("Failed to open %s, error: %d\n", png_name.c_str(), png_handler.error_code());
Tianjie Xu29d55752017-09-20 17:53:46 -0700364 return {};
365 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700366 if (png_handler.channels() != 1) {
367 printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels());
Tianjie Xu29d55752017-09-20 17:53:46 -0700368 return {};
369 }
370
371 std::vector<std::string> result;
Tao Bao44820ac2018-10-30 23:34:50 -0700372 std::vector<uint8_t> row(png_handler.width());
Tianjie Xuacba38c2017-09-27 17:53:34 -0700373 for (png_uint_32 y = 0; y < png_handler.height(); ++y) {
374 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700375 int h = (row[3] << 8) | row[2];
376 std::string loc(reinterpret_cast<char*>(&row[5]));
377 if (!loc.empty()) {
378 result.push_back(loc);
379 }
380 for (int i = 0; i < h; ++i, ++y) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700381 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700382 }
383 }
384
Tianjie Xu29d55752017-09-20 17:53:46 -0700385 return result;
386}
387
Doug Zongkera418aa72014-03-17 12:10:02 -0700388int res_create_localized_alpha_surface(const char* name,
389 const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700390 GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700391 *pSurface = nullptr;
392 if (locale == nullptr) {
393 return 0;
394 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700395
Tianjie Xuacba38c2017-09-27 17:53:34 -0700396 PngHandler png_handler(name);
397 if (!png_handler) return png_handler.error_code();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700398
Tianjie Xuacba38c2017-09-27 17:53:34 -0700399 if (png_handler.channels() != 1) {
400 return -7;
401 }
402
403 png_structp png_ptr = png_handler.png_ptr();
404 png_uint_32 width = png_handler.width();
405 png_uint_32 height = png_handler.height();
406
407 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700408 std::vector<uint8_t> row(width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700409 png_read_row(png_ptr, row.data(), nullptr);
410 int w = (row[1] << 8) | row[0];
411 int h = (row[3] << 8) | row[2];
412 __unused int len = row[4];
413 char* loc = reinterpret_cast<char*>(&row[5]);
414
415 if (y + 1 + h >= height || matches_locale(loc, locale)) {
416 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
417
Tao Bao44820ac2018-10-30 23:34:50 -0700418 auto surface = GRSurface::Create(w, h, w, 1, w * h);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700419 if (!surface) {
420 return -8;
421 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700422
423 for (int i = 0; i < h; ++i, ++y) {
424 png_read_row(png_ptr, row.data(), nullptr);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700425 memcpy(surface->data() + i * w, row.data(), w);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700426 }
427
Tao Bao92bdb5a2018-10-21 12:12:37 -0700428 *pSurface = surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700429 break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700430 }
431
Tianjie Xuacba38c2017-09-27 17:53:34 -0700432 for (int i = 0; i < h; ++i, ++y) {
433 png_read_row(png_ptr, row.data(), nullptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700434 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700435 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700436
Tianjie Xuacba38c2017-09-27 17:53:34 -0700437 return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700438}
439
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700440void res_free_surface(GRSurface* surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700441 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800442}