blob: 9027bc6682f93a28fcd12d1f8f1e5bd870d900af [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 Bao63b59dc2018-10-31 15:23:04 -070049 result->data_ = static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_));
Tao Bao92bdb5a2018-10-21 12:12:37 -070050 if (result->data_ == nullptr) return nullptr;
51 return result;
52}
53
Tao Bao63b59dc2018-10-31 15:23:04 -070054std::unique_ptr<GRSurface> GRSurface::Clone() const {
55 auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes, data_size_);
56 memcpy(result->data_, data_, data_size_);
57 return result;
58}
59
Tao Bao92bdb5a2018-10-21 12:12:37 -070060GRSurface::~GRSurface() {
61 if (data_ != nullptr) {
62 free(data_);
63 data_ = nullptr;
64 }
Doug Zongker19faefa2009-03-27 17:06:24 -070065}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066
Tao Bao3d0cd1d2018-04-13 13:41:58 -070067PngHandler::PngHandler(const std::string& name) {
Tao Bao6cd81682018-05-03 21:53:11 -070068 std::string res_path = g_resource_dir + "/" + name + ".png";
Tianjie Xuacba38c2017-09-27 17:53:34 -070069 png_fp_.reset(fopen(res_path.c_str(), "rbe"));
Tao Bao3d0cd1d2018-04-13 13:41:58 -070070 // Try to read from |name| if the resource path does not work.
71 if (!png_fp_) {
72 png_fp_.reset(fopen(name.c_str(), "rbe"));
73 }
Tianjie Xuacba38c2017-09-27 17:53:34 -070074 if (!png_fp_) {
75 error_code_ = -1;
76 return;
77 }
Doug Zongker55a36ac2013-03-04 15:49:02 -080078
Tao Bao44820ac2018-10-30 23:34:50 -070079 uint8_t header[8];
Tianjie Xuacba38c2017-09-27 17:53:34 -070080 size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
81 if (bytesRead != sizeof(header)) {
82 error_code_ = -2;
83 return;
84 }
Doug Zongkera418aa72014-03-17 12:10:02 -070085
Tianjie Xuacba38c2017-09-27 17:53:34 -070086 if (png_sig_cmp(header, 0, sizeof(header))) {
87 error_code_ = -3;
88 return;
89 }
90
91 png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
92 if (!png_ptr_) {
93 error_code_ = -4;
94 return;
95 }
96
97 info_ptr_ = png_create_info_struct(png_ptr_);
98 if (!info_ptr_) {
99 error_code_ = -5;
100 return;
101 }
102
103 if (setjmp(png_jmpbuf(png_ptr_))) {
104 error_code_ = -6;
105 return;
106 }
107
108 png_init_io(png_ptr_, png_fp_.get());
109 png_set_sig_bytes(png_ptr_, sizeof(header));
110 png_read_info(png_ptr_, info_ptr_);
111
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700112 png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700113 nullptr);
114
115 channels_ = png_get_channels(png_ptr_, info_ptr_);
116
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700117 if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700118 // 8-bit RGB images: great, nothing to do.
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700119 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700120 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
121 png_set_expand_gray_1_2_4_to_8(png_ptr_);
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700122 } else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700123 // paletted images: expand to 8-bit RGB. Note that we DON'T
124 // currently expand the tRNS chunk (if any) to an alpha
125 // channel, because minui doesn't support alpha channels in
126 // general.
127 png_set_palette_to_rgb(png_ptr_);
128 channels_ = 3;
129 } else {
Tao Bao3d0cd1d2018-04-13 13:41:58 -0700130 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_,
131 channels_, color_type_);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700132 error_code_ = -7;
133 }
134}
135
136PngHandler::~PngHandler() {
137 if (png_ptr_) {
138 png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr);
139 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700140}
141
Tao Bao44820ac2018-10-30 23:34:50 -0700142// "display" surfaces are transformed into the framebuffer's required pixel format (currently only
143// 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 -0700144
Tao Bao44820ac2018-10-30 23:34:50 -0700145// Copies 'input_row' to 'output_row', transforming it to the framebuffer pixel format. The input
146// format depends on the value of 'channels':
Doug Zongkera418aa72014-03-17 12:10:02 -0700147//
148// 1 - input is 8-bit grayscale
149// 3 - input is 24-bit RGB
150// 4 - input is 32-bit RGBA/RGBX
151//
152// 'width' is the number of pixels in the row.
Tao Bao44820ac2018-10-30 23:34:50 -0700153static void TransformRgbToDraw(const uint8_t* input_row, uint8_t* output_row, int channels,
154 int width) {
155 const uint8_t* ip = input_row;
156 uint8_t* op = output_row;
Doug Zongkera418aa72014-03-17 12:10:02 -0700157
Tao Bao44820ac2018-10-30 23:34:50 -0700158 switch (channels) {
159 case 1:
160 // expand gray level to RGBX
161 for (int x = 0; x < width; ++x) {
162 *op++ = *ip;
163 *op++ = *ip;
164 *op++ = *ip;
165 *op++ = 0xff;
166 ip++;
167 }
168 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700169
Tao Bao44820ac2018-10-30 23:34:50 -0700170 case 3:
171 // expand RGBA to RGBX
172 for (int x = 0; x < width; ++x) {
173 *op++ = *ip++;
174 *op++ = *ip++;
175 *op++ = *ip++;
176 *op++ = 0xff;
177 }
178 break;
Doug Zongkera418aa72014-03-17 12:10:02 -0700179
Tao Bao44820ac2018-10-30 23:34:50 -0700180 case 4:
181 // copy RGBA to RGBX
182 memcpy(output_row, input_row, width * 4);
183 break;
184 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700185}
186
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700187int res_create_display_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700188 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700189
Tianjie Xuacba38c2017-09-27 17:53:34 -0700190 PngHandler png_handler(name);
191 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700192
Tianjie Xuacba38c2017-09-27 17:53:34 -0700193 png_structp png_ptr = png_handler.png_ptr();
194 png_uint_32 width = png_handler.width();
195 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700196
Tao Bao44820ac2018-10-30 23:34:50 -0700197 auto surface = GRSurface::Create(width, height, width * 4, 4, width * height * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700198 if (!surface) {
199 return -8;
200 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800201
Tao Baoed876a72018-07-31 21:32:50 -0700202 PixelFormat pixel_format = gr_pixel_format();
203 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
204 png_set_bgr(png_ptr);
205 }
Tony Kuofd778e32015-02-05 21:25:56 +0800206
Tianjie Xuacba38c2017-09-27 17:53:34 -0700207 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700208 std::vector<uint8_t> p_row(width * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700209 png_read_row(png_ptr, p_row.data(), nullptr);
Tao Bao44820ac2018-10-30 23:34:50 -0700210 TransformRgbToDraw(p_row.data(), surface->data() + y * surface->row_bytes,
211 png_handler.channels(), width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700212 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700213
Tao Bao92bdb5a2018-10-21 12:12:37 -0700214 *pSurface = surface.release();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215
Tianjie Xuacba38c2017-09-27 17:53:34 -0700216 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217}
218
Tao Baob723f4f2015-12-11 15:18:51 -0800219int res_create_multi_display_surface(const char* name, int* frames, int* fps,
Tianjie Xuacba38c2017-09-27 17:53:34 -0700220 GRSurface*** pSurface) {
221 *pSurface = nullptr;
222 *frames = -1;
Doug Zongker469954f2014-03-07 09:21:25 -0800223
Tianjie Xuacba38c2017-09-27 17:53:34 -0700224 PngHandler png_handler(name);
225 if (!png_handler) return png_handler.error_code();
Doug Zongker469954f2014-03-07 09:21:25 -0800226
Tianjie Xuacba38c2017-09-27 17:53:34 -0700227 png_structp png_ptr = png_handler.png_ptr();
228 png_uint_32 width = png_handler.width();
229 png_uint_32 height = png_handler.height();
Doug Zongker469954f2014-03-07 09:21:25 -0800230
Tianjie Xuacba38c2017-09-27 17:53:34 -0700231 *frames = 1;
232 *fps = 20;
233 png_textp text;
234 int num_text;
235 if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) {
236 for (int i = 0; i < num_text; ++i) {
237 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
238 *frames = atoi(text[i].text);
239 } else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
240 *fps = atoi(text[i].text);
241 }
Tao Baob723f4f2015-12-11 15:18:51 -0800242 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700243 printf(" found frames = %d\n", *frames);
244 printf(" found fps = %d\n", *fps);
245 }
Tao Baob723f4f2015-12-11 15:18:51 -0800246
Tianjie Xuacba38c2017-09-27 17:53:34 -0700247 int result = 0;
248 GRSurface** surface = nullptr;
249 if (*frames <= 0 || *fps <= 0) {
250 printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
251 result = -10;
252 goto exit;
253 }
Doug Zongker469954f2014-03-07 09:21:25 -0800254
Tianjie Xuacba38c2017-09-27 17:53:34 -0700255 if (height % *frames != 0) {
256 printf("bad height (%d) for frame count (%d)\n", height, *frames);
257 result = -9;
258 goto exit;
259 }
Doug Zongker469954f2014-03-07 09:21:25 -0800260
Tianjie Xuacba38c2017-09-27 17:53:34 -0700261 surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
262 if (!surface) {
263 result = -8;
264 goto exit;
265 }
266 for (int i = 0; i < *frames; ++i) {
Tao Bao44820ac2018-10-30 23:34:50 -0700267 auto height_per_frame = height / *frames;
268 auto created_surface =
269 GRSurface::Create(width, height_per_frame, width * 4, 4, width * height_per_frame);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700270 if (!created_surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700271 result = -8;
272 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800273 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700274 surface[i] = created_surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700275 }
Doug Zongker469954f2014-03-07 09:21:25 -0800276
Tao Baoed876a72018-07-31 21:32:50 -0700277 if (gr_pixel_format() == PixelFormat::ABGR || gr_pixel_format() == PixelFormat::BGRA) {
278 png_set_bgr(png_ptr);
279 }
Tony Kuofd778e32015-02-05 21:25:56 +0800280
Tianjie Xuacba38c2017-09-27 17:53:34 -0700281 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700282 std::vector<uint8_t> p_row(width * 4);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700283 png_read_row(png_ptr, p_row.data(), nullptr);
284 int frame = y % *frames;
Tao Bao44820ac2018-10-30 23:34:50 -0700285 uint8_t* out_row = surface[frame]->data() + (y / *frames) * surface[frame]->row_bytes;
286 TransformRgbToDraw(p_row.data(), out_row, png_handler.channels(), width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700287 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700288
Tianjie Xuacba38c2017-09-27 17:53:34 -0700289 *pSurface = surface;
Doug Zongker469954f2014-03-07 09:21:25 -0800290
291exit:
Tianjie Xuacba38c2017-09-27 17:53:34 -0700292 if (result < 0) {
293 if (surface) {
294 for (int i = 0; i < *frames; ++i) {
295 free(surface[i]);
296 }
297 free(surface);
Doug Zongker469954f2014-03-07 09:21:25 -0800298 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700299 }
300 return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800301}
302
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700303int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700304 *pSurface = nullptr;
Doug Zongkera418aa72014-03-17 12:10:02 -0700305
Tianjie Xuacba38c2017-09-27 17:53:34 -0700306 PngHandler png_handler(name);
307 if (!png_handler) return png_handler.error_code();
Doug Zongkera418aa72014-03-17 12:10:02 -0700308
Tianjie Xuacba38c2017-09-27 17:53:34 -0700309 if (png_handler.channels() != 1) {
310 return -7;
311 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700312
Tianjie Xuacba38c2017-09-27 17:53:34 -0700313 png_structp png_ptr = png_handler.png_ptr();
314 png_uint_32 width = png_handler.width();
315 png_uint_32 height = png_handler.height();
Doug Zongkera418aa72014-03-17 12:10:02 -0700316
Tao Bao44820ac2018-10-30 23:34:50 -0700317 auto surface = GRSurface::Create(width, height, width, 1, width * height);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700318 if (!surface) {
319 return -8;
320 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700321
Tao Baoed876a72018-07-31 21:32:50 -0700322 PixelFormat pixel_format = gr_pixel_format();
323 if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) {
324 png_set_bgr(png_ptr);
325 }
Tony Kuofd778e32015-02-05 21:25:56 +0800326
Tianjie Xuacba38c2017-09-27 17:53:34 -0700327 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700328 uint8_t* p_row = surface->data() + y * surface->row_bytes;
Tianjie Xuacba38c2017-09-27 17:53:34 -0700329 png_read_row(png_ptr, p_row, nullptr);
330 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700331
Tao Bao92bdb5a2018-10-21 12:12:37 -0700332 *pSurface = surface.release();
Doug Zongkera418aa72014-03-17 12:10:02 -0700333
Tianjie Xuacba38c2017-09-27 17:53:34 -0700334 return 0;
Doug Zongkera418aa72014-03-17 12:10:02 -0700335}
336
Tao Bao6cd81682018-05-03 21:53:11 -0700337void res_set_resource_dir(const std::string& dirname) {
338 g_resource_dir = dirname;
339}
340
Tianjie Xu2430e292016-04-19 15:02:41 -0700341// This function tests if a locale string stored in PNG (prefix) matches
342// the locale string provided by the system (locale).
Tianjie Xu2078b222017-03-22 12:27:26 -0700343bool matches_locale(const std::string& prefix, const std::string& locale) {
344 // According to the BCP 47 format, A locale string may consists of:
345 // language-{extlang}-{script}-{region}-{variant}
346 // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
347 // android's system locale can have the format language-{script}-{region}.
Doug Zongker02ec6b82012-08-22 17:26:40 -0700348
Tianjie Xu2078b222017-03-22 12:27:26 -0700349 // Return true if the whole string of prefix matches the top part of locale. Otherwise try to
350 // match the locale string without the {script} section.
351 // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
352 // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
Elliott Hughes1d65c952017-12-20 12:36:31 -0800353 if (android::base::StartsWith(locale, prefix)) {
Tianjie Xu2078b222017-03-22 12:27:26 -0700354 return true;
355 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700356
Tianjie Xu2078b222017-03-22 12:27:26 -0700357 size_t separator = prefix.find('-');
358 if (separator == std::string::npos) {
359 return false;
360 }
361 std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator));
362 return std::regex_match(locale, loc_regex);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700363}
364
Tianjie Xu29d55752017-09-20 17:53:46 -0700365std::vector<std::string> get_locales_in_png(const std::string& png_name) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700366 PngHandler png_handler(png_name);
367 if (!png_handler) {
368 printf("Failed to open %s, error: %d\n", png_name.c_str(), png_handler.error_code());
Tianjie Xu29d55752017-09-20 17:53:46 -0700369 return {};
370 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700371 if (png_handler.channels() != 1) {
372 printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels());
Tianjie Xu29d55752017-09-20 17:53:46 -0700373 return {};
374 }
375
376 std::vector<std::string> result;
Tao Bao44820ac2018-10-30 23:34:50 -0700377 std::vector<uint8_t> row(png_handler.width());
Tianjie Xuacba38c2017-09-27 17:53:34 -0700378 for (png_uint_32 y = 0; y < png_handler.height(); ++y) {
379 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700380 int h = (row[3] << 8) | row[2];
381 std::string loc(reinterpret_cast<char*>(&row[5]));
382 if (!loc.empty()) {
383 result.push_back(loc);
384 }
385 for (int i = 0; i < h; ++i, ++y) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700386 png_read_row(png_handler.png_ptr(), row.data(), nullptr);
Tianjie Xu29d55752017-09-20 17:53:46 -0700387 }
388 }
389
Tianjie Xu29d55752017-09-20 17:53:46 -0700390 return result;
391}
392
Doug Zongkera418aa72014-03-17 12:10:02 -0700393int res_create_localized_alpha_surface(const char* name,
394 const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700395 GRSurface** pSurface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700396 *pSurface = nullptr;
397 if (locale == nullptr) {
398 return 0;
399 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700400
Tianjie Xuacba38c2017-09-27 17:53:34 -0700401 PngHandler png_handler(name);
402 if (!png_handler) return png_handler.error_code();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700403
Tianjie Xuacba38c2017-09-27 17:53:34 -0700404 if (png_handler.channels() != 1) {
405 return -7;
406 }
407
408 png_structp png_ptr = png_handler.png_ptr();
409 png_uint_32 width = png_handler.width();
410 png_uint_32 height = png_handler.height();
411
412 for (png_uint_32 y = 0; y < height; ++y) {
Tao Bao44820ac2018-10-30 23:34:50 -0700413 std::vector<uint8_t> row(width);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700414 png_read_row(png_ptr, row.data(), nullptr);
415 int w = (row[1] << 8) | row[0];
416 int h = (row[3] << 8) | row[2];
417 __unused int len = row[4];
418 char* loc = reinterpret_cast<char*>(&row[5]);
419
420 if (y + 1 + h >= height || matches_locale(loc, locale)) {
421 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
422
Tao Bao44820ac2018-10-30 23:34:50 -0700423 auto surface = GRSurface::Create(w, h, w, 1, w * h);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700424 if (!surface) {
425 return -8;
426 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700427
428 for (int i = 0; i < h; ++i, ++y) {
429 png_read_row(png_ptr, row.data(), nullptr);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700430 memcpy(surface->data() + i * w, row.data(), w);
Tianjie Xuacba38c2017-09-27 17:53:34 -0700431 }
432
Tao Bao92bdb5a2018-10-21 12:12:37 -0700433 *pSurface = surface.release();
Tianjie Xuacba38c2017-09-27 17:53:34 -0700434 break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700435 }
436
Tianjie Xuacba38c2017-09-27 17:53:34 -0700437 for (int i = 0; i < h; ++i, ++y) {
438 png_read_row(png_ptr, row.data(), nullptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700439 }
Tianjie Xuacba38c2017-09-27 17:53:34 -0700440 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700441
Tianjie Xuacba38c2017-09-27 17:53:34 -0700442 return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700443}
444
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700445void res_free_surface(GRSurface* surface) {
Tianjie Xuacba38c2017-09-27 17:53:34 -0700446 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447}