2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
4 This is a small image library.
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * author: John Tsiombikas 2003
24 * modified: John Tsiombikas 2004
27 #include "3dengfx_config.h"
36 #include "color_bits.h"
37 #include "common/types.h"
39 #define FILE_SIG_BYTES 8
41 int check_png(FILE *fp) {
42 unsigned char sig[FILE_SIG_BYTES];
44 fread(sig, 1, FILE_SIG_BYTES, fp);
46 return png_sig_cmp(sig, 0, FILE_SIG_BYTES) == 0 ? 1 : 0;
49 void *load_png(FILE *fp, unsigned long *xsz, unsigned long *ysz) {
53 uint32_t **lineptr, *pixels;
54 int channel_bits, color_type, ilace_type, compression, filtering;
56 if(!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
61 if(!(info_ptr = png_create_info_struct(png_ptr))) {
62 png_destroy_read_struct(&png_ptr, 0, 0);
67 if(setjmp(png_jmpbuf(png_ptr))) {
68 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
73 png_init_io(png_ptr, fp);
74 png_set_sig_bytes(png_ptr, FILE_SIG_BYTES);
76 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, 0);
78 png_get_IHDR(png_ptr, info_ptr, xsz, ysz, &channel_bits, &color_type, &ilace_type, &compression, &filtering);
79 pixels = malloc(*xsz * *ysz * sizeof(uint32_t));
81 lineptr = (uint32_t**)png_get_rows(png_ptr, info_ptr);
83 for(i=0; i<*ysz; i++) {
86 case PNG_COLOR_TYPE_RGB:
89 unsigned char *ptr = (unsigned char*)lineptr[i];
90 for(j=0; j<*xsz; j++) {
93 pixel = PACK_COLOR24(*(ptr+2), *(ptr+1), *ptr);
95 pixels[i * *xsz + j] = pixel;
100 case PNG_COLOR_TYPE_RGB_ALPHA:
101 memcpy(&pixels[i * *xsz], lineptr[i], *xsz * sizeof(uint32_t));
105 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
112 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
118 /* TODO: implement this */
119 int save_png(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz) {
120 fprintf(stderr, "saving png files is not implemented yet");
124 #endif /* IMGLIB_USE_PNG */