initial commit, eq circuit emulator
[eqemu] / libs / libimago / src / file_png.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
21 #include <stdlib.h>
22 #include <png.h>
23 #include "imago2.h"
24 #include "ftype_module.h"
25
26 static int check_file(struct img_io *io);
27 static int read_file(struct img_pixmap *img, struct img_io *io);
28 static int write_file(struct img_pixmap *img, struct img_io *io);
29
30 static void read_func(png_struct *png, unsigned char *data, size_t len);
31 static void write_func(png_struct *png, unsigned char *data, size_t len);
32 static void flush_func(png_struct *png);
33
34 static int png_type_to_fmt(int color_type, int channel_bits);
35 static int fmt_to_png_type(enum img_fmt fmt);
36
37
38 int img_register_png(void)
39 {
40         static struct ftype_module mod = {".png", check_file, read_file, write_file};
41         return img_register_module(&mod);
42 }
43
44 static int check_file(struct img_io *io)
45 {
46         unsigned char sig[8];
47         int res;
48         long pos = io->seek(0, SEEK_CUR, io->uptr);
49
50         if(io->read(sig, 8, io->uptr) < 8) {
51                 io->seek(pos, SEEK_SET, io->uptr);
52                 return -1;
53         }
54
55         res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
56         io->seek(pos, SEEK_SET, io->uptr);
57         return res;
58 }
59
60 static int read_file(struct img_pixmap *img, struct img_io *io)
61 {
62         png_struct *png;
63         png_info *info;
64         int channel_bits, color_type, ilace_type, compression, filtering, fmt;
65         png_uint_32 xsz, ysz;
66
67         if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
68                 return -1;
69         }
70
71         if(!(info = png_create_info_struct(png))) {
72                 png_destroy_read_struct(&png, 0, 0);
73                 return -1;
74         }
75
76         if(setjmp(png_jmpbuf(png))) {
77                 png_destroy_read_struct(&png, &info, 0);
78                 return -1;
79         }
80
81         png_set_read_fn(png, io, read_func);
82         png_set_sig_bytes(png, 0);
83         png_read_png(png, info, 0, 0);
84
85         png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
86                         &compression, &filtering);
87         if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
88                 png_destroy_read_struct(&png, &info, 0);
89                 return -1;
90         }
91
92         if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
93                 png_destroy_read_struct(&png, &info, 0);
94                 return -1;
95         }
96
97
98         if(channel_bits == 8) {
99                 unsigned int i;
100                 unsigned char **lineptr = (unsigned char**)png_get_rows(png, info);
101                 unsigned char *dest = img->pixels;
102
103                 for(i=0; i<ysz; i++) {
104                         memcpy(dest, lineptr[i], xsz * img->pixelsz);
105                         dest += xsz * img->pixelsz;
106                 }
107         } else {
108                 unsigned int i, j, num_elem;
109                 unsigned char **lineptr = (unsigned char**)png_get_rows(png, info);
110                 float *dest = img->pixels;
111
112                 num_elem = img->pixelsz / sizeof(float);
113                 for(i=0; i<ysz; i++) {
114                         for(j=0; j<xsz * num_elem; j++) {
115                                 unsigned short val = (lineptr[i][j * 2] << 8) | lineptr[i][j * 2 + 1];
116                                 *dest++ = (float)val / 65535.0;
117                         }
118                 }
119         }
120
121
122         png_destroy_read_struct(&png, &info, 0);
123         return 0;
124 }
125
126
127 static int write_file(struct img_pixmap *img, struct img_io *io)
128 {
129         png_struct *png;
130         png_info *info;
131         png_text txt;
132         struct img_pixmap tmpimg;
133         unsigned char **rows;
134         unsigned char *pixptr;
135         int i, coltype;
136
137         img_init(&tmpimg);
138
139         if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
140                 return -1;
141         }
142         if(!(info = png_create_info_struct(png))) {
143                 png_destroy_write_struct(&png, 0);
144                 return -1;
145         }
146
147         /* if the input image is floating-point, we need to convert it to integer */
148         if(img_is_float(img)) {
149                 if(img_copy(&tmpimg, img) == -1) {
150                         return -1;
151                 }
152                 if(img_to_integer(&tmpimg) == -1) {
153                         img_destroy(&tmpimg);
154                         return -1;
155                 }
156                 img = &tmpimg;
157         }
158
159         txt.compression = PNG_TEXT_COMPRESSION_NONE;
160         txt.key = "Software";
161         txt.text = "libimago2";
162         txt.text_length = 0;
163
164         if(setjmp(png_jmpbuf(png))) {
165                 png_destroy_write_struct(&png, &info);
166                 img_destroy(&tmpimg);
167                 return -1;
168         }
169         png_set_write_fn(png, io, write_func, flush_func);
170
171         coltype = fmt_to_png_type(img->fmt);
172         png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
173                         PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
174         png_set_text(png, info, &txt, 1);
175
176         if(!(rows = malloc(img->height * sizeof *rows))) {
177                 png_destroy_write_struct(&png, &info);
178                 img_destroy(&tmpimg);
179                 return -1;
180         }
181
182         pixptr = img->pixels;
183         for(i=0; i<img->height; i++) {
184                 rows[i] = pixptr;
185                 pixptr += img->width * img->pixelsz;
186         }
187         png_set_rows(png, info, rows);
188
189         png_write_png(png, info, 0, 0);
190         png_write_end(png, info);
191         png_destroy_write_struct(&png, &info);
192
193         free(rows);
194
195         img_destroy(&tmpimg);
196         return 0;
197 }
198
199 static void read_func(png_struct *png, unsigned char *data, size_t len)
200 {
201         struct img_io *io = (struct img_io*)png_get_io_ptr(png);
202
203         if(io->read(data, len, io->uptr) == -1) {
204                 longjmp(png_jmpbuf(png), 1);
205         }
206 }
207
208 static void write_func(png_struct *png, unsigned char *data, size_t len)
209 {
210         struct img_io *io = (struct img_io*)png_get_io_ptr(png);
211
212         if(io->write(data, len, io->uptr) == -1) {
213                 longjmp(png_jmpbuf(png), 1);
214         }
215 }
216
217 static void flush_func(png_struct *png)
218 {
219         /* XXX does it matter that we can't flush? */
220 }
221
222 static int png_type_to_fmt(int color_type, int channel_bits)
223 {
224         /* only 8 and 16 bits per channel ar supported at the moment */
225         if(channel_bits != 8 && channel_bits != 16) {
226                 return -1;
227         }
228
229         switch(color_type) {
230         case PNG_COLOR_TYPE_RGB:
231                 return channel_bits == 16 ? IMG_FMT_RGBF : IMG_FMT_RGB24;
232
233         case PNG_COLOR_TYPE_RGB_ALPHA:
234                 return channel_bits == 16 ? IMG_FMT_RGBAF : IMG_FMT_RGBA32;
235
236         case PNG_COLOR_TYPE_GRAY:
237                 return channel_bits == 16 ? IMG_FMT_GREYF : IMG_FMT_GREY8;
238
239         default:
240                 break;
241         }
242         return -1;
243 }
244
245 static int fmt_to_png_type(enum img_fmt fmt)
246 {
247         switch(fmt) {
248         case IMG_FMT_GREY8:
249                 return PNG_COLOR_TYPE_GRAY;
250
251         case IMG_FMT_RGB24:
252                 return PNG_COLOR_TYPE_RGB;
253
254         case IMG_FMT_RGBA32:
255                 return PNG_COLOR_TYPE_RGBA;
256
257         default:
258                 break;
259         }
260         return -1;
261 }