added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / gfx / image.c
1 /*
2 This is a small image library.
3 Copyright (C) 2004 John Tsiombikas <nuclear@siggraph.org>
4
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 /* image file library
21  *
22  * author: John Tsiombikas 2003
23  * modified: John Tsiombikas 2004
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "image.h"
29
30 #ifdef IMGLIB_USE_PNG
31 int check_png(FILE *fp);
32 void *load_png(FILE *fp, unsigned long *xsz, unsigned long *ysz);
33 int save_png(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz);
34 #endif  /* IMGLIB_USE_PNG */
35
36 #ifdef IMGLIB_USE_JPEG
37 int check_jpeg(FILE *fp);
38 void *load_jpeg(FILE *fp, unsigned long *xsz, unsigned long *ysz);
39 int save_jpeg(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz);
40 #endif  /* IMGLIB_USE_JPEG */
41
42 #ifdef IMGLIB_USE_TGA
43 int check_tga(FILE *fp);
44 void *load_tga(FILE *fp, unsigned long *xsz, unsigned long *ysz);
45 int save_tga(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz);
46 #endif  /* IMGLIB_USE_TGA */
47
48 #ifdef IMGLIB_USE_PPM
49 int check_ppm(FILE *fp);
50 void *load_ppm(FILE *fp, unsigned long *xsz, unsigned long *ysz);
51 int save_ppm(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz);
52 #endif  /* IMGLIB_USE_PPM */
53
54
55 static unsigned long save_flags;
56
57
58 void *load_image(const char *fname, unsigned long *xsz, unsigned long *ysz) {
59         FILE *file;
60
61         if(!(file = fopen(fname, "rb"))) {
62                 fprintf(stderr, "Image loading error: could not open file %s\n", fname);
63                 return 0;
64         }
65         
66 #ifdef IMGLIB_USE_PNG
67         if(check_png(file)) {
68                 return load_png(file, xsz, ysz);
69         }
70 #endif  /* IMGLIB_USE_PNG */
71         
72 #ifdef IMGLIB_USE_JPEG
73         if(check_jpeg(file)) {
74                 return load_jpeg(file, xsz, ysz);
75         }
76 #endif  /* IMGLIB_USE_JPEG */
77         
78 #ifdef IMGLIB_USE_TGA
79         if(check_tga(file)) {
80                 return load_tga(file, xsz, ysz);
81         }
82 #endif  /* IMGLIB_USE_TGA */
83
84 #ifdef IMGLIB_USE_PPM
85         if(check_ppm(file)) {
86                 return load_ppm(file, xsz, ysz);
87         }
88 #endif  /* IMGLIB_USE_PPM */
89
90         fclose(file);
91         return 0;
92 }
93
94 void free_image(void *img) {
95         free(img);
96 }
97
98 int save_image(const char *fname, void *pixels, unsigned long xsz, unsigned long ysz, enum image_file_format fmt) {
99         FILE *fp;
100
101         if(!(fp = fopen(fname, "wb"))) {
102                 fprintf(stderr, "Image saving error: could not open file %s for writing\n", fname);
103                 return -1;
104         }
105
106         switch(fmt) {
107         case IMG_FMT_PNG:
108 #ifdef IMGLIB_USE_PNG
109                 save_png(fp, pixels, xsz, ysz);
110                 break;
111 #endif  /* IMGLIB_USE_PNG */
112
113         case IMG_FMT_JPEG:
114 #ifdef IMGLIB_USE_JPEG
115                 save_jpeg(fp, pixels, xsz, ysz);
116 #endif  /* IMGLIB_USE_JPEG */
117                 break;
118                 
119         case IMG_FMT_TGA:
120 #ifdef IMGLIB_USE_TGA
121                 save_tga(fp, pixels, xsz, ysz);
122 #endif  /* IMGLIB_USE_TGA */
123                 break;
124
125         case IMG_FMT_PPM:
126 #ifdef IMGLIB_USE_PPM
127                 save_ppm(fp, pixels, xsz, ysz);
128 #endif  /* IMGLIB_USE_PPM */
129                 break;
130
131         default:
132                 fprintf(stderr, "Image saving error: error saving %s, invalid format specification", fname);
133                 break;
134         }
135
136         fclose(fp);
137         
138         return 0;
139 }
140
141
142 void set_image_save_flags(unsigned int flags) {
143         save_flags = flags;
144 }
145
146 unsigned int get_image_save_flags(void) {
147         return save_flags;
148 }