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 uint32_t width, height;
55 int channel_bits, color_type, ilace_type, compression, filtering;
57 if(!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
62 if(!(info_ptr = png_create_info_struct(png_ptr))) {
63 png_destroy_read_struct(&png_ptr, 0, 0);
68 if(setjmp(png_jmpbuf(png_ptr))) {
69 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
74 png_init_io(png_ptr, fp);
75 png_set_sig_bytes(png_ptr, FILE_SIG_BYTES);
77 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, 0);
79 png_get_IHDR(png_ptr, info_ptr, &width, &height, &channel_bits, &color_type, &ilace_type, &compression, &filtering);
82 pixels = malloc(*xsz * *ysz * sizeof(uint32_t));
84 lineptr = (uint32_t**)png_get_rows(png_ptr, info_ptr);
86 for(i=0; i<*ysz; i++) {
89 case PNG_COLOR_TYPE_RGB:
92 unsigned char *ptr = (unsigned char*)lineptr[i];
93 for(j=0; j<*xsz; j++) {
96 pixel = PACK_COLOR24(*(ptr+2), *(ptr+1), *ptr);
98 pixels[i * *xsz + j] = pixel;
103 case PNG_COLOR_TYPE_RGB_ALPHA:
104 memcpy(&pixels[i * *xsz], lineptr[i], *xsz * sizeof(uint32_t));
108 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
115 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
121 /* TODO: implement this */
122 int save_png(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz) {
123 fprintf(stderr, "saving png files is not implemented yet");
127 #endif /* IMGLIB_USE_PNG */