blob: f645c4b675595fd04d98b2567f35ec318f713e20 [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>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080018#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <unistd.h>
20
21#include <fcntl.h>
22#include <stdio.h>
23
24#include <sys/ioctl.h>
25#include <sys/mman.h>
26#include <sys/types.h>
27
28#include <linux/fb.h>
29#include <linux/kd.h>
30
Doug Zongker19faefa2009-03-27 17:06:24 -070031#include <png.h>
32
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "minui.h"
34
Doug Zongker02ec6b82012-08-22 17:26:40 -070035extern char* locale;
36
Doug Zongker16f97c32014-03-06 16:16:05 -080037#define SURFACE_DATA_ALIGNMENT 8
38
39static gr_surface malloc_surface(size_t data_size) {
40 unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT);
41 if (temp == NULL) return NULL;
42 gr_surface surface = (gr_surface) temp;
43 surface->data = temp + sizeof(GRSurface) +
44 (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
45 return surface;
Doug Zongker19faefa2009-03-27 17:06:24 -070046}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Doug Zongkera418aa72014-03-17 12:10:02 -070048static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
49 png_uint_32* width, png_uint_32* height, png_byte* channels) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050 char resPath[256];
Doug Zongker19faefa2009-03-27 17:06:24 -070051 unsigned char header[8];
Doug Zongkera418aa72014-03-17 12:10:02 -070052 int result = 0;
Doug Zongker6809c512011-03-01 14:04:34 -080053
Doug Zongker19faefa2009-03-27 17:06:24 -070054 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055 resPath[sizeof(resPath)-1] = '\0';
Doug Zongker19faefa2009-03-27 17:06:24 -070056 FILE* fp = fopen(resPath, "rb");
57 if (fp == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 result = -1;
59 goto exit;
60 }
Doug Zongker19faefa2009-03-27 17:06:24 -070061
62 size_t bytesRead = fread(header, 1, sizeof(header), fp);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063 if (bytesRead != sizeof(header)) {
64 result = -2;
65 goto exit;
66 }
Doug Zongker19faefa2009-03-27 17:06:24 -070067
68 if (png_sig_cmp(header, 0, sizeof(header))) {
69 result = -3;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070 goto exit;
71 }
Doug Zongker19faefa2009-03-27 17:06:24 -070072
Doug Zongkera418aa72014-03-17 12:10:02 -070073 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
74 if (!*png_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 result = -4;
76 goto exit;
77 }
Doug Zongker19faefa2009-03-27 17:06:24 -070078
Doug Zongkera418aa72014-03-17 12:10:02 -070079 *info_ptr = png_create_info_struct(*png_ptr);
80 if (!*info_ptr) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 result = -5;
82 goto exit;
83 }
Doug Zongker19faefa2009-03-27 17:06:24 -070084
Doug Zongkera418aa72014-03-17 12:10:02 -070085 if (setjmp(png_jmpbuf(*png_ptr))) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086 result = -6;
87 goto exit;
88 }
Doug Zongker19faefa2009-03-27 17:06:24 -070089
Doug Zongkera418aa72014-03-17 12:10:02 -070090 png_init_io(*png_ptr, fp);
91 png_set_sig_bytes(*png_ptr, sizeof(header));
92 png_read_info(*png_ptr, *info_ptr);
Doug Zongker19faefa2009-03-27 17:06:24 -070093
John Reck94fd07b2013-08-26 16:45:33 -070094 int color_type, bit_depth;
Doug Zongkera418aa72014-03-17 12:10:02 -070095 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
John Reck94fd07b2013-08-26 16:45:33 -070096 &color_type, NULL, NULL, NULL);
97
Doug Zongkera418aa72014-03-17 12:10:02 -070098 *channels = png_get_channels(*png_ptr, *info_ptr);
John Reck94fd07b2013-08-26 16:45:33 -070099
Doug Zongkera418aa72014-03-17 12:10:02 -0700100 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
101 // 8-bit RGB images: great, nothing to do.
102 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
103 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
104 png_set_expand_gray_1_2_4_to_8(*png_ptr);
Doug Zongker577a1302014-03-20 08:27:01 -0700105 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700106 // paletted images: expand to 8-bit RGB. Note that we DON'T
107 // currently expand the tRNS chunk (if any) to an alpha
108 // channel, because minui doesn't support alpha channels in
109 // general.
110 png_set_palette_to_rgb(*png_ptr);
111 *channels = 3;
112 } else {
113 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
Doug Zongkera388a762014-03-17 16:51:47 -0700114 bit_depth, *channels, color_type);
Doug Zongkera418aa72014-03-17 12:10:02 -0700115 result = -7;
Doug Zongker19faefa2009-03-27 17:06:24 -0700116 goto exit;
117 }
118
Doug Zongkera418aa72014-03-17 12:10:02 -0700119 return result;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800120
Doug Zongkera418aa72014-03-17 12:10:02 -0700121 exit:
122 if (result < 0) {
123 png_destroy_read_struct(png_ptr, info_ptr, NULL);
124 }
125 if (fp != NULL) {
126 fclose(fp);
127 }
128
129 return result;
130}
131
132// "display" surfaces are transformed into the framebuffer's required
133// pixel format (currently only RGBX is supported) at load time, so
134// gr_blit() can be nothing more than a memcpy() for each row. The
135// next two functions are the only ones that know anything about the
136// framebuffer pixel format; they need to be modified if the
137// framebuffer format changes (but nothing else should).
138
139// Allocate and return a gr_surface sufficient for storing an image of
140// the indicated size in the framebuffer pixel format.
141static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) {
142 gr_surface surface;
143
144 surface = malloc_surface(width * height * 4);
145 if (surface == NULL) return NULL;
146
147 surface->width = width;
148 surface->height = height;
149 surface->row_bytes = width * 4;
150 surface->pixel_bytes = 4;
151
152 return surface;
153}
154
155// Copy 'input_row' to 'output_row', transforming it to the
156// framebuffer pixel format. The input format depends on the value of
157// 'channels':
158//
159// 1 - input is 8-bit grayscale
160// 3 - input is 24-bit RGB
161// 4 - input is 32-bit RGBA/RGBX
162//
163// 'width' is the number of pixels in the row.
164static void transform_rgb_to_draw(unsigned char* input_row,
165 unsigned char* output_row,
166 int channels, int width) {
167 int x;
168 unsigned char* ip = input_row;
169 unsigned char* op = output_row;
170
171 switch (channels) {
172 case 1:
173 // expand gray level to RGBX
174 for (x = 0; x < width; ++x) {
175 *op++ = *ip;
176 *op++ = *ip;
177 *op++ = *ip;
178 *op++ = 0xff;
179 ip++;
180 }
181 break;
182
183 case 3:
184 // expand RGBA to RGBX
185 for (x = 0; x < width; ++x) {
186 *op++ = *ip++;
187 *op++ = *ip++;
188 *op++ = *ip++;
189 *op++ = 0xff;
190 }
191 break;
192
193 case 4:
194 // copy RGBA to RGBX
195 memcpy(output_row, input_row, width*4);
196 break;
197 }
198}
199
200int res_create_display_surface(const char* name, gr_surface* pSurface) {
201 gr_surface surface = NULL;
202 int result = 0;
203 png_structp png_ptr = NULL;
204 png_infop info_ptr = NULL;
205 png_uint_32 width, height;
206 png_byte channels;
207
208 *pSurface = NULL;
209
210 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
211 if (result < 0) return result;
212
213 surface = init_display_surface(width, height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214 if (surface == NULL) {
Doug Zongker19faefa2009-03-27 17:06:24 -0700215 result = -8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 goto exit;
217 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218
Doug Zongkera418aa72014-03-17 12:10:02 -0700219 unsigned char* p_row = malloc(width * 4);
Edwin Vaneedc5d172012-07-27 11:32:23 -0400220 unsigned int y;
Doug Zongkera418aa72014-03-17 12:10:02 -0700221 for (y = 0; y < height; ++y) {
222 png_read_row(png_ptr, p_row, NULL);
223 transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224 }
Doug Zongkera418aa72014-03-17 12:10:02 -0700225 free(p_row);
Doug Zongker19faefa2009-03-27 17:06:24 -0700226
Doug Zongkera418aa72014-03-17 12:10:02 -0700227 *pSurface = surface;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800228
Doug Zongkera418aa72014-03-17 12:10:02 -0700229 exit:
Doug Zongker19faefa2009-03-27 17:06:24 -0700230 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700231 if (result < 0 && surface != NULL) free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232 return result;
233}
234
Doug Zongkera418aa72014-03-17 12:10:02 -0700235int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) {
236 gr_surface* surface = NULL;
Doug Zongker469954f2014-03-07 09:21:25 -0800237 int result = 0;
Doug Zongker469954f2014-03-07 09:21:25 -0800238 png_structp png_ptr = NULL;
239 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700240 png_uint_32 width, height;
241 png_byte channels;
Doug Zongker469954f2014-03-07 09:21:25 -0800242 int i;
Doug Zongker469954f2014-03-07 09:21:25 -0800243
244 *pSurface = NULL;
245 *frames = -1;
246
Doug Zongkera418aa72014-03-17 12:10:02 -0700247 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
248 if (result < 0) return result;
Doug Zongker469954f2014-03-07 09:21:25 -0800249
250 *frames = 1;
251 png_textp text;
252 int num_text;
253 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
254 for (i = 0; i < num_text; ++i) {
255 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
256 *frames = atoi(text[i].text);
257 break;
258 }
259 }
260 printf(" found frames = %d\n", *frames);
261 }
262
263 if (height % *frames != 0) {
264 printf("bad height (%d) for frame count (%d)\n", height, *frames);
265 result = -9;
266 goto exit;
267 }
268
Doug Zongker16f97c32014-03-06 16:16:05 -0800269 surface = malloc(*frames * sizeof(gr_surface));
Doug Zongker469954f2014-03-07 09:21:25 -0800270 if (surface == NULL) {
271 result = -8;
272 goto exit;
273 }
274 for (i = 0; i < *frames; ++i) {
Doug Zongkera418aa72014-03-17 12:10:02 -0700275 surface[i] = init_display_surface(width, height / *frames);
276 if (surface[i] == NULL) {
277 result = -8;
278 goto exit;
Doug Zongker469954f2014-03-07 09:21:25 -0800279 }
280 }
281
Doug Zongkera418aa72014-03-17 12:10:02 -0700282 unsigned char* p_row = malloc(width * 4);
283 unsigned int y;
284 for (y = 0; y < height; ++y) {
285 png_read_row(png_ptr, p_row, NULL);
286 int frame = y % *frames;
287 unsigned char* out_row = surface[frame]->data +
288 (y / *frames) * surface[frame]->row_bytes;
289 transform_rgb_to_draw(p_row, out_row, channels, width);
290 }
291 free(p_row);
292
Doug Zongker469954f2014-03-07 09:21:25 -0800293 *pSurface = (gr_surface*) surface;
294
295exit:
296 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
297
Doug Zongker469954f2014-03-07 09:21:25 -0800298 if (result < 0) {
299 if (surface) {
300 for (i = 0; i < *frames; ++i) {
301 if (surface[i]) free(surface[i]);
302 }
303 free(surface);
304 }
305 }
306 return result;
307}
308
Doug Zongkera418aa72014-03-17 12:10:02 -0700309int res_create_alpha_surface(const char* name, gr_surface* pSurface) {
310 gr_surface surface = NULL;
311 int result = 0;
312 png_structp png_ptr = NULL;
313 png_infop info_ptr = NULL;
314 png_uint_32 width, height;
315 png_byte channels;
316
317 *pSurface = NULL;
318
319 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
320 if (result < 0) return result;
321
322 if (channels != 1) {
323 result = -7;
324 goto exit;
325 }
326
327 surface = malloc_surface(width * height);
328 if (surface == NULL) {
329 result = -8;
330 goto exit;
331 }
332 surface->width = width;
333 surface->height = height;
334 surface->row_bytes = width;
335 surface->pixel_bytes = 1;
336
337 unsigned char* p_row;
338 unsigned int y;
339 for (y = 0; y < height; ++y) {
340 p_row = surface->data + y * surface->row_bytes;
341 png_read_row(png_ptr, p_row, NULL);
342 }
343
344 *pSurface = surface;
345
346 exit:
347 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
348 if (result < 0 && surface != NULL) free(surface);
349 return result;
350}
351
352static int matches_locale(const char* loc, const char* locale) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700353 if (locale == NULL) return 0;
354
Doug Zongker02ec6b82012-08-22 17:26:40 -0700355 if (strcmp(loc, locale) == 0) return 1;
356
357 // if loc does *not* have an underscore, and it matches the start
358 // of locale, and the next character in locale *is* an underscore,
359 // that's a match. For instance, loc == "en" matches locale ==
360 // "en_US".
361
362 int i;
363 for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
364 if (loc[i] == '_') return 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700365
366 return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
367}
368
Doug Zongkera418aa72014-03-17 12:10:02 -0700369int res_create_localized_alpha_surface(const char* name,
370 const char* locale,
371 gr_surface* pSurface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800372 gr_surface surface = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700373 int result = 0;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700374 png_structp png_ptr = NULL;
375 png_infop info_ptr = NULL;
Doug Zongkera418aa72014-03-17 12:10:02 -0700376 png_uint_32 width, height;
377 png_byte channels;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700378
379 *pSurface = NULL;
380
Doug Zongkera418aa72014-03-17 12:10:02 -0700381 if (locale == NULL) {
382 surface = malloc_surface(0);
383 surface->width = 0;
384 surface->height = 0;
385 surface->row_bytes = 0;
386 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700387 goto exit;
388 }
389
Doug Zongkera418aa72014-03-17 12:10:02 -0700390 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
391 if (result < 0) return result;
392
393 if (channels != 1) {
394 result = -7;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700395 goto exit;
396 }
397
Doug Zongker02ec6b82012-08-22 17:26:40 -0700398 unsigned char* row = malloc(width);
Doug Zongker469954f2014-03-07 09:21:25 -0800399 png_uint_32 y;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700400 for (y = 0; y < height; ++y) {
401 png_read_row(png_ptr, row, NULL);
402 int w = (row[1] << 8) | row[0];
403 int h = (row[3] << 8) | row[2];
404 int len = row[4];
Doug Zongker469954f2014-03-07 09:21:25 -0800405 char* loc = (char*)row+5;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700406
Doug Zongkera418aa72014-03-17 12:10:02 -0700407 if (y+1+h >= height || matches_locale(loc, locale)) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700408 printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
409
Doug Zongker16f97c32014-03-06 16:16:05 -0800410 surface = malloc_surface(w*h);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700411 if (surface == NULL) {
412 result = -8;
413 goto exit;
414 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700415 surface->width = w;
416 surface->height = h;
Doug Zongker16f97c32014-03-06 16:16:05 -0800417 surface->row_bytes = w;
418 surface->pixel_bytes = 1;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700419
420 int i;
421 for (i = 0; i < h; ++i, ++y) {
422 png_read_row(png_ptr, row, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700423 memcpy(surface->data + i*w, row, w);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700424 }
425
426 *pSurface = (gr_surface) surface;
427 break;
428 } else {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700429 int i;
430 for (i = 0; i < h; ++i, ++y) {
431 png_read_row(png_ptr, row, NULL);
432 }
433 }
434 }
435
436exit:
437 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
Doug Zongkera418aa72014-03-17 12:10:02 -0700438 if (result < 0 && surface != NULL) free(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700439 return result;
440}
441
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800442void res_free_surface(gr_surface surface) {
Doug Zongker16f97c32014-03-06 16:16:05 -0800443 free(surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800444}