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