blob: 72f39fbaa51a29e1cdda3f4c482597ef3203cd85 [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
Doug Zongker19faefa2009-03-27 17:06:24 -070096 int color_type = info_ptr->color_type;
97 int bit_depth = info_ptr->bit_depth;
98 int channels = info_ptr->channels;
Doug Zongkerd93a2542009-10-08 16:32:58 -070099 if (!(bit_depth == 8 &&
100 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
101 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
Doug Zongker55a36ac2013-03-04 15:49:02 -0800102 (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE ||
103 color_type == PNG_COLOR_TYPE_GRAY))))) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700104 return -7;
105 goto exit;
106 }
107
Doug Zongker55a36ac2013-03-04 15:49:02 -0800108 size_t width = info_ptr->width;
109 size_t height = info_ptr->height;
110 size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
111 size_t pixelSize = stride * height;
112
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 surface = malloc(sizeof(GGLSurface) + pixelSize);
114 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700115 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 goto exit;
117 }
118 unsigned char* pData = (unsigned char*) (surface + 1);
119 surface->version = sizeof(GGLSurface);
120 surface->width = width;
121 surface->height = height;
122 surface->stride = width; /* Yes, pixels, not bytes */
123 surface->data = pData;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800124 surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 :
125 ((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126
Doug Zongker68189f22011-03-04 16:28:48 -0800127 int alpha = 0;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700128 if (color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongker68189f22011-03-04 16:28:48 -0800129 png_set_palette_to_rgb(png_ptr);
130 }
131 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
132 png_set_tRNS_to_alpha(png_ptr);
133 alpha = 1;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700134 }
Doug Zongker55a36ac2013-03-04 15:49:02 -0800135 if (color_type == PNG_COLOR_TYPE_GRAY) {
136 alpha = 1;
137 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700138
Edwin Vaneedc5d172012-07-27 11:32:23 -0400139 unsigned int y;
Doug Zongker68189f22011-03-04 16:28:48 -0800140 if (channels == 3 || (channels == 1 && !alpha)) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700141 for (y = 0; y < height; ++y) {
142 unsigned char* pRow = pData + y * stride;
143 png_read_row(png_ptr, pRow, NULL);
144
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145 int x;
146 for(x = width - 1; x >= 0; x--) {
147 int sx = x * 3;
148 int dx = x * 4;
Doug Zongker19faefa2009-03-27 17:06:24 -0700149 unsigned char r = pRow[sx];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150 unsigned char g = pRow[sx + 1];
Doug Zongker19faefa2009-03-27 17:06:24 -0700151 unsigned char b = pRow[sx + 2];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 unsigned char a = 0xff;
153 pRow[dx ] = r; // r
154 pRow[dx + 1] = g; // g
Doug Zongker19faefa2009-03-27 17:06:24 -0700155 pRow[dx + 2] = b; // b
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156 pRow[dx + 3] = a;
157 }
158 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700159 } else {
160 for (y = 0; y < height; ++y) {
161 unsigned char* pRow = pData + y * stride;
162 png_read_row(png_ptr, pRow, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163 }
164 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700165
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166 *pSurface = (gr_surface) surface;
167
168exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700169 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
170
171 if (fp != NULL) {
172 fclose(fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173 }
174 if (result < 0) {
175 if (surface) {
176 free(surface);
177 }
178 }
179 return result;
180}
181
Doug Zongker02ec6b82012-08-22 17:26:40 -0700182static int matches_locale(const char* loc) {
183 if (locale == NULL) return 0;
184
Doug Zongker02ec6b82012-08-22 17:26:40 -0700185 if (strcmp(loc, locale) == 0) return 1;
186
187 // if loc does *not* have an underscore, and it matches the start
188 // of locale, and the next character in locale *is* an underscore,
189 // that's a match. For instance, loc == "en" matches locale ==
190 // "en_US".
191
192 int i;
193 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
194 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700195
196 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
197}
198
199int res_create_localized_surface(const char* name, gr_surface* pSurface) {
200 char resPath[256];
201 GGLSurface* surface = NULL;
202 int result = 0;
203 unsigned char header[8];
204 png_structp png_ptr = NULL;
205 png_infop info_ptr = NULL;
206
207 *pSurface = NULL;
208
209 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
210 resPath[sizeof(resPath)-1] = '\0';
211 FILE* fp = fopen(resPath, "rb");
212 if (fp == NULL) {
213 result = -1;
214 goto exit;
215 }
216
217 size_t bytesRead = fread(header, 1, sizeof(header), fp);
218 if (bytesRead != sizeof(header)) {
219 result = -2;
220 goto exit;
221 }
222
223 if (png_sig_cmp(header, 0, sizeof(header))) {
224 result = -3;
225 goto exit;
226 }
227
228 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
229 if (!png_ptr) {
230 result = -4;
231 goto exit;
232 }
233
234 info_ptr = png_create_info_struct(png_ptr);
235 if (!info_ptr) {
236 result = -5;
237 goto exit;
238 }
239
240 if (setjmp(png_jmpbuf(png_ptr))) {
241 result = -6;
242 goto exit;
243 }
244
245 png_init_io(png_ptr, fp);
246 png_set_sig_bytes(png_ptr, sizeof(header));
247 png_read_info(png_ptr, info_ptr);
248
249 size_t width = info_ptr->width;
250 size_t height = info_ptr->height;
251 size_t stride = 4 * width;
252
Doug Zongker02ec6b82012-08-22 17:26:40 -0700253 int color_type = info_ptr->color_type;
254 int bit_depth = info_ptr->bit_depth;
255 int channels = info_ptr->channels;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700256
257 if (!(bit_depth == 8 &&
258 (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
259 return -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700260 goto exit;
261 }
262
263 unsigned char* row = malloc(width);
264 int y;
265 for (y = 0; y < height; ++y) {
266 png_read_row(png_ptr, row, NULL);
267 int w = (row[1] << 8) | row[0];
268 int h = (row[3] << 8) | row[2];
269 int len = row[4];
270 char* loc = row+5;
271
Doug Zongker02ec6b82012-08-22 17:26:40 -0700272 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700273 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
274
Doug Zongker02ec6b82012-08-22 17:26:40 -0700275 surface = malloc(sizeof(GGLSurface));
276 if (surface == NULL) {
277 result = -8;
278 goto exit;
279 }
280 unsigned char* pData = malloc(w*h);
281
282 surface->version = sizeof(GGLSurface);
283 surface->width = w;
284 surface->height = h;
285 surface->stride = w; /* Yes, pixels, not bytes */
286 surface->data = pData;
287 surface->format = GGL_PIXEL_FORMAT_A_8;
288
289 int i;
290 for (i = 0; i < h; ++i, ++y) {
291 png_read_row(png_ptr, row, NULL);
292 memcpy(pData + i*w, row, w);
293 }
294
295 *pSurface = (gr_surface) surface;
296 break;
297 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700298 int i;
299 for (i = 0; i < h; ++i, ++y) {
300 png_read_row(png_ptr, row, NULL);
301 }
302 }
303 }
304
305exit:
306 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
307
308 if (fp != NULL) {
309 fclose(fp);
310 }
311 if (result < 0) {
312 if (surface) {
313 free(surface);
314 }
315 }
316 return result;
317}
318
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800319void res_free_surface(gr_surface surface) {
320 GGLSurface* pSurface = (GGLSurface*) surface;
321 if (pSurface) {
322 free(pSurface);
323 }
324}