dungeon_crawler

annotate prototype/imago2/file_png.c @ 67:2560a7ab0243

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