blob: 065f4317ef16f1f16f5afd9d487968671c9b03cc [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
17#include <stdlib.h>
18#include <unistd.h>
19
20#include <fcntl.h>
21#include <stdio.h>
22
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25#include <sys/types.h>
26
27#include <linux/fb.h>
28#include <linux/kd.h>
29
30#include <pixelflinger/pixelflinger.h>
31
Doug Zongker19faefa2009-03-27 17:06:24 -070032#include <png.h>
33
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "minui.h"
35
Doug Zongker02ec6b82012-08-22 17:26:40 -070036extern char* locale;
37
Doug Zongker19faefa2009-03-27 17:06:24 -070038// libpng gives "undefined reference to 'pow'" errors, and I have no
39// idea how to convince the build system to link with -lm. We don't
40// need this functionality (it's used for gamma adjustment) so provide
41// a dummy implementation to satisfy the linker.
42double pow(double x, double y) {
Edwin Vaneedc5d172012-07-27 11:32:23 -040043 return x * y;
Doug Zongker19faefa2009-03-27 17:06:24 -070044}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
46int res_create_surface(const char* name, gr_surface* pSurface) {
47 char resPath[256];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048 GGLSurface* surface = NULL;
49 int result = 0;
Doug Zongker19faefa2009-03-27 17:06:24 -070050 unsigned char header[8];
51 png_structp png_ptr = NULL;
52 png_infop info_ptr = NULL;
53
Doug Zongker6809c512011-03-01 14:04:34 -080054 *pSurface = NULL;
55
Doug Zongker19faefa2009-03-27 17:06:24 -070056 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070058 FILE* fp = fopen(resPath, "rb");
59 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 result = -1;
61 goto exit;
62 }
Doug Zongker19faefa2009-03-27 17:06:24 -070063
64 size_t bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065 if (bytesRead != sizeof(header)) {
66 result = -2;
67 goto exit;
68 }
Doug Zongker19faefa2009-03-27 17:06:24 -070069
70 if (png_sig_cmp(header, 0, sizeof(header))) {
71 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 goto exit;
73 }
Doug Zongker19faefa2009-03-27 17:06:24 -070074
75 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
76 if (!png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 result = -4;
78 goto exit;
79 }
Doug Zongker19faefa2009-03-27 17:06:24 -070080
81 info_ptr = png_create_info_struct(png_ptr);
82 if (!info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083 result = -5;
84 goto exit;
85 }
Doug Zongker19faefa2009-03-27 17:06:24 -070086
87 if (setjmp(png_jmpbuf(png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088 result = -6;
89 goto exit;
90 }
Doug Zongker19faefa2009-03-27 17:06:24 -070091
92 png_init_io(png_ptr, fp);
93 png_set_sig_bytes(png_ptr, sizeof(header));
94 png_read_info(png_ptr, info_ptr);
95
96 size_t width = info_ptr->width;
97 size_t height = info_ptr->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098 size_t stride = 4 * width;
99 size_t pixelSize = stride * height;
Doug Zongker19faefa2009-03-27 17:06:24 -0700100
101 int color_type = info_ptr->color_type;
102 int bit_depth = info_ptr->bit_depth;
103 int channels = info_ptr->channels;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700104 if (!(bit_depth == 8 &&
105 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
106 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
107 (channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE)))) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700108 return -7;
109 goto exit;
110 }
111
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 surface = malloc(sizeof(GGLSurface) + pixelSize);
113 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700114 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800115 goto exit;
116 }
117 unsigned char* pData = (unsigned char*) (surface + 1);
118 surface->version = sizeof(GGLSurface);
119 surface->width = width;
120 surface->height = height;
121 surface->stride = width; /* Yes, pixels, not bytes */
122 surface->data = pData;
Doug Zongker19faefa2009-03-27 17:06:24 -0700123 surface->format = (channels == 3) ?
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124 GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888;
125
Doug Zongker68189f22011-03-04 16:28:48 -0800126 int alpha = 0;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700127 if (color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongker68189f22011-03-04 16:28:48 -0800128 png_set_palette_to_rgb(png_ptr);
129 }
130 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
131 png_set_tRNS_to_alpha(png_ptr);
132 alpha = 1;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700133 }
134
Edwin Vaneedc5d172012-07-27 11:32:23 -0400135 unsigned int y;
Doug Zongker68189f22011-03-04 16:28:48 -0800136 if (channels == 3 || (channels == 1 && !alpha)) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700137 for (y = 0; y < height; ++y) {
138 unsigned char* pRow = pData + y * stride;
139 png_read_row(png_ptr, pRow, NULL);
140
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141 int x;
142 for(x = width - 1; x >= 0; x--) {
143 int sx = x * 3;
144 int dx = x * 4;
Doug Zongker19faefa2009-03-27 17:06:24 -0700145 unsigned char r = pRow[sx];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146 unsigned char g = pRow[sx + 1];
Doug Zongker19faefa2009-03-27 17:06:24 -0700147 unsigned char b = pRow[sx + 2];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 unsigned char a = 0xff;
149 pRow[dx ] = r; // r
150 pRow[dx + 1] = g; // g
Doug Zongker19faefa2009-03-27 17:06:24 -0700151 pRow[dx + 2] = b; // b
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 pRow[dx + 3] = a;
153 }
154 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700155 } else {
156 for (y = 0; y < height; ++y) {
157 unsigned char* pRow = pData + y * stride;
158 png_read_row(png_ptr, pRow, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159 }
160 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700161
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162 *pSurface = (gr_surface) surface;
163
164exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700165 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
166
167 if (fp != NULL) {
168 fclose(fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169 }
170 if (result < 0) {
171 if (surface) {
172 free(surface);
173 }
174 }
175 return result;
176}
177
Doug Zongker02ec6b82012-08-22 17:26:40 -0700178static int matches_locale(const char* loc) {
179 if (locale == NULL) return 0;
180
Doug Zongker02ec6b82012-08-22 17:26:40 -0700181 if (strcmp(loc, locale) == 0) return 1;
182
183 // if loc does *not* have an underscore, and it matches the start
184 // of locale, and the next character in locale *is* an underscore,
185 // that's a match. For instance, loc == "en" matches locale ==
186 // "en_US".
187
188 int i;
189 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
190 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700191
192 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
193}
194
195int res_create_localized_surface(const char* name, gr_surface* pSurface) {
196 char resPath[256];
197 GGLSurface* surface = NULL;
198 int result = 0;
199 unsigned char header[8];
200 png_structp png_ptr = NULL;
201 png_infop info_ptr = NULL;
202
203 *pSurface = NULL;
204
205 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
206 resPath[sizeof(resPath)-1] = '\0';
207 FILE* fp = fopen(resPath, "rb");
208 if (fp == NULL) {
209 result = -1;
210 goto exit;
211 }
212
213 size_t bytesRead = fread(header, 1, sizeof(header), fp);
214 if (bytesRead != sizeof(header)) {
215 result = -2;
216 goto exit;
217 }
218
219 if (png_sig_cmp(header, 0, sizeof(header))) {
220 result = -3;
221 goto exit;
222 }
223
224 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
225 if (!png_ptr) {
226 result = -4;
227 goto exit;
228 }
229
230 info_ptr = png_create_info_struct(png_ptr);
231 if (!info_ptr) {
232 result = -5;
233 goto exit;
234 }
235
236 if (setjmp(png_jmpbuf(png_ptr))) {
237 result = -6;
238 goto exit;
239 }
240
241 png_init_io(png_ptr, fp);
242 png_set_sig_bytes(png_ptr, sizeof(header));
243 png_read_info(png_ptr, info_ptr);
244
245 size_t width = info_ptr->width;
246 size_t height = info_ptr->height;
247 size_t stride = 4 * width;
248
Doug Zongker02ec6b82012-08-22 17:26:40 -0700249 int color_type = info_ptr->color_type;
250 int bit_depth = info_ptr->bit_depth;
251 int channels = info_ptr->channels;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700252
253 if (!(bit_depth == 8 &&
254 (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
255 return -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700256 goto exit;
257 }
258
259 unsigned char* row = malloc(width);
260 int y;
261 for (y = 0; y < height; ++y) {
262 png_read_row(png_ptr, row, NULL);
263 int w = (row[1] << 8) | row[0];
264 int h = (row[3] << 8) | row[2];
265 int len = row[4];
266 char* loc = row+5;
267
Doug Zongker02ec6b82012-08-22 17:26:40 -0700268 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700269 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
270
Doug Zongker02ec6b82012-08-22 17:26:40 -0700271 surface = malloc(sizeof(GGLSurface));
272 if (surface == NULL) {
273 result = -8;
274 goto exit;
275 }
276 unsigned char* pData = malloc(w*h);
277
278 surface->version = sizeof(GGLSurface);
279 surface->width = w;
280 surface->height = h;
281 surface->stride = w; /* Yes, pixels, not bytes */
282 surface->data = pData;
283 surface->format = GGL_PIXEL_FORMAT_A_8;
284
285 int i;
286 for (i = 0; i < h; ++i, ++y) {
287 png_read_row(png_ptr, row, NULL);
288 memcpy(pData + i*w, row, w);
289 }
290
291 *pSurface = (gr_surface) surface;
292 break;
293 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700294 int i;
295 for (i = 0; i < h; ++i, ++y) {
296 png_read_row(png_ptr, row, NULL);
297 }
298 }
299 }
300
301exit:
302 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
303
304 if (fp != NULL) {
305 fclose(fp);
306 }
307 if (result < 0) {
308 if (surface) {
309 free(surface);
310 }
311 }
312 return result;
313}
314
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315void res_free_surface(gr_surface surface) {
316 GGLSurface* pSurface = (GGLSurface*) surface;
317 if (pSurface) {
318 free(pSurface);
319 }
320}