eqemu

annotate libs/libimago/src/file_png.c @ 10:819c7ebb1bec

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