15 int readpng(void *dest, const char *fname, readpng_what what)
18 png_structp png_ptr = NULL;
19 png_infop info_ptr = NULL;
20 png_bytepp row_ptr = NULL;
23 if (dest == NULL || fname == NULL)
28 fp = fopen(fname, "rb");
31 lprintf(__FILE__ ": failed to open: %s\n", fname);
35 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
38 lprintf(__FILE__ ": png_create_read_struct() failed\n");
43 info_ptr = png_create_info_struct(png_ptr);
46 lprintf(__FILE__ ": png_create_info_struct() failed\n");
51 png_init_io(png_ptr, fp);
52 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
53 row_ptr = png_get_rows(png_ptr, info_ptr);
56 lprintf(__FILE__ ": png_get_rows() failed\n");
60 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
67 unsigned short *dst = dest;
68 if (info_ptr->pixel_depth != 24)
70 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
73 height = info_ptr->height;
74 if (height > BG_HEIGHT) height = BG_HEIGHT;
75 width = info_ptr->width;
76 if (width > BG_WIDTH) width = BG_WIDTH;
78 for (h = 0; h < height; h++)
80 unsigned char *src = row_ptr[h];
85 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
87 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
91 dst += BG_WIDTH - width;
99 unsigned char *dst = dest;
100 if (info_ptr->width != 128 || info_ptr->height != 160)
102 lprintf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n",
103 (int)info_ptr->width, (int)info_ptr->height);
106 if (info_ptr->pixel_depth != 8)
108 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
111 for (y = 0; y < 16; y++)
113 for (x = 0; x < 16; x++)
115 for (y1 = 0; y1 < 10; y1++)
117 unsigned char *src = row_ptr[y*10 + y1] + x*8;
118 for (x1 = 8/2; x1 > 0; x1--, src+=2)
119 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
126 case READPNG_SELECTOR:
129 unsigned char *dst = dest;
130 if (info_ptr->width != 8 || info_ptr->height != 10)
132 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n",
133 (int)info_ptr->width, (int)info_ptr->height);
136 if (info_ptr->pixel_depth != 8)
138 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
141 for (y1 = 0; y1 < 10; y1++)
143 unsigned char *src = row_ptr[y1];
144 for (x1 = 8/2; x1 > 0; x1--, src+=2)
145 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
153 int height, width, h;
154 int needw = (what == READPNG_480_24) ? 480 : 320;
155 unsigned char *dst = dest;
156 if (info_ptr->pixel_depth != 24)
158 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
161 height = info_ptr->height;
162 if (height > 240) height = 240;
163 width = info_ptr->width;
164 if (width > needw) width = needw;
166 for (h = 0; h < height; h++)
169 unsigned char *src = row_ptr[h];
170 dst += (needw - width) * 3;
171 for (len = width; len > 0; len--, dst+=3, src+=3)
172 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
181 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);