update license in source code itself
[libpicofe.git] / readpng.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2008-2011
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.
8  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <png.h>
16 #include "readpng.h"
17 #include "lprintf.h"
18
19 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
20 {
21         FILE *fp;
22         png_structp png_ptr = NULL;
23         png_infop info_ptr = NULL;
24         png_bytepp row_ptr = NULL;
25         int ret = -1;
26
27         if (dest == NULL || fname == NULL)
28         {
29                 return -1;
30         }
31
32         fp = fopen(fname, "rb");
33         if (fp == NULL)
34         {
35                 lprintf(__FILE__ ": failed to open: %s\n", fname);
36                 return -1;
37         }
38
39         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
40         if (!png_ptr)
41         {
42                 lprintf(__FILE__ ": png_create_read_struct() failed\n");
43                 fclose(fp);
44                 return -1;
45         }
46
47         info_ptr = png_create_info_struct(png_ptr);
48         if (!info_ptr)
49         {
50                 lprintf(__FILE__ ": png_create_info_struct() failed\n");
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         {
60                 lprintf(__FILE__ ": png_get_rows() failed\n");
61                 goto done;
62         }
63
64         // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
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                         {
74                                 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
75                                 break;
76                         }
77                         height = info_ptr->height;
78                         if (height > req_h)
79                                 height = req_h;
80                         width = info_ptr->width;
81                         if (width > req_w)
82                                 width = req_w;
83
84                         for (h = 0; h < height; h++)
85                         {
86                                 unsigned char *src = row_ptr[h];
87                                 int len = width;
88                                 while (len--)
89                                 {
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
95                                         src += 3;
96                                 }
97                                 dst += req_w - width;
98                         }
99                         break;
100                 }
101
102                 case READPNG_FONT:
103                 {
104                         int x, y, x1, y1;
105                         unsigned char *dst = dest;
106                         if (info_ptr->width != req_w || info_ptr->height != req_h)
107                         {
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);
110                                 break;
111                         }
112                         if (info_ptr->pixel_depth != 8)
113                         {
114                                 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
115                                 break;
116                         }
117                         for (y = 0; y < 16; y++)
118                         {
119                                 for (x = 0; x < 16; x++)
120                                 {
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++)
125                                         {
126                                                 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
127                                                 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
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;
139                         if (info_ptr->width != req_w || info_ptr->height != req_h)
140                         {
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);
143                                 break;
144                         }
145                         if (info_ptr->pixel_depth != 8)
146                         {
147                                 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
148                                 break;
149                         }
150                         for (y1 = 0; y1 < req_h; y1++)
151                         {
152                                 unsigned char *src = row_ptr[y1];
153                                 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
154                                         *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
155                         }
156                         break;
157                 }
158
159                 case READPNG_24:
160                 {
161                         int height, width, h;
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;
169                         if (height > req_h)
170                                 height = req_h;
171                         width = info_ptr->width;
172                         if (width > req_w)
173                                 width = req_w;
174
175                         for (h = 0; h < height; h++)
176                         {
177                                 int len = width;
178                                 unsigned char *src = row_ptr[h];
179                                 dst += (req_w - width) * 3;
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                 }
185         }
186
187
188         ret = 0;
189 done:
190         png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
191         fclose(fp);
192         return ret;
193 }
194
195 int 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
255 end4:
256 //      png_destroy_info_struct(png_ptr, &info_ptr); // freed below
257 end3:
258         png_destroy_write_struct(&png_ptr, &info_ptr);
259 end2:
260         for (i = 0; i < h; i++)
261                 free(row_pointers[i]);
262         free(row_pointers);
263 end1:
264         fclose(f);
265         return ret;
266 }
267