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,
41 #include "imgformat.h"
43 #define RETRY( x ) do { \
45 } while (errno == EINTR);
47 typedef struct RCachedImage
{
50 time_t last_modif
; /* last time file was modified */
51 time_t last_use
; /* last time image was used */
57 static int RImageCacheSize
= -1;
60 * Max. size of image to store in cache
62 static int RImageCacheMaxImage
= -1; /* 0 = any size */
64 #define IMAGE_CACHE_SIZE 8
66 #define IMAGE_CACHE_MAX_IMAGE 64*64
68 static RCachedImage
*RImageCache
;
71 static WRImgFormat
identFile(const char *path
);
74 char **RSupportedFileFormats(void)
76 static char *tmp
[IM_TYPES
+ 1];
100 static void init_cache(void)
104 tmp
= getenv("RIMAGE_CACHE");
105 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheSize
) != 1) {
106 RImageCacheSize
= IMAGE_CACHE_SIZE
;
108 if (RImageCacheSize
< 0)
111 tmp
= getenv("RIMAGE_CACHE_SIZE");
112 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheMaxImage
) != 1) {
113 RImageCacheMaxImage
= IMAGE_CACHE_MAX_IMAGE
;
116 if (RImageCacheSize
> 0) {
117 RImageCache
= malloc(sizeof(RCachedImage
) * RImageCacheSize
);
118 if (RImageCache
== NULL
) {
119 printf("wrlib: out of memory for image cache\n");
122 memset(RImageCache
, 0, sizeof(RCachedImage
) * RImageCacheSize
);
126 RImage
*RLoadImage(RContext
* context
, const char *file
, int index
)
128 RImage
*image
= NULL
;
132 assert(file
!= NULL
);
134 if (RImageCacheSize
< 0) {
138 if (RImageCacheSize
> 0) {
140 for (i
= 0; i
< RImageCacheSize
; i
++) {
141 if (RImageCache
[i
].file
&& strcmp(file
, RImageCache
[i
].file
) == 0) {
143 if (stat(file
, &st
) == 0 && st
.st_mtime
== RImageCache
[i
].last_modif
) {
144 RImageCache
[i
].last_use
= time(NULL
);
146 return RCloneImage(RImageCache
[i
].image
);
149 free(RImageCache
[i
].file
);
150 RImageCache
[i
].file
= NULL
;
151 RReleaseImage(RImageCache
[i
].image
);
157 switch (identFile(file
)) {
162 RErrorCode
= RERR_BADFORMAT
;
166 image
= RLoadXPM(context
, file
);
171 image
= RLoadTIFF(file
, index
);
173 #endif /* USE_TIFF */
177 image
= RLoadPNG(context
, file
);
183 image
= RLoadJPEG(context
, file
);
185 #endif /* USE_JPEG */
189 image
= RLoadGIF(file
, index
);
194 image
= RLoadPPM(file
);
198 RErrorCode
= RERR_BADFORMAT
;
202 /* store image in cache */
203 if (RImageCacheSize
> 0 && image
&&
204 (RImageCacheMaxImage
== 0 || RImageCacheMaxImage
>= image
->width
* image
->height
)) {
205 time_t oldest
= time(NULL
);
209 for (i
= 0; i
< RImageCacheSize
; i
++) {
210 if (!RImageCache
[i
].file
) {
211 RImageCache
[i
].file
= malloc(strlen(file
) + 1);
212 strcpy(RImageCache
[i
].file
, file
);
213 RImageCache
[i
].image
= RCloneImage(image
);
214 RImageCache
[i
].last_modif
= st
.st_mtime
;
215 RImageCache
[i
].last_use
= time(NULL
);
219 if (oldest
> RImageCache
[i
].last_use
) {
220 oldest
= RImageCache
[i
].last_use
;
226 /* if no slot available, dump least recently used one */
228 free(RImageCache
[oldest_idx
].file
);
229 RReleaseImage(RImageCache
[oldest_idx
].image
);
230 RImageCache
[oldest_idx
].file
= malloc(strlen(file
) + 1);
231 strcpy(RImageCache
[oldest_idx
].file
, file
);
232 RImageCache
[oldest_idx
].image
= RCloneImage(image
);
233 RImageCache
[oldest_idx
].last_modif
= st
.st_mtime
;
234 RImageCache
[oldest_idx
].last_use
= time(NULL
);
241 char *RGetImageFileFormat(const char *file
)
243 switch (identFile(file
)) {
250 #endif /* USE_TIFF */
260 #endif /* USE_JPEG */
275 static WRImgFormat
identFile(const char *path
)
278 unsigned char buffer
[32];
281 assert(path
!= NULL
);
283 RETRY( file
= fopen(path
, "rb") )
285 RErrorCode
= RERR_OPEN
;
289 RETRY( nread
= fread(buffer
, 1, sizeof(buffer
), file
) )
290 if (nread
< sizeof(buffer
) || ferror(file
)) {
291 RETRY( fclose(file
) )
292 RErrorCode
= RERR_READ
;
295 RETRY( fclose(file
) )
298 if (strncmp((char *)buffer
, "/* XPM */", 9) == 0)
302 if ((buffer
[0] == 'I' && buffer
[1] == 'I' && buffer
[2] == '*' && buffer
[3] == 0)
303 || (buffer
[0] == 'M' && buffer
[1] == 'M' && buffer
[2] == 0 && buffer
[3] == '*'))
308 if (!png_sig_cmp(buffer
, 0, 8))
312 /* check for raw PPM or PGM */
313 if (buffer
[0] == 'P' && (buffer
[1] == '5' || buffer
[1] == '6'))
317 if (buffer
[0] == 0xff && buffer
[1] == 0xd8)
321 if (buffer
[0] == 'G' && buffer
[1] == 'I' && buffer
[2] == 'F')