Moved parameters of WPrefs's expert check-buttons to a single place
[wmaker-crm.git] / wrlib / load.c
blobd636250fa8f55fadb027364c09b33c3998f23af6
1 /* load.c - load image from file
3 * Raster graphics library
5 * Copyright (c) 1997-2003 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 <errno.h>
25 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <time.h>
33 #include <assert.h>
35 #ifdef USE_PNG
36 #include <png.h>
37 #endif
39 #include "wraster.h"
41 #define RETRY( x ) do { \
42 x; \
43 } while (errno == EINTR);
45 typedef struct RCachedImage {
46 RImage *image;
47 char *file;
48 time_t last_modif; /* last time file was modified */
49 time_t last_use; /* last time image was used */
50 } RCachedImage;
53 * Size of image cache
55 static int RImageCacheSize = -1;
58 * Max. size of image to store in cache
60 static int RImageCacheMaxImage = -1; /* 0 = any size */
62 #define IMAGE_CACHE_SIZE 8
64 #define IMAGE_CACHE_MAX_IMAGE 64*64
66 static RCachedImage *RImageCache;
68 #define IM_ERROR -1
69 #define IM_UNKNOWN 0
70 #define IM_XPM 1
71 #define IM_TIFF 2
72 #define IM_PNG 3
73 #define IM_PPM 4
74 #define IM_JPEG 5
75 #define IM_GIF 6
76 /* How many image types do we have. */
77 /* Increase this when adding new image types! */
78 #define IM_TYPES 6
80 static int identFile(char *path);
82 extern RImage *RLoadPPM(char *file_name);
84 extern RImage *RLoadXPM(RContext * context, char *file);
86 #ifdef USE_TIFF
87 extern RImage *RLoadTIFF(char *file, int index);
88 #endif
89 #ifdef USE_PNG
90 extern RImage *RLoadPNG(RContext * context, char *file);
91 #endif
92 #ifdef USE_JPEG
93 extern RImage *RLoadJPEG(RContext * context, char *file_name);
94 #endif
95 #ifdef USE_GIF
96 extern RImage *RLoadGIF(char *file_name, int index);
97 #endif
99 char **RSupportedFileFormats(void)
101 static char *tmp[IM_TYPES + 1];
102 int i = 0;
104 /* built-in */
105 tmp[i++] = "XPM";
106 /* built-in */
107 tmp[i++] = "PPM";
108 #ifdef USE_TIFF
109 tmp[i++] = "TIFF";
110 #endif
111 #ifdef USE_PNG
112 tmp[i++] = "PNG";
113 #endif
114 #ifdef USE_JPEG
115 tmp[i++] = "JPEG";
116 #endif
117 #ifdef USE_GIF
118 tmp[i++] = "GIF";
119 #endif
120 tmp[i] = NULL;
122 return tmp;
125 static void init_cache()
127 char *tmp;
129 tmp = getenv("RIMAGE_CACHE");
130 if (!tmp || sscanf(tmp, "%i", &RImageCacheSize) != 1) {
131 RImageCacheSize = IMAGE_CACHE_SIZE;
133 if (RImageCacheSize < 0)
134 RImageCacheSize = 0;
136 tmp = getenv("RIMAGE_CACHE_SIZE");
137 if (!tmp || sscanf(tmp, "%i", &RImageCacheMaxImage) != 1) {
138 RImageCacheMaxImage = IMAGE_CACHE_MAX_IMAGE;
141 if (RImageCacheSize > 0) {
142 RImageCache = malloc(sizeof(RCachedImage) * RImageCacheSize);
143 if (RImageCache == NULL) {
144 printf("wrlib: out of memory for image cache\n");
145 return;
147 memset(RImageCache, 0, sizeof(RCachedImage) * RImageCacheSize);
151 RImage *RLoadImage(RContext * context, char *file, int index)
153 RImage *image = NULL;
154 int i;
155 struct stat st;
157 assert(file != NULL);
159 if (RImageCacheSize < 0) {
160 init_cache();
163 if (RImageCacheSize > 0) {
165 for (i = 0; i < RImageCacheSize; i++) {
166 if (RImageCache[i].file && strcmp(file, RImageCache[i].file) == 0) {
168 if (stat(file, &st) == 0 && st.st_mtime == RImageCache[i].last_modif) {
169 RImageCache[i].last_use = time(NULL);
171 return RCloneImage(RImageCache[i].image);
173 } else {
174 free(RImageCache[i].file);
175 RImageCache[i].file = NULL;
176 RReleaseImage(RImageCache[i].image);
182 switch (identFile(file)) {
183 case IM_ERROR:
184 return NULL;
186 case IM_UNKNOWN:
187 RErrorCode = RERR_BADFORMAT;
188 return NULL;
190 case IM_XPM:
191 image = RLoadXPM(context, file);
192 break;
194 #ifdef USE_TIFF
195 case IM_TIFF:
196 image = RLoadTIFF(file, index);
197 break;
198 #endif /* USE_TIFF */
200 #ifdef USE_PNG
201 case IM_PNG:
202 image = RLoadPNG(context, file);
203 break;
204 #endif /* USE_PNG */
206 #ifdef USE_JPEG
207 case IM_JPEG:
208 image = RLoadJPEG(context, file);
209 break;
210 #endif /* USE_JPEG */
212 #ifdef USE_GIF
213 case IM_GIF:
214 image = RLoadGIF(file, index);
215 break;
216 #endif /* USE_GIF */
218 case IM_PPM:
219 image = RLoadPPM(file);
220 break;
222 default:
223 RErrorCode = RERR_BADFORMAT;
224 return NULL;
227 /* store image in cache */
228 if (RImageCacheSize > 0 && image &&
229 (RImageCacheMaxImage == 0 || RImageCacheMaxImage >= image->width * image->height)) {
230 time_t oldest = time(NULL);
231 int oldest_idx = 0;
232 int done = 0;
234 for (i = 0; i < RImageCacheSize; i++) {
235 if (!RImageCache[i].file) {
236 RImageCache[i].file = malloc(strlen(file) + 1);
237 strcpy(RImageCache[i].file, file);
238 RImageCache[i].image = RCloneImage(image);
239 RImageCache[i].last_modif = st.st_mtime;
240 RImageCache[i].last_use = time(NULL);
241 done = 1;
242 break;
243 } else {
244 if (oldest > RImageCache[i].last_use) {
245 oldest = RImageCache[i].last_use;
246 oldest_idx = i;
251 /* if no slot available, dump least recently used one */
252 if (!done) {
253 free(RImageCache[oldest_idx].file);
254 RReleaseImage(RImageCache[oldest_idx].image);
255 RImageCache[oldest_idx].file = malloc(strlen(file) + 1);
256 strcpy(RImageCache[oldest_idx].file, file);
257 RImageCache[oldest_idx].image = RCloneImage(image);
258 RImageCache[oldest_idx].last_modif = st.st_mtime;
259 RImageCache[oldest_idx].last_use = time(NULL);
263 return image;
266 char *RGetImageFileFormat(char *file)
268 switch (identFile(file)) {
269 case IM_XPM:
270 return "XPM";
272 #ifdef USE_TIFF
273 case IM_TIFF:
274 return "TIFF";
275 #endif /* USE_TIFF */
277 #ifdef USE_PNG
278 case IM_PNG:
279 return "PNG";
280 #endif /* USE_PNG */
282 #ifdef USE_JPEG
283 case IM_JPEG:
284 return "JPEG";
285 #endif /* USE_JPEG */
287 #ifdef USE_GIF
288 case IM_GIF:
289 return "GIF";
290 #endif /* USE_GIF */
292 case IM_PPM:
293 return "PPM";
295 default:
296 return NULL;
300 static int identFile(char *path)
302 FILE *file;
303 unsigned char buffer[32];
304 size_t nread;
306 assert(path != NULL);
308 RETRY( file = fopen(path, "rb") )
309 if (file == NULL) {
310 RErrorCode = RERR_OPEN;
311 return IM_ERROR;
314 RETRY( nread = fread(buffer, 1, sizeof(buffer), file) )
315 if (nread < sizeof(buffer) || ferror(file)) {
316 RETRY( fclose(file) )
317 RErrorCode = RERR_READ;
318 return IM_ERROR;
320 RETRY( fclose(file) )
322 /* check for XPM */
323 if (strncmp((char *)buffer, "/* XPM */", 9) == 0)
324 return IM_XPM;
326 /* check for TIFF */
327 if ((buffer[0] == 'I' && buffer[1] == 'I' && buffer[2] == '*' && buffer[3] == 0)
328 || (buffer[0] == 'M' && buffer[1] == 'M' && buffer[2] == 0 && buffer[3] == '*'))
329 return IM_TIFF;
331 #ifdef USE_PNG
332 /* check for PNG */
333 if (!png_sig_cmp(buffer, 0, 8))
334 return IM_PNG;
335 #endif
337 /* check for raw PPM or PGM */
338 if (buffer[0] == 'P' && (buffer[1] == '5' || buffer[1] == '6'))
339 return IM_PPM;
341 /* check for JPEG */
342 if (buffer[0] == 0xff && buffer[1] == 0xd8)
343 return IM_JPEG;
345 /* check for GIF */
346 if (buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F')
347 return IM_GIF;
349 return IM_UNKNOWN;