blob: 4c8d8a93f2c84d5532b64010ac623275693d5189 [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_Troy2673cec2013-04-02 20:22:16 +000033#include "../libjpegtwrp/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
45int res_create_surface_png(const char* name, gr_surface* pSurface) {
46 GGLSurface* surface = NULL;
47 int result = 0;
48 unsigned char header[8];
49 png_structp png_ptr = NULL;
50 png_infop info_ptr = NULL;
51
52 FILE* fp = fopen(name, "rb");
53 if (fp == NULL) {
54 char resPath[256];
55
56 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
57 resPath[sizeof(resPath)-1] = '\0';
58 fp = fopen(resPath, "rb");
59 if (fp == NULL)
60 {
61 result = -1;
62 goto exit;
63 }
64 }
65
66 size_t bytesRead = fread(header, 1, sizeof(header), fp);
67 if (bytesRead != sizeof(header)) {
68 result = -2;
69 goto exit;
70 }
71
72 if (png_sig_cmp(header, 0, sizeof(header))) {
73 result = -3;
74 goto exit;
75 }
76
77 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
78 if (!png_ptr) {
79 result = -4;
80 goto exit;
81 }
82
83 info_ptr = png_create_info_struct(png_ptr);
84 if (!info_ptr) {
85 result = -5;
86 goto exit;
87 }
88
89 if (setjmp(png_jmpbuf(png_ptr))) {
90 result = -6;
91 goto exit;
92 }
93
94 png_set_packing(png_ptr);
95
96 png_init_io(png_ptr, fp);
97 png_set_sig_bytes(png_ptr, sizeof(header));
98 png_read_info(png_ptr, info_ptr);
99
100 size_t width = info_ptr->width;
101 size_t height = info_ptr->height;
102 size_t stride = 4 * width;
103 size_t pixelSize = stride * height;
104
105 int color_type = info_ptr->color_type;
106 int bit_depth = info_ptr->bit_depth;
107 int channels = info_ptr->channels;
108 if (!(bit_depth == 8 &&
109 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
110 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
111 (channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE)))) {
112 return -7;
113 goto exit;
114 }
115
116 surface = malloc(sizeof(GGLSurface) + pixelSize);
117 if (surface == NULL) {
118 result = -8;
119 goto exit;
120 }
121 unsigned char* pData = (unsigned char*) (surface + 1);
122 surface->version = sizeof(GGLSurface);
123 surface->width = width;
124 surface->height = height;
125 surface->stride = width; /* Yes, pixels, not bytes */
126 surface->data = pData;
127 surface->format = (channels == 3) ?
128 GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888;
129
130 if (color_type == PNG_COLOR_TYPE_PALETTE) {
131 png_set_palette_to_rgb(png_ptr);
132 }
133
134 int y;
135 if (channels < 4) {
136 for (y = 0; y < (int) height; ++y) {
137 unsigned char* pRow = pData + y * stride;
138 png_read_row(png_ptr, pRow, NULL);
139
140 int x;
141 for(x = width - 1; x >= 0; x--) {
142 int sx = x * 3;
143 int dx = x * 4;
144 unsigned char r = pRow[sx];
145 unsigned char g = pRow[sx + 1];
146 unsigned char b = pRow[sx + 2];
147 unsigned char a = 0xff;
148 pRow[dx ] = r; // r
149 pRow[dx + 1] = g; // g
150 pRow[dx + 2] = b; // b
151 pRow[dx + 3] = a;
152 }
153 }
154 } else {
155 for (y = 0; y < (int) height; ++y) {
156 unsigned char* pRow = pData + y * stride;
157 png_read_row(png_ptr, pRow, NULL);
158 }
159 }
160
161 *pSurface = (gr_surface) surface;
162
163exit:
164 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
165
166 if (fp != NULL) {
167 fclose(fp);
168 }
169 if (result < 0) {
170 if (surface) {
171 free(surface);
172 }
173 }
174 return result;
175}
176
177int res_create_surface_jpg(const char* name, gr_surface* pSurface) {
178 GGLSurface* surface = NULL;
179 int result = 0;
180 struct jpeg_decompress_struct cinfo;
181 struct jpeg_error_mgr jerr;
182
183 FILE* fp = fopen(name, "rb");
184 if (fp == NULL) {
185 char resPath[256];
186
187 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s", name);
188 resPath[sizeof(resPath)-1] = '\0';
189 fp = fopen(resPath, "rb");
190 if (fp == NULL) {
191 result = -1;
192 goto exit;
193 }
194 }
195
196 cinfo.err = jpeg_std_error(&jerr);
197 jpeg_create_decompress(&cinfo);
198
199 /* Specify data source for decompression */
200 jpeg_stdio_src(&cinfo, fp);
201
202 /* Read file header, set default decompression parameters */
203 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
204 goto exit;
205
206 /* Start decompressor */
207 (void) jpeg_start_decompress(&cinfo);
208
209 size_t width = cinfo.image_width;
210 size_t height = cinfo.image_height;
211 size_t stride = 4 * width;
212 size_t pixelSize = stride * height;
213
214 surface = malloc(sizeof(GGLSurface) + pixelSize);
215 if (surface == NULL) {
216 result = -8;
217 goto exit;
218 }
219
220 unsigned char* pData = (unsigned char*) (surface + 1);
221 surface->version = sizeof(GGLSurface);
222 surface->width = width;
223 surface->height = height;
224 surface->stride = width; /* Yes, pixels, not bytes */
225 surface->data = pData;
226 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
227
228 int y;
229 for (y = 0; y < (int) height; ++y) {
230 unsigned char* pRow = pData + y * stride;
231 jpeg_read_scanlines(&cinfo, &pRow, 1);
232
233 int x;
234 for(x = width - 1; x >= 0; x--) {
235 int sx = x * 3;
236 int dx = x * 4;
237 unsigned char r = pRow[sx];
238 unsigned char g = pRow[sx + 1];
239 unsigned char b = pRow[sx + 2];
240 unsigned char a = 0xff;
241 pRow[dx ] = r; // r
242 pRow[dx + 1] = g; // g
243 pRow[dx + 2] = b; // b
244 pRow[dx + 3] = a;
245 }
246 }
247 *pSurface = (gr_surface) surface;
248
249exit:
250 if (fp != NULL)
251 {
252 if (surface)
253 {
254 (void) jpeg_finish_decompress(&cinfo);
255 if (result < 0)
256 {
257 free(surface);
258 }
259 }
260 jpeg_destroy_decompress(&cinfo);
261 fclose(fp);
262 }
263 return result;
264}
265
266int res_create_surface(const char* name, gr_surface* pSurface) {
267 int ret;
268
269 if (!name) return -1;
270
271 if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0)
272 return res_create_surface_jpg(name,pSurface);
273
274 ret = res_create_surface_png(name,pSurface);
275 if (ret < 0)
276 ret = res_create_surface_jpg(name,pSurface);
277
278 return ret;
279}
280
281void res_free_surface(gr_surface surface) {
282 GGLSurface* pSurface = (GGLSurface*) surface;
283 if (pSurface) {
284 free(pSurface);
285 }
286}