in_sdl: give names to gamepad buttons
[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>
ff8abddd 16#include "plat.h"
13059a60 17#include "readpng.h"
c7a4ff64 18#include "lprintf.h"
13059a60 19
f6eaae4f 20int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
13059a60 21{
22 FILE *fp;
23 png_structp png_ptr = NULL;
24 png_infop info_ptr = NULL;
25 png_bytepp row_ptr = NULL;
ff63afa1 26 int ret = -1;
13059a60 27
28 if (dest == NULL || fname == NULL)
29 {
ff63afa1 30 return -1;
13059a60 31 }
32
33 fp = fopen(fname, "rb");
34 if (fp == NULL)
35 {
c7a4ff64 36 lprintf(__FILE__ ": failed to open: %s\n", fname);
ff63afa1 37 return -1;
13059a60 38 }
39
40 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
41 if (!png_ptr)
42 {
c7a4ff64 43 lprintf(__FILE__ ": png_create_read_struct() failed\n");
13059a60 44 fclose(fp);
ff63afa1 45 return -1;
13059a60 46 }
47
48 info_ptr = png_create_info_struct(png_ptr);
49 if (!info_ptr)
50 {
c7a4ff64 51 lprintf(__FILE__ ": png_create_info_struct() failed\n");
13059a60 52 goto done;
53 }
54
55 // Start reading
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);
59 if (row_ptr == NULL)
60 {
c7a4ff64 61 lprintf(__FILE__ ": png_get_rows() failed\n");
13059a60 62 goto done;
63 }
64
a085ae5e 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));
13059a60 67
68 switch (what)
69 {
4563c131 70 case READPNG_SCALE:
71 {
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
75
76 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
77 {
78 lprintf(__FILE__ ": scaled image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
697806c4 79 goto done;
4563c131 80 }
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)
86 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;
90
91 dst += y_ofs/2*req_w + x_ofs/2;
92 for (y_pos = 0; y_pos < height*65536; y_pos += y_scale+1)
93 {
94 unsigned char *src = row_ptr[y_pos >> 16];
95 int len = 0;
96 for (x_pos = 0; x_pos < width*65536; x_pos += x_scale+1, len++)
97 {
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]);
101 }
102 dst += req_w - len;
103 }
104 break;
105 }
106
13059a60 107 case READPNG_BG:
108 {
8ef1ad32 109 int height, width, h, x_ofs = 0, y_ofs = 0;
13059a60 110 unsigned short *dst = dest;
8ef1ad32 111
a085ae5e 112 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
13059a60 113 {
a085ae5e 114 lprintf(__FILE__ ": bg image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
697806c4 115 goto done;
13059a60 116 }
a085ae5e 117 width = png_get_image_width(png_ptr, info_ptr);
8ef1ad32
GI
118 if (width > req_w) {
119 x_ofs = (width - req_w) / 2;
f6eaae4f 120 width = req_w;
3ccc039b 121 } else
122 dst += (req_w - width) / 2;
a085ae5e 123 height = png_get_image_height(png_ptr, info_ptr);
8ef1ad32
GI
124 if (height > req_h) {
125 y_ofs = (height - req_h) / 2;
a085ae5e 126 height = req_h;
3ccc039b 127 } else
128 dst += (req_h - height) / 2 * req_w;
13059a60 129
130 for (h = 0; h < height; h++)
131 {
8ef1ad32 132 unsigned char *src = row_ptr[h + y_ofs] + x_ofs * 3;
13059a60 133 int len = width;
134 while (len--)
135 {
ff8abddd 136 *dst++ = PXMAKE(src[0], src[1], src[2]);
13059a60 137 src += 3;
138 }
f6eaae4f 139 dst += req_w - width;
13059a60 140 }
141 break;
142 }
143
144 case READPNG_FONT:
145 {
146 int x, y, x1, y1;
147 unsigned char *dst = dest;
a085ae5e 148 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
13059a60 149 {
f6eaae4f 150 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
a085ae5e 151 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
697806c4 152 goto done;
13059a60 153 }
a085ae5e 154 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
13059a60 155 {
a085ae5e 156 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
697806c4 157 goto done;
13059a60 158 }
159 for (y = 0; y < 16; y++)
160 {
161 for (x = 0; x < 16; x++)
162 {
f6eaae4f 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++)
13059a60 167 {
f6eaae4f 168 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
169 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
13059a60 170 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
171 }
172 }
173 }
174 break;
175 }
176
177 case READPNG_SELECTOR:
178 {
179 int x1, y1;
180 unsigned char *dst = dest;
a085ae5e 181 if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
13059a60 182 {
f6eaae4f 183 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
a085ae5e 184 (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
697806c4 185 goto done;
13059a60 186 }
a085ae5e 187 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
13059a60 188 {
a085ae5e 189 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
697806c4 190 goto done;
13059a60 191 }
f6eaae4f 192 for (y1 = 0; y1 < req_h; y1++)
13059a60 193 {
194 unsigned char *src = row_ptr[y1];
f6eaae4f 195 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
13059a60 196 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
197 }
198 break;
199 }
ff63afa1 200
f6eaae4f 201 case READPNG_24:
ff63afa1 202 {
203 int height, width, h;
ff63afa1 204 unsigned char *dst = dest;
a085ae5e 205 if (png_get_bit_depth(png_ptr, info_ptr) != 8)
ff63afa1 206 {
a085ae5e 207 lprintf(__FILE__ ": image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
697806c4 208 goto done;
ff63afa1 209 }
a085ae5e 210 width = png_get_image_width(png_ptr, info_ptr);
f6eaae4f 211 if (width > req_w)
212 width = req_w;
a085ae5e 213 height = png_get_image_height(png_ptr, info_ptr);
214 if (height > req_h)
215 height = req_h;
ff63afa1 216
217 for (h = 0; h < height; h++)
218 {
219 int len = width;
220 unsigned char *src = row_ptr[h];
f6eaae4f 221 dst += (req_w - width) * 3;
ff63afa1 222 for (len = width; len > 0; len--, dst+=3, src+=3)
223 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
224 }
225 break;
226 }
13059a60 227 }
228
229
ff63afa1 230 ret = 0;
13059a60 231done:
232 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
233 fclose(fp);
ff63afa1 234 return ret;
13059a60 235}
236
c68a2bca 237int writepngpp(const char *fname, unsigned short *src, int w, int h, int pitch)
7fd8dbbb 238{
239 png_structp png_ptr = NULL;
240 png_infop info_ptr = NULL;
241 png_bytepp row_pointers;
242 int i, j, ret = -1;
243 FILE *f;
244
245 f = fopen(fname, "wb");
246 if (f == NULL) {
247 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
248 return -1;
249 }
250
251 row_pointers = calloc(h, sizeof(row_pointers[0]));
252 if (row_pointers == NULL)
253 goto end1;
254
255 for (i = 0; i < h; i++) {
256 unsigned char *dst = malloc(w * 3);
257 if (dst == NULL)
258 goto end2;
259 row_pointers[i] = dst;
260 for (j = 0; j < w; j++, src++, dst += 3) {
ff8abddd 261 dst[0] = PXGETR(*src);
262 dst[1] = PXGETG(*src);
263 dst[2] = PXGETB(*src);
7fd8dbbb 264 }
c68a2bca 265 src += pitch-w;
7fd8dbbb 266 }
267
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");
272 goto end2;
273 }
274
275 info_ptr = png_create_info_struct(png_ptr);
276 if (info_ptr == NULL) {
277 fprintf(stderr, "png_create_info_struct() failed");
278 goto end3;
279 }
280
281 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
282 fprintf(stderr, "error in png code\n");
283 goto end4;
284 }
285
286 png_init_io(png_ptr, f);
287
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);
291
292 png_write_info(png_ptr, info_ptr);
293 png_write_image(png_ptr, row_pointers);
294 png_write_end(png_ptr, NULL);
295
296 ret = 0;
297
298end4:
299// png_destroy_info_struct(png_ptr, &info_ptr); // freed below
300end3:
301 png_destroy_write_struct(&png_ptr, &info_ptr);
302end2:
303 for (i = 0; i < h; i++)
304 free(row_pointers[i]);
305 free(row_pointers);
306end1:
307 fclose(f);
308 return ret;
309}
13059a60 310
c68a2bca 311int writepng(const char *fname, unsigned short *src, int w, int h)
312{
313 return writepngpp(fname, src, w, h, w);
314}