7 void readpng(void *dest, const char *fname, readpng_what what)
10 png_structp png_ptr = NULL;
11 png_infop info_ptr = NULL;
12 png_bytepp row_ptr = NULL;
14 if (dest == NULL || fname == NULL)
19 fp = fopen(fname, "rb");
22 lprintf(__FILE__ ": failed to open: %s\n", fname);
26 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
29 lprintf(__FILE__ ": png_create_read_struct() failed\n");
34 info_ptr = png_create_info_struct(png_ptr);
37 lprintf(__FILE__ ": png_create_info_struct() failed\n");
42 png_init_io(png_ptr, fp);
43 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
44 row_ptr = png_get_rows(png_ptr, info_ptr);
47 lprintf(__FILE__ ": png_get_rows() failed\n");
51 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
58 unsigned short *dst = dest;
59 if (info_ptr->pixel_depth != 24)
61 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
64 height = info_ptr->height;
65 if (height > 240) height = 240;
66 width = info_ptr->width;
67 if (width > 320) width = 320;
69 for (h = 0; h < height; h++)
71 unsigned char *src = row_ptr[h];
75 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3);
86 unsigned char *dst = dest;
87 if (info_ptr->width != 128 || info_ptr->height != 160)
89 lprintf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n",
90 (int)info_ptr->width, (int)info_ptr->height);
93 if (info_ptr->pixel_depth != 8)
95 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
98 for (y = 0; y < 16; y++)
100 for (x = 0; x < 16; x++)
102 for (y1 = 0; y1 < 10; y1++)
104 unsigned char *src = row_ptr[y*10 + y1] + x*8;
105 for (x1 = 8/2; x1 > 0; x1--, src+=2)
106 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
113 case READPNG_SELECTOR:
116 unsigned char *dst = dest;
117 if (info_ptr->width != 8 || info_ptr->height != 10)
119 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n",
120 (int)info_ptr->width, (int)info_ptr->height);
123 if (info_ptr->pixel_depth != 8)
125 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
128 for (y1 = 0; y1 < 10; y1++)
130 unsigned char *src = row_ptr[y1];
131 for (x1 = 8/2; x1 > 0; x1--, src+=2)
132 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
140 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);