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.
40 typedef struct RCachedImage
{
43 time_t last_modif
; /* last time file was modified */
44 time_t last_use
; /* last time image was used */
51 static int RImageCacheSize
= -1;
54 * Max. size of image to store in cache
56 static int RImageCacheMaxImage
= -1; /* 0 = any size */
58 #define IMAGE_CACHE_SIZE 8
60 #define IMAGE_CACHE_MAX_IMAGE 64*64
62 static RCachedImage
*RImageCache
;
76 /* How many image types do we have. */
77 /* Increase this when adding new image types! */
81 static int identFile(char *path
);
83 extern RImage
*RLoadPPM(RContext
*context
, char *file_name
, int index
);
85 extern RImage
*RLoadXPM(RContext
*context
, char *file
, int index
);
89 extern RImage
*RLoadTIFF(RContext
*context
, char *file
, int index
);
92 extern RImage
*RLoadPNG(RContext
*context
, char *file
, int index
);
95 extern RImage
*RLoadJPEG(RContext
*context
, char *file_name
, int index
);
98 extern RImage
*RLoadGIF(RContext
*context
, char *file_name
, int index
);
107 tmp
= malloc(strlen(s
)+1);
110 return strcpy(tmp
, s
);
114 RSupportedFileFormats(void)
119 tmp
= malloc(sizeof(char*)*(IM_TYPES
+1));
122 memset(tmp
, 0, sizeof(char*)*(IM_TYPES
+1));
125 tmp
[i
++] = wstrdup("XPM");
127 RFreeStringList(tmp
);
131 tmp
[i
++] = wstrdup("PPM");
133 RFreeStringList(tmp
);
137 tmp
[i
++] = wstrdup("TIFF");
139 RFreeStringList(tmp
);
144 tmp
[i
++] = wstrdup("PNG");
146 RFreeStringList(tmp
);
151 tmp
[i
++] = wstrdup("JPEG");
153 RFreeStringList(tmp
);
158 tmp
[i
++] = wstrdup("GIF");
160 RFreeStringList(tmp
);
171 RFreeStringList(char **list
)
175 for (i
= 0; list
[i
]!=NULL
; i
++) {
187 tmp
= getenv("RIMAGE_CACHE");
188 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheSize
)!=1) {
189 RImageCacheSize
= IMAGE_CACHE_SIZE
;
191 if (RImageCacheSize
<0)
194 tmp
= getenv("RIMAGE_CACHE_SIZE");
195 if (!tmp
|| sscanf(tmp
, "%i", &RImageCacheMaxImage
)!=1) {
196 RImageCacheMaxImage
= IMAGE_CACHE_MAX_IMAGE
;
199 if (RImageCacheSize
>0) {
200 RImageCache
= malloc(sizeof(RCachedImage
)*RImageCacheSize
);
201 if (RImageCache
==NULL
) {
202 printf("wrlib: out of memory for image cache\n");
205 memset(RImageCache
, 0, sizeof(RCachedImage
)*RImageCacheSize
);
211 RLoadImage(RContext
*context
, char *file
, int index
)
213 RImage
*image
= NULL
;
219 if (RImageCacheSize
<0) {
223 if (RImageCacheSize
>0) {
225 for (i
=0; i
<RImageCacheSize
; i
++) {
226 if (RImageCache
[i
].file
227 && strcmp(file
, RImageCache
[i
].file
)==0) {
229 if (stat(file
, &st
)==0
230 && st
.st_mtime
== RImageCache
[i
].last_modif
) {
231 RImageCache
[i
].last_use
= time(NULL
);
233 return RCloneImage(RImageCache
[i
].image
);
236 free(RImageCache
[i
].file
);
237 RImageCache
[i
].file
= NULL
;
238 RDestroyImage(RImageCache
[i
].image
);
244 switch (identFile(file
)) {
246 sprintf(RErrorString
, "error opening file");
250 sprintf(RErrorString
, "unknown image format");
254 image
= RLoadXPM(context
, file
, index
);
259 image
= RLoadTIFF(context
, file
, index
);
261 #endif /* USE_TIFF */
265 image
= RLoadPNG(context
, file
, index
);
271 image
= RLoadJPEG(context
, file
, index
);
273 #endif /* USE_JPEG */
277 image
= RLoadGIF(context
, file
, index
);
282 image
= RLoadPPM(context
, file
, index
);
286 sprintf(RErrorString
, "unsupported image format");
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
);
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
);
309 if (oldest
> RImageCache
[i
].last_use
) {
310 oldest
= RImageCache
[i
].last_use
;
316 /* if no slot available, dump least recently used one */
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
);
336 identFile(char *path
)
339 unsigned char buffer
[32];
344 fd
= open(path
, O_RDONLY
);
347 if (read(fd
, buffer
, 32)<1) {
354 if (strncmp((char*)buffer
, "/* XPM */", 9)==0)
358 if ((buffer
[0]=='I' && buffer
[1]=='I' && buffer
[2]=='*' && buffer
[3]==0)
359 ||(buffer
[0]=='M' && buffer
[1]=='M' && buffer
[2]==0 && buffer
[3]=='*'))
364 if (png_check_sig(buffer
, 8))
368 /* check for raw PPM or PGM */
369 if (buffer
[0]=='P' && (buffer
[1]=='5' || buffer
[1]=='6'))
373 if (buffer
[0] == 0xff && buffer
[1] == 0xd8)
377 if (buffer
[0] == 'G' && buffer
[1] == 'I' && buffer
[2] == 'F')