update license in source code itself
[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
c7a4ff64 64 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
13059a60 65
66 switch (what)
67 {
68 case READPNG_BG:
69 {
70 int height, width, h;
71 unsigned short *dst = dest;
72 if (info_ptr->pixel_depth != 24)
73 {
c7a4ff64 74 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
13059a60 75 break;
76 }
77 height = info_ptr->height;
f6eaae4f 78 if (height > req_h)
79 height = req_h;
13059a60 80 width = info_ptr->width;
f6eaae4f 81 if (width > req_w)
82 width = req_w;
13059a60 83
84 for (h = 0; h < height; h++)
85 {
86 unsigned char *src = row_ptr[h];
87 int len = width;
88 while (len--)
89 {
703e4c7b 90#ifdef PSP
91 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
92#else
93 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
94#endif
13059a60 95 src += 3;
96 }
f6eaae4f 97 dst += req_w - width;
13059a60 98 }
99 break;
100 }
101
102 case READPNG_FONT:
103 {
104 int x, y, x1, y1;
105 unsigned char *dst = dest;
f6eaae4f 106 if (info_ptr->width != req_w || info_ptr->height != req_h)
13059a60 107 {
f6eaae4f 108 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
109 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
13059a60 110 break;
111 }
112 if (info_ptr->pixel_depth != 8)
113 {
c7a4ff64 114 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
13059a60 115 break;
116 }
117 for (y = 0; y < 16; y++)
118 {
119 for (x = 0; x < 16; x++)
120 {
f6eaae4f 121 /* 16x16 grid of syms */
122 int sym_w = req_w / 16;
123 int sym_h = req_h / 16;
124 for (y1 = 0; y1 < sym_h; y1++)
13059a60 125 {
f6eaae4f 126 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
127 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
13059a60 128 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
129 }
130 }
131 }
132 break;
133 }
134
135 case READPNG_SELECTOR:
136 {
137 int x1, y1;
138 unsigned char *dst = dest;
f6eaae4f 139 if (info_ptr->width != req_w || info_ptr->height != req_h)
13059a60 140 {
f6eaae4f 141 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
142 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
13059a60 143 break;
144 }
145 if (info_ptr->pixel_depth != 8)
146 {
c7a4ff64 147 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
13059a60 148 break;
149 }
f6eaae4f 150 for (y1 = 0; y1 < req_h; y1++)
13059a60 151 {
152 unsigned char *src = row_ptr[y1];
f6eaae4f 153 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
13059a60 154 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
155 }
156 break;
157 }
ff63afa1 158
f6eaae4f 159 case READPNG_24:
ff63afa1 160 {
161 int height, width, h;
ff63afa1 162 unsigned char *dst = dest;
163 if (info_ptr->pixel_depth != 24)
164 {
165 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
166 break;
167 }
168 height = info_ptr->height;
f6eaae4f 169 if (height > req_h)
170 height = req_h;
ff63afa1 171 width = info_ptr->width;
f6eaae4f 172 if (width > req_w)
173 width = req_w;
ff63afa1 174
175 for (h = 0; h < height; h++)
176 {
177 int len = width;
178 unsigned char *src = row_ptr[h];
f6eaae4f 179 dst += (req_w - width) * 3;
ff63afa1 180 for (len = width; len > 0; len--, dst+=3, src+=3)
181 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
182 }
183 break;
184 }
13059a60 185 }
186
187
ff63afa1 188 ret = 0;
13059a60 189done:
190 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
191 fclose(fp);
ff63afa1 192 return ret;
13059a60 193}
194
7fd8dbbb 195int writepng(const char *fname, unsigned short *src, int w, int h)
196{
197 png_structp png_ptr = NULL;
198 png_infop info_ptr = NULL;
199 png_bytepp row_pointers;
200 int i, j, ret = -1;
201 FILE *f;
202
203 f = fopen(fname, "wb");
204 if (f == NULL) {
205 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
206 return -1;
207 }
208
209 row_pointers = calloc(h, sizeof(row_pointers[0]));
210 if (row_pointers == NULL)
211 goto end1;
212
213 for (i = 0; i < h; i++) {
214 unsigned char *dst = malloc(w * 3);
215 if (dst == NULL)
216 goto end2;
217 row_pointers[i] = dst;
218 for (j = 0; j < w; j++, src++, dst += 3) {
219 dst[0] = (*src & 0xf800) >> 8;
220 dst[1] = (*src & 0x07e0) >> 3;
221 dst[2] = (*src & 0x001f) << 3;
222 }
223 }
224
225 /* initialize stuff */
226 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
227 if (png_ptr == NULL) {
228 fprintf(stderr, "png_create_write_struct() failed");
229 goto end2;
230 }
231
232 info_ptr = png_create_info_struct(png_ptr);
233 if (info_ptr == NULL) {
234 fprintf(stderr, "png_create_info_struct() failed");
235 goto end3;
236 }
237
238 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
239 fprintf(stderr, "error in png code\n");
240 goto end4;
241 }
242
243 png_init_io(png_ptr, f);
244
245 png_set_IHDR(png_ptr, info_ptr, w, h,
246 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
247 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
248
249 png_write_info(png_ptr, info_ptr);
250 png_write_image(png_ptr, row_pointers);
251 png_write_end(png_ptr, NULL);
252
253 ret = 0;
254
255end4:
256// png_destroy_info_struct(png_ptr, &info_ptr); // freed below
257end3:
258 png_destroy_write_struct(&png_ptr, &info_ptr);
259end2:
260 for (i = 0; i < h; i++)
261 free(row_pointers[i]);
262 free(row_pointers);
263end1:
264 fclose(f);
265 return ret;
266}
13059a60 267