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