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.
8 * See the COPYING file in the top-level directory.
18 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
21 png_structp png_ptr = NULL;
22 png_infop info_ptr = NULL;
23 png_bytepp row_ptr = NULL;
26 if (dest == NULL || fname == NULL)
31 fp = fopen(fname, "rb");
34 lprintf(__FILE__ ": failed to open: %s\n", fname);
38 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
41 lprintf(__FILE__ ": png_create_read_struct() failed\n");
46 info_ptr = png_create_info_struct(png_ptr);
49 lprintf(__FILE__ ": png_create_info_struct() failed\n");
54 png_init_io(png_ptr, fp);
55 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
56 row_ptr = png_get_rows(png_ptr, info_ptr);
59 lprintf(__FILE__ ": png_get_rows() failed\n");
63 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
70 unsigned short *dst = dest;
71 if (info_ptr->pixel_depth != 24)
73 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
76 height = info_ptr->height;
79 width = info_ptr->width;
83 for (h = 0; h < height; h++)
85 unsigned char *src = row_ptr[h];
90 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
92 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
104 unsigned char *dst = dest;
105 if (info_ptr->width != req_w || info_ptr->height != req_h)
107 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
108 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
111 if (info_ptr->pixel_depth != 8)
113 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
116 for (y = 0; y < 16; y++)
118 for (x = 0; x < 16; x++)
120 /* 16x16 grid of syms */
121 int sym_w = req_w / 16;
122 int sym_h = req_h / 16;
123 for (y1 = 0; y1 < sym_h; y1++)
125 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
126 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
127 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
134 case READPNG_SELECTOR:
137 unsigned char *dst = dest;
138 if (info_ptr->width != req_w || info_ptr->height != req_h)
140 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
141 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
144 if (info_ptr->pixel_depth != 8)
146 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
149 for (y1 = 0; y1 < req_h; y1++)
151 unsigned char *src = row_ptr[y1];
152 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
153 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
160 int height, width, h;
161 unsigned char *dst = dest;
162 if (info_ptr->pixel_depth != 24)
164 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
167 height = info_ptr->height;
170 width = info_ptr->width;
174 for (h = 0; h < height; h++)
177 unsigned char *src = row_ptr[h];
178 dst += (req_w - width) * 3;
179 for (len = width; len > 0; len--, dst+=3, src+=3)
180 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
189 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
194 int writepng(const char *fname, unsigned short *src, int w, int h)
196 png_structp png_ptr = NULL;
197 png_infop info_ptr = NULL;
198 png_bytepp row_pointers;
202 f = fopen(fname, "wb");
204 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
208 row_pointers = calloc(h, sizeof(row_pointers[0]));
209 if (row_pointers == NULL)
212 for (i = 0; i < h; i++) {
213 unsigned char *dst = malloc(w * 3);
216 row_pointers[i] = dst;
217 for (j = 0; j < w; j++, src++, dst += 3) {
218 dst[0] = (*src & 0xf800) >> 8;
219 dst[1] = (*src & 0x07e0) >> 3;
220 dst[2] = (*src & 0x001f) << 3;
224 /* initialize stuff */
225 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
226 if (png_ptr == NULL) {
227 fprintf(stderr, "png_create_write_struct() failed");
231 info_ptr = png_create_info_struct(png_ptr);
232 if (info_ptr == NULL) {
233 fprintf(stderr, "png_create_info_struct() failed");
237 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
238 fprintf(stderr, "error in png code\n");
242 png_init_io(png_ptr, f);
244 png_set_IHDR(png_ptr, info_ptr, w, h,
245 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
246 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
248 png_write_info(png_ptr, info_ptr);
249 png_write_image(png_ptr, row_pointers);
250 png_write_end(png_ptr, NULL);
255 // png_destroy_info_struct(png_ptr, &info_ptr); // freed below
257 png_destroy_write_struct(&png_ptr, &info_ptr);
259 for (i = 0; i < h; i++)
260 free(row_pointers[i]);