2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
4 * This work is licensed under the terms of any of these licenses
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.
17 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
20 png_structp png_ptr = NULL;
21 png_infop info_ptr = NULL;
22 png_bytepp row_ptr = NULL;
25 if (dest == NULL || fname == NULL)
30 fp = fopen(fname, "rb");
33 lprintf(__FILE__ ": failed to open: %s\n", fname);
37 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
40 lprintf(__FILE__ ": png_create_read_struct() failed\n");
45 info_ptr = png_create_info_struct(png_ptr);
48 lprintf(__FILE__ ": png_create_info_struct() failed\n");
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);
58 lprintf(__FILE__ ": png_get_rows() failed\n");
62 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
69 unsigned short *dst = dest;
70 if (info_ptr->pixel_depth != 24)
72 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
75 height = info_ptr->height;
78 width = info_ptr->width;
82 for (h = 0; h < height; h++)
84 unsigned char *src = row_ptr[h];
89 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
91 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
103 unsigned char *dst = dest;
104 if (info_ptr->width != req_w || info_ptr->height != req_h)
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);
110 if (info_ptr->pixel_depth != 8)
112 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
115 for (y = 0; y < 16; y++)
117 for (x = 0; x < 16; x++)
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++)
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);
133 case READPNG_SELECTOR:
136 unsigned char *dst = dest;
137 if (info_ptr->width != req_w || info_ptr->height != req_h)
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);
143 if (info_ptr->pixel_depth != 8)
145 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
148 for (y1 = 0; y1 < req_h; y1++)
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);
159 int height, width, h;
160 unsigned char *dst = dest;
161 if (info_ptr->pixel_depth != 24)
163 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
166 height = info_ptr->height;
169 width = info_ptr->width;
173 for (h = 0; h < height; h++)
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];
188 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);