istereo
view libs/imago2/file_png.c @ 33:33e9eedad390
ah.. blunder
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Fri, 09 Sep 2011 00:03:37 +0300 |
parents | |
children |
line source
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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 */
19 /* -- PNG module -- */
21 #include <stdlib.h>
22 #include <png.h>
23 #include "imago2.h"
24 #include "ftype_module.h"
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);
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);
34 static int png_type_to_fmt(int color_type, int channel_bits);
35 static int fmt_to_png_type(enum img_fmt fmt);
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 }
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);
50 if(io->read(sig, 8, io->uptr) < 8) {
51 io->seek(pos, SEEK_SET, io->uptr);
52 return -1;
53 }
55 res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
56 io->seek(pos, SEEK_SET, io->uptr);
57 return res;
58 }
60 static int read_file(struct img_pixmap *img, struct img_io *io)
61 {
62 png_struct *png;
63 png_info *info;
64 unsigned char **lineptr, *dest;
65 int i, channel_bits, color_type, ilace_type, compression, filtering, fmt;
66 unsigned long xsz, ysz;
68 if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
69 return -1;
70 }
72 if(!(info = png_create_info_struct(png))) {
73 png_destroy_read_struct(&png, 0, 0);
74 return -1;
75 }
77 if(setjmp(png_jmpbuf(png))) {
78 png_destroy_read_struct(&png, &info, 0);
79 return -1;
80 }
82 png_set_read_fn(png, io, read_func);
83 png_set_sig_bytes(png, 0);
84 png_read_png(png, info, 0, 0);
86 png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
87 &compression, &filtering);
88 if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
89 png_destroy_read_struct(&png, &info, 0);
90 return -1;
91 }
93 if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
94 png_destroy_read_struct(&png, &info, 0);
95 return -1;
96 }
98 lineptr = (unsigned char**)png_get_rows(png, info);
100 dest = img->pixels;
101 for(i=0; i<ysz; i++) {
102 memcpy(dest, lineptr[i], xsz * img->pixelsz);
103 dest += xsz * img->pixelsz;
104 }
105 png_destroy_read_struct(&png, &info, 0);
106 return 0;
107 }
110 static int write_file(struct img_pixmap *img, struct img_io *io)
111 {
112 png_struct *png;
113 png_info *info;
114 png_text txt;
115 struct img_pixmap tmpimg;
116 unsigned char **rows;
117 unsigned char *pixptr;
118 int i, coltype;
120 img_init(&tmpimg);
122 if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
123 return -1;
124 }
125 if(!(info = png_create_info_struct(png))) {
126 png_destroy_write_struct(&png, 0);
127 return -1;
128 }
130 /* if the input image is floating-point, we need to convert it to integer */
131 if(img_is_float(img)) {
132 if(img_copy(&tmpimg, img) == -1) {
133 return -1;
134 }
135 if(img_to_integer(&tmpimg) == -1) {
136 img_destroy(&tmpimg);
137 return -1;
138 }
139 img = &tmpimg;
140 }
142 txt.compression = PNG_TEXT_COMPRESSION_NONE;
143 txt.key = "Software";
144 txt.text = "libimago2";
145 txt.text_length = 0;
147 if(setjmp(png_jmpbuf(png))) {
148 png_destroy_write_struct(&png, &info);
149 img_destroy(&tmpimg);
150 return -1;
151 }
152 png_set_write_fn(png, io, write_func, flush_func);
154 coltype = fmt_to_png_type(img->fmt);
155 png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
156 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
157 png_set_text(png, info, &txt, 1);
159 if(!(rows = malloc(img->height * sizeof *rows))) {
160 png_destroy_write_struct(&png, &info);
161 img_destroy(&tmpimg);
162 return -1;
163 }
165 pixptr = img->pixels;
166 for(i=0; i<img->height; i++) {
167 rows[i] = pixptr;
168 pixptr += img->width * img->pixelsz;
169 }
170 png_set_rows(png, info, rows);
172 png_write_png(png, info, 0, 0);
173 png_write_end(png, info);
174 png_destroy_write_struct(&png, &info);
176 free(rows);
178 img_destroy(&tmpimg);
179 return 0;
180 }
182 static void read_func(png_struct *png, unsigned char *data, size_t len)
183 {
184 struct img_io *io = (struct img_io*)png_get_io_ptr(png);
186 if(io->read(data, len, io->uptr) == -1) {
187 longjmp(png_jmpbuf(png), 1);
188 }
189 }
191 static void write_func(png_struct *png, unsigned char *data, size_t len)
192 {
193 struct img_io *io = (struct img_io*)png_get_io_ptr(png);
195 if(io->write(data, len, io->uptr) == -1) {
196 longjmp(png_jmpbuf(png), 1);
197 }
198 }
200 static void flush_func(png_struct *png)
201 {
202 /* XXX does it matter that we can't flush? */
203 }
205 static int png_type_to_fmt(int color_type, int channel_bits)
206 {
207 /* we don't support non-8bit-per-channel images yet */
208 if(channel_bits > 8) {
209 return -1;
210 }
212 switch(color_type) {
213 case PNG_COLOR_TYPE_RGB:
214 return IMG_FMT_RGB24;
216 case PNG_COLOR_TYPE_RGB_ALPHA:
217 return IMG_FMT_RGBA32;
219 case PNG_COLOR_TYPE_GRAY:
220 return IMG_FMT_GREY8;
222 default:
223 break;
224 }
225 return -1;
226 }
228 static int fmt_to_png_type(enum img_fmt fmt)
229 {
230 switch(fmt) {
231 case IMG_FMT_GREY8:
232 return PNG_COLOR_TYPE_GRAY;
234 case IMG_FMT_RGB24:
235 return PNG_COLOR_TYPE_RGB;
237 case IMG_FMT_RGBA32:
238 return PNG_COLOR_TYPE_RGBA;
240 default:
241 break;
242 }
243 return -1;
244 }