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.
20 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
23 png_structp png_ptr = NULL;
24 png_infop info_ptr = NULL;
25 png_bytepp row_ptr = NULL;
28 if (dest == NULL || fname == NULL)
33 fp = fopen(fname, "rb");
36 lprintf(__FILE__ ": failed to open: %s\n", fname);
40 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
43 lprintf(__FILE__ ": png_create_read_struct() failed\n");
48 info_ptr = png_create_info_struct(png_ptr);
51 lprintf(__FILE__ ": png_create_info_struct() failed\n");
56 png_init_io(png_ptr, fp);
57 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
58 row_ptr = png_get_rows(png_ptr, info_ptr);
61 lprintf(__FILE__ ": png_get_rows() failed\n");
65 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)png_get_image_width(png_ptr, info_ptr),
66 // (int)png_get_image_height(png_ptr, info_ptr), png_get_bit_depth(png_ptr, info_ptr));
72 int height, width, x_ofs = 0, y_ofs = 0;
73 unsigned short *dst = dest;
74 int x_scale, y_scale, x_pos, y_pos; // Q16
76 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
78 lprintf(__FILE__ ": scaled image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
81 width = png_get_image_width(png_ptr, info_ptr);
82 x_scale = width*65536 / req_w;
83 height = png_get_image_height(png_ptr, info_ptr);
84 y_scale = height*65536 / req_h;
85 if (x_scale < y_scale)
87 else y_scale = x_scale;
88 x_ofs = req_w - width*65536 / x_scale;
89 y_ofs = req_h - height*65536 / y_scale;
91 dst += y_ofs/2*req_w + x_ofs/2;
92 for (y_pos = 0; y_pos < height*65536; y_pos += y_scale+1)
94 unsigned char *src = row_ptr[y_pos >> 16];
96 for (x_pos = 0; x_pos < width*65536; x_pos += x_scale+1, len++)
98 int o = 3*(x_pos >> 16);
99 // TODO: could use bilinear if upsampling?
100 *dst++ = PXMAKE(src[o], src[o+1], src[o+2]);
109 int height, width, h, x_ofs = 0, y_ofs = 0;
110 unsigned short *dst = dest;
112 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
114 lprintf(__FILE__ ": bg image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
117 width = png_get_image_width(png_ptr, info_ptr);
119 x_ofs = (width - req_w) / 2;
122 dst += (req_w - width) / 2;
123 height = png_get_image_height(png_ptr, info_ptr);
124 if (height > req_h) {
125 y_ofs = (height - req_h) / 2;
128 dst += (req_h - height) / 2 * req_w;
130 for (h = 0; h < height; h++)
132 unsigned char *src = row_ptr[h + y_ofs] + x_ofs * 3;
136 *dst++ = PXMAKE(src[0], src[1], src[2]);
139 dst += req_w - width;
147 unsigned char *dst = dest;
148 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
150 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
151 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
154 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
156 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
159 for (y = 0; y < 16; y++)
161 for (x = 0; x < 16; x++)
163 /* 16x16 grid of syms */
164 int sym_w = req_w / 16;
165 int sym_h = req_h / 16;
166 for (y1 = 0; y1 < sym_h; y1++)
168 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
169 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
170 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
177 case READPNG_SELECTOR:
180 unsigned char *dst = dest;
181 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
183 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
184 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
187 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
189 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
192 for (y1 = 0; y1 < req_h; y1++)
194 unsigned char *src = row_ptr[y1];
195 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
196 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
203 int height, width, h;
204 unsigned char *dst = dest;
205 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
207 lprintf(__FILE__ ": image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
210 width = png_get_image_width(png_ptr, info_ptr);
213 height = png_get_image_height(png_ptr, info_ptr);
217 for (h = 0; h < height; h++)
220 unsigned char *src = row_ptr[h];
221 dst += (req_w - width) * 3;
222 for (len = width; len > 0; len--, dst+=3, src+=3)
223 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
232 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
237 int writepngpp(const char *fname, unsigned short *src, int w, int h, int pitch)
239 png_structp png_ptr = NULL;
240 png_infop info_ptr = NULL;
241 png_bytepp row_pointers;
245 f = fopen(fname, "wb");
247 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
251 row_pointers = calloc(h, sizeof(row_pointers[0]));
252 if (row_pointers == NULL)
255 for (i = 0; i < h; i++) {
256 unsigned char *dst = malloc(w * 3);
259 row_pointers[i] = dst;
260 for (j = 0; j < w; j++, src++, dst += 3) {
261 dst[0] = PXGETR(*src);
262 dst[1] = PXGETG(*src);
263 dst[2] = PXGETB(*src);
268 /* initialize stuff */
269 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
270 if (png_ptr == NULL) {
271 fprintf(stderr, "png_create_write_struct() failed");
275 info_ptr = png_create_info_struct(png_ptr);
276 if (info_ptr == NULL) {
277 fprintf(stderr, "png_create_info_struct() failed");
281 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
282 fprintf(stderr, "error in png code\n");
286 png_init_io(png_ptr, f);
288 png_set_IHDR(png_ptr, info_ptr, w, h,
289 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
290 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
292 png_write_info(png_ptr, info_ptr);
293 png_write_image(png_ptr, row_pointers);
294 png_write_end(png_ptr, NULL);
299 // png_destroy_info_struct(png_ptr, &info_ptr); // freed below
301 png_destroy_write_struct(&png_ptr, &info_ptr);
303 for (i = 0; i < h; i++)
304 free(row_pointers[i]);
311 int writepng(const char *fname, unsigned short *src, int w, int h)
313 return writepngpp(fname, src, w, h, w);