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