blob: 4dd10a9de99c4f4f4d71cc7e5efbc2026cc26810 [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
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
Ethan Yonker304f32f2014-11-07 10:14:05 -0600100 int color_type, bit_depth;
101 size_t width, height;
102
Ethan Yonker5a95c3f2014-11-07 13:32:55 -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 Yonker5a95c3f2014-11-07 13:32:55 -0600106 png_byte channels = png_get_channels(png_ptr, info_ptr);
Dees_Troy51a0e822012-09-05 15:24:24 -0400107 size_t stride = 4 * width;
108 size_t pixelSize = stride * height;
109
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 if (!(bit_depth == 8 &&
111 ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) ||
112 (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) ||
113 (channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE)))) {
114 return -7;
115 goto exit;
116 }
117
118 surface = malloc(sizeof(GGLSurface) + pixelSize);
119 if (surface == NULL) {
120 result = -8;
121 goto exit;
122 }
123 unsigned char* pData = (unsigned char*) (surface + 1);
124 surface->version = sizeof(GGLSurface);
125 surface->width = width;
126 surface->height = height;
127 surface->stride = width; /* Yes, pixels, not bytes */
128 surface->data = pData;
129 surface->format = (channels == 3) ?
130 GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888;
131
132 if (color_type == PNG_COLOR_TYPE_PALETTE) {
133 png_set_palette_to_rgb(png_ptr);
134 }
135
136 int y;
137 if (channels < 4) {
138 for (y = 0; y < (int) height; ++y) {
139 unsigned char* pRow = pData + y * stride;
140 png_read_row(png_ptr, pRow, NULL);
141
142 int x;
143 for(x = width - 1; x >= 0; x--) {
144 int sx = x * 3;
145 int dx = x * 4;
146 unsigned char r = pRow[sx];
147 unsigned char g = pRow[sx + 1];
148 unsigned char b = pRow[sx + 2];
149 unsigned char a = 0xff;
150 pRow[dx ] = r; // r
151 pRow[dx + 1] = g; // g
152 pRow[dx + 2] = b; // b
153 pRow[dx + 3] = a;
154 }
155 }
156 } else {
157 for (y = 0; y < (int) height; ++y) {
158 unsigned char* pRow = pData + y * stride;
159 png_read_row(png_ptr, pRow, NULL);
160 }
161 }
162
163 *pSurface = (gr_surface) surface;
164
165exit:
166 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
167
168 if (fp != NULL) {
169 fclose(fp);
170 }
171 if (result < 0) {
172 if (surface) {
173 free(surface);
174 }
175 }
176 return result;
177}
178
179int res_create_surface_jpg(const char* name, gr_surface* pSurface) {
180 GGLSurface* surface = NULL;
181 int result = 0;
182 struct jpeg_decompress_struct cinfo;
183 struct jpeg_error_mgr jerr;
184
185 FILE* fp = fopen(name, "rb");
186 if (fp == NULL) {
187 char resPath[256];
188
189 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s", name);
190 resPath[sizeof(resPath)-1] = '\0';
191 fp = fopen(resPath, "rb");
192 if (fp == NULL) {
193 result = -1;
194 goto exit;
195 }
196 }
197
198 cinfo.err = jpeg_std_error(&jerr);
199 jpeg_create_decompress(&cinfo);
200
201 /* Specify data source for decompression */
202 jpeg_stdio_src(&cinfo, fp);
203
204 /* Read file header, set default decompression parameters */
205 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
206 goto exit;
207
208 /* Start decompressor */
209 (void) jpeg_start_decompress(&cinfo);
210
211 size_t width = cinfo.image_width;
212 size_t height = cinfo.image_height;
213 size_t stride = 4 * width;
214 size_t pixelSize = stride * height;
215
216 surface = malloc(sizeof(GGLSurface) + pixelSize);
217 if (surface == NULL) {
218 result = -8;
219 goto exit;
220 }
221
222 unsigned char* pData = (unsigned char*) (surface + 1);
223 surface->version = sizeof(GGLSurface);
224 surface->width = width;
225 surface->height = height;
226 surface->stride = width; /* Yes, pixels, not bytes */
227 surface->data = pData;
228 surface->format = GGL_PIXEL_FORMAT_RGBX_8888;
229
230 int y;
231 for (y = 0; y < (int) height; ++y) {
232 unsigned char* pRow = pData + y * stride;
233 jpeg_read_scanlines(&cinfo, &pRow, 1);
234
235 int x;
236 for(x = width - 1; x >= 0; x--) {
237 int sx = x * 3;
238 int dx = x * 4;
239 unsigned char r = pRow[sx];
240 unsigned char g = pRow[sx + 1];
241 unsigned char b = pRow[sx + 2];
242 unsigned char a = 0xff;
243 pRow[dx ] = r; // r
244 pRow[dx + 1] = g; // g
245 pRow[dx + 2] = b; // b
246 pRow[dx + 3] = a;
247 }
248 }
249 *pSurface = (gr_surface) surface;
250
251exit:
252 if (fp != NULL)
253 {
254 if (surface)
255 {
256 (void) jpeg_finish_decompress(&cinfo);
257 if (result < 0)
258 {
259 free(surface);
260 }
261 }
262 jpeg_destroy_decompress(&cinfo);
263 fclose(fp);
264 }
265 return result;
266}
267
268int res_create_surface(const char* name, gr_surface* pSurface) {
269 int ret;
270
271 if (!name) return -1;
272
273 if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0)
274 return res_create_surface_jpg(name,pSurface);
275
276 ret = res_create_surface_png(name,pSurface);
277 if (ret < 0)
278 ret = res_create_surface_jpg(name,pSurface);
279
280 return ret;
281}
282
283void res_free_surface(gr_surface surface) {
284 GGLSurface* pSurface = (GGLSurface*) surface;
285 if (pSurface) {
286 free(pSurface);
287 }
288}