6 void readpng(void *dest, const char *fname, readpng_what what)
9 png_structp png_ptr = NULL;
10 png_infop info_ptr = NULL;
11 png_bytepp row_ptr = NULL;
13 if (dest == NULL || fname == NULL)
18 fp = fopen(fname, "rb");
21 printf(__FILE__ ": failed to open: %s\n", fname);
25 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
28 printf(__FILE__ ": png_create_read_struct() failed\n");
33 info_ptr = png_create_info_struct(png_ptr);
36 printf(__FILE__ ": png_create_info_struct() failed\n");
41 png_init_io(png_ptr, fp);
42 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
43 row_ptr = png_get_rows(png_ptr, info_ptr);
46 printf(__FILE__ ": png_get_rows() failed\n");
50 // printf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
57 unsigned short *dst = dest;
58 if (info_ptr->pixel_depth != 24)
60 printf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
63 height = info_ptr->height;
64 if (height > 240) height = 240;
65 width = info_ptr->width;
66 if (width > 320) width = 320;
68 for (h = 0; h < height; h++)
70 unsigned char *src = row_ptr[h];
74 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3);
85 unsigned char *dst = dest;
86 if (info_ptr->width != 128 || info_ptr->height != 160)
88 printf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n",
89 (int)info_ptr->width, (int)info_ptr->height);
92 if (info_ptr->pixel_depth != 8)
94 printf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
97 for (y = 0; y < 16; y++)
99 for (x = 0; x < 16; x++)
101 for (y1 = 0; y1 < 10; y1++)
103 unsigned char *src = row_ptr[y*10 + y1] + x*8;
104 for (x1 = 8/2; x1 > 0; x1--, src+=2)
105 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
112 case READPNG_SELECTOR:
115 unsigned char *dst = dest;
116 if (info_ptr->width != 8 || info_ptr->height != 10)
118 printf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n",
119 (int)info_ptr->width, (int)info_ptr->height);
122 if (info_ptr->pixel_depth != 8)
124 printf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
127 for (y1 = 0; y1 < 10; y1++)
129 unsigned char *src = row_ptr[y1];
130 for (x1 = 8/2; x1 > 0; x1--, src+=2)
131 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
139 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);