nuclear@14: /* nuclear@14: libimago - a multi-format image file input/output library. nuclear@14: Copyright (C) 2010 John Tsiombikas nuclear@14: nuclear@14: This program is free software: you can redistribute it and/or modify nuclear@14: it under the terms of the GNU Lesser General Public License as published nuclear@14: by the Free Software Foundation, either version 3 of the License, or nuclear@14: (at your option) any later version. nuclear@14: nuclear@14: This program is distributed in the hope that it will be useful, nuclear@14: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@14: GNU Lesser General Public License for more details. nuclear@14: nuclear@14: You should have received a copy of the GNU Lesser General Public License nuclear@14: along with this program. If not, see . nuclear@14: */ nuclear@14: nuclear@14: /* -- PNG module -- */ nuclear@14: nuclear@14: #include nuclear@14: #include nuclear@14: #include "imago2.h" nuclear@14: #include "ftype_module.h" nuclear@14: nuclear@14: static int check_file(struct img_io *io); nuclear@14: static int read_file(struct img_pixmap *img, struct img_io *io); nuclear@14: static int write_file(struct img_pixmap *img, struct img_io *io); nuclear@14: nuclear@14: static void read_func(png_struct *png, unsigned char *data, size_t len); nuclear@14: static void write_func(png_struct *png, unsigned char *data, size_t len); nuclear@14: static void flush_func(png_struct *png); nuclear@14: nuclear@14: static int png_type_to_fmt(int color_type, int channel_bits); nuclear@14: static int fmt_to_png_type(enum img_fmt fmt); nuclear@14: nuclear@14: nuclear@14: int img_register_png(void) nuclear@14: { nuclear@14: static struct ftype_module mod = {".png", check_file, read_file, write_file}; nuclear@14: return img_register_module(&mod); nuclear@14: } nuclear@14: nuclear@14: static int check_file(struct img_io *io) nuclear@14: { nuclear@14: unsigned char sig[8]; nuclear@14: int res; nuclear@14: long pos = io->seek(0, SEEK_CUR, io->uptr); nuclear@14: nuclear@14: if(io->read(sig, 8, io->uptr) < 8) { nuclear@14: io->seek(pos, SEEK_SET, io->uptr); nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1; nuclear@14: io->seek(pos, SEEK_SET, io->uptr); nuclear@14: return res; nuclear@14: } nuclear@14: nuclear@14: static int read_file(struct img_pixmap *img, struct img_io *io) nuclear@14: { nuclear@14: png_struct *png; nuclear@14: png_info *info; nuclear@14: int channel_bits, color_type, ilace_type, compression, filtering, fmt; nuclear@14: png_uint_32 xsz, ysz; nuclear@14: nuclear@14: if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) { nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: if(!(info = png_create_info_struct(png))) { nuclear@14: png_destroy_read_struct(&png, 0, 0); nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: if(setjmp(png_jmpbuf(png))) { nuclear@14: png_destroy_read_struct(&png, &info, 0); nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: png_set_read_fn(png, io, read_func); nuclear@14: png_set_sig_bytes(png, 0); nuclear@14: png_read_png(png, info, 0, 0); nuclear@14: nuclear@14: png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type, nuclear@14: &compression, &filtering); nuclear@14: if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) { nuclear@14: png_destroy_read_struct(&png, &info, 0); nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) { nuclear@14: png_destroy_read_struct(&png, &info, 0); nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: nuclear@14: if(channel_bits == 8) { nuclear@14: unsigned int i; nuclear@14: unsigned char **lineptr = (unsigned char**)png_get_rows(png, info); nuclear@14: unsigned char *dest = img->pixels; nuclear@14: nuclear@14: for(i=0; ipixelsz); nuclear@14: dest += xsz * img->pixelsz; nuclear@14: } nuclear@14: } else { nuclear@14: unsigned int i, j, num_elem; nuclear@14: unsigned char **lineptr = (unsigned char**)png_get_rows(png, info); nuclear@14: float *dest = img->pixels; nuclear@14: nuclear@14: num_elem = img->pixelsz / sizeof(float); nuclear@14: for(i=0; ifmt); nuclear@14: png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE, nuclear@14: PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); nuclear@14: png_set_text(png, info, &txt, 1); nuclear@14: nuclear@14: if(!(rows = malloc(img->height * sizeof *rows))) { nuclear@14: png_destroy_write_struct(&png, &info); nuclear@14: img_destroy(&tmpimg); nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: pixptr = img->pixels; nuclear@14: for(i=0; iheight; i++) { nuclear@14: rows[i] = pixptr; nuclear@14: pixptr += img->width * img->pixelsz; nuclear@14: } nuclear@14: png_set_rows(png, info, rows); nuclear@14: nuclear@14: png_write_png(png, info, 0, 0); nuclear@14: png_write_end(png, info); nuclear@14: png_destroy_write_struct(&png, &info); nuclear@14: nuclear@14: free(rows); nuclear@14: nuclear@14: img_destroy(&tmpimg); nuclear@14: return 0; nuclear@14: } nuclear@14: nuclear@14: static void read_func(png_struct *png, unsigned char *data, size_t len) nuclear@14: { nuclear@14: struct img_io *io = (struct img_io*)png_get_io_ptr(png); nuclear@14: nuclear@14: if(io->read(data, len, io->uptr) == -1) { nuclear@14: longjmp(png_jmpbuf(png), 1); nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static void write_func(png_struct *png, unsigned char *data, size_t len) nuclear@14: { nuclear@14: struct img_io *io = (struct img_io*)png_get_io_ptr(png); nuclear@14: nuclear@14: if(io->write(data, len, io->uptr) == -1) { nuclear@14: longjmp(png_jmpbuf(png), 1); nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static void flush_func(png_struct *png) nuclear@14: { nuclear@14: /* XXX does it matter that we can't flush? */ nuclear@14: } nuclear@14: nuclear@14: static int png_type_to_fmt(int color_type, int channel_bits) nuclear@14: { nuclear@14: /* only 8 and 16 bits per channel ar supported at the moment */ nuclear@14: if(channel_bits != 8 && channel_bits != 16) { nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: switch(color_type) { nuclear@14: case PNG_COLOR_TYPE_RGB: nuclear@14: return channel_bits == 16 ? IMG_FMT_RGBF : IMG_FMT_RGB24; nuclear@14: nuclear@14: case PNG_COLOR_TYPE_RGB_ALPHA: nuclear@14: return channel_bits == 16 ? IMG_FMT_RGBAF : IMG_FMT_RGBA32; nuclear@14: nuclear@14: case PNG_COLOR_TYPE_GRAY: nuclear@14: return channel_bits == 16 ? IMG_FMT_GREYF : IMG_FMT_GREY8; nuclear@14: nuclear@14: default: nuclear@14: break; nuclear@14: } nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: static int fmt_to_png_type(enum img_fmt fmt) nuclear@14: { nuclear@14: switch(fmt) { nuclear@14: case IMG_FMT_GREY8: nuclear@14: return PNG_COLOR_TYPE_GRAY; nuclear@14: nuclear@14: case IMG_FMT_RGB24: nuclear@14: return PNG_COLOR_TYPE_RGB; nuclear@14: nuclear@14: case IMG_FMT_RGBA32: nuclear@14: return PNG_COLOR_TYPE_RGBA; nuclear@14: nuclear@14: default: nuclear@14: break; nuclear@14: } nuclear@14: return -1; nuclear@14: }