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