include local png/jpeg header files
[summerhack] / src / 3dengfx / src / gfx / image_png.c
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This is a small image library.
5
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.
10
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.
15
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
19 */
20
21 /* png support
22  * 
23  * author: John Tsiombikas 2003
24  * modified: John Tsiombikas 2004
25  */
26
27 #include "3dengfx_config.h"
28 #include "image.h"
29 #include <stdio.h>
30 #include <string.h>
31
32 #ifdef IMGLIB_USE_PNG
33
34 #include <stdlib.h>
35 #include "png.h"
36 #include "color_bits.h"
37 #include "common/types.h"
38
39 #define FILE_SIG_BYTES  8
40
41 int check_png(FILE *fp) {
42         unsigned char sig[FILE_SIG_BYTES];
43
44         fread(sig, 1, FILE_SIG_BYTES, fp);
45
46         return png_sig_cmp(sig, 0, FILE_SIG_BYTES) == 0 ? 1 : 0;
47 }
48
49 void *load_png(FILE *fp, unsigned long *xsz, unsigned long *ysz) {
50         png_struct *png_ptr;
51         png_info *info_ptr;
52         int i;
53         uint32_t **lineptr, *pixels;
54         png_uint_32 width, height;
55         int channel_bits, color_type, ilace_type, compression, filtering;
56         
57         if(!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
58                 fclose(fp);
59                 return 0;
60         }
61         
62         if(!(info_ptr = png_create_info_struct(png_ptr))) {
63                 png_destroy_read_struct(&png_ptr, 0, 0);
64                 fclose(fp);
65                 return 0;
66         }
67
68         if(setjmp(png_jmpbuf(png_ptr))) {
69                 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
70                 fclose(fp);
71                 return 0;
72         }
73         
74         png_init_io(png_ptr, fp);       
75         png_set_sig_bytes(png_ptr, FILE_SIG_BYTES);
76         
77         png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, 0);
78                 
79         png_get_IHDR(png_ptr, info_ptr, &width, &height, &channel_bits, &color_type, &ilace_type, &compression, &filtering);
80         *xsz = width;
81         *ysz = height;
82         pixels = malloc(*xsz * *ysz * sizeof(uint32_t));
83         
84         lineptr = (uint32_t**)png_get_rows(png_ptr, info_ptr);
85         
86         for(i=0; i<*ysz; i++) {
87                 
88                 switch(color_type) {
89                 case PNG_COLOR_TYPE_RGB:
90                         {
91                                 int j;
92                                 unsigned char *ptr = (unsigned char*)lineptr[i];
93                                 for(j=0; j<*xsz; j++) {
94                         
95                                         uint32_t pixel;
96                                         pixel = PACK_COLOR24(*(ptr+2), *(ptr+1), *ptr);
97                                         ptr+=3;
98                                         pixels[i * *xsz + j] = pixel;                   
99                                 }
100                         }
101                         break;
102                         
103                 case PNG_COLOR_TYPE_RGB_ALPHA:
104                         memcpy(&pixels[i * *xsz], lineptr[i], *xsz * sizeof(uint32_t));
105                         break;
106                         
107                 default:
108                         png_destroy_read_struct(&png_ptr, &info_ptr, 0);
109                         fclose(fp);
110                         return 0;
111                 }
112                                 
113         }
114         
115         png_destroy_read_struct(&png_ptr, &info_ptr, 0);
116         fclose(fp);
117         
118         return pixels;
119 }
120
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");
124         return -1;
125 }
126
127 #endif  /* IMGLIB_USE_PNG */