0.51.1 pre snapshot. Be careful, it may be buggy. It fixes some bugs though.
[wmaker-crm.git] / wrlib / load.c
blob0e736a25edb377d8398dbc4d3b70a2fa45fe1208
1 /* load.c - load image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <config.h>
24 #include <X11/Xlib.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <time.h>
32 #include <assert.h>
34 #ifdef USE_PNG
35 #include <png.h>
36 #endif
38 #include "wraster.h"
41 typedef struct RCachedImage {
42 RImage *image;
43 char *file;
44 time_t last_modif; /* last time file was modified */
45 time_t last_use; /* last time image was used */
46 } RCachedImage;
50 * Size of image cache
52 static int RImageCacheSize = -1;
55 * Max. size of image to store in cache
57 static int RImageCacheMaxImage = -1; /* 0 = any size */
59 #define IMAGE_CACHE_SIZE 8
61 #define IMAGE_CACHE_MAX_IMAGE 64*64
63 static RCachedImage *RImageCache;
69 #define IM_ERROR -1
70 #define IM_UNKNOWN 0
71 #define IM_XPM 1
72 #define IM_TIFF 2
73 #define IM_PNG 3
74 #define IM_PPM 4
75 #define IM_JPEG 5
76 #define IM_GIF 6
77 /* How many image types do we have. */
78 /* Increase this when adding new image types! */
79 #define IM_TYPES 6
82 static int identFile(char *path);
84 extern RImage *RLoadPPM(RContext *context, char *file_name, int index);
86 extern RImage *RLoadXPM(RContext *context, char *file, int index);
89 #ifdef USE_TIFF
90 extern RImage *RLoadTIFF(RContext *context, char *file, int index);
91 #endif
92 #ifdef USE_PNG
93 extern RImage *RLoadPNG(RContext *context, char *file, int index);
94 #endif
95 #ifdef USE_JPEG
96 extern RImage *RLoadJPEG(RContext *context, char *file_name, int index);
97 #endif
98 #ifdef USE_GIF
99 extern RImage *RLoadGIF(RContext *context, char *file_name, int index);
100 #endif
103 static char*
104 wstrdup(char *s)
106 char *tmp;
108 tmp = malloc(strlen(s)+1);
109 if (!tmp)
110 return NULL;
111 return strcpy(tmp, s);
115 char**
116 RSupportedFileFormats(void)
118 static char *tmp[IM_TYPES+1];
119 int i = 0;
121 /* built-in */
122 tmp[i++] = "XPM";
123 /* built-in */
124 tmp[i++] = "PPM";
125 #ifdef USE_TIFF
126 tmp[i++] = "TIFF";
127 #endif
128 #ifdef USE_PNG
129 tmp[i++] = "PNG";
130 #endif
131 #ifdef USE_JPEG
132 tmp[i++] = "JPEG";
133 #endif
134 #ifdef USE_GIF
135 tmp[i++] = "GIF";
136 #endif
137 tmp[i] = NULL;
139 return tmp;
143 static void
144 init_cache()
146 char *tmp;
148 tmp = getenv("RIMAGE_CACHE");
149 if (!tmp || sscanf(tmp, "%i", &RImageCacheSize)!=1) {
150 RImageCacheSize = IMAGE_CACHE_SIZE;
152 if (RImageCacheSize<0)
153 RImageCacheSize = 0;
155 tmp = getenv("RIMAGE_CACHE_SIZE");
156 if (!tmp || sscanf(tmp, "%i", &RImageCacheMaxImage)!=1) {
157 RImageCacheMaxImage = IMAGE_CACHE_MAX_IMAGE;
160 if (RImageCacheSize>0) {
161 RImageCache = malloc(sizeof(RCachedImage)*RImageCacheSize);
162 if (RImageCache==NULL) {
163 printf("wrlib: out of memory for image cache\n");
164 return;
166 memset(RImageCache, 0, sizeof(RCachedImage)*RImageCacheSize);
171 RImage*
172 RLoadImage(RContext *context, char *file, int index)
174 RImage *image = NULL;
175 int i;
176 struct stat st;
178 assert(file!=NULL);
180 if (RImageCacheSize<0) {
181 init_cache();
184 if (RImageCacheSize>0) {
186 for (i=0; i<RImageCacheSize; i++) {
187 if (RImageCache[i].file
188 && strcmp(file, RImageCache[i].file)==0) {
190 if (stat(file, &st)==0
191 && st.st_mtime == RImageCache[i].last_modif) {
192 RImageCache[i].last_use = time(NULL);
194 return RCloneImage(RImageCache[i].image);
196 } else {
197 free(RImageCache[i].file);
198 RImageCache[i].file = NULL;
199 RDestroyImage(RImageCache[i].image);
205 switch (identFile(file)) {
206 case IM_ERROR:
207 return NULL;
209 case IM_UNKNOWN:
210 RErrorCode = RERR_BADFORMAT;
211 return NULL;
213 case IM_XPM:
214 image = RLoadXPM(context, file, index);
215 break;
217 #ifdef USE_TIFF
218 case IM_TIFF:
219 image = RLoadTIFF(context, file, index);
220 break;
221 #endif /* USE_TIFF */
223 #ifdef USE_PNG
224 case IM_PNG:
225 image = RLoadPNG(context, file, index);
226 break;
227 #endif /* USE_PNG */
229 #ifdef USE_JPEG
230 case IM_JPEG:
231 image = RLoadJPEG(context, file, index);
232 break;
233 #endif /* USE_JPEG */
235 #ifdef USE_GIF
236 case IM_GIF:
237 image = RLoadGIF(context, file, index);
238 break;
239 #endif /* USE_GIF */
241 case IM_PPM:
242 image = RLoadPPM(context, file, index);
243 break;
245 default:
246 RErrorCode = RERR_BADFORMAT;
247 return NULL;
251 /* store image in cache */
252 if (RImageCacheSize>0 && image &&
253 (RImageCacheMaxImage==0
254 || RImageCacheMaxImage >= image->width*image->height)) {
255 time_t oldest=time(NULL);
256 int oldest_idx = 0;
257 int done = 0;
259 for (i=0; i<RImageCacheSize; i++) {
260 if (!RImageCache[i].file) {
261 RImageCache[i].file = malloc(strlen(file)+1);
262 strcpy(RImageCache[i].file, file);
263 RImageCache[i].image = RCloneImage(image);
264 RImageCache[i].last_modif = st.st_mtime;
265 RImageCache[i].last_use = time(NULL);
266 done = 1;
267 break;
268 } else {
269 if (oldest > RImageCache[i].last_use) {
270 oldest = RImageCache[i].last_use;
271 oldest_idx = i;
276 /* if no slot available, dump least recently used one */
277 if (!done) {
278 free(RImageCache[oldest_idx].file);
279 RDestroyImage(RImageCache[oldest_idx].image);
280 RImageCache[oldest_idx].file = malloc(strlen(file)+1);
281 strcpy(RImageCache[oldest_idx].file, file);
282 RImageCache[oldest_idx].image = RCloneImage(image);
283 RImageCache[oldest_idx].last_modif = st.st_mtime;
284 RImageCache[oldest_idx].last_use = time(NULL);
288 return image;
295 static int
296 identFile(char *path)
298 int fd;
299 unsigned char buffer[32];
301 assert(path!=NULL);
303 fd = open(path, O_RDONLY);
304 if (fd < 0) {
305 RErrorCode = RERR_OPEN;
306 return IM_ERROR;
308 if (read(fd, buffer, 32)<1) {
309 close(fd);
310 RErrorCode = RERR_READ;
311 return IM_ERROR;
313 close(fd);
315 /* check for XPM */
316 if (strncmp((char*)buffer, "/* XPM */", 9)==0)
317 return IM_XPM;
319 /* check for TIFF */
320 if ((buffer[0]=='I' && buffer[1]=='I' && buffer[2]=='*' && buffer[3]==0)
321 ||(buffer[0]=='M' && buffer[1]=='M' && buffer[2]==0 && buffer[3]=='*'))
322 return IM_TIFF;
324 #ifdef USE_PNG
325 /* check for PNG */
326 if (png_check_sig(buffer, 8))
327 return IM_PNG;
328 #endif
330 /* check for raw PPM or PGM */
331 if (buffer[0]=='P' && (buffer[1]=='5' || buffer[1]=='6'))
332 return IM_PPM;
334 /* check for JPEG */
335 if (buffer[0] == 0xff && buffer[1] == 0xd8)
336 return IM_JPEG;
338 /* check for GIF */
339 if (buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F')
340 return IM_GIF;
342 return IM_UNKNOWN;