forked from notaz/libpicofe
-
Notifications
You must be signed in to change notification settings - Fork 5
/
readpng.c
309 lines (276 loc) · 7.58 KB
/
readpng.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* (C) Gražvydas "notaz" Ignotas, 2008-2011
*
* This work is licensed under the terms of any of these licenses
* (at your option):
* - GNU GPL, version 2 or later.
* - GNU LGPL, version 2.1 or later.
* - MAME license.
* See the COPYING file in the top-level directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include "plat.h"
#include "readpng.h"
#include "lprintf.h"
int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
{
FILE *fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytepp row_ptr = NULL;
int ret = -1;
if (dest == NULL || fname == NULL)
{
return -1;
}
fp = fopen(fname, "rb");
if (fp == NULL)
{
lprintf(__FILE__ ": failed to open: %s\n", fname);
return -1;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
lprintf(__FILE__ ": png_create_read_struct() failed\n");
fclose(fp);
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
lprintf(__FILE__ ": png_create_info_struct() failed\n");
goto done;
}
// Start reading
png_init_io(png_ptr, fp);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
row_ptr = png_get_rows(png_ptr, info_ptr);
if (row_ptr == NULL)
{
lprintf(__FILE__ ": png_get_rows() failed\n");
goto done;
}
// lprintf("%s: %ix%i @ %ibpp\n", fname, (int)png_get_image_width(png_ptr, info_ptr),
// (int)png_get_image_height(png_ptr, info_ptr), png_get_bit_depth(png_ptr, info_ptr));
switch (what)
{
case READPNG_SCALE:
{
int height, width, x_ofs = 0, y_ofs = 0;
unsigned short *dst = dest;
int x_scale, y_scale, x_pos, y_pos; // Q16
if (png_get_bit_depth(png_ptr, info_ptr) != 8)
{
lprintf(__FILE__ ": scaled image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
goto done;
}
width = png_get_image_width(png_ptr, info_ptr);
x_scale = width*65536 / req_w;
height = png_get_image_height(png_ptr, info_ptr);
y_scale = height*65536 / req_h;
if (x_scale < y_scale)
x_scale = y_scale;
else y_scale = x_scale;
x_ofs = req_w - width*65536 / x_scale;
y_ofs = req_h - height*65536 / y_scale;
dst += y_ofs/2*req_w + x_ofs/2;
for (y_pos = 0; y_pos < height*65536; y_pos += y_scale+1)
{
unsigned char *src = row_ptr[y_pos >> 16];
int len = 0;
for (x_pos = 0; x_pos < width*65536; x_pos += x_scale+1, len++)
{
int o = 3*(x_pos >> 16);
// TODO: could use bilinear if upsampling?
*dst++ = PXMAKE(src[o], src[o+1], src[o+2]);
}
dst += req_w - len;
}
break;
}
case READPNG_BG:
{
int height, width, h, x_ofs = 0, y_ofs = 0;
unsigned short *dst = dest;
if (png_get_bit_depth(png_ptr, info_ptr) != 8)
{
lprintf(__FILE__ ": bg image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
goto done;
}
width = png_get_image_width(png_ptr, info_ptr);
if (width > req_w) {
x_ofs = (width - req_w) / 2;
width = req_w;
} else
dst += (req_w - width) / 2;
height = png_get_image_height(png_ptr, info_ptr);
if (height > req_h) {
y_ofs = (height - req_h) / 2;
height = req_h;
} else
dst += (req_h - height) / 2 * req_w;
for (h = 0; h < height; h++)
{
unsigned char *src = row_ptr[h + y_ofs] + x_ofs * 3;
int len = width;
while (len--)
{
*dst++ = PXMAKE(src[0], src[1], src[2]);
src += 3;
}
dst += req_w - width;
}
break;
}
case READPNG_FONT:
{
int x, y, x1, y1;
unsigned char *dst = dest;
if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
{
lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
(int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
goto done;
}
if (png_get_bit_depth(png_ptr, info_ptr) != 8)
{
lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
goto done;
}
for (y = 0; y < 16; y++)
{
for (x = 0; x < 16; x++)
{
/* 16x16 grid of syms */
int sym_w = req_w / 16;
int sym_h = req_h / 16;
for (y1 = 0; y1 < sym_h; y1++)
{
unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
*dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
}
}
}
break;
}
case READPNG_SELECTOR:
{
int x1, y1;
unsigned char *dst = dest;
if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
{
lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
(int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
goto done;
}
if (png_get_bit_depth(png_ptr, info_ptr) != 8)
{
lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
goto done;
}
for (y1 = 0; y1 < req_h; y1++)
{
unsigned char *src = row_ptr[y1];
for (x1 = req_w/2; x1 > 0; x1--, src+=2)
*dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
}
break;
}
case READPNG_24:
{
int height, width, h;
unsigned char *dst = dest;
if (png_get_bit_depth(png_ptr, info_ptr) != 8)
{
lprintf(__FILE__ ": image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
goto done;
}
width = png_get_image_width(png_ptr, info_ptr);
if (width > req_w)
width = req_w;
height = png_get_image_height(png_ptr, info_ptr);
if (height > req_h)
height = req_h;
for (h = 0; h < height; h++)
{
int len = width;
unsigned char *src = row_ptr[h];
dst += (req_w - width) * 3;
for (len = width; len > 0; len--, dst+=3, src+=3)
dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
}
break;
}
}
ret = 0;
done:
png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
fclose(fp);
return ret;
}
int writepng(const char *fname, unsigned short *src, int w, int h)
{
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytepp row_pointers;
int i, j, ret = -1;
FILE *f;
f = fopen(fname, "wb");
if (f == NULL) {
lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
return -1;
}
row_pointers = calloc(h, sizeof(row_pointers[0]));
if (row_pointers == NULL)
goto end1;
for (i = 0; i < h; i++) {
unsigned char *dst = malloc(w * 3);
if (dst == NULL)
goto end2;
row_pointers[i] = dst;
for (j = 0; j < w; j++, src++, dst += 3) {
dst[0] = PXGETR(*src);
dst[1] = PXGETG(*src);
dst[2] = PXGETB(*src);
}
}
/* initialize stuff */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "png_create_write_struct() failed");
goto end2;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "png_create_info_struct() failed");
goto end3;
}
if (setjmp(png_jmpbuf(png_ptr)) != 0) {
fprintf(stderr, "error in png code\n");
goto end4;
}
png_init_io(png_ptr, f);
png_set_IHDR(png_ptr, info_ptr, w, h,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, NULL);
ret = 0;
end4:
// png_destroy_info_struct(png_ptr, &info_ptr); // freed below
end3:
png_destroy_write_struct(&png_ptr, &info_ptr);
end2:
for (i = 0; i < h; i++)
free(row_pointers[i]);
free(row_pointers);
end1:
fclose(f);
return ret;
}