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)png_get_image_width(png_ptr, info_ptr),
65 // (int)png_get_image_height(png_ptr, info_ptr), png_get_bit_depth(png_ptr, info_ptr));
72 unsigned short *dst = dest;
73 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
75 lprintf(__FILE__ ": bg image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
78 width = png_get_image_width(png_ptr, info_ptr);
81 height = png_get_image_height(png_ptr, info_ptr);
85 for (h = 0; h < height; h++)
87 unsigned char *src = row_ptr[h];
92 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
94 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
106 unsigned char *dst = dest;
107 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
109 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
110 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
113 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
115 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
118 for (y = 0; y < 16; y++)
120 for (x = 0; x < 16; x++)
122 /* 16x16 grid of syms */
123 int sym_w = req_w / 16;
124 int sym_h = req_h / 16;
125 for (y1 = 0; y1 < sym_h; y1++)
127 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
128 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
129 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
136 case READPNG_SELECTOR:
139 unsigned char *dst = dest;
140 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
142 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
143 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
146 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
148 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
151 for (y1 = 0; y1 < req_h; y1++)
153 unsigned char *src = row_ptr[y1];
154 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
155 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
162 int height, width, h;
163 unsigned char *dst = dest;
164 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
166 lprintf(__FILE__ ": image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
169 width = png_get_image_width(png_ptr, info_ptr);
172 height = png_get_image_height(png_ptr, info_ptr);
176 for (h = 0; h < height; h++)
179 unsigned char *src = row_ptr[h];
180 dst += (req_w - width) * 3;
181 for (len = width; len > 0; len--, dst+=3, src+=3)
182 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
191 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
196 int writepng(const char *fname, unsigned short *src, int w, int h)
198 png_structp png_ptr = NULL;
199 png_infop info_ptr = NULL;
200 png_bytepp row_pointers;
204 f = fopen(fname, "wb");
206 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
210 row_pointers = calloc(h, sizeof(row_pointers[0]));
211 if (row_pointers == NULL)
214 for (i = 0; i < h; i++) {
215 unsigned char *dst = malloc(w * 3);
218 row_pointers[i] = dst;
219 for (j = 0; j < w; j++, src++, dst += 3) {
220 dst[0] = (*src & 0xf800) >> 8;
221 dst[1] = (*src & 0x07e0) >> 3;
222 dst[2] = (*src & 0x001f) << 3;
226 /* initialize stuff */
227 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
228 if (png_ptr == NULL) {
229 fprintf(stderr, "png_create_write_struct() failed");
233 info_ptr = png_create_info_struct(png_ptr);
234 if (info_ptr == NULL) {
235 fprintf(stderr, "png_create_info_struct() failed");
239 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
240 fprintf(stderr, "error in png code\n");
244 png_init_io(png_ptr, f);
246 png_set_IHDR(png_ptr, info_ptr, w, h,
247 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
248 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
250 png_write_info(png_ptr, info_ptr);
251 png_write_image(png_ptr, row_pointers);
252 png_write_end(png_ptr, NULL);
257 // png_destroy_info_struct(png_ptr, &info_ptr); // freed below
259 png_destroy_write_struct(&png_ptr, &info_ptr);
261 for (i = 0; i < h; i++)
262 free(row_pointers[i]);