fix corruption on empty sel_fname
[libpicofe.git] / readpng.c
CommitLineData
b7c7cd5d 1/*
7fd8dbbb 2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2011
b7c7cd5d 3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
f89d8471 8 * - MAME license.
b7c7cd5d 9 * See the COPYING file in the top-level directory.
10 */
11
13059a60 12#include <stdio.h>
7fd8dbbb 13#include <stdlib.h>
13059a60 14#include <string.h>
15#include <png.h>
16#include "readpng.h"
c7a4ff64 17#include "lprintf.h"
13059a60 18
f6eaae4f 19int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
13059a60 20{
21 FILE *fp;
22 png_structp png_ptr = NULL;
23 png_infop info_ptr = NULL;
24 png_bytepp row_ptr = NULL;
ff63afa1 25 int ret = -1;
13059a60 26
27 if (dest == NULL || fname == NULL)
28 {
ff63afa1 29 return -1;
13059a60 30 }
31
32 fp = fopen(fname, "rb");
33 if (fp == NULL)
34 {
c7a4ff64 35 lprintf(__FILE__ ": failed to open: %s\n", fname);
ff63afa1 36 return -1;
13059a60 37 }
38
39 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
40 if (!png_ptr)
41 {
c7a4ff64 42 lprintf(__FILE__ ": png_create_read_struct() failed\n");
13059a60 43 fclose(fp);
ff63afa1 44 return -1;
13059a60 45 }
46
47 info_ptr = png_create_info_struct(png_ptr);
48 if (!info_ptr)
49 {
c7a4ff64 50 lprintf(__FILE__ ": png_create_info_struct() failed\n");
13059a60 51 goto done;
52 }
53
54 // Start reading
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);
58 if (row_ptr == NULL)
59 {
c7a4ff64 60 lprintf(__FILE__ ": png_get_rows() failed\n");
13059a60 61 goto done;
62 }
63
a085ae5e 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));
13059a60 66
67 switch (what)
68 {
69 case READPNG_BG:
70 {
71 int height, width, h;
72 unsigned short *dst = dest;
a085ae5e 73 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
13059a60 74 {
a085ae5e 75 lprintf(__FILE__ ": bg image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
13059a60 76 break;
77 }
a085ae5e 78 width = png_get_image_width(png_ptr, info_ptr);
f6eaae4f 79 if (width > req_w)
80 width = req_w;
a085ae5e 81 height = png_get_image_height(png_ptr, info_ptr);
82 if (height > req_h)
83 height = req_h;
13059a60 84
85 for (h = 0; h < height; h++)
86 {
87 unsigned char *src = row_ptr[h];
88 int len = width;
89 while (len--)
90 {
703e4c7b 91#ifdef PSP
92 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
93#else
94 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
95#endif
13059a60 96 src += 3;
97 }
f6eaae4f 98 dst += req_w - width;
13059a60 99 }
100 break;
101 }
102
103 case READPNG_FONT:
104 {
105 int x, y, x1, y1;
106 unsigned char *dst = dest;
a085ae5e 107 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
13059a60 108 {
f6eaae4f 109 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
a085ae5e 110 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
13059a60 111 break;
112 }
a085ae5e 113 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
13059a60 114 {
a085ae5e 115 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
13059a60 116 break;
117 }
118 for (y = 0; y < 16; y++)
119 {
120 for (x = 0; x < 16; x++)
121 {
f6eaae4f 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++)
13059a60 126 {
f6eaae4f 127 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
128 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
13059a60 129 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
130 }
131 }
132 }
133 break;
134 }
135
136 case READPNG_SELECTOR:
137 {
138 int x1, y1;
139 unsigned char *dst = dest;
a085ae5e 140 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
13059a60 141 {
f6eaae4f 142 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
a085ae5e 143 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
13059a60 144 break;
145 }
a085ae5e 146 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
13059a60 147 {
a085ae5e 148 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
13059a60 149 break;
150 }
f6eaae4f 151 for (y1 = 0; y1 < req_h; y1++)
13059a60 152 {
153 unsigned char *src = row_ptr[y1];
f6eaae4f 154 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
13059a60 155 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
156 }
157 break;
158 }
ff63afa1 159
f6eaae4f 160 case READPNG_24:
ff63afa1 161 {
162 int height, width, h;
ff63afa1 163 unsigned char *dst = dest;
a085ae5e 164 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
ff63afa1 165 {
a085ae5e 166 lprintf(__FILE__ ": image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
ff63afa1 167 break;
168 }
a085ae5e 169 width = png_get_image_width(png_ptr, info_ptr);
f6eaae4f 170 if (width > req_w)
171 width = req_w;
a085ae5e 172 height = png_get_image_height(png_ptr, info_ptr);
173 if (height > req_h)
174 height = req_h;
ff63afa1 175
176 for (h = 0; h < height; h++)
177 {
178 int len = width;
179 unsigned char *src = row_ptr[h];
f6eaae4f 180 dst += (req_w - width) * 3;
ff63afa1 181 for (len = width; len > 0; len--, dst+=3, src+=3)
182 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
183 }
184 break;
185 }
13059a60 186 }
187
188
ff63afa1 189 ret = 0;
13059a60 190done:
191 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
192 fclose(fp);
ff63afa1 193 return ret;
13059a60 194}
195
7fd8dbbb 196int writepng(const char *fname, unsigned short *src, int w, int h)
197{
198 png_structp png_ptr = NULL;
199 png_infop info_ptr = NULL;
200 png_bytepp row_pointers;
201 int i, j, ret = -1;
202 FILE *f;
203
204 f = fopen(fname, "wb");
205 if (f == NULL) {
206 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
207 return -1;
208 }
209
210 row_pointers = calloc(h, sizeof(row_pointers[0]));
211 if (row_pointers == NULL)
212 goto end1;
213
214 for (i = 0; i < h; i++) {
215 unsigned char *dst = malloc(w * 3);
216 if (dst == NULL)
217 goto end2;
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;
223 }
224 }
225
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");
230 goto end2;
231 }
232
233 info_ptr = png_create_info_struct(png_ptr);
234 if (info_ptr == NULL) {
235 fprintf(stderr, "png_create_info_struct() failed");
236 goto end3;
237 }
238
239 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
240 fprintf(stderr, "error in png code\n");
241 goto end4;
242 }
243
244 png_init_io(png_ptr, f);
245
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);
249
250 png_write_info(png_ptr, info_ptr);
251 png_write_image(png_ptr, row_pointers);
252 png_write_end(png_ptr, NULL);
253
254 ret = 0;
255
256end4:
257// png_destroy_info_struct(png_ptr, &info_ptr); // freed below
258end3:
259 png_destroy_write_struct(&png_ptr, &info_ptr);
260end2:
261 for (i = 0; i < h; i++)
262 free(row_pointers[i]);
263 free(row_pointers);
264end1:
265 fclose(f);
266 return ret;
267}
13059a60 268