blob: 2e58311af7279f1da7cd189474b90be35e3919ef [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
Dees Troy3454ade2015-01-20 19:21:04 +000062 snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s.png", name);
63 printf("open_png %s\n", resPath);
Ethan Yonker448c8dc2014-12-08 15:34:34 -060064 resPath[sizeof(resPath)-1] = '\0';
65 FILE* fp = fopen(resPath, "rb");
Dees_Troy51a0e822012-09-05 15:24:24 -040066 if (fp == NULL) {
Ethan Yonkerac21cb52014-12-11 18:05:59 -060067 fp = fopen(name, "rb");
68 if (fp == NULL) {
69 result = -1;
70 goto exit;
71 }
Dees_Troy51a0e822012-09-05 15:24:24 -040072 }
73
74 size_t bytesRead = fread(header, 1, sizeof(header), fp);
75 if (bytesRead != sizeof(header)) {
76 result = -2;
77 goto exit;
78 }
79
80 if (png_sig_cmp(header, 0, sizeof(header))) {
81 result = -3;
82 goto exit;
83 }
84
Ethan Yonker448c8dc2014-12-08 15:34:34 -060085 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
86 if (!*png_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040087 result = -4;
88 goto exit;
89 }
90
Ethan Yonker448c8dc2014-12-08 15:34:34 -060091 *info_ptr = png_create_info_struct(*png_ptr);
92 if (!*info_ptr) {
Dees_Troy51a0e822012-09-05 15:24:24 -040093 result = -5;
94 goto exit;
95 }
96
Ethan Yonker448c8dc2014-12-08 15:34:34 -060097 if (setjmp(png_jmpbuf(*png_ptr))) {
Dees_Troy51a0e822012-09-05 15:24:24 -040098 result = -6;
99 goto exit;
100 }
101
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600102 png_init_io(*png_ptr, fp);
103 png_set_sig_bytes(*png_ptr, sizeof(header));
104 png_read_info(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Ethan Yonker304f32f2014-11-07 10:14:05 -0600106 int color_type, bit_depth;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600107 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
Ethan Yonker304f32f2014-11-07 10:14:05 -0600108 &color_type, NULL, NULL, NULL);
109
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600110 *channels = png_get_channels(*png_ptr, *info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400111
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600112 /*if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
113 // 8-bit RGB images: great, nothing to do.
114 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
115 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
116 png_set_expand_gray_1_2_4_to_8(*png_ptr);
117 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
118 // paletted images: expand to 8-bit RGB. Note that we DON'T
119 // currently expand the tRNS chunk (if any) to an alpha
120 // channel, because minui doesn't support alpha channels in
121 // general.
122 png_set_palette_to_rgb(*png_ptr);
123 *channels = 3;
124 } else {
125 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
126 bit_depth, *channels, color_type);
127 result = -7;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128 goto exit;
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600129 }*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400130 if (color_type == PNG_COLOR_TYPE_PALETTE) {
131 png_set_palette_to_rgb(png_ptr);
132 }
133
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600134 return result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600136 exit:
137 if (result < 0) {
138 png_destroy_read_struct(png_ptr, info_ptr, NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 if (fp != NULL) {
141 fclose(fp);
142 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600143
144 return result;
145}
146
147// "display" surfaces are transformed into the framebuffer's required
148// pixel format (currently only RGBX is supported) at load time, so
149// gr_blit() can be nothing more than a memcpy() for each row. The
150// next two functions are the only ones that know anything about the
151// framebuffer pixel format; they need to be modified if the
152// framebuffer format changes (but nothing else should).
153
154// Allocate and return a gr_surface sufficient for storing an image of
155// the indicated size in the framebuffer pixel format.
156static GGLSurface* init_display_surface(png_uint_32 width, png_uint_32 height) {
157 GGLSurface* surface;
158
159 surface = (GGLSurface*) malloc_surface(width * height * 4);
160 if (surface == NULL) return NULL;
161
162 surface->version = sizeof(GGLSurface);
163 surface->width = width;
164 surface->height = height;
165 surface->stride = width;
166
167 return surface;
168}
169
170// Copy 'input_row' to 'output_row', transforming it to the
171// framebuffer pixel format. The input format depends on the value of
172// 'channels':
173//
174// 1 - input is 8-bit grayscale
175// 3 - input is 24-bit RGB
176// 4 - input is 32-bit RGBA/RGBX
177//
178// 'width' is the number of pixels in the row.
179static void transform_rgb_to_draw(unsigned char* input_row,
180 unsigned char* output_row,
181 int channels, int width) {
182 int x;
183 unsigned char* ip = input_row;
184 unsigned char* op = output_row;
185
186 switch (channels) {
187 case 1:
188 // expand gray level to RGBX
189 for (x = 0; x < width; ++x) {
190 *op++ = *ip;
191 *op++ = *ip;
192 *op++ = *ip;
193 *op++ = 0xff;
194 ip++;
195 }
196 break;
197
198 case 3:
199 // expand RGBA to RGBX
200 for (x = 0; x < width; ++x) {
201 *op++ = *ip++;
202 *op++ = *ip++;
203 *op++ = *ip++;
204 *op++ = 0xff;
205 }
206 break;
207
208 case 4:
209 // copy RGBA to RGBX
210 memcpy(output_row, input_row, width*4);
211 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 }
Ethan Yonker448c8dc2014-12-08 15:34:34 -0600213}
214
215int res_create_surface_png(const char* name, gr_surface* pSurface) {
216 GGLSurface* surface = NULL;
217 int result = 0;
218 png_structp png_ptr = NULL;
219 png_infop info_ptr = NULL;
220 png_uint_32 width, height;
221 png_byte channels;
222
223 *pSurface = NULL;
224
225 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
226 if (result < 0) return result;
227
228 surface = init_display_surface(width, height);
229 if (surface == NULL) {
230 result = -8;
231 goto exit;
232 }
233
234 unsigned char* p_row = malloc(width * 4);
235 unsigned int y;
236 for (y = 0; y < height; ++y) {
237 png_read_row(png_ptr, p_row, NULL);
238 transform_rgb_to_draw(p_row, surface->data + y * width * 4, channels, width);
239 }
240 free(p_row);
241
242 if (channels == 3)
243 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
244 else
245 surface->format = GGL_PIXEL_FORMAT_RGBA_8888;
246
247 *pSurface = (gr_surface) surface;
248
249 exit:
250 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
251 if (result < 0 && surface != NULL) free(surface);
Dees_Troy51a0e822012-09-05 15:24:24 -0400252 return result;
253}
254
255int res_create_surface_jpg(const char* name, gr_surface* pSurface) {
256 GGLSurface* surface = NULL;
257 int result = 0;
258 struct jpeg_decompress_struct cinfo;
259 struct jpeg_error_mgr jerr;
260
261 FILE* fp = fopen(name, "rb");
262 if (fp == NULL) {
263 char resPath[256];
264
Dees Troy3454ade2015-01-20 19:21:04 +0000265 snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s", name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400266 resPath[sizeof(resPath)-1] = '\0';
267 fp = fopen(resPath, "rb");
268 if (fp == NULL) {
269 result = -1;
270 goto exit;
271 }
272 }
273
274 cinfo.err = jpeg_std_error(&jerr);
275 jpeg_create_decompress(&cinfo);
276
277 /* Specify data source for decompression */
278 jpeg_stdio_src(&cinfo, fp);
279
280 /* Read file header, set default decompression parameters */
281 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
282 goto exit;
283
284 /* Start decompressor */
285 (void) jpeg_start_decompress(&cinfo);
286
287 size_t width = cinfo.image_width;
288 size_t height = cinfo.image_height;
289 size_t stride = 4 * width;
290 size_t pixelSize = stride * height;
291
292 surface = malloc(sizeof(GGLSurface) + pixelSize);
293 if (surface == NULL) {
294 result = -8;
295 goto exit;
296 }
297
298 unsigned char* pData = (unsigned char*) (surface + 1);
299 surface->version = sizeof(GGLSurface);
300 surface->width = width;
301 surface->height = height;
302 surface->stride = width; /* Yes, pixels, not bytes */
303 surface->data = pData;
304 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
305
306 int y;
307 for (y = 0; y < (int) height; ++y) {
308 unsigned char* pRow = pData + y * stride;
309 jpeg_read_scanlines(&cinfo, &pRow, 1);
310
311 int x;
312 for(x = width - 1; x >= 0; x--) {
313 int sx = x * 3;
314 int dx = x * 4;
315 unsigned char r = pRow[sx];
316 unsigned char g = pRow[sx + 1];
317 unsigned char b = pRow[sx + 2];
318 unsigned char a = 0xff;
319 pRow[dx ] = r; // r
320 pRow[dx + 1] = g; // g
321 pRow[dx + 2] = b; // b
322 pRow[dx + 3] = a;
323 }
324 }
325 *pSurface = (gr_surface) surface;
326
327exit:
328 if (fp != NULL)
329 {
330 if (surface)
331 {
332 (void) jpeg_finish_decompress(&cinfo);
333 if (result < 0)
334 {
335 free(surface);
336 }
337 }
338 jpeg_destroy_decompress(&cinfo);
339 fclose(fp);
340 }
341 return result;
342}
343
344int res_create_surface(const char* name, gr_surface* pSurface) {
345 int ret;
346
347 if (!name) return -1;
348
349 if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0)
350 return res_create_surface_jpg(name,pSurface);
351
352 ret = res_create_surface_png(name,pSurface);
353 if (ret < 0)
354 ret = res_create_surface_jpg(name,pSurface);
355
356 return ret;
357}
358
359void res_free_surface(gr_surface surface) {
360 GGLSurface* pSurface = (GGLSurface*) surface;
361 if (pSurface) {
362 free(pSurface);
363 }
364}