istereo2

diff libs/imago2/file_png.c @ 2:81d35769f546

added the tunnel effect source
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Sep 2015 05:51:51 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/imago2/file_png.c	Sat Sep 19 05:51:51 2015 +0300
     1.3 @@ -0,0 +1,244 @@
     1.4 +/*
     1.5 +libimago - a multi-format image file input/output library.
     1.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +/* -- PNG module -- */
    1.23 +
    1.24 +#include <stdlib.h>
    1.25 +#include <png.h>
    1.26 +#include "imago2.h"
    1.27 +#include "ftype_module.h"
    1.28 +
    1.29 +static int check_file(struct img_io *io);
    1.30 +static int read_file(struct img_pixmap *img, struct img_io *io);
    1.31 +static int write_file(struct img_pixmap *img, struct img_io *io);
    1.32 +
    1.33 +static void read_func(png_struct *png, unsigned char *data, size_t len);
    1.34 +static void write_func(png_struct *png, unsigned char *data, size_t len);
    1.35 +static void flush_func(png_struct *png);
    1.36 +
    1.37 +static int png_type_to_fmt(int color_type, int channel_bits);
    1.38 +static int fmt_to_png_type(enum img_fmt fmt);
    1.39 +
    1.40 +
    1.41 +int img_register_png(void)
    1.42 +{
    1.43 +	static struct ftype_module mod = {".png", check_file, read_file, write_file};
    1.44 +	return img_register_module(&mod);
    1.45 +}
    1.46 +
    1.47 +static int check_file(struct img_io *io)
    1.48 +{
    1.49 +	unsigned char sig[8];
    1.50 +	int res;
    1.51 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    1.52 +
    1.53 +	if(io->read(sig, 8, io->uptr) < 8) {
    1.54 +		io->seek(pos, SEEK_SET, io->uptr);
    1.55 +		return -1;
    1.56 +	}
    1.57 +
    1.58 +	res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
    1.59 +	io->seek(pos, SEEK_SET, io->uptr);
    1.60 +	return res;
    1.61 +}
    1.62 +
    1.63 +static int read_file(struct img_pixmap *img, struct img_io *io)
    1.64 +{
    1.65 +	png_struct *png;
    1.66 +	png_info *info;
    1.67 +	unsigned char **lineptr, *dest;
    1.68 +	int i, channel_bits, color_type, ilace_type, compression, filtering, fmt;
    1.69 +	unsigned long xsz, ysz;
    1.70 +
    1.71 +	if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
    1.72 +		return -1;
    1.73 +	}
    1.74 +
    1.75 +	if(!(info = png_create_info_struct(png))) {
    1.76 +		png_destroy_read_struct(&png, 0, 0);
    1.77 +		return -1;
    1.78 +	}
    1.79 +
    1.80 +	if(setjmp(png_jmpbuf(png))) {
    1.81 +		png_destroy_read_struct(&png, &info, 0);
    1.82 +		return -1;
    1.83 +	}
    1.84 +
    1.85 +	png_set_read_fn(png, io, read_func);
    1.86 +	png_set_sig_bytes(png, 0);
    1.87 +	png_read_png(png, info, 0, 0);
    1.88 +
    1.89 +	png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
    1.90 +			&compression, &filtering);
    1.91 +	if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
    1.92 +		png_destroy_read_struct(&png, &info, 0);
    1.93 +		return -1;
    1.94 +	}
    1.95 +
    1.96 +	if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
    1.97 +		png_destroy_read_struct(&png, &info, 0);
    1.98 +		return -1;
    1.99 +	}
   1.100 +
   1.101 +	lineptr = (unsigned char**)png_get_rows(png, info);
   1.102 +
   1.103 +	dest = img->pixels;
   1.104 +	for(i=0; i<ysz; i++) {
   1.105 +		memcpy(dest, lineptr[i], xsz * img->pixelsz);
   1.106 +		dest += xsz * img->pixelsz;
   1.107 +	}
   1.108 +	png_destroy_read_struct(&png, &info, 0);
   1.109 +	return 0;
   1.110 +}
   1.111 +
   1.112 +
   1.113 +static int write_file(struct img_pixmap *img, struct img_io *io)
   1.114 +{
   1.115 +	png_struct *png;
   1.116 +	png_info *info;
   1.117 +	png_text txt;
   1.118 +	struct img_pixmap tmpimg;
   1.119 +	unsigned char **rows;
   1.120 +	unsigned char *pixptr;
   1.121 +	int i, coltype;
   1.122 +
   1.123 +	img_init(&tmpimg);
   1.124 +
   1.125 +	if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
   1.126 +		return -1;
   1.127 +	}
   1.128 +	if(!(info = png_create_info_struct(png))) {
   1.129 +		png_destroy_write_struct(&png, 0);
   1.130 +		return -1;
   1.131 +	}
   1.132 +
   1.133 +	/* if the input image is floating-point, we need to convert it to integer */
   1.134 +	if(img_is_float(img)) {
   1.135 +		if(img_copy(&tmpimg, img) == -1) {
   1.136 +			return -1;
   1.137 +		}
   1.138 +		if(img_to_integer(&tmpimg) == -1) {
   1.139 +			img_destroy(&tmpimg);
   1.140 +			return -1;
   1.141 +		}
   1.142 +		img = &tmpimg;
   1.143 +	}
   1.144 +
   1.145 +	txt.compression = PNG_TEXT_COMPRESSION_NONE;
   1.146 +	txt.key = "Software";
   1.147 +	txt.text = "libimago2";
   1.148 +	txt.text_length = 0;
   1.149 +
   1.150 +	if(setjmp(png_jmpbuf(png))) {
   1.151 +		png_destroy_write_struct(&png, &info);
   1.152 +		img_destroy(&tmpimg);
   1.153 +		return -1;
   1.154 +	}
   1.155 +	png_set_write_fn(png, io, write_func, flush_func);
   1.156 +
   1.157 +	coltype = fmt_to_png_type(img->fmt);
   1.158 +	png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
   1.159 +			PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
   1.160 +	png_set_text(png, info, &txt, 1);
   1.161 +
   1.162 +	if(!(rows = malloc(img->height * sizeof *rows))) {
   1.163 +		png_destroy_write_struct(&png, &info);
   1.164 +		img_destroy(&tmpimg);
   1.165 +		return -1;
   1.166 +	}
   1.167 +
   1.168 +	pixptr = img->pixels;
   1.169 +	for(i=0; i<img->height; i++) {
   1.170 +		rows[i] = pixptr;
   1.171 +		pixptr += img->width * img->pixelsz;
   1.172 +	}
   1.173 +	png_set_rows(png, info, rows);
   1.174 +
   1.175 +	png_write_png(png, info, 0, 0);
   1.176 +	png_write_end(png, info);
   1.177 +	png_destroy_write_struct(&png, &info);
   1.178 +
   1.179 +	free(rows);
   1.180 +
   1.181 +	img_destroy(&tmpimg);
   1.182 +	return 0;
   1.183 +}
   1.184 +
   1.185 +static void read_func(png_struct *png, unsigned char *data, size_t len)
   1.186 +{
   1.187 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   1.188 +
   1.189 +	if(io->read(data, len, io->uptr) == -1) {
   1.190 +		longjmp(png_jmpbuf(png), 1);
   1.191 +	}
   1.192 +}
   1.193 +
   1.194 +static void write_func(png_struct *png, unsigned char *data, size_t len)
   1.195 +{
   1.196 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   1.197 +
   1.198 +	if(io->write(data, len, io->uptr) == -1) {
   1.199 +		longjmp(png_jmpbuf(png), 1);
   1.200 +	}
   1.201 +}
   1.202 +
   1.203 +static void flush_func(png_struct *png)
   1.204 +{
   1.205 +	/* XXX does it matter that we can't flush? */
   1.206 +}
   1.207 +
   1.208 +static int png_type_to_fmt(int color_type, int channel_bits)
   1.209 +{
   1.210 +	/* we don't support non-8bit-per-channel images yet */
   1.211 +	if(channel_bits > 8) {
   1.212 +		return -1;
   1.213 +	}
   1.214 +
   1.215 +	switch(color_type) {
   1.216 +	case PNG_COLOR_TYPE_RGB:
   1.217 +		return IMG_FMT_RGB24;
   1.218 +
   1.219 +	case PNG_COLOR_TYPE_RGB_ALPHA:
   1.220 +		return IMG_FMT_RGBA32;
   1.221 +
   1.222 +	case PNG_COLOR_TYPE_GRAY:
   1.223 +		return IMG_FMT_GREY8;
   1.224 +
   1.225 +	default:
   1.226 +		break;
   1.227 +	}
   1.228 +	return -1;
   1.229 +}
   1.230 +
   1.231 +static int fmt_to_png_type(enum img_fmt fmt)
   1.232 +{
   1.233 +	switch(fmt) {
   1.234 +	case IMG_FMT_GREY8:
   1.235 +		return PNG_COLOR_TYPE_GRAY;
   1.236 +
   1.237 +	case IMG_FMT_RGB24:
   1.238 +		return PNG_COLOR_TYPE_RGB;
   1.239 +
   1.240 +	case IMG_FMT_RGBA32:
   1.241 +		return PNG_COLOR_TYPE_RGBA;
   1.242 +
   1.243 +	default:
   1.244 +		break;
   1.245 +	}
   1.246 +	return -1;
   1.247 +}