Updating to version 0.20.2
[wmaker-crm.git] / wrlib / load.c
blobffb8023a82ed2b45ad998aa447872613c3e479b6
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);
114 char**
115 RSupportedFileFormats(void)
117 char **tmp;
118 int i = 0;
120 tmp = malloc(sizeof(char*)*(IM_TYPES+1));
121 if (!tmp)
122 return NULL;
123 memset(tmp, 0, sizeof(char*)*(IM_TYPES+1));
125 /* built-in */
126 tmp[i++] = wstrdup("XPM");
127 if (!tmp[i-1]) {
128 RFreeStringList(tmp);
129 return NULL;
131 /* built-in */
132 tmp[i++] = wstrdup("PPM");
133 if (!tmp[i-1]) {
134 RFreeStringList(tmp);
135 return NULL;
137 #ifdef USE_TIFF
138 tmp[i++] = wstrdup("TIFF");
139 if (!tmp[i-1]) {
140 RFreeStringList(tmp);
141 return NULL;
143 #endif
144 #ifdef USE_PNG
145 tmp[i++] = wstrdup("PNG");
146 if (!tmp[i-1]) {
147 RFreeStringList(tmp);
148 return NULL;
150 #endif
151 #ifdef USE_JPEG
152 tmp[i++] = wstrdup("JPEG");
153 if (!tmp[i-1]) {
154 RFreeStringList(tmp);
155 return NULL;
157 #endif
158 #ifdef USE_GIF
159 tmp[i++] = wstrdup("GIF");
160 if (!tmp[i-1]) {
161 RFreeStringList(tmp);
162 return NULL;
164 #endif
165 tmp[i] = NULL;
167 return tmp;
171 void
172 RFreeStringList(char **list)
174 int i;
176 for (i = 0; list[i]!=NULL; i++) {
177 free(list[i]);
179 free(list);
183 static void
184 init_cache()
186 char *tmp;
188 tmp = getenv("RIMAGE_CACHE");
189 if (!tmp || sscanf(tmp, "%i", &RImageCacheSize)!=1) {
190 RImageCacheSize = IMAGE_CACHE_SIZE;
192 if (RImageCacheSize<0)
193 RImageCacheSize = 0;
195 tmp = getenv("RIMAGE_CACHE_SIZE");
196 if (!tmp || sscanf(tmp, "%i", &RImageCacheMaxImage)!=1) {
197 RImageCacheMaxImage = IMAGE_CACHE_MAX_IMAGE;
200 if (RImageCacheSize>0) {
201 RImageCache = malloc(sizeof(RCachedImage)*RImageCacheSize);
202 if (RImageCache==NULL) {
203 printf("wrlib: out of memory for image cache\n");
204 return;
206 memset(RImageCache, 0, sizeof(RCachedImage)*RImageCacheSize);
211 RImage*
212 RLoadImage(RContext *context, char *file, int index)
214 RImage *image = NULL;
215 int i;
216 struct stat st;
218 assert(file!=NULL);
220 if (RImageCacheSize<0) {
221 init_cache();
224 if (RImageCacheSize>0) {
226 for (i=0; i<RImageCacheSize; i++) {
227 if (RImageCache[i].file
228 && strcmp(file, RImageCache[i].file)==0) {
230 if (stat(file, &st)==0
231 && st.st_mtime == RImageCache[i].last_modif) {
232 RImageCache[i].last_use = time(NULL);
234 return RCloneImage(RImageCache[i].image);
236 } else {
237 free(RImageCache[i].file);
238 RImageCache[i].file = NULL;
239 RDestroyImage(RImageCache[i].image);
245 switch (identFile(file)) {
246 case IM_ERROR:
247 return NULL;
249 case IM_UNKNOWN:
250 RErrorCode = RERR_BADFORMAT;
251 return NULL;
253 case IM_XPM:
254 image = RLoadXPM(context, file, index);
255 break;
257 #ifdef USE_TIFF
258 case IM_TIFF:
259 image = RLoadTIFF(context, file, index);
260 break;
261 #endif /* USE_TIFF */
263 #ifdef USE_PNG
264 case IM_PNG:
265 image = RLoadPNG(context, file, index);
266 break;
267 #endif /* USE_PNG */
269 #ifdef USE_JPEG
270 case IM_JPEG:
271 image = RLoadJPEG(context, file, index);
272 break;
273 #endif /* USE_JPEG */
275 #ifdef USE_GIF
276 case IM_GIF:
277 image = RLoadGIF(context, file, index);
278 break;
279 #endif /* USE_GIF */
281 case IM_PPM:
282 image = RLoadPPM(context, file, index);
283 break;
285 default:
286 RErrorCode = RERR_BADFORMAT;
287 return NULL;
291 /* store image in cache */
292 if (RImageCacheSize>0 && image &&
293 (RImageCacheMaxImage==0
294 || RImageCacheMaxImage >= image->width*image->height)) {
295 time_t oldest=time(NULL);
296 int oldest_idx = 0;
297 int done = 0;
299 for (i=0; i<RImageCacheSize; i++) {
300 if (!RImageCache[i].file) {
301 RImageCache[i].file = malloc(strlen(file)+1);
302 strcpy(RImageCache[i].file, file);
303 RImageCache[i].image = RCloneImage(image);
304 RImageCache[i].last_modif = st.st_mtime;
305 RImageCache[i].last_use = time(NULL);
306 done = 1;
307 break;
308 } else {
309 if (oldest > RImageCache[i].last_use) {
310 oldest = RImageCache[i].last_use;
311 oldest_idx = i;
316 /* if no slot available, dump least recently used one */
317 if (!done) {
318 free(RImageCache[oldest_idx].file);
319 RDestroyImage(RImageCache[oldest_idx].image);
320 RImageCache[oldest_idx].file = malloc(strlen(file)+1);
321 strcpy(RImageCache[oldest_idx].file, file);
322 RImageCache[oldest_idx].image = RCloneImage(image);
323 RImageCache[oldest_idx].last_modif = st.st_mtime;
324 RImageCache[oldest_idx].last_use = time(NULL);
328 return image;
335 static int
336 identFile(char *path)
338 int fd;
339 unsigned char buffer[32];
341 assert(path!=NULL);
343 fd = open(path, O_RDONLY);
344 if (fd < 0) {
345 RErrorCode = RERR_OPEN;
346 return IM_ERROR;
348 if (read(fd, buffer, 32)<1) {
349 close(fd);
350 RErrorCode = RERR_READ;
351 return IM_ERROR;
353 close(fd);
355 /* check for XPM */
356 if (strncmp((char*)buffer, "/* XPM */", 9)==0)
357 return IM_XPM;
359 /* check for TIFF */
360 if ((buffer[0]=='I' && buffer[1]=='I' && buffer[2]=='*' && buffer[3]==0)
361 ||(buffer[0]=='M' && buffer[1]=='M' && buffer[2]==0 && buffer[3]=='*'))
362 return IM_TIFF;
364 #ifdef USE_PNG
365 /* check for PNG */
366 if (png_check_sig(buffer, 8))
367 return IM_PNG;
368 #endif
370 /* check for raw PPM or PGM */
371 if (buffer[0]=='P' && (buffer[1]=='5' || buffer[1]=='6'))
372 return IM_PPM;
374 /* check for JPEG */
375 if (buffer[0] == 0xff && buffer[1] == 0xd8)
376 return IM_JPEG;
378 /* check for GIF */
379 if (buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F')
380 return IM_GIF;
382 return IM_UNKNOWN;