added scr_lvled, a bunch of libraries, and improved framework code
[raydungeon] / libs / imago / src / filepng.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* -- PNG module -- */
20 #ifndef NO_PNG
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <png.h>
25 #include "imago2.h"
26 #include "ftmodule.h"
27
28 static int check_file(struct img_io *io);
29 static int read_file(struct img_pixmap *img, struct img_io *io);
30 static int write_file(struct img_pixmap *img, struct img_io *io);
31
32 static void read_func(png_struct *png, unsigned char *data, size_t len);
33 static void write_func(png_struct *png, unsigned char *data, size_t len);
34 static void flush_func(png_struct *png);
35
36 static int png_type_to_fmt(int color_type, int channel_bits);
37 static int fmt_to_png_type(enum img_fmt fmt);
38
39
40 int img_register_png(void)
41 {
42         static struct ftype_module mod = {".png", check_file, read_file, write_file};
43         return img_register_module(&mod);
44 }
45
46 static int check_file(struct img_io *io)
47 {
48         unsigned char sig[8];
49         int res;
50         long pos = io->seek(0, SEEK_CUR, io->uptr);
51
52         if(io->read(sig, 8, io->uptr) < 8) {
53                 io->seek(pos, SEEK_SET, io->uptr);
54                 return -1;
55         }
56
57         res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
58         io->seek(pos, SEEK_SET, io->uptr);
59         return res;
60 }
61
62 static int read_file(struct img_pixmap *img, struct img_io *io)
63 {
64         unsigned int i, j, num_elem;
65         unsigned char **lineptr;
66         png_struct *png;
67         png_info *info;
68         int channel_bits, color_type, ilace_type, compression, filtering, fmt;
69         png_uint_32 xsz, ysz;
70         png_color *palette;
71         struct img_colormap *cmap;
72
73         if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
74                 return -1;
75         }
76
77         if(!(info = png_create_info_struct(png))) {
78                 png_destroy_read_struct(&png, 0, 0);
79                 return -1;
80         }
81
82         if(setjmp(png_jmpbuf(png))) {
83                 png_destroy_read_struct(&png, &info, 0);
84                 return -1;
85         }
86
87         png_set_read_fn(png, io, read_func);
88         png_set_sig_bytes(png, 0);
89         png_read_png(png, info, 0, 0);
90
91         png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
92                         &compression, &filtering);
93         if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
94                 png_destroy_read_struct(&png, &info, 0);
95                 return -1;
96         }
97
98         if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
99                 png_destroy_read_struct(&png, &info, 0);
100                 return -1;
101         }
102
103         if(color_type == PNG_COLOR_TYPE_PALETTE) {
104                 cmap = img_colormap(img);
105                 png_get_PLTE(png, info, &palette, &cmap->ncolors);
106                 memcpy(cmap->color, palette, cmap->ncolors * sizeof *cmap->color);
107         }
108
109         lineptr = (unsigned char**)png_get_rows(png, info);
110         if(channel_bits == 8) {
111                 unsigned char *dest = img->pixels;
112
113                 for(i=0; i<ysz; i++) {
114                         memcpy(dest, lineptr[i], xsz * img->pixelsz);
115                         dest += xsz * img->pixelsz;
116                 }
117
118         } else if(channel_bits < 8) {
119                 unsigned char *src, *dest = img->pixels;
120                 unsigned char data, bit;
121
122                 for(i=0; i<ysz; i++) {
123                         src = lineptr[i];
124                         bit = 0;
125                         for(j=0; j<xsz; j++) {
126                                 if(!bit) {
127                                         data = *src++;
128                                 }
129                                 *dest++ = data >> (8 - channel_bits);
130                                 data <<= channel_bits;
131                                 bit += channel_bits;
132                                 if(bit > 8 - channel_bits) bit = 0;
133                         }
134                 }
135
136         } else {
137                 float *dest = img->pixels;
138
139                 num_elem = img->pixelsz / sizeof(float);
140                 for(i=0; i<ysz; i++) {
141                         for(j=0; j<xsz * num_elem; j++) {
142                                 unsigned short val = (lineptr[i][j * 2] << 8) | lineptr[i][j * 2 + 1];
143                                 *dest++ = (float)val / 65535.0;
144                         }
145                 }
146         }
147
148
149         png_destroy_read_struct(&png, &info, 0);
150         return 0;
151 }
152
153 static int write_file(struct img_pixmap *img, struct img_io *io)
154 {
155         png_struct *png;
156         png_info *info;
157         png_text txt;
158         struct img_pixmap tmpimg;
159         unsigned char **rows;
160         unsigned char *pixptr;
161         int i, coltype;
162         struct img_colormap *cmap;
163
164         img_init(&tmpimg);
165
166         if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
167                 return -1;
168         }
169         if(!(info = png_create_info_struct(png))) {
170                 png_destroy_write_struct(&png, 0);
171                 return -1;
172         }
173
174         /* if the input image is floating-point, we need to convert it to integer */
175         if(img_is_float(img)) {
176                 if(img_copy(&tmpimg, img) == -1) {
177                         return -1;
178                 }
179                 if(img_to_integer(&tmpimg) == -1) {
180                         img_destroy(&tmpimg);
181                         return -1;
182                 }
183                 img = &tmpimg;
184         }
185
186         txt.compression = PNG_TEXT_COMPRESSION_NONE;
187         txt.key = "Software";
188         txt.text = "libimago2";
189         txt.text_length = 0;
190
191         if(setjmp(png_jmpbuf(png))) {
192                 png_destroy_write_struct(&png, &info);
193                 img_destroy(&tmpimg);
194                 return -1;
195         }
196         png_set_write_fn(png, io, write_func, flush_func);
197
198         coltype = fmt_to_png_type(img->fmt);
199         png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
200                         PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
201         png_set_text(png, info, &txt, 1);
202
203         if(img->fmt == IMG_FMT_IDX8) {
204                 cmap = img_colormap(img);
205                 png_set_PLTE(png, info, (png_color*)cmap->color, cmap->ncolors);
206         }
207
208         if(!(rows = malloc(img->height * sizeof *rows))) {
209                 png_destroy_write_struct(&png, &info);
210                 img_destroy(&tmpimg);
211                 return -1;
212         }
213
214         pixptr = img->pixels;
215         for(i=0; i<img->height; i++) {
216                 rows[i] = pixptr;
217                 pixptr += img->width * img->pixelsz;
218         }
219         png_set_rows(png, info, rows);
220
221         png_write_png(png, info, 0, 0);
222         png_write_end(png, info);
223         png_destroy_write_struct(&png, &info);
224
225         free(rows);
226
227         img_destroy(&tmpimg);
228         return 0;
229 }
230
231 static void read_func(png_struct *png, unsigned char *data, size_t len)
232 {
233         struct img_io *io = (struct img_io*)png_get_io_ptr(png);
234
235         if(io->read(data, len, io->uptr) == -1) {
236                 longjmp(png_jmpbuf(png), 1);
237         }
238 }
239
240 static void write_func(png_struct *png, unsigned char *data, size_t len)
241 {
242         struct img_io *io = (struct img_io*)png_get_io_ptr(png);
243
244         if(io->write(data, len, io->uptr) == -1) {
245                 longjmp(png_jmpbuf(png), 1);
246         }
247 }
248
249 static void flush_func(png_struct *png)
250 {
251         /* XXX does it matter that we can't flush? */
252 }
253
254 static int png_type_to_fmt(int color_type, int channel_bits)
255 {
256         if(channel_bits > 8 && channel_bits != 16) {
257                 return -1;
258         }
259
260         switch(color_type) {
261         case PNG_COLOR_TYPE_RGB:
262                 return channel_bits == 16 ? IMG_FMT_RGBF : IMG_FMT_RGB24;
263
264         case PNG_COLOR_TYPE_RGB_ALPHA:
265                 return channel_bits == 16 ? IMG_FMT_RGBAF : IMG_FMT_RGBA32;
266
267         case PNG_COLOR_TYPE_GRAY:
268                 return channel_bits == 16 ? IMG_FMT_GREYF : IMG_FMT_GREY8;
269
270         case PNG_COLOR_TYPE_PALETTE:
271                 return channel_bits <= 8 ? IMG_FMT_IDX8 : -1;
272
273         default:
274                 break;
275         }
276         return -1;
277 }
278
279 static int fmt_to_png_type(enum img_fmt fmt)
280 {
281         switch(fmt) {
282         case IMG_FMT_GREY8:
283                 return PNG_COLOR_TYPE_GRAY;
284
285         case IMG_FMT_RGB24:
286                 return PNG_COLOR_TYPE_RGB;
287
288         case IMG_FMT_RGBA32:
289                 return PNG_COLOR_TYPE_RGBA;
290
291         case IMG_FMT_IDX8:
292                 return PNG_COLOR_TYPE_PALETTE;
293
294         default:
295                 break;
296         }
297         return -1;
298 }
299
300 #else
301 /* building with PNG support disabled */
302
303 int img_register_png(void)
304 {
305         return -1;
306 }
307
308 #endif