xref: /OK3568_Linux_fs/external/recovery/minui/resources.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
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 #include <string.h>
27 
28 #include <linux/fb.h>
29 #include <linux/kd.h>
30 
31 #include <png.h>
32 
33 #include "minui.h"
34 
35 extern char* locale;
36 
37 #define SURFACE_DATA_ALIGNMENT 8
38 
malloc_surface(size_t data_size)39 static gr_surface malloc_surface(size_t data_size)
40 {
41     unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT);
42     if (temp == NULL) return NULL;
43     gr_surface surface = (gr_surface) temp;
44     surface->data = temp + sizeof(GRSurface) +
45                     (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT));
46     return surface;
47 }
48 
open_png(const char * name,png_structp * png_ptr,png_infop * info_ptr,png_uint_32 * width,png_uint_32 * height,png_byte * channels)49 static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
50                     png_uint_32* width, png_uint_32* height, png_byte* channels)
51 {
52     char resPath[256];
53     unsigned char header[8];
54     int result = 0;
55 
56     snprintf(resPath, sizeof(resPath) - 1, "/res/images/%s.png", name);
57     resPath[sizeof(resPath) - 1] = '\0';
58     FILE* fp = fopen(resPath, "rb");
59     if (fp == NULL) {
60         result = -1;
61         goto exit;
62     }
63 
64     size_t bytesRead = fread(header, 1, sizeof(header), fp);
65     if (bytesRead != sizeof(header)) {
66         result = -2;
67         goto exit;
68     }
69 
70     if (png_sig_cmp(header, 0, sizeof(header))) {
71         result = -3;
72         goto exit;
73     }
74 
75     *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
76     if (!*png_ptr) {
77         result = -4;
78         goto exit;
79     }
80 
81     *info_ptr = png_create_info_struct(*png_ptr);
82     if (!*info_ptr) {
83         result = -5;
84         goto exit;
85     }
86 
87     if (setjmp(png_jmpbuf(*png_ptr))) {
88         result = -6;
89         goto exit;
90     }
91 
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     int color_type, bit_depth;
97     png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
98                  &color_type, NULL, NULL, NULL);
99 
100     *channels = png_get_channels(*png_ptr, *info_ptr);
101 
102     if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
103         // 8-bit RGB images: great, nothing to do.
104     } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
105         // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
106         png_set_expand_gray_1_2_4_to_8(*png_ptr);
107     } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
108         // paletted images: expand to 8-bit RGB.  Note that we DON'T
109         // currently expand the tRNS chunk (if any) to an alpha
110         // channel, because minui doesn't support alpha channels in
111         // general.
112         png_set_palette_to_rgb(*png_ptr);
113         *channels = 3;
114     } else {
115         fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
116                 bit_depth, *channels, color_type);
117         result = -7;
118         goto exit;
119     }
120 
121     return result;
122 
123 exit:
124     if (result < 0) {
125         png_destroy_read_struct(png_ptr, info_ptr, NULL);
126     }
127     if (fp != NULL) {
128         fclose(fp);
129     }
130 
131     return result;
132 }
133 
134 // "display" surfaces are transformed into the framebuffer's required
135 // pixel format (currently only RGBX is supported) at load time, so
136 // gr_blit() can be nothing more than a memcpy() for each row.  The
137 // next two functions are the only ones that know anything about the
138 // framebuffer pixel format; they need to be modified if the
139 // framebuffer format changes (but nothing else should).
140 
141 // Allocate and return a gr_surface sufficient for storing an image of
142 // the indicated size in the framebuffer pixel format.
init_display_surface(png_uint_32 width,png_uint_32 height)143 static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height)
144 {
145     gr_surface surface;
146 
147     surface = malloc_surface(width * height * 4);
148     if (surface == NULL) return NULL;
149 
150     surface->width = width;
151     surface->height = height;
152     surface->row_bytes = width * 4;
153     surface->pixel_bytes = 4;
154 
155     return surface;
156 }
157 
158 // Copy 'input_row' to 'output_row', transforming it to the
159 // framebuffer pixel format.  The input format depends on the value of
160 // 'channels':
161 //
162 //   1 - input is 8-bit grayscale
163 //   3 - input is 24-bit RGB
164 //   4 - input is 32-bit RGBA/RGBX
165 //
166 // 'width' is the number of pixels in the row.
transform_rgb_to_draw(unsigned char * input_row,unsigned char * output_row,int channels,int width)167 static void transform_rgb_to_draw(unsigned char* input_row,
168                                   unsigned char* output_row,
169                                   int channels, int width)
170 {
171     int x;
172     unsigned char* ip = input_row;
173     unsigned char* op = output_row;
174 
175     switch (channels) {
176     case 1:
177         // expand gray level to RGBX
178         for (x = 0; x < width; ++x) {
179             *op++ = *ip;
180             *op++ = *ip;
181             *op++ = *ip;
182             *op++ = 0xff;
183             ip++;
184         }
185         break;
186 
187     case 3:
188         // expand RGBA to RGBX
189         for (x = 0; x < width; ++x) {
190             *op++ = *ip++;
191             *op++ = *ip++;
192             *op++ = *ip++;
193             *op++ = 0xff;
194         }
195         break;
196 
197     case 4:
198         // copy RGBA to RGBX
199         memcpy(output_row, input_row, width * 4);
200         break;
201     }
202 }
203 
res_create_display_surface(const char * name,gr_surface * pSurface)204 int res_create_display_surface(const char* name, gr_surface* pSurface)
205 {
206     gr_surface surface = NULL;
207     int result = 0;
208     png_structp png_ptr = NULL;
209     png_infop info_ptr = NULL;
210     png_uint_32 width, height;
211     png_byte channels;
212 
213     *pSurface = NULL;
214 
215     result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
216     if (result < 0) return result;
217 
218     surface = init_display_surface(width, height);
219     if (surface == NULL) {
220         result = -8;
221         goto exit;
222     }
223 
224     unsigned char* p_row = malloc(width * 4);
225     unsigned int y;
226     for (y = 0; y < height; ++y) {
227         png_read_row(png_ptr, p_row, NULL);
228         transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
229     }
230     free(p_row);
231 
232     *pSurface = surface;
233 
234 exit:
235     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
236     if (result < 0 && surface != NULL) free(surface);
237     return result;
238 }
239 
res_create_multi_display_surface(const char * name,int * frames,gr_surface ** pSurface)240 int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface)
241 {
242     gr_surface* surface = NULL;
243     int result = 0;
244     png_structp png_ptr = NULL;
245     png_infop info_ptr = NULL;
246     png_uint_32 width, height;
247     png_byte channels;
248     int i;
249 
250     *pSurface = NULL;
251     *frames = -1;
252 
253     result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
254     if (result < 0) return result;
255 
256     *frames = 1;
257     png_textp text;
258     int num_text;
259     if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
260         for (i = 0; i < num_text; ++i) {
261             if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
262                 *frames = atoi(text[i].text);
263                 break;
264             }
265         }
266         printf("  found frames = %d\n", *frames);
267     }
268 
269     if (height % *frames != 0) {
270         printf("bad height (%d) for frame count (%d)\n", height, *frames);
271         result = -9;
272         goto exit;
273     }
274 
275     surface = malloc(*frames * sizeof(gr_surface));
276     if (surface == NULL) {
277         result = -8;
278         goto exit;
279     }
280     for (i = 0; i < *frames; ++i) {
281         surface[i] = init_display_surface(width, height / *frames);
282         if (surface[i] == NULL) {
283             result = -8;
284             goto exit;
285         }
286     }
287 
288     unsigned char* p_row = malloc(width * 4);
289     unsigned int y;
290     for (y = 0; y < height; ++y) {
291         png_read_row(png_ptr, p_row, NULL);
292         int frame = y % *frames;
293         unsigned char* out_row = surface[frame]->data +
294                                  (y / *frames) * surface[frame]->row_bytes;
295         transform_rgb_to_draw(p_row, out_row, channels, width);
296     }
297     free(p_row);
298 
299     *pSurface = (gr_surface*) surface;
300 
301 exit:
302     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
303 
304     if (result < 0) {
305         if (surface) {
306             for (i = 0; i < *frames; ++i) {
307                 if (surface[i]) free(surface[i]);
308             }
309             free(surface);
310         }
311     }
312     return result;
313 }
314 
res_create_alpha_surface(const char * name,gr_surface * pSurface)315 int res_create_alpha_surface(const char* name, gr_surface* pSurface)
316 {
317     gr_surface surface = NULL;
318     int result = 0;
319     png_structp png_ptr = NULL;
320     png_infop info_ptr = NULL;
321     png_uint_32 width, height;
322     png_byte channels;
323 
324     *pSurface = NULL;
325 
326     result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
327     if (result < 0) return result;
328 
329     if (channels != 1) {
330         result = -7;
331         goto exit;
332     }
333 
334     surface = malloc_surface(width * height);
335     if (surface == NULL) {
336         result = -8;
337         goto exit;
338     }
339     surface->width = width;
340     surface->height = height;
341     surface->row_bytes = width;
342     surface->pixel_bytes = 1;
343 
344     unsigned char* p_row;
345     unsigned int y;
346     for (y = 0; y < height; ++y) {
347         p_row = surface->data + y * surface->row_bytes;
348         png_read_row(png_ptr, p_row, NULL);
349     }
350 
351     *pSurface = surface;
352 
353 exit:
354     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
355     if (result < 0 && surface != NULL) free(surface);
356     return result;
357 }
358 
matches_locale(const char * loc,const char * locale)359 static int matches_locale(const char* loc, const char* locale)
360 {
361     if (locale == NULL) return 0;
362 
363     if (strcmp(loc, locale) == 0) return 1;
364 
365     // if loc does *not* have an underscore, and it matches the start
366     // of locale, and the next character in locale *is* an underscore,
367     // that's a match.  For instance, loc == "en" matches locale ==
368     // "en_US".
369 
370     int i;
371     for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i);
372     if (loc[i] == '_') return 0;
373 
374     return (strncmp(locale, loc, i) == 0 && locale[i] == '_');
375 }
376 
res_create_localized_alpha_surface(const char * name,const char * locale,gr_surface * pSurface)377 int res_create_localized_alpha_surface(const char* name,
378                                        const char* locale,
379                                        gr_surface* pSurface)
380 {
381     gr_surface surface = NULL;
382     int result = 0;
383     png_structp png_ptr = NULL;
384     png_infop info_ptr = NULL;
385     png_uint_32 width, height;
386     png_byte channels;
387 
388     *pSurface = NULL;
389 
390     if (locale == NULL) {
391         surface = malloc_surface(0);
392         surface->width = 0;
393         surface->height = 0;
394         surface->row_bytes = 0;
395         surface->pixel_bytes = 1;
396         goto exit;
397     }
398 
399     result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
400     if (result < 0) return result;
401 
402     if (channels != 1) {
403         result = -7;
404         goto exit;
405     }
406 
407     unsigned char* row = malloc(width);
408     png_uint_32 y;
409     for (y = 0; y < height; ++y) {
410         png_read_row(png_ptr, row, NULL);
411         int w = (row[1] << 8) | row[0];
412         int h = (row[3] << 8) | row[2];
413         int len = row[4];
414         char* loc = (char*)row + 5;
415 
416         if (y + 1 + h >= height || matches_locale(loc, locale)) {
417             printf("  %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
418 
419             surface = malloc_surface(w * h);
420             if (surface == NULL) {
421                 result = -8;
422                 goto exit;
423             }
424             surface->width = w;
425             surface->height = h;
426             surface->row_bytes = w;
427             surface->pixel_bytes = 1;
428 
429             int i;
430             for (i = 0; i < h; ++i, ++y) {
431                 png_read_row(png_ptr, row, NULL);
432                 memcpy(surface->data + i * w, row, w);
433             }
434 
435             *pSurface = (gr_surface) surface;
436             break;
437         } else {
438             int i;
439             for (i = 0; i < h; ++i, ++y) {
440                 png_read_row(png_ptr, row, NULL);
441             }
442         }
443     }
444 
445 exit:
446     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
447     if (result < 0 && surface != NULL) free(surface);
448     return result;
449 }
450 
res_free_surface(gr_surface surface)451 void res_free_surface(gr_surface surface)
452 {
453     free(surface);
454 }
455