1 /* load.c - load image from file
3 * Raster graphics library
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.
41 typedef struct RCachedImage
{
44 time_t last_modif
; /* last time file was modified */
45 time_t last_use
; /* last time image was used */
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
;
77 /* How many image types do we have. */
78 /* Increase this when adding new image types! */
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
);
90 extern RImage
*RLoadTIFF(RContext
*context
, char *file
, int index
);
93 extern RImage
*RLoadPNG(RContext
*context
, char *file
, int index
);
96 extern RImage
*RLoadJPEG(RContext
*context
, char *file_name
, int index
);
99 extern RImage
*RLoadGIF(RContext
*context
, char *file_name
, int index
);
104 RSupportedFileFormats(void)
106 static char *tmp
[IM_TYPES
+1];
136 tmp
= getenv("RIMAGE_CACHE");
137 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheSize
)!=1) {
138 RImageCacheSize
= IMAGE_CACHE_SIZE
;
140 if (RImageCacheSize
<0)
143 tmp
= getenv("RIMAGE_CACHE_SIZE");
144 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheMaxImage
)!=1) {
145 RImageCacheMaxImage
= IMAGE_CACHE_MAX_IMAGE
;
148 if (RImageCacheSize
>0) {
149 RImageCache
= malloc(sizeof(RCachedImage
)*RImageCacheSize
);
150 if (RImageCache
==NULL
) {
151 printf("wrlib: out of memory for image cache\n");
154 memset(RImageCache
, 0, sizeof(RCachedImage
)*RImageCacheSize
);
160 RLoadImage(RContext
*context
, char *file
, int index
)
162 RImage
*image
= NULL
;
168 if (RImageCacheSize
<0) {
172 if (RImageCacheSize
>0) {
174 for (i
=0; i
<RImageCacheSize
; i
++) {
175 if (RImageCache
[i
].file
176 && strcmp(file
, RImageCache
[i
].file
)==0) {
178 if (stat(file
, &st
)==0
179 && st
.st_mtime
== RImageCache
[i
].last_modif
) {
180 RImageCache
[i
].last_use
= time(NULL
);
182 return RCloneImage(RImageCache
[i
].image
);
185 free(RImageCache
[i
].file
);
186 RImageCache
[i
].file
= NULL
;
187 RDestroyImage(RImageCache
[i
].image
);
193 switch (identFile(file
)) {
198 RErrorCode
= RERR_BADFORMAT
;
202 image
= RLoadXPM(context
, file
, index
);
207 image
= RLoadTIFF(context
, file
, index
);
209 #endif /* USE_TIFF */
213 image
= RLoadPNG(context
, file
, index
);
219 image
= RLoadJPEG(context
, file
, index
);
221 #endif /* USE_JPEG */
225 image
= RLoadGIF(context
, file
, index
);
230 image
= RLoadPPM(context
, file
, index
);
234 RErrorCode
= RERR_BADFORMAT
;
239 /* store image in cache */
240 if (RImageCacheSize
>0 && image
&&
241 (RImageCacheMaxImage
==0
242 || RImageCacheMaxImage
>= image
->width
*image
->height
)) {
243 time_t oldest
=time(NULL
);
247 for (i
=0; i
<RImageCacheSize
; i
++) {
248 if (!RImageCache
[i
].file
) {
249 RImageCache
[i
].file
= malloc(strlen(file
)+1);
250 strcpy(RImageCache
[i
].file
, file
);
251 RImageCache
[i
].image
= RCloneImage(image
);
252 RImageCache
[i
].last_modif
= st
.st_mtime
;
253 RImageCache
[i
].last_use
= time(NULL
);
257 if (oldest
> RImageCache
[i
].last_use
) {
258 oldest
= RImageCache
[i
].last_use
;
264 /* if no slot available, dump least recently used one */
266 free(RImageCache
[oldest_idx
].file
);
267 RDestroyImage(RImageCache
[oldest_idx
].image
);
268 RImageCache
[oldest_idx
].file
= malloc(strlen(file
)+1);
269 strcpy(RImageCache
[oldest_idx
].file
, file
);
270 RImageCache
[oldest_idx
].image
= RCloneImage(image
);
271 RImageCache
[oldest_idx
].last_modif
= st
.st_mtime
;
272 RImageCache
[oldest_idx
].last_use
= time(NULL
);
281 RGetImageFileFormat(char *file
)
283 switch (identFile(file
)) {
290 #endif /* USE_TIFF */
300 #endif /* USE_JPEG */
317 identFile(char *path
)
320 unsigned char buffer
[32];
324 fd
= open(path
, O_RDONLY
);
326 RErrorCode
= RERR_OPEN
;
329 if (read(fd
, buffer
, 32)<1) {
331 RErrorCode
= RERR_READ
;
337 if (strncmp((char*)buffer
, "/* XPM */", 9)==0)
341 if ((buffer
[0]=='I' && buffer
[1]=='I' && buffer
[2]=='*' && buffer
[3]==0)
342 ||(buffer
[0]=='M' && buffer
[1]=='M' && buffer
[2]==0 && buffer
[3]=='*'))
347 if (png_check_sig(buffer
, 8))
351 /* check for raw PPM or PGM */
352 if (buffer
[0]=='P' && (buffer
[1]=='5' || buffer
[1]=='6'))
356 if (buffer
[0] == 0xff && buffer
[1] == 0xd8)
360 if (buffer
[0] == 'G' && buffer
[1] == 'I' && buffer
[2] == 'F')