7 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
10 png_structp png_ptr = NULL;
11 png_infop info_ptr = NULL;
12 png_bytepp row_ptr = NULL;
15 if (dest == NULL || fname == NULL)
20 fp = fopen(fname, "rb");
23 lprintf(__FILE__ ": failed to open: %s\n", fname);
27 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
30 lprintf(__FILE__ ": png_create_read_struct() failed\n");
35 info_ptr = png_create_info_struct(png_ptr);
38 lprintf(__FILE__ ": png_create_info_struct() failed\n");
43 png_init_io(png_ptr, fp);
44 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
45 row_ptr = png_get_rows(png_ptr, info_ptr);
48 lprintf(__FILE__ ": png_get_rows() failed\n");
52 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
59 unsigned short *dst = dest;
60 if (info_ptr->pixel_depth != 24)
62 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
65 height = info_ptr->height;
68 width = info_ptr->width;
72 for (h = 0; h < height; h++)
74 unsigned char *src = row_ptr[h];
79 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
81 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
93 unsigned char *dst = dest;
94 if (info_ptr->width != req_w || info_ptr->height != req_h)
96 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
97 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
100 if (info_ptr->pixel_depth != 8)
102 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
105 for (y = 0; y < 16; y++)
107 for (x = 0; x < 16; x++)
109 /* 16x16 grid of syms */
110 int sym_w = req_w / 16;
111 int sym_h = req_h / 16;
112 for (y1 = 0; y1 < sym_h; y1++)
114 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
115 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
116 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
123 case READPNG_SELECTOR:
126 unsigned char *dst = dest;
127 if (info_ptr->width != req_w || info_ptr->height != req_h)
129 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
130 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
133 if (info_ptr->pixel_depth != 8)
135 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
138 for (y1 = 0; y1 < req_h; y1++)
140 unsigned char *src = row_ptr[y1];
141 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
142 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
149 int height, width, h;
150 unsigned char *dst = dest;
151 if (info_ptr->pixel_depth != 24)
153 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
156 height = info_ptr->height;
159 width = info_ptr->width;
163 for (h = 0; h < height; h++)
166 unsigned char *src = row_ptr[h];
167 dst += (req_w - width) * 3;
168 for (len = width; len > 0; len--, dst+=3, src+=3)
169 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
178 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);