2 This is a small image library.
3 Copyright (C) 2004 John Tsiombikas <nuclear@siggraph.org>
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.
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.
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
22 * author: John Tsiombikas 2003
23 * modified: John Tsiombikas 2004
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 */
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 */
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 */
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 */
55 static unsigned long save_flags;
58 void *load_image(const char *fname, unsigned long *xsz, unsigned long *ysz) {
61 if(!(file = fopen(fname, "rb"))) {
62 fprintf(stderr, "Image loading error: could not open file %s\n", fname);
68 return load_png(file, xsz, ysz);
70 #endif /* IMGLIB_USE_PNG */
72 #ifdef IMGLIB_USE_JPEG
73 if(check_jpeg(file)) {
74 return load_jpeg(file, xsz, ysz);
76 #endif /* IMGLIB_USE_JPEG */
80 return load_tga(file, xsz, ysz);
82 #endif /* IMGLIB_USE_TGA */
86 return load_ppm(file, xsz, ysz);
88 #endif /* IMGLIB_USE_PPM */
94 void free_image(void *img) {
98 int save_image(const char *fname, void *pixels, unsigned long xsz, unsigned long ysz, enum image_file_format fmt) {
101 if(!(fp = fopen(fname, "wb"))) {
102 fprintf(stderr, "Image saving error: could not open file %s for writing\n", fname);
108 #ifdef IMGLIB_USE_PNG
109 save_png(fp, pixels, xsz, ysz);
111 #endif /* IMGLIB_USE_PNG */
114 #ifdef IMGLIB_USE_JPEG
115 save_jpeg(fp, pixels, xsz, ysz);
116 #endif /* IMGLIB_USE_JPEG */
120 #ifdef IMGLIB_USE_TGA
121 save_tga(fp, pixels, xsz, ysz);
122 #endif /* IMGLIB_USE_TGA */
126 #ifdef IMGLIB_USE_PPM
127 save_ppm(fp, pixels, xsz, ysz);
128 #endif /* IMGLIB_USE_PPM */
132 fprintf(stderr, "Image saving error: error saving %s, invalid format specification", fname);
142 void set_image_save_flags(unsigned int flags) {
146 unsigned int get_image_save_flags(void) {