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., 51 Franklin St, Fifth Floor, Boston,
42 #define RETRY( x ) do { \
44 } while (errno == EINTR);
46 typedef struct RCachedImage
{
49 time_t last_modif
; /* last time file was modified */
50 time_t last_use
; /* last time image was used */
56 static int RImageCacheSize
= -1;
59 * Max. size of image to store in cache
61 static int RImageCacheMaxImage
= -1; /* 0 = any size */
63 #define IMAGE_CACHE_SIZE 8
65 #define IMAGE_CACHE_MAX_IMAGE 64*64
67 static RCachedImage
*RImageCache
;
77 /* How many image types do we have. */
78 /* Increase this when adding new image types! */
81 static int identFile(char *path
);
83 extern RImage
*RLoadPPM(char *file_name
);
85 extern RImage
*RLoadXPM(RContext
* context
, char *file
);
88 extern RImage
*RLoadTIFF(char *file
, int index
);
91 extern RImage
*RLoadPNG(RContext
* context
, char *file
);
94 extern RImage
*RLoadJPEG(RContext
* context
, char *file_name
);
97 extern RImage
*RLoadGIF(char *file_name
, int index
);
100 char **RSupportedFileFormats(void)
102 static char *tmp
[IM_TYPES
+ 1];
126 static void init_cache()
130 tmp
= getenv("RIMAGE_CACHE");
131 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheSize
) != 1) {
132 RImageCacheSize
= IMAGE_CACHE_SIZE
;
134 if (RImageCacheSize
< 0)
137 tmp
= getenv("RIMAGE_CACHE_SIZE");
138 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheMaxImage
) != 1) {
139 RImageCacheMaxImage
= IMAGE_CACHE_MAX_IMAGE
;
142 if (RImageCacheSize
> 0) {
143 RImageCache
= malloc(sizeof(RCachedImage
) * RImageCacheSize
);
144 if (RImageCache
== NULL
) {
145 printf("wrlib: out of memory for image cache\n");
148 memset(RImageCache
, 0, sizeof(RCachedImage
) * RImageCacheSize
);
152 RImage
*RLoadImage(RContext
* context
, char *file
, int index
)
154 RImage
*image
= NULL
;
158 assert(file
!= NULL
);
160 if (RImageCacheSize
< 0) {
164 if (RImageCacheSize
> 0) {
166 for (i
= 0; i
< RImageCacheSize
; i
++) {
167 if (RImageCache
[i
].file
&& strcmp(file
, RImageCache
[i
].file
) == 0) {
169 if (stat(file
, &st
) == 0 && st
.st_mtime
== RImageCache
[i
].last_modif
) {
170 RImageCache
[i
].last_use
= time(NULL
);
172 return RCloneImage(RImageCache
[i
].image
);
175 free(RImageCache
[i
].file
);
176 RImageCache
[i
].file
= NULL
;
177 RReleaseImage(RImageCache
[i
].image
);
183 switch (identFile(file
)) {
188 RErrorCode
= RERR_BADFORMAT
;
192 image
= RLoadXPM(context
, file
);
197 image
= RLoadTIFF(file
, index
);
199 #endif /* USE_TIFF */
203 image
= RLoadPNG(context
, file
);
209 image
= RLoadJPEG(context
, file
);
211 #endif /* USE_JPEG */
215 image
= RLoadGIF(file
, index
);
220 image
= RLoadPPM(file
);
224 RErrorCode
= RERR_BADFORMAT
;
228 /* store image in cache */
229 if (RImageCacheSize
> 0 && image
&&
230 (RImageCacheMaxImage
== 0 || RImageCacheMaxImage
>= image
->width
* image
->height
)) {
231 time_t oldest
= time(NULL
);
235 for (i
= 0; i
< RImageCacheSize
; i
++) {
236 if (!RImageCache
[i
].file
) {
237 RImageCache
[i
].file
= malloc(strlen(file
) + 1);
238 strcpy(RImageCache
[i
].file
, file
);
239 RImageCache
[i
].image
= RCloneImage(image
);
240 RImageCache
[i
].last_modif
= st
.st_mtime
;
241 RImageCache
[i
].last_use
= time(NULL
);
245 if (oldest
> RImageCache
[i
].last_use
) {
246 oldest
= RImageCache
[i
].last_use
;
252 /* if no slot available, dump least recently used one */
254 free(RImageCache
[oldest_idx
].file
);
255 RReleaseImage(RImageCache
[oldest_idx
].image
);
256 RImageCache
[oldest_idx
].file
= malloc(strlen(file
) + 1);
257 strcpy(RImageCache
[oldest_idx
].file
, file
);
258 RImageCache
[oldest_idx
].image
= RCloneImage(image
);
259 RImageCache
[oldest_idx
].last_modif
= st
.st_mtime
;
260 RImageCache
[oldest_idx
].last_use
= time(NULL
);
267 char *RGetImageFileFormat(char *file
)
269 switch (identFile(file
)) {
276 #endif /* USE_TIFF */
286 #endif /* USE_JPEG */
301 static int identFile(char *path
)
304 unsigned char buffer
[32];
307 assert(path
!= NULL
);
309 RETRY( file
= fopen(path
, "rb") )
311 RErrorCode
= RERR_OPEN
;
315 RETRY( nread
= fread(buffer
, 1, sizeof(buffer
), file
) )
316 if (nread
< sizeof(buffer
) || ferror(file
)) {
317 RETRY( fclose(file
) )
318 RErrorCode
= RERR_READ
;
321 RETRY( fclose(file
) )
324 if (strncmp((char *)buffer
, "/* XPM */", 9) == 0)
328 if ((buffer
[0] == 'I' && buffer
[1] == 'I' && buffer
[2] == '*' && buffer
[3] == 0)
329 || (buffer
[0] == 'M' && buffer
[1] == 'M' && buffer
[2] == 0 && buffer
[3] == '*'))
334 if (!png_sig_cmp(buffer
, 0, 8))
338 /* check for raw PPM or PGM */
339 if (buffer
[0] == 'P' && (buffer
[1] == '5' || buffer
[1] == '6'))
343 if (buffer
[0] == 0xff && buffer
[1] == 0xd8)
347 if (buffer
[0] == 'G' && buffer
[1] == 'I' && buffer
[2] == 'F')