2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2011
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.
9 * See the COPYING file in the top-level directory.
19 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
22 png_structp png_ptr = NULL;
23 png_infop info_ptr = NULL;
24 png_bytepp row_ptr = NULL;
27 if (dest == NULL || fname == NULL)
32 fp = fopen(fname, "rb");
35 lprintf(__FILE__ ": failed to open: %s\n", fname);
39 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
42 lprintf(__FILE__ ": png_create_read_struct() failed\n");
47 info_ptr = png_create_info_struct(png_ptr);
50 lprintf(__FILE__ ": png_create_info_struct() failed\n");
55 png_init_io(png_ptr, fp);
56 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
57 row_ptr = png_get_rows(png_ptr, info_ptr);
60 lprintf(__FILE__ ": png_get_rows() failed\n");
64 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
71 unsigned short *dst = dest;
72 if (info_ptr->pixel_depth != 24)
74 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
77 height = info_ptr->height;
80 width = info_ptr->width;
84 for (h = 0; h < height; h++)
86 unsigned char *src = row_ptr[h];
91 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
93 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
105 unsigned char *dst = dest;
106 if (info_ptr->width != req_w || info_ptr->height != req_h)
108 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
109 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
112 if (info_ptr->pixel_depth != 8)
114 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
117 for (y = 0; y < 16; y++)
119 for (x = 0; x < 16; x++)
121 /* 16x16 grid of syms */
122 int sym_w = req_w / 16;
123 int sym_h = req_h / 16;
124 for (y1 = 0; y1 < sym_h; y1++)
126 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
127 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
128 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
135 case READPNG_SELECTOR:
138 unsigned char *dst = dest;
139 if (info_ptr->width != req_w || info_ptr->height != req_h)
141 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
142 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
145 if (info_ptr->pixel_depth != 8)
147 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
150 for (y1 = 0; y1 < req_h; y1++)
152 unsigned char *src = row_ptr[y1];
153 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
154 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
161 int height, width, h;
162 unsigned char *dst = dest;
163 if (info_ptr->pixel_depth != 24)
165 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
168 height = info_ptr->height;
171 width = info_ptr->width;
175 for (h = 0; h < height; h++)
178 unsigned char *src = row_ptr[h];
179 dst += (req_w - width) * 3;
180 for (len = width; len > 0; len--, dst+=3, src+=3)
181 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
190 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
195 int writepng(const char *fname, unsigned short *src, int w, int h)
197 png_structp png_ptr = NULL;
198 png_infop info_ptr = NULL;
199 png_bytepp row_pointers;
203 f = fopen(fname, "wb");
205 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
209 row_pointers = calloc(h, sizeof(row_pointers[0]));
210 if (row_pointers == NULL)
213 for (i = 0; i < h; i++) {
214 unsigned char *dst = malloc(w * 3);
217 row_pointers[i] = dst;
218 for (j = 0; j < w; j++, src++, dst += 3) {
219 dst[0] = (*src & 0xf800) >> 8;
220 dst[1] = (*src & 0x07e0) >> 3;
221 dst[2] = (*src & 0x001f) << 3;
225 /* initialize stuff */
226 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
227 if (png_ptr == NULL) {
228 fprintf(stderr, "png_create_write_struct() failed");
232 info_ptr = png_create_info_struct(png_ptr);
233 if (info_ptr == NULL) {
234 fprintf(stderr, "png_create_info_struct() failed");
238 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
239 fprintf(stderr, "error in png code\n");
243 png_init_io(png_ptr, f);
245 png_set_IHDR(png_ptr, info_ptr, w, h,
246 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
247 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
249 png_write_info(png_ptr, info_ptr);
250 png_write_image(png_ptr, row_pointers);
251 png_write_end(png_ptr, NULL);
256 // png_destroy_info_struct(png_ptr, &info_ptr); // freed below
258 png_destroy_write_struct(&png_ptr, &info_ptr);
260 for (i = 0; i < h; i++)
261 free(row_pointers[i]);