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