| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2008-2010 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 6 | * - GNU GPL, version 2 or later. |
| 7 | * - GNU LGPL, version 2.1 or later. |
| 8 | * See the COPYING file in the top-level directory. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <png.h> |
| 14 | #include "readpng.h" |
| 15 | #include "lprintf.h" |
| 16 | |
| 17 | int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h) |
| 18 | { |
| 19 | FILE *fp; |
| 20 | png_structp png_ptr = NULL; |
| 21 | png_infop info_ptr = NULL; |
| 22 | png_bytepp row_ptr = NULL; |
| 23 | int ret = -1; |
| 24 | |
| 25 | if (dest == NULL || fname == NULL) |
| 26 | { |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | fp = fopen(fname, "rb"); |
| 31 | if (fp == NULL) |
| 32 | { |
| 33 | lprintf(__FILE__ ": failed to open: %s\n", fname); |
| 34 | return -1; |
| 35 | } |
| 36 | |
| 37 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 38 | if (!png_ptr) |
| 39 | { |
| 40 | lprintf(__FILE__ ": png_create_read_struct() failed\n"); |
| 41 | fclose(fp); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | info_ptr = png_create_info_struct(png_ptr); |
| 46 | if (!info_ptr) |
| 47 | { |
| 48 | lprintf(__FILE__ ": png_create_info_struct() failed\n"); |
| 49 | goto done; |
| 50 | } |
| 51 | |
| 52 | // Start reading |
| 53 | png_init_io(png_ptr, fp); |
| 54 | png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL); |
| 55 | row_ptr = png_get_rows(png_ptr, info_ptr); |
| 56 | if (row_ptr == NULL) |
| 57 | { |
| 58 | lprintf(__FILE__ ": png_get_rows() failed\n"); |
| 59 | goto done; |
| 60 | } |
| 61 | |
| 62 | // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth); |
| 63 | |
| 64 | switch (what) |
| 65 | { |
| 66 | case READPNG_BG: |
| 67 | { |
| 68 | int height, width, h; |
| 69 | unsigned short *dst = dest; |
| 70 | if (info_ptr->pixel_depth != 24) |
| 71 | { |
| 72 | lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth); |
| 73 | break; |
| 74 | } |
| 75 | height = info_ptr->height; |
| 76 | if (height > req_h) |
| 77 | height = req_h; |
| 78 | width = info_ptr->width; |
| 79 | if (width > req_w) |
| 80 | width = req_w; |
| 81 | |
| 82 | for (h = 0; h < height; h++) |
| 83 | { |
| 84 | unsigned char *src = row_ptr[h]; |
| 85 | int len = width; |
| 86 | while (len--) |
| 87 | { |
| 88 | #ifdef PSP |
| 89 | *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR |
| 90 | #else |
| 91 | *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB |
| 92 | #endif |
| 93 | src += 3; |
| 94 | } |
| 95 | dst += req_w - width; |
| 96 | } |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | case READPNG_FONT: |
| 101 | { |
| 102 | int x, y, x1, y1; |
| 103 | unsigned char *dst = dest; |
| 104 | if (info_ptr->width != req_w || info_ptr->height != req_h) |
| 105 | { |
| 106 | lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n", |
| 107 | (int)info_ptr->width, (int)info_ptr->height, req_w, req_h); |
| 108 | break; |
| 109 | } |
| 110 | if (info_ptr->pixel_depth != 8) |
| 111 | { |
| 112 | lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth); |
| 113 | break; |
| 114 | } |
| 115 | for (y = 0; y < 16; y++) |
| 116 | { |
| 117 | for (x = 0; x < 16; x++) |
| 118 | { |
| 119 | /* 16x16 grid of syms */ |
| 120 | int sym_w = req_w / 16; |
| 121 | int sym_h = req_h / 16; |
| 122 | for (y1 = 0; y1 < sym_h; y1++) |
| 123 | { |
| 124 | unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w; |
| 125 | for (x1 = sym_w/2; x1 > 0; x1--, src+=2) |
| 126 | *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | case READPNG_SELECTOR: |
| 134 | { |
| 135 | int x1, y1; |
| 136 | unsigned char *dst = dest; |
| 137 | if (info_ptr->width != req_w || info_ptr->height != req_h) |
| 138 | { |
| 139 | lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n", |
| 140 | (int)info_ptr->width, (int)info_ptr->height, req_w, req_h); |
| 141 | break; |
| 142 | } |
| 143 | if (info_ptr->pixel_depth != 8) |
| 144 | { |
| 145 | lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth); |
| 146 | break; |
| 147 | } |
| 148 | for (y1 = 0; y1 < req_h; y1++) |
| 149 | { |
| 150 | unsigned char *src = row_ptr[y1]; |
| 151 | for (x1 = req_w/2; x1 > 0; x1--, src+=2) |
| 152 | *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4); |
| 153 | } |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | case READPNG_24: |
| 158 | { |
| 159 | int height, width, h; |
| 160 | unsigned char *dst = dest; |
| 161 | if (info_ptr->pixel_depth != 24) |
| 162 | { |
| 163 | lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth); |
| 164 | break; |
| 165 | } |
| 166 | height = info_ptr->height; |
| 167 | if (height > req_h) |
| 168 | height = req_h; |
| 169 | width = info_ptr->width; |
| 170 | if (width > req_w) |
| 171 | width = req_w; |
| 172 | |
| 173 | for (h = 0; h < height; h++) |
| 174 | { |
| 175 | int len = width; |
| 176 | unsigned char *src = row_ptr[h]; |
| 177 | dst += (req_w - width) * 3; |
| 178 | for (len = width; len > 0; len--, dst+=3, src+=3) |
| 179 | dst[0] = src[2], dst[1] = src[1], dst[2] = src[0]; |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | ret = 0; |
| 187 | done: |
| 188 | png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL); |
| 189 | fclose(fp); |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | |