Ethan Yonker | a27d02f | 2014-11-06 10:24:39 -0600 | [diff] [blame^] | 1 | /* |
| 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> |
| 33 | #include "jpeglib.h" |
| 34 | |
| 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. |
| 41 | double pow(double x, double y) { |
| 42 | return x; |
| 43 | } |
| 44 | |
| 45 | #define SURFACE_DATA_ALIGNMENT 8 |
| 46 | |
| 47 | static gr_surface malloc_surface(size_t data_size) { |
| 48 | unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT); |
| 49 | if (temp == NULL) return NULL; |
| 50 | gr_surface surface = (gr_surface) temp; |
| 51 | surface->data = temp + sizeof(GRSurface) + |
| 52 | (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); |
| 53 | return surface; |
| 54 | } |
| 55 | |
| 56 | static 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]; |
| 59 | unsigned char header[8]; |
| 60 | int result = 0; |
| 61 | |
| 62 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
| 63 | resPath[sizeof(resPath)-1] = '\0'; |
| 64 | FILE* fp = fopen(resPath, "rb"); |
| 65 | if (fp == NULL) { |
| 66 | result = -1; |
| 67 | goto exit; |
| 68 | } |
| 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 | |
| 81 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 82 | if (!*png_ptr) { |
| 83 | result = -4; |
| 84 | goto exit; |
| 85 | } |
| 86 | |
| 87 | *info_ptr = png_create_info_struct(*png_ptr); |
| 88 | if (!*info_ptr) { |
| 89 | result = -5; |
| 90 | goto exit; |
| 91 | } |
| 92 | |
| 93 | if (setjmp(png_jmpbuf(*png_ptr))) { |
| 94 | result = -6; |
| 95 | goto exit; |
| 96 | } |
| 97 | |
| 98 | png_init_io(*png_ptr, fp); |
| 99 | png_set_sig_bytes(*png_ptr, sizeof(header)); |
| 100 | png_read_info(*png_ptr, *info_ptr); |
| 101 | |
| 102 | int color_type, bit_depth; |
| 103 | png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, |
| 104 | &color_type, NULL, NULL, NULL); |
| 105 | |
| 106 | *channels = png_get_channels(*png_ptr, *info_ptr); |
| 107 | |
| 108 | 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; |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | return result; |
| 128 | |
| 129 | exit: |
| 130 | if (result < 0) { |
| 131 | png_destroy_read_struct(png_ptr, info_ptr, NULL); |
| 132 | } |
| 133 | if (fp != NULL) { |
| 134 | fclose(fp); |
| 135 | } |
| 136 | |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | // "display" surfaces are transformed into the framebuffer's required |
| 141 | // pixel format (currently only RGBX is supported) at load time, so |
| 142 | // gr_blit() can be nothing more than a memcpy() for each row. The |
| 143 | // next two functions are the only ones that know anything about the |
| 144 | // framebuffer pixel format; they need to be modified if the |
| 145 | // framebuffer format changes (but nothing else should). |
| 146 | |
| 147 | // Allocate and return a gr_surface sufficient for storing an image of |
| 148 | // the indicated size in the framebuffer pixel format. |
| 149 | static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) { |
| 150 | gr_surface surface; |
| 151 | |
| 152 | surface = malloc_surface(width * height * 4); |
| 153 | if (surface == NULL) return NULL; |
| 154 | |
| 155 | surface->width = width; |
| 156 | surface->height = height; |
| 157 | surface->row_bytes = width * 4; |
| 158 | surface->pixel_bytes = 4; |
| 159 | |
| 160 | return surface; |
| 161 | } |
| 162 | |
| 163 | // Copy 'input_row' to 'output_row', transforming it to the |
| 164 | // framebuffer pixel format. The input format depends on the value of |
| 165 | // 'channels': |
| 166 | // |
| 167 | // 1 - input is 8-bit grayscale |
| 168 | // 3 - input is 24-bit RGB |
| 169 | // 4 - input is 32-bit RGBA/RGBX |
| 170 | // |
| 171 | // 'width' is the number of pixels in the row. |
| 172 | static void transform_rgb_to_draw(unsigned char* input_row, |
| 173 | unsigned char* output_row, |
| 174 | int channels, int width) { |
| 175 | int x; |
| 176 | unsigned char* ip = input_row; |
| 177 | unsigned char* op = output_row; |
| 178 | |
| 179 | switch (channels) { |
| 180 | case 1: |
| 181 | // expand gray level to RGBX |
| 182 | for (x = 0; x < width; ++x) { |
| 183 | *op++ = *ip; |
| 184 | *op++ = *ip; |
| 185 | *op++ = *ip; |
| 186 | *op++ = 0xff; |
| 187 | ip++; |
| 188 | } |
| 189 | break; |
| 190 | |
| 191 | case 3: |
| 192 | // expand RGBA to RGBX |
| 193 | for (x = 0; x < width; ++x) { |
| 194 | *op++ = *ip++; |
| 195 | *op++ = *ip++; |
| 196 | *op++ = *ip++; |
| 197 | *op++ = 0xff; |
| 198 | } |
| 199 | break; |
| 200 | |
| 201 | case 4: |
| 202 | // copy RGBA to RGBX |
| 203 | memcpy(output_row, input_row, width*4); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | int res_create_surface_png(const char* name, gr_surface* pSurface) { |
| 209 | gr_surface surface = NULL; |
| 210 | int result = 0; |
| 211 | png_structp png_ptr = NULL; |
| 212 | png_infop info_ptr = NULL; |
| 213 | png_uint_32 width, height; |
| 214 | png_byte channels; |
| 215 | |
| 216 | *pSurface = NULL; |
| 217 | printf("png trying to open '%s'\n", name); |
| 218 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); |
| 219 | if (result < 0) return result; |
| 220 | |
| 221 | surface = init_display_surface(width, height); |
| 222 | if (surface == NULL) { |
| 223 | result = -8; |
| 224 | goto exit; |
| 225 | } |
| 226 | |
| 227 | unsigned char* p_row = malloc(width * 4); |
| 228 | unsigned int y; |
| 229 | for (y = 0; y < height; ++y) { |
| 230 | png_read_row(png_ptr, p_row, NULL); |
| 231 | transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width); |
| 232 | } |
| 233 | free(p_row); |
| 234 | |
| 235 | *pSurface = surface->data; |
| 236 | |
| 237 | exit: |
| 238 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 239 | if (result < 0 && surface != NULL) free(surface); |
| 240 | printf("res_create_surface_png returning: %i\n", result); |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | int res_create_surface_jpg(const char* name, gr_surface* pSurface) { |
| 245 | GGLSurface* surface = NULL; |
| 246 | int result = 0; |
| 247 | struct jpeg_decompress_struct cinfo; |
| 248 | struct jpeg_error_mgr jerr; |
| 249 | |
| 250 | FILE* fp = fopen(name, "rb"); |
| 251 | if (fp == NULL) { |
| 252 | char resPath[256]; |
| 253 | |
| 254 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s", name); |
| 255 | resPath[sizeof(resPath)-1] = '\0'; |
| 256 | fp = fopen(resPath, "rb"); |
| 257 | if (fp == NULL) { |
| 258 | result = -1; |
| 259 | goto exit; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | cinfo.err = jpeg_std_error(&jerr); |
| 264 | jpeg_create_decompress(&cinfo); |
| 265 | |
| 266 | /* Specify data source for decompression */ |
| 267 | jpeg_stdio_src(&cinfo, fp); |
| 268 | |
| 269 | /* Read file header, set default decompression parameters */ |
| 270 | if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) |
| 271 | goto exit; |
| 272 | |
| 273 | /* Start decompressor */ |
| 274 | (void) jpeg_start_decompress(&cinfo); |
| 275 | |
| 276 | size_t width = cinfo.image_width; |
| 277 | size_t height = cinfo.image_height; |
| 278 | size_t stride = 4 * width; |
| 279 | size_t pixelSize = stride * height; |
| 280 | |
| 281 | surface = malloc(sizeof(GGLSurface) + pixelSize); |
| 282 | if (surface == NULL) { |
| 283 | result = -8; |
| 284 | goto exit; |
| 285 | } |
| 286 | |
| 287 | unsigned char* pData = (unsigned char*) (surface + 1); |
| 288 | surface->version = sizeof(GGLSurface); |
| 289 | surface->width = width; |
| 290 | surface->height = height; |
| 291 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 292 | surface->data = pData; |
| 293 | surface->format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 294 | |
| 295 | int y; |
| 296 | for (y = 0; y < (int) height; ++y) { |
| 297 | unsigned char* pRow = pData + y * stride; |
| 298 | jpeg_read_scanlines(&cinfo, &pRow, 1); |
| 299 | |
| 300 | int x; |
| 301 | for(x = width - 1; x >= 0; x--) { |
| 302 | int sx = x * 3; |
| 303 | int dx = x * 4; |
| 304 | unsigned char r = pRow[sx]; |
| 305 | unsigned char g = pRow[sx + 1]; |
| 306 | unsigned char b = pRow[sx + 2]; |
| 307 | unsigned char a = 0xff; |
| 308 | pRow[dx ] = r; // r |
| 309 | pRow[dx + 1] = g; // g |
| 310 | pRow[dx + 2] = b; // b |
| 311 | pRow[dx + 3] = a; |
| 312 | } |
| 313 | } |
| 314 | *pSurface = (gr_surface) surface; |
| 315 | |
| 316 | exit: |
| 317 | if (fp != NULL) |
| 318 | { |
| 319 | if (surface) |
| 320 | { |
| 321 | (void) jpeg_finish_decompress(&cinfo); |
| 322 | if (result < 0) |
| 323 | { |
| 324 | free(surface); |
| 325 | } |
| 326 | } |
| 327 | jpeg_destroy_decompress(&cinfo); |
| 328 | fclose(fp); |
| 329 | } |
| 330 | return result; |
| 331 | } |
| 332 | |
| 333 | int res_create_surface(const char* name, gr_surface* pSurface) { |
| 334 | int ret; |
| 335 | |
| 336 | if (!name) return -1; |
| 337 | |
| 338 | if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0) |
| 339 | return res_create_surface_jpg(name,pSurface); |
| 340 | |
| 341 | ret = res_create_surface_png(name,pSurface); |
| 342 | if (ret < 0) |
| 343 | ret = res_create_surface_jpg(name,pSurface); |
| 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | void res_free_surface(gr_surface surface) { |
| 349 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 350 | if (pSurface) { |
| 351 | free(pSurface); |
| 352 | } |
| 353 | } |