2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2017-01-14 16:46:10 +01:00
|
|
|
#include <linux/fb.h>
|
|
|
|
#include <linux/kd.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <stdio.h>
|
2017-01-14 16:46:10 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
2017-01-14 16:46:10 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-03-22 20:27:26 +01:00
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
2016-02-10 22:47:32 +01:00
|
|
|
#include <vector>
|
2017-01-14 16:46:10 +01:00
|
|
|
|
2017-03-22 20:27:26 +01:00
|
|
|
#include <android-base/strings.h>
|
2009-03-28 01:06:24 +01:00
|
|
|
#include <png.h>
|
|
|
|
|
2017-01-17 06:16:58 +01:00
|
|
|
#include "minui/minui.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2014-03-07 01:16:05 +01:00
|
|
|
#define SURFACE_DATA_ALIGNMENT 8
|
|
|
|
|
2015-04-15 19:58:56 +02:00
|
|
|
static GRSurface* malloc_surface(size_t data_size) {
|
2015-04-10 22:12:05 +02:00
|
|
|
size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT;
|
2016-11-09 22:17:01 +01:00
|
|
|
unsigned char* temp = static_cast<unsigned char*>(malloc(size));
|
2014-03-07 01:16:05 +01:00
|
|
|
if (temp == NULL) return NULL;
|
2015-04-15 19:58:56 +02:00
|
|
|
GRSurface* surface = reinterpret_cast<GRSurface*>(temp);
|
2014-03-07 01:16:05 +01:00
|
|
|
surface->data = temp + sizeof(GRSurface) +
|
|
|
|
(SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
|
|
|
|
return surface;
|
2009-03-28 01:06:24 +01:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
|
|
|
|
png_uint_32* width, png_uint_32* height, png_byte* channels) {
|
2009-03-04 04:28:42 +01:00
|
|
|
char resPath[256];
|
2009-03-28 01:06:24 +01:00
|
|
|
unsigned char header[8];
|
2014-03-17 20:10:02 +01:00
|
|
|
int result = 0;
|
2015-04-10 22:12:05 +02:00
|
|
|
int color_type, bit_depth;
|
|
|
|
size_t bytesRead;
|
2011-03-01 23:04:34 +01:00
|
|
|
|
2009-03-28 01:06:24 +01:00
|
|
|
snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
|
2009-03-04 04:28:42 +01:00
|
|
|
resPath[sizeof(resPath)-1] = '\0';
|
2017-07-11 00:13:33 +02:00
|
|
|
FILE* fp = fopen(resPath, "rbe");
|
2009-03-28 01:06:24 +01:00
|
|
|
if (fp == NULL) {
|
2009-03-04 04:28:42 +01:00
|
|
|
result = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2015-04-10 22:12:05 +02:00
|
|
|
bytesRead = fread(header, 1, sizeof(header), fp);
|
2009-03-04 04:28:42 +01:00
|
|
|
if (bytesRead != sizeof(header)) {
|
|
|
|
result = -2;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-28 01:06:24 +01:00
|
|
|
|
|
|
|
if (png_sig_cmp(header, 0, sizeof(header))) {
|
|
|
|
result = -3;
|
2009-03-04 04:28:42 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
*png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
if (!*png_ptr) {
|
2009-03-04 04:28:42 +01:00
|
|
|
result = -4;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
*info_ptr = png_create_info_struct(*png_ptr);
|
|
|
|
if (!*info_ptr) {
|
2009-03-04 04:28:42 +01:00
|
|
|
result = -5;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
if (setjmp(png_jmpbuf(*png_ptr))) {
|
2009-03-04 04:28:42 +01:00
|
|
|
result = -6;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
png_init_io(*png_ptr, fp);
|
|
|
|
png_set_sig_bytes(*png_ptr, sizeof(header));
|
|
|
|
png_read_info(*png_ptr, *info_ptr);
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
|
2013-08-27 01:45:33 +02:00
|
|
|
&color_type, NULL, NULL, NULL);
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
*channels = png_get_channels(*png_ptr, *info_ptr);
|
|
|
|
|
|
|
|
if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
|
|
|
|
// 8-bit RGB images: great, nothing to do.
|
|
|
|
} else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
|
|
|
|
// 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
|
|
|
|
png_set_expand_gray_1_2_4_to_8(*png_ptr);
|
2014-03-20 16:27:01 +01:00
|
|
|
} else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
|
2014-03-17 20:10:02 +01:00
|
|
|
// paletted images: expand to 8-bit RGB. Note that we DON'T
|
|
|
|
// currently expand the tRNS chunk (if any) to an alpha
|
|
|
|
// channel, because minui doesn't support alpha channels in
|
|
|
|
// general.
|
|
|
|
png_set_palette_to_rgb(*png_ptr);
|
|
|
|
*channels = 3;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
|
2014-03-18 00:51:47 +01:00
|
|
|
bit_depth, *channels, color_type);
|
2014-03-17 20:10:02 +01:00
|
|
|
result = -7;
|
2009-03-28 01:06:24 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
return result;
|
2013-03-05 00:49:02 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
exit:
|
|
|
|
if (result < 0) {
|
|
|
|
png_destroy_read_struct(png_ptr, info_ptr, NULL);
|
|
|
|
}
|
|
|
|
if (fp != NULL) {
|
|
|
|
fclose(fp);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2014-03-17 20:10:02 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "display" surfaces are transformed into the framebuffer's required
|
|
|
|
// pixel format (currently only RGBX is supported) at load time, so
|
|
|
|
// gr_blit() can be nothing more than a memcpy() for each row. The
|
|
|
|
// next two functions are the only ones that know anything about the
|
|
|
|
// framebuffer pixel format; they need to be modified if the
|
|
|
|
// framebuffer format changes (but nothing else should).
|
|
|
|
|
2015-04-15 19:58:56 +02:00
|
|
|
// Allocate and return a GRSurface* sufficient for storing an image of
|
2014-03-17 20:10:02 +01:00
|
|
|
// the indicated size in the framebuffer pixel format.
|
2015-04-15 19:58:56 +02:00
|
|
|
static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
|
|
|
|
GRSurface* surface = malloc_surface(width * height * 4);
|
2014-03-17 20:10:02 +01:00
|
|
|
if (surface == NULL) return NULL;
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
surface->width = width;
|
|
|
|
surface->height = height;
|
2014-03-17 20:10:02 +01:00
|
|
|
surface->row_bytes = width * 4;
|
|
|
|
surface->pixel_bytes = 4;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
return surface;
|
|
|
|
}
|
2009-10-09 01:32:58 +02:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
// Copy 'input_row' to 'output_row', transforming it to the
|
|
|
|
// framebuffer pixel format. The input format depends on the value of
|
|
|
|
// 'channels':
|
|
|
|
//
|
|
|
|
// 1 - input is 8-bit grayscale
|
|
|
|
// 3 - input is 24-bit RGB
|
|
|
|
// 4 - input is 32-bit RGBA/RGBX
|
|
|
|
//
|
|
|
|
// 'width' is the number of pixels in the row.
|
|
|
|
static void transform_rgb_to_draw(unsigned char* input_row,
|
|
|
|
unsigned char* output_row,
|
|
|
|
int channels, int width) {
|
|
|
|
int x;
|
|
|
|
unsigned char* ip = input_row;
|
|
|
|
unsigned char* op = output_row;
|
|
|
|
|
|
|
|
switch (channels) {
|
|
|
|
case 1:
|
|
|
|
// expand gray level to RGBX
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
*op++ = *ip;
|
|
|
|
*op++ = *ip;
|
|
|
|
*op++ = *ip;
|
|
|
|
*op++ = 0xff;
|
|
|
|
ip++;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2014-03-17 20:10:02 +01:00
|
|
|
break;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
case 3:
|
|
|
|
// expand RGBA to RGBX
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
*op++ = *ip++;
|
|
|
|
*op++ = *ip++;
|
|
|
|
*op++ = *ip++;
|
|
|
|
*op++ = 0xff;
|
|
|
|
}
|
|
|
|
break;
|
2009-03-28 01:06:24 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
case 4:
|
|
|
|
// copy RGBA to RGBX
|
|
|
|
memcpy(output_row, input_row, width*4);
|
|
|
|
break;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:58:56 +02:00
|
|
|
int res_create_display_surface(const char* name, GRSurface** pSurface) {
|
|
|
|
GRSurface* surface = NULL;
|
2014-03-07 18:21:25 +01:00
|
|
|
int result = 0;
|
|
|
|
png_structp png_ptr = NULL;
|
|
|
|
png_infop info_ptr = NULL;
|
2014-03-17 20:10:02 +01:00
|
|
|
png_uint_32 width, height;
|
|
|
|
png_byte channels;
|
2015-04-10 22:12:05 +02:00
|
|
|
unsigned char* p_row;
|
|
|
|
unsigned int y;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
|
|
|
*pSurface = NULL;
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
|
|
|
if (result < 0) return result;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
surface = init_display_surface(width, height);
|
|
|
|
if (surface == NULL) {
|
|
|
|
result = -8;
|
2014-03-07 18:21:25 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:25:56 +01:00
|
|
|
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
|
|
|
png_set_bgr(png_ptr);
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 22:17:01 +01:00
|
|
|
p_row = static_cast<unsigned char*>(malloc(width * 4));
|
2014-03-17 20:10:02 +01:00
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
png_read_row(png_ptr, p_row, NULL);
|
|
|
|
transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
|
2014-03-07 18:21:25 +01:00
|
|
|
}
|
2014-03-17 20:10:02 +01:00
|
|
|
free(p_row);
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
*pSurface = surface;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
exit:
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
|
|
if (result < 0 && surface != NULL) free(surface);
|
|
|
|
return result;
|
|
|
|
}
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2015-12-12 00:18:51 +01:00
|
|
|
int res_create_multi_display_surface(const char* name, int* frames, int* fps,
|
|
|
|
GRSurface*** pSurface) {
|
2015-04-15 19:58:56 +02:00
|
|
|
GRSurface** surface = NULL;
|
2014-03-17 20:10:02 +01:00
|
|
|
int result = 0;
|
|
|
|
png_structp png_ptr = NULL;
|
|
|
|
png_infop info_ptr = NULL;
|
2014-03-07 18:21:25 +01:00
|
|
|
png_uint_32 width, height;
|
2014-03-17 20:10:02 +01:00
|
|
|
png_byte channels;
|
2015-04-10 22:12:05 +02:00
|
|
|
png_textp text;
|
|
|
|
int num_text;
|
|
|
|
unsigned char* p_row;
|
|
|
|
unsigned int y;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
*pSurface = NULL;
|
|
|
|
*frames = -1;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
|
|
|
if (result < 0) return result;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
|
|
|
*frames = 1;
|
2015-12-12 00:18:51 +01:00
|
|
|
*fps = 20;
|
2014-03-07 18:21:25 +01:00
|
|
|
if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
|
2015-12-12 00:18:51 +01:00
|
|
|
for (int i = 0; i < num_text; ++i) {
|
2014-03-07 18:21:25 +01:00
|
|
|
if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
|
|
|
|
*frames = atoi(text[i].text);
|
2015-12-12 00:18:51 +01:00
|
|
|
} else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
|
|
|
|
*fps = atoi(text[i].text);
|
2014-03-07 18:21:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" found frames = %d\n", *frames);
|
2015-12-12 00:18:51 +01:00
|
|
|
printf(" found fps = %d\n", *fps);
|
|
|
|
}
|
|
|
|
|
2016-11-11 06:26:06 +01:00
|
|
|
if (*frames <= 0 || *fps <= 0) {
|
2015-12-12 00:18:51 +01:00
|
|
|
printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
|
|
|
|
result = -10;
|
|
|
|
goto exit;
|
2014-03-07 18:21:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (height % *frames != 0) {
|
|
|
|
printf("bad height (%d) for frame count (%d)\n", height, *frames);
|
|
|
|
result = -9;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:17:01 +01:00
|
|
|
surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
|
2014-03-07 18:21:25 +01:00
|
|
|
if (surface == NULL) {
|
|
|
|
result = -8;
|
|
|
|
goto exit;
|
|
|
|
}
|
2015-12-12 00:18:51 +01:00
|
|
|
for (int i = 0; i < *frames; ++i) {
|
2014-03-17 20:10:02 +01:00
|
|
|
surface[i] = init_display_surface(width, height / *frames);
|
|
|
|
if (surface[i] == NULL) {
|
|
|
|
result = -8;
|
|
|
|
goto exit;
|
|
|
|
}
|
2014-03-07 18:21:25 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 14:25:56 +01:00
|
|
|
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
|
|
|
png_set_bgr(png_ptr);
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 22:17:01 +01:00
|
|
|
p_row = static_cast<unsigned char*>(malloc(width * 4));
|
2014-03-17 20:10:02 +01:00
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
png_read_row(png_ptr, p_row, NULL);
|
|
|
|
int frame = y % *frames;
|
|
|
|
unsigned char* out_row = surface[frame]->data +
|
|
|
|
(y / *frames) * surface[frame]->row_bytes;
|
|
|
|
transform_rgb_to_draw(p_row, out_row, channels, width);
|
2014-03-07 18:21:25 +01:00
|
|
|
}
|
2014-03-17 20:10:02 +01:00
|
|
|
free(p_row);
|
2014-03-07 18:21:25 +01:00
|
|
|
|
2016-11-09 01:35:22 +01:00
|
|
|
*pSurface = surface;
|
2014-03-07 18:21:25 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
if (surface) {
|
2015-12-12 00:18:51 +01:00
|
|
|
for (int i = 0; i < *frames; ++i) {
|
2016-02-19 19:33:01 +01:00
|
|
|
free(surface[i]);
|
2014-03-07 18:21:25 +01:00
|
|
|
}
|
|
|
|
free(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:58:56 +02:00
|
|
|
int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
|
|
|
|
GRSurface* surface = NULL;
|
2014-03-17 20:10:02 +01:00
|
|
|
int result = 0;
|
|
|
|
png_structp png_ptr = NULL;
|
|
|
|
png_infop info_ptr = NULL;
|
|
|
|
png_uint_32 width, height;
|
|
|
|
png_byte channels;
|
|
|
|
|
|
|
|
*pSurface = NULL;
|
|
|
|
|
|
|
|
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
|
|
|
if (result < 0) return result;
|
|
|
|
|
|
|
|
if (channels != 1) {
|
|
|
|
result = -7;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
surface = malloc_surface(width * height);
|
|
|
|
if (surface == NULL) {
|
|
|
|
result = -8;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
surface->width = width;
|
|
|
|
surface->height = height;
|
|
|
|
surface->row_bytes = width;
|
|
|
|
surface->pixel_bytes = 1;
|
|
|
|
|
2015-02-05 14:25:56 +01:00
|
|
|
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
|
|
|
png_set_bgr(png_ptr);
|
|
|
|
#endif
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
unsigned char* p_row;
|
|
|
|
unsigned int y;
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
p_row = surface->data + y * surface->row_bytes;
|
|
|
|
png_read_row(png_ptr, p_row, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pSurface = surface;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
|
|
if (result < 0 && surface != NULL) free(surface);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 00:02:41 +02:00
|
|
|
// This function tests if a locale string stored in PNG (prefix) matches
|
|
|
|
// the locale string provided by the system (locale).
|
2017-03-22 20:27:26 +01:00
|
|
|
bool matches_locale(const std::string& prefix, const std::string& locale) {
|
|
|
|
// According to the BCP 47 format, A locale string may consists of:
|
|
|
|
// language-{extlang}-{script}-{region}-{variant}
|
|
|
|
// The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some
|
|
|
|
// android's system locale can have the format language-{script}-{region}.
|
|
|
|
|
|
|
|
// Return true if the whole string of prefix matches the top part of locale. Otherwise try to
|
|
|
|
// match the locale string without the {script} section.
|
|
|
|
// For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale
|
|
|
|
// == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN".
|
|
|
|
if (android::base::StartsWith(locale, prefix.c_str())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t separator = prefix.find('-');
|
|
|
|
if (separator == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator));
|
|
|
|
return std::regex_match(locale, loc_regex);
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 02:53:46 +02:00
|
|
|
std::vector<std::string> get_locales_in_png(const std::string& png_name) {
|
|
|
|
png_structp png_ptr = nullptr;
|
|
|
|
png_infop info_ptr = nullptr;
|
|
|
|
png_uint_32 width, height;
|
|
|
|
png_byte channels;
|
|
|
|
|
|
|
|
int status = open_png(png_name.c_str(), &png_ptr, &info_ptr, &width, &height, &channels);
|
|
|
|
if (status < 0) {
|
|
|
|
printf("Failed to open %s\n", png_name.c_str());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (channels != 1) {
|
|
|
|
printf("Expect input png to have 1 data channel, this file has %d\n", channels);
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::vector<unsigned char> row(width);
|
|
|
|
for (png_uint_32 y = 0; y < height; ++y) {
|
|
|
|
png_read_row(png_ptr, row.data(), nullptr);
|
|
|
|
int h = (row[3] << 8) | row[2];
|
|
|
|
std::string loc(reinterpret_cast<char*>(&row[5]));
|
|
|
|
if (!loc.empty()) {
|
|
|
|
result.push_back(loc);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < h; ++i, ++y) {
|
|
|
|
png_read_row(png_ptr, row.data(), NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
int res_create_localized_alpha_surface(const char* name,
|
|
|
|
const char* locale,
|
2015-04-15 19:58:56 +02:00
|
|
|
GRSurface** pSurface) {
|
|
|
|
GRSurface* surface = NULL;
|
2012-08-23 02:26:40 +02:00
|
|
|
int result = 0;
|
|
|
|
png_structp png_ptr = NULL;
|
|
|
|
png_infop info_ptr = NULL;
|
2014-03-17 20:10:02 +01:00
|
|
|
png_uint_32 width, height;
|
|
|
|
png_byte channels;
|
2015-04-10 22:12:05 +02:00
|
|
|
png_uint_32 y;
|
2016-02-10 22:47:32 +01:00
|
|
|
std::vector<unsigned char> row;
|
2012-08-23 02:26:40 +02:00
|
|
|
|
|
|
|
*pSurface = NULL;
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
if (locale == NULL) {
|
2016-02-10 22:47:32 +01:00
|
|
|
return result;
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
|
|
|
if (result < 0) return result;
|
2012-08-23 02:26:40 +02:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
if (channels != 1) {
|
|
|
|
result = -7;
|
2012-08-23 02:26:40 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:47:32 +01:00
|
|
|
row.resize(width);
|
2012-08-23 02:26:40 +02:00
|
|
|
for (y = 0; y < height; ++y) {
|
2016-02-10 22:47:32 +01:00
|
|
|
png_read_row(png_ptr, row.data(), NULL);
|
2012-08-23 02:26:40 +02:00
|
|
|
int w = (row[1] << 8) | row[0];
|
|
|
|
int h = (row[3] << 8) | row[2];
|
2016-02-19 19:33:01 +01:00
|
|
|
__unused int len = row[4];
|
2016-02-10 22:47:32 +01:00
|
|
|
char* loc = reinterpret_cast<char*>(&row[5]);
|
2012-08-23 02:26:40 +02:00
|
|
|
|
2014-03-17 20:10:02 +01:00
|
|
|
if (y+1+h >= height || matches_locale(loc, locale)) {
|
2012-09-04 23:28:25 +02:00
|
|
|
printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
|
|
|
|
|
2014-03-07 01:16:05 +01:00
|
|
|
surface = malloc_surface(w*h);
|
2012-08-23 02:26:40 +02:00
|
|
|
if (surface == NULL) {
|
|
|
|
result = -8;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
surface->width = w;
|
|
|
|
surface->height = h;
|
2014-03-07 01:16:05 +01:00
|
|
|
surface->row_bytes = w;
|
|
|
|
surface->pixel_bytes = 1;
|
2012-08-23 02:26:40 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < h; ++i, ++y) {
|
2016-02-10 22:47:32 +01:00
|
|
|
png_read_row(png_ptr, row.data(), NULL);
|
|
|
|
memcpy(surface->data + i*w, row.data(), w);
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 01:35:22 +01:00
|
|
|
*pSurface = surface;
|
2012-08-23 02:26:40 +02:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < h; ++i, ++y) {
|
2016-02-10 22:47:32 +01:00
|
|
|
png_read_row(png_ptr, row.data(), NULL);
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
2014-03-17 20:10:02 +01:00
|
|
|
if (result < 0 && surface != NULL) free(surface);
|
2012-08-23 02:26:40 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:58:56 +02:00
|
|
|
void res_free_surface(GRSurface* surface) {
|
2014-03-07 01:16:05 +01:00
|
|
|
free(surface);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|