143e9a87 |
1 | /* |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2008-2010 |
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 | |
a12e0116 |
11 | #include <stdio.h> |
12 | #include <string.h> |
13 | #include <png.h> |
14 | #include "readpng.h" |
e5f426aa |
15 | #include "lprintf.h" |
a12e0116 |
16 | |
be672de7 |
17 | int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h) |
a12e0116 |
18 | { |
19 | FILE *fp; |
20 | png_structp png_ptr = NULL; |
21 | png_infop info_ptr = NULL; |
22 | png_bytepp row_ptr = NULL; |
7a87643e |
23 | int ret = -1; |
a12e0116 |
24 | |
25 | if (dest == NULL || fname == NULL) |
26 | { |
7a87643e |
27 | return -1; |
a12e0116 |
28 | } |
29 | |
30 | fp = fopen(fname, "rb"); |
31 | if (fp == NULL) |
32 | { |
e5f426aa |
33 | lprintf(__FILE__ ": failed to open: %s\n", fname); |
7a87643e |
34 | return -1; |
a12e0116 |
35 | } |
36 | |
37 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
38 | if (!png_ptr) |
39 | { |
e5f426aa |
40 | lprintf(__FILE__ ": png_create_read_struct() failed\n"); |
a12e0116 |
41 | fclose(fp); |
7a87643e |
42 | return -1; |
a12e0116 |
43 | } |
44 | |
45 | info_ptr = png_create_info_struct(png_ptr); |
46 | if (!info_ptr) |
47 | { |
e5f426aa |
48 | lprintf(__FILE__ ": png_create_info_struct() failed\n"); |
a12e0116 |
49 | goto done; |
50 | } |
51 | |
52 | // Start reading |
53 | png_init_io(png_ptr, fp); |
54 | png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL); |
55 | row_ptr = png_get_rows(png_ptr, info_ptr); |
56 | if (row_ptr == NULL) |
57 | { |
e5f426aa |
58 | lprintf(__FILE__ ": png_get_rows() failed\n"); |
a12e0116 |
59 | goto done; |
60 | } |
61 | |
e5f426aa |
62 | // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth); |
a12e0116 |
63 | |
64 | switch (what) |
65 | { |
66 | case READPNG_BG: |
67 | { |
68 | int height, width, h; |
69 | unsigned short *dst = dest; |
70 | if (info_ptr->pixel_depth != 24) |
71 | { |
e5f426aa |
72 | lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth); |
a12e0116 |
73 | break; |
74 | } |
75 | height = info_ptr->height; |
be672de7 |
76 | if (height > req_h) |
77 | height = req_h; |
a12e0116 |
78 | width = info_ptr->width; |
be672de7 |
79 | if (width > req_w) |
80 | width = req_w; |
a12e0116 |
81 | |
82 | for (h = 0; h < height; h++) |
83 | { |
84 | unsigned char *src = row_ptr[h]; |
85 | int len = width; |
86 | while (len--) |
87 | { |
7d4906bf |
88 | #ifdef PSP |
89 | *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR |
90 | #else |
91 | *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB |
92 | #endif |
a12e0116 |
93 | src += 3; |
94 | } |
be672de7 |
95 | dst += req_w - width; |
a12e0116 |
96 | } |
97 | break; |
98 | } |
99 | |
100 | case READPNG_FONT: |
101 | { |
102 | int x, y, x1, y1; |
103 | unsigned char *dst = dest; |
be672de7 |
104 | if (info_ptr->width != req_w || info_ptr->height != req_h) |
a12e0116 |
105 | { |
be672de7 |
106 | lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n", |
107 | (int)info_ptr->width, (int)info_ptr->height, req_w, req_h); |
a12e0116 |
108 | break; |
109 | } |
110 | if (info_ptr->pixel_depth != 8) |
111 | { |
e5f426aa |
112 | lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth); |
a12e0116 |
113 | break; |
114 | } |
115 | for (y = 0; y < 16; y++) |
116 | { |
117 | for (x = 0; x < 16; x++) |
118 | { |
be672de7 |
119 | /* 16x16 grid of syms */ |
120 | int sym_w = req_w / 16; |
121 | int sym_h = req_h / 16; |
122 | for (y1 = 0; y1 < sym_h; y1++) |
a12e0116 |
123 | { |
be672de7 |
124 | unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w; |
125 | for (x1 = sym_w/2; x1 > 0; x1--, src+=2) |
a12e0116 |
126 | *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4); |
127 | } |
128 | } |
129 | } |
130 | break; |
131 | } |
132 | |
133 | case READPNG_SELECTOR: |
134 | { |
135 | int x1, y1; |
136 | unsigned char *dst = dest; |
be672de7 |
137 | if (info_ptr->width != req_w || info_ptr->height != req_h) |
a12e0116 |
138 | { |
be672de7 |
139 | lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n", |
140 | (int)info_ptr->width, (int)info_ptr->height, req_w, req_h); |
a12e0116 |
141 | break; |
142 | } |
143 | if (info_ptr->pixel_depth != 8) |
144 | { |
e5f426aa |
145 | lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth); |
a12e0116 |
146 | break; |
147 | } |
be672de7 |
148 | for (y1 = 0; y1 < req_h; y1++) |
a12e0116 |
149 | { |
150 | unsigned char *src = row_ptr[y1]; |
be672de7 |
151 | for (x1 = req_w/2; x1 > 0; x1--, src+=2) |
a12e0116 |
152 | *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4); |
153 | } |
154 | break; |
155 | } |
7a87643e |
156 | |
be672de7 |
157 | case READPNG_24: |
7a87643e |
158 | { |
159 | int height, width, h; |
7a87643e |
160 | unsigned char *dst = dest; |
161 | if (info_ptr->pixel_depth != 24) |
162 | { |
163 | lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth); |
164 | break; |
165 | } |
166 | height = info_ptr->height; |
be672de7 |
167 | if (height > req_h) |
168 | height = req_h; |
7a87643e |
169 | width = info_ptr->width; |
be672de7 |
170 | if (width > req_w) |
171 | width = req_w; |
7a87643e |
172 | |
173 | for (h = 0; h < height; h++) |
174 | { |
175 | int len = width; |
176 | unsigned char *src = row_ptr[h]; |
be672de7 |
177 | dst += (req_w - width) * 3; |
7a87643e |
178 | for (len = width; len > 0; len--, dst+=3, src+=3) |
179 | dst[0] = src[2], dst[1] = src[1], dst[2] = src[0]; |
180 | } |
181 | break; |
182 | } |
a12e0116 |
183 | } |
184 | |
185 | |
7a87643e |
186 | ret = 0; |
a12e0116 |
187 | done: |
188 | png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL); |
189 | fclose(fp); |
7a87643e |
190 | return ret; |
a12e0116 |
191 | } |
192 | |
193 | |