istereo

annotate libs/imago2/file_png.c @ 35:23e5d274b2a2

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