blob: bb301477f8f23f5fdfd8d75f12c91caeb205314b [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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
32#include <png.h>
Dees_Troy930bf012013-08-10 22:19:03 +000033#include "jpeglib.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35#include "minui.h"
36
37// libpng gives "undefined reference to 'pow'" errors, and I have no
38// idea how to convince the build system to link with -lm. We don't
39// need this functionality (it's used for gamma adjustment) so provide
40// a dummy implementation to satisfy the linker.
41double pow(double x, double y) {
42 return x;
43}
44
Ethan Yonker448c8dc2014-12-08 15:34:34 -060045#define SURFACE_DATA_ALIGNMENT 8
46
47static GGLSurface* malloc_surface(size_t data_size) {
48 unsigned char* temp = malloc(sizeof(GGLSurface) + data_size + SURFACE_DATA_ALIGNMENT);
49 if (temp == NULL) return NULL;
50 GGLSurface* surface = (GGLSurface*) temp;
51 surface->data = temp + sizeof(GGLSurface) +
52 (SURFACE_DATA_ALIGNMENT - (sizeof(GGLSurface) % SURFACE_DATA_ALIGNMENT));
53 return surface;
54}
55
56static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
57 png_uint_32* width, png_uint_32* height, png_byte* channels) {
58 char resPath[256];
Dees_Troy51a0e822012-09-05 15:24:24 -040059 unsigned char header[8];
Ethan Yonker448c8dc2014-12-08 15:34:34 -060060 int result = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Ethan Yonker448c8dc2014-12-08 15:34:34 -060062 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
63 resPath[sizeof(resPath)-1] = '\0';
64 FILE* fp = fopen(resPath, "rb");
Dees_Troy51a0e822012-09-05 15:24:24 -040065 if (fp == NULL) {
Ethan Yonkerac21cb52014-12-11 18:05:59 -060066 fp = fopen(name, "rb");
67 if (fp == NULL) {
68 result = -1;
69 goto exit;
70 }
Dees_Troy51a0e822012-09-05 15:24:24 -040071 }
72
73 size_t bytesRead = fread(header, 1, sizeof(header), fp);
74 if (bytesRead != sizeof(header)) {
75 result = -2;
76 goto exit;
77 }
78
79 if (png_sig_cmp(header, 0, sizeof(header))) {
80 result = -3;
81 goto exit;
82 }
83
Ethan Yonker448c8dc2014-12-08 15:34:34 -060084 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
85 if (!*png_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040086 result = -4;
87 goto exit;
88 }
89
Ethan Yonker448c8dc2014-12-08 15:34:34 -060090 *info_ptr = png_create_info_struct(*png_ptr);
91 if (!*info_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040092 result = -5;
93 goto exit;
94 }
95
Ethan Yonker448c8dc2014-12-08 15:34:34 -060096 if (setjmp(png_jmpbuf(*png_ptr))) {
Dees_Troy51a0e822012-09-05 15:24:24 -040097 result = -6;
98 goto exit;
99 }
100
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600101 png_init_io(*png_ptr, fp);
102 png_set_sig_bytes(*png_ptr, sizeof(header));
103 png_read_info(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
Ethan Yonker304f32f2014-11-07 10:14:05 -0600105 int color_type, bit_depth;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600106 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
Ethan Yonker304f32f2014-11-07 10:14:05 -0600107 &color_type, NULL, NULL, NULL);
108
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600109 *channels = png_get_channels(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400110
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600111 /*if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
112 // 8-bit RGB images: great, nothing to do.
113 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
114 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
115 png_set_expand_gray_1_2_4_to_8(*png_ptr);
116 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
117 // paletted images: expand to 8-bit RGB. Note that we DON'T
118 // currently expand the tRNS chunk (if any) to an alpha
119 // channel, because minui doesn't support alpha channels in
120 // general.
121 png_set_palette_to_rgb(*png_ptr);
122 *channels = 3;
123 } else {
124 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
125 bit_depth, *channels, color_type);
126 result = -7;
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 goto exit;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600128 }*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 if (color_type == PNG_COLOR_TYPE_PALETTE) {
130 png_set_palette_to_rgb(png_ptr);
131 }
132
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600133 return result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600135 exit:
136 if (result < 0) {
137 png_destroy_read_struct(png_ptr, info_ptr, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400138 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 if (fp != NULL) {
140 fclose(fp);
141 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600142
143 return result;
144}
145
146// "display" surfaces are transformed into the framebuffer's required
147// pixel format (currently only RGBX is supported) at load time, so
148// gr_blit() can be nothing more than a memcpy() for each row. The
149// next two functions are the only ones that know anything about the
150// framebuffer pixel format; they need to be modified if the
151// framebuffer format changes (but nothing else should).
152
153// Allocate and return a gr_surface sufficient for storing an image of
154// the indicated size in the framebuffer pixel format.
155static GGLSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
156 GGLSurface* surface;
157
158 surface = (GGLSurface*) malloc_surface(width * height * 4);
159 if (surface == NULL) return NULL;
160
161 surface->version = sizeof(GGLSurface);
162 surface->width = width;
163 surface->height = height;
164 surface->stride = width;
165
166 return surface;
167}
168
169// Copy 'input_row' to 'output_row', transforming it to the
170// framebuffer pixel format. The input format depends on the value of
171// 'channels':
172//
173// 1 - input is 8-bit grayscale
174// 3 - input is 24-bit RGB
175// 4 - input is 32-bit RGBA/RGBX
176//
177// 'width' is the number of pixels in the row.
178static void transform_rgb_to_draw(unsigned char* input_row,
179 unsigned char* output_row,
180 int channels, int width) {
181 int x;
182 unsigned char* ip = input_row;
183 unsigned char* op = output_row;
184
185 switch (channels) {
186 case 1:
187 // expand gray level to RGBX
188 for (x = 0; x < width; ++x) {
189 *op++ = *ip;
190 *op++ = *ip;
191 *op++ = *ip;
192 *op++ = 0xff;
193 ip++;
194 }
195 break;
196
197 case 3:
198 // expand RGBA to RGBX
199 for (x = 0; x < width; ++x) {
200 *op++ = *ip++;
201 *op++ = *ip++;
202 *op++ = *ip++;
203 *op++ = 0xff;
204 }
205 break;
206
207 case 4:
208 // copy RGBA to RGBX
209 memcpy(output_row, input_row, width*4);
210 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600212}
213
214int res_create_surface_png(const char* name, gr_surface* pSurface) {
215 GGLSurface* surface = NULL;
216 int result = 0;
217 png_structp png_ptr = NULL;
218 png_infop info_ptr = NULL;
219 png_uint_32 width, height;
220 png_byte channels;
221
222 *pSurface = NULL;
223
224 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
225 if (result < 0) return result;
226
227 surface = init_display_surface(width, height);
228 if (surface == NULL) {
229 result = -8;
230 goto exit;
231 }
232
233 unsigned char* p_row = malloc(width * 4);
234 unsigned int y;
235 for (y = 0; y < height; ++y) {
236 png_read_row(png_ptr, p_row, NULL);
237 transform_rgb_to_draw(p_row, surface->data + y * width * 4, channels, width);
238 }
239 free(p_row);
240
241 if (channels == 3)
242 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
243 else
244 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
245
246 *pSurface = (gr_surface) surface;
247
248 exit:
249 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
250 if (result < 0 && surface != NULL) free(surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 return result;
252}
253
254int res_create_surface_jpg(const char* name, gr_surface* pSurface) {
255 GGLSurface* surface = NULL;
256 int result = 0;
257 struct jpeg_decompress_struct cinfo;
258 struct jpeg_error_mgr jerr;
259
260 FILE* fp = fopen(name, "rb");
261 if (fp == NULL) {
262 char resPath[256];
263
264 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s", name);
265 resPath[sizeof(resPath)-1] = '\0';
266 fp = fopen(resPath, "rb");
267 if (fp == NULL) {
268 result = -1;
269 goto exit;
270 }
271 }
272
273 cinfo.err = jpeg_std_error(&jerr);
274 jpeg_create_decompress(&cinfo);
275
276 /* Specify data source for decompression */
277 jpeg_stdio_src(&cinfo, fp);
278
279 /* Read file header, set default decompression parameters */
280 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
281 goto exit;
282
283 /* Start decompressor */
284 (void) jpeg_start_decompress(&cinfo);
285
286 size_t width = cinfo.image_width;
287 size_t height = cinfo.image_height;
288 size_t stride = 4 * width;
289 size_t pixelSize = stride * height;
290
291 surface = malloc(sizeof(GGLSurface) + pixelSize);
292 if (surface == NULL) {
293 result = -8;
294 goto exit;
295 }
296
297 unsigned char* pData = (unsigned char*) (surface + 1);
298 surface->version = sizeof(GGLSurface);
299 surface->width = width;
300 surface->height = height;
301 surface->stride = width; /* Yes, pixels, not bytes */
302 surface->data = pData;
303 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
304
305 int y;
306 for (y = 0; y < (int) height; ++y) {
307 unsigned char* pRow = pData + y * stride;
308 jpeg_read_scanlines(&cinfo, &pRow, 1);
309
310 int x;
311 for(x = width - 1; x >= 0; x--) {
312 int sx = x * 3;
313 int dx = x * 4;
314 unsigned char r = pRow[sx];
315 unsigned char g = pRow[sx + 1];
316 unsigned char b = pRow[sx + 2];
317 unsigned char a = 0xff;
318 pRow[dx ] = r; // r
319 pRow[dx + 1] = g; // g
320 pRow[dx + 2] = b; // b
321 pRow[dx + 3] = a;
322 }
323 }
324 *pSurface = (gr_surface) surface;
325
326exit:
327 if (fp != NULL)
328 {
329 if (surface)
330 {
331 (void) jpeg_finish_decompress(&cinfo);
332 if (result < 0)
333 {
334 free(surface);
335 }
336 }
337 jpeg_destroy_decompress(&cinfo);
338 fclose(fp);
339 }
340 return result;
341}
342
343int res_create_surface(const char* name, gr_surface* pSurface) {
344 int ret;
345
346 if (!name) return -1;
347
348 if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0)
349 return res_create_surface_jpg(name,pSurface);
350
351 ret = res_create_surface_png(name,pSurface);
352 if (ret < 0)
353 ret = res_create_surface_jpg(name,pSurface);
354
355 return ret;
356}
357
358void res_free_surface(gr_surface surface) {
359 GGLSurface* pSurface = (GGLSurface*) surface;
360 if (pSurface) {
361 free(pSurface);
362 }
363}