blob: b20c00abd337045dfd9f545db5362c35e834e75e [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
John Reck41329c52013-08-13 13:01:29 -070096 int color_type, bit_depth;
97 size_t width, height;
98 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
99 &color_type, NULL, NULL, NULL);
100
101 int channels = png_get_channels(png_ptr, info_ptr);
102
Doug Zongkerd93a2542009-10-08 16:32:58 -0700103 if (!(bit_depth == 8 &&
104 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
105 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
Doug Zongker55a36ac2013-03-04 15:49:02 -0800106 (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE ||
107 color_type == PNG_COLOR_TYPE_GRAY))))) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700108 return -7;
109 goto exit;
110 }
111
Doug Zongker55a36ac2013-03-04 15:49:02 -0800112 size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width;
113 size_t pixelSize = stride * height;
114
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800115 surface = malloc(sizeof(GGLSurface) + pixelSize);
116 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700117 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 goto exit;
119 }
120 unsigned char* pData = (unsigned char*) (surface + 1);
121 surface->version = sizeof(GGLSurface);
122 surface->width = width;
123 surface->height = height;
124 surface->stride = width; /* Yes, pixels, not bytes */
125 surface->data = pData;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126
Doug Zongker58207b82013-09-25 16:41:07 -0700127 if (channels == 3) {
128 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
129 } else if (color_type == PNG_COLOR_TYPE_PALETTE) {
130 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
131 } else if (channels == 1) {
132 surface->format = GGL_PIXEL_FORMAT_L_8;
133 } else {
134 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
135 }
136
137 int alpha = (channels == 4);
Doug Zongkerd93a2542009-10-08 16:32:58 -0700138 if (color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongker68189f22011-03-04 16:28:48 -0800139 png_set_palette_to_rgb(png_ptr);
140 }
141 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
142 png_set_tRNS_to_alpha(png_ptr);
143 alpha = 1;
Doug Zongkerd93a2542009-10-08 16:32:58 -0700144 }
Doug Zongker55a36ac2013-03-04 15:49:02 -0800145 if (color_type == PNG_COLOR_TYPE_GRAY) {
146 alpha = 1;
147 }
Doug Zongkerd93a2542009-10-08 16:32:58 -0700148
Edwin Vaneedc5d172012-07-27 11:32:23 -0400149 unsigned int y;
Doug Zongker68189f22011-03-04 16:28:48 -0800150 if (channels == 3 || (channels == 1 && !alpha)) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700151 for (y = 0; y < height; ++y) {
152 unsigned char* pRow = pData + y * stride;
153 png_read_row(png_ptr, pRow, NULL);
154
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 int x;
156 for(x = width - 1; x >= 0; x--) {
157 int sx = x * 3;
158 int dx = x * 4;
Doug Zongker19faefa2009-03-27 17:06:24 -0700159 unsigned char r = pRow[sx];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160 unsigned char g = pRow[sx + 1];
Doug Zongker19faefa2009-03-27 17:06:24 -0700161 unsigned char b = pRow[sx + 2];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162 unsigned char a = 0xff;
163 pRow[dx ] = r; // r
164 pRow[dx + 1] = g; // g
Doug Zongker19faefa2009-03-27 17:06:24 -0700165 pRow[dx + 2] = b; // b
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166 pRow[dx + 3] = a;
167 }
168 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700169 } else {
170 for (y = 0; y < height; ++y) {
171 unsigned char* pRow = pData + y * stride;
172 png_read_row(png_ptr, pRow, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173 }
174 }
Doug Zongker19faefa2009-03-27 17:06:24 -0700175
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176 *pSurface = (gr_surface) surface;
177
178exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700179 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
180
181 if (fp != NULL) {
182 fclose(fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183 }
184 if (result < 0) {
185 if (surface) {
186 free(surface);
187 }
188 }
189 return result;
190}
191
Doug Zongker02ec6b82012-08-22 17:26:40 -0700192static int matches_locale(const char* loc) {
193 if (locale == NULL) return 0;
194
Doug Zongker02ec6b82012-08-22 17:26:40 -0700195 if (strcmp(loc, locale) == 0) return 1;
196
197 // if loc does *not* have an underscore, and it matches the start
198 // of locale, and the next character in locale *is* an underscore,
199 // that's a match. For instance, loc == "en" matches locale ==
200 // "en_US".
201
202 int i;
203 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
204 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700205
206 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
207}
208
209int res_create_localized_surface(const char* name, gr_surface* pSurface) {
210 char resPath[256];
211 GGLSurface* surface = NULL;
212 int result = 0;
213 unsigned char header[8];
214 png_structp png_ptr = NULL;
215 png_infop info_ptr = NULL;
216
217 *pSurface = NULL;
218
219 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
220 resPath[sizeof(resPath)-1] = '\0';
221 FILE* fp = fopen(resPath, "rb");
222 if (fp == NULL) {
223 result = -1;
224 goto exit;
225 }
226
227 size_t bytesRead = fread(header, 1, sizeof(header), fp);
228 if (bytesRead != sizeof(header)) {
229 result = -2;
230 goto exit;
231 }
232
233 if (png_sig_cmp(header, 0, sizeof(header))) {
234 result = -3;
235 goto exit;
236 }
237
238 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
239 if (!png_ptr) {
240 result = -4;
241 goto exit;
242 }
243
244 info_ptr = png_create_info_struct(png_ptr);
245 if (!info_ptr) {
246 result = -5;
247 goto exit;
248 }
249
250 if (setjmp(png_jmpbuf(png_ptr))) {
251 result = -6;
252 goto exit;
253 }
254
255 png_init_io(png_ptr, fp);
256 png_set_sig_bytes(png_ptr, sizeof(header));
257 png_read_info(png_ptr, info_ptr);
258
John Reck41329c52013-08-13 13:01:29 -0700259 int color_type, bit_depth;
260 size_t width, height;
261 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
262 &color_type, NULL, NULL, NULL);
263 int channels = png_get_channels(png_ptr, info_ptr);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700264
265 if (!(bit_depth == 8 &&
266 (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
267 return -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700268 goto exit;
269 }
270
271 unsigned char* row = malloc(width);
272 int y;
273 for (y = 0; y < height; ++y) {
274 png_read_row(png_ptr, row, NULL);
275 int w = (row[1] << 8) | row[0];
276 int h = (row[3] << 8) | row[2];
277 int len = row[4];
278 char* loc = row+5;
279
Doug Zongker02ec6b82012-08-22 17:26:40 -0700280 if (y+1+h >= height || matches_locale(loc)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700281 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
282
Doug Zongker02ec6b82012-08-22 17:26:40 -0700283 surface = malloc(sizeof(GGLSurface));
284 if (surface == NULL) {
285 result = -8;
286 goto exit;
287 }
288 unsigned char* pData = malloc(w*h);
289
290 surface->version = sizeof(GGLSurface);
291 surface->width = w;
292 surface->height = h;
293 surface->stride = w; /* Yes, pixels, not bytes */
294 surface->data = pData;
295 surface->format = GGL_PIXEL_FORMAT_A_8;
296
297 int i;
298 for (i = 0; i < h; ++i, ++y) {
299 png_read_row(png_ptr, row, NULL);
300 memcpy(pData + i*w, row, w);
301 }
302
303 *pSurface = (gr_surface) surface;
304 break;
305 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700306 int i;
307 for (i = 0; i < h; ++i, ++y) {
308 png_read_row(png_ptr, row, NULL);
309 }
310 }
311 }
312
313exit:
314 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
315
316 if (fp != NULL) {
317 fclose(fp);
318 }
319 if (result < 0) {
320 if (surface) {
321 free(surface);
322 }
323 }
324 return result;
325}
326
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800327void res_free_surface(gr_surface surface) {
328 GGLSurface* pSurface = (GGLSurface*) surface;
329 if (pSurface) {
330 free(pSurface);
331 }
332}