merge from libretro
[pcsx_rearmed.git] / common / 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  * See the COPYING file in the top-level directory.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <png.h>
15 #include "readpng.h"
16 #include "lprintf.h"
17
18 int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
19 {
20         FILE *fp;
21         png_structp png_ptr = NULL;
22         png_infop info_ptr = NULL;
23         png_bytepp row_ptr = NULL;
24         int ret = -1;
25
26         if (dest == NULL || fname == NULL)
27         {
28                 return -1;
29         }
30
31         fp = fopen(fname, "rb");
32         if (fp == NULL)
33         {
34                 lprintf(__FILE__ ": failed to open: %s\n", fname);
35                 return -1;
36         }
37
38         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
39         if (!png_ptr)
40         {
41                 lprintf(__FILE__ ": png_create_read_struct() failed\n");
42                 fclose(fp);
43                 return -1;
44         }
45
46         info_ptr = png_create_info_struct(png_ptr);
47         if (!info_ptr)
48         {
49                 lprintf(__FILE__ ": png_create_info_struct() failed\n");
50                 goto done;
51         }
52
53         // Start reading
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);
57         if (row_ptr == NULL)
58         {
59                 lprintf(__FILE__ ": png_get_rows() failed\n");
60                 goto done;
61         }
62
63         // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
64
65         switch (what)
66         {
67                 case READPNG_BG:
68                 {
69                         int height, width, h;
70                         unsigned short *dst = dest;
71                         if (info_ptr->pixel_depth != 24)
72                         {
73                                 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
74                                 break;
75                         }
76                         height = info_ptr->height;
77                         if (height > req_h)
78                                 height = req_h;
79                         width = info_ptr->width;
80                         if (width > req_w)
81                                 width = req_w;
82
83                         for (h = 0; h < height; h++)
84                         {
85                                 unsigned char *src = row_ptr[h];
86                                 int len = width;
87                                 while (len--)
88                                 {
89 #ifdef PSP
90                                         *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
91 #else
92                                         *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
93 #endif
94                                         src += 3;
95                                 }
96                                 dst += req_w - width;
97                         }
98                         break;
99                 }
100
101                 case READPNG_FONT:
102                 {
103                         int x, y, x1, y1;
104                         unsigned char *dst = dest;
105                         if (info_ptr->width != req_w || info_ptr->height != req_h)
106                         {
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);
109                                 break;
110                         }
111                         if (info_ptr->pixel_depth != 8)
112                         {
113                                 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
114                                 break;
115                         }
116                         for (y = 0; y < 16; y++)
117                         {
118                                 for (x = 0; x < 16; x++)
119                                 {
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++)
124                                         {
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);
128                                         }
129                                 }
130                         }
131                         break;
132                 }
133
134                 case READPNG_SELECTOR:
135                 {
136                         int x1, y1;
137                         unsigned char *dst = dest;
138                         if (info_ptr->width != req_w || info_ptr->height != req_h)
139                         {
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);
142                                 break;
143                         }
144                         if (info_ptr->pixel_depth != 8)
145                         {
146                                 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
147                                 break;
148                         }
149                         for (y1 = 0; y1 < req_h; y1++)
150                         {
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);
154                         }
155                         break;
156                 }
157
158                 case READPNG_24:
159                 {
160                         int height, width, h;
161                         unsigned char *dst = dest;
162                         if (info_ptr->pixel_depth != 24)
163                         {
164                                 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
165                                 break;
166                         }
167                         height = info_ptr->height;
168                         if (height > req_h)
169                                 height = req_h;
170                         width = info_ptr->width;
171                         if (width > req_w)
172                                 width = req_w;
173
174                         for (h = 0; h < height; h++)
175                         {
176                                 int len = width;
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];
181                         }
182                         break;
183                 }
184         }
185
186
187         ret = 0;
188 done:
189         png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
190         fclose(fp);
191         return ret;
192 }
193
194 int writepng(const char *fname, unsigned short *src, int w, int h)
195 {
196         png_structp png_ptr = NULL;
197         png_infop info_ptr = NULL;
198         png_bytepp row_pointers;
199         int i, j, ret = -1;
200         FILE *f;
201
202         f = fopen(fname, "wb");
203         if (f == NULL) {
204                 lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
205                 return -1;
206         }
207
208         row_pointers = calloc(h, sizeof(row_pointers[0]));
209         if (row_pointers == NULL)
210                 goto end1;
211
212         for (i = 0; i < h; i++) {
213                 unsigned char *dst = malloc(w * 3);
214                 if (dst == NULL)
215                         goto end2;
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;
221                 }
222         }
223
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");
228                 goto end2;
229         }
230
231         info_ptr = png_create_info_struct(png_ptr);
232         if (info_ptr == NULL) {
233                 fprintf(stderr, "png_create_info_struct() failed");
234                 goto end3;
235         }
236
237         if (setjmp(png_jmpbuf(png_ptr)) != 0) {
238                 fprintf(stderr, "error in png code\n");
239                 goto end4;
240         }
241
242         png_init_io(png_ptr, f);
243
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);
247
248         png_write_info(png_ptr, info_ptr);
249         png_write_image(png_ptr, row_pointers);
250         png_write_end(png_ptr, NULL);
251
252         ret = 0;
253
254 end4:
255 //      png_destroy_info_struct(png_ptr, &info_ptr); // freed below
256 end3:
257         png_destroy_write_struct(&png_ptr, &info_ptr);
258 end2:
259         for (i = 0; i < h; i++)
260                 free(row_pointers[i]);
261         free(row_pointers);
262 end1:
263         fclose(f);
264         return ret;
265 }
266