Fix msgfmt error with Armenian translation.
[wmaker-crm.git] / wrlib / load.c
blobbfd46b948704b0c035fe8ffd892a47547f0f7a9e
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., 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"
40 /* Silly hack for Windows systems with cygwin */
41 #ifndef O_BINARY
42 # define O_BINARY 0
43 #endif
45 typedef struct RCachedImage {
46 RImage *image;
47 char *file;
48 time_t last_modif; /* last time file was modified */
49 time_t last_use; /* last time image was used */
50 } RCachedImage;
53 * Size of image cache
55 static int RImageCacheSize = -1;
58 * Max. size of image to store in cache
60 static int RImageCacheMaxImage = -1; /* 0 = any size */
62 #define IMAGE_CACHE_SIZE 8
64 #define IMAGE_CACHE_MAX_IMAGE 64*64
66 static RCachedImage *RImageCache;
68 #define IM_ERROR -1
69 #define IM_UNKNOWN 0
70 #define IM_XPM 1
71 #define IM_TIFF 2
72 #define IM_PNG 3
73 #define IM_PPM 4
74 #define IM_JPEG 5
75 #define IM_GIF 6
76 /* How many image types do we have. */
77 /* Increase this when adding new image types! */
78 #define IM_TYPES 6
80 static int identFile(char *path);
82 extern RImage *RLoadPPM(RContext * context, char *file_name, int index);
84 extern RImage *RLoadXPM(RContext * context, char *file, int index);
86 #ifdef USE_TIFF
87 extern RImage *RLoadTIFF(RContext * context, char *file, int index);
88 #endif
89 #ifdef USE_PNG
90 extern RImage *RLoadPNG(RContext * context, char *file, int index);
91 #endif
92 #ifdef USE_JPEG
93 extern RImage *RLoadJPEG(RContext * context, char *file_name, int index);
94 #endif
95 #ifdef USE_GIF
96 extern RImage *RLoadGIF(RContext * context, char *file_name, int index);
97 #endif
99 char **RSupportedFileFormats(void)
101 static char *tmp[IM_TYPES + 1];
102 int i = 0;
104 /* built-in */
105 tmp[i++] = "XPM";
106 /* built-in */
107 tmp[i++] = "PPM";
108 #ifdef USE_TIFF
109 tmp[i++] = "TIFF";
110 #endif
111 #ifdef USE_PNG
112 tmp[i++] = "PNG";
113 #endif
114 #ifdef USE_JPEG
115 tmp[i++] = "JPEG";
116 #endif
117 #ifdef USE_GIF
118 tmp[i++] = "GIF";
119 #endif
120 tmp[i] = NULL;
122 return tmp;
125 static void init_cache()
127 char *tmp;
129 tmp = getenv("RIMAGE_CACHE");
130 if (!tmp || sscanf(tmp, "%i", &RImageCacheSize) != 1) {
131 RImageCacheSize = IMAGE_CACHE_SIZE;
133 if (RImageCacheSize < 0)
134 RImageCacheSize = 0;
136 tmp = getenv("RIMAGE_CACHE_SIZE");
137 if (!tmp || sscanf(tmp, "%i", &RImageCacheMaxImage) != 1) {
138 RImageCacheMaxImage = IMAGE_CACHE_MAX_IMAGE;
141 if (RImageCacheSize > 0) {
142 RImageCache = malloc(sizeof(RCachedImage) * RImageCacheSize);
143 if (RImageCache == NULL) {
144 printf("wrlib: out of memory for image cache\n");
145 return;
147 memset(RImageCache, 0, sizeof(RCachedImage) * RImageCacheSize);
151 RImage *RLoadImage(RContext * context, char *file, int index)
153 RImage *image = NULL;
154 int i;
155 struct stat st;
157 assert(file != NULL);
159 if (RImageCacheSize < 0) {
160 init_cache();
163 if (RImageCacheSize > 0) {
165 for (i = 0; i < RImageCacheSize; i++) {
166 if (RImageCache[i].file && strcmp(file, RImageCache[i].file) == 0) {
168 if (stat(file, &st) == 0 && st.st_mtime == RImageCache[i].last_modif) {
169 RImageCache[i].last_use = time(NULL);
171 return RCloneImage(RImageCache[i].image);
173 } else {
174 free(RImageCache[i].file);
175 RImageCache[i].file = NULL;
176 RReleaseImage(RImageCache[i].image);
182 switch (identFile(file)) {
183 case IM_ERROR:
184 return NULL;
186 case IM_UNKNOWN:
187 RErrorCode = RERR_BADFORMAT;
188 return NULL;
190 case IM_XPM:
191 image = RLoadXPM(context, file, index);
192 break;
194 #ifdef USE_TIFF
195 case IM_TIFF:
196 image = RLoadTIFF(context, file, index);
197 break;
198 #endif /* USE_TIFF */
200 #ifdef USE_PNG
201 case IM_PNG:
202 image = RLoadPNG(context, file, index);
203 break;
204 #endif /* USE_PNG */
206 #ifdef USE_JPEG
207 case IM_JPEG:
208 image = RLoadJPEG(context, file, index);
209 break;
210 #endif /* USE_JPEG */
212 #ifdef USE_GIF
213 case IM_GIF:
214 image = RLoadGIF(context, file, index);
215 break;
216 #endif /* USE_GIF */
218 case IM_PPM:
219 image = RLoadPPM(context, file, index);
220 break;
222 default:
223 RErrorCode = RERR_BADFORMAT;
224 return NULL;
227 /* store image in cache */
228 if (RImageCacheSize > 0 && image &&
229 (RImageCacheMaxImage == 0 || RImageCacheMaxImage >= image->width * image->height)) {
230 time_t oldest = time(NULL);
231 int oldest_idx = 0;
232 int done = 0;
234 for (i = 0; i < RImageCacheSize; i++) {
235 if (!RImageCache[i].file) {
236 RImageCache[i].file = malloc(strlen(file) + 1);
237 strcpy(RImageCache[i].file, file);
238 RImageCache[i].image = RCloneImage(image);
239 RImageCache[i].last_modif = st.st_mtime;
240 RImageCache[i].last_use = time(NULL);
241 done = 1;
242 break;
243 } else {
244 if (oldest > RImageCache[i].last_use) {
245 oldest = RImageCache[i].last_use;
246 oldest_idx = i;
251 /* if no slot available, dump least recently used one */
252 if (!done) {
253 free(RImageCache[oldest_idx].file);
254 RReleaseImage(RImageCache[oldest_idx].image);
255 RImageCache[oldest_idx].file = malloc(strlen(file) + 1);
256 strcpy(RImageCache[oldest_idx].file, file);
257 RImageCache[oldest_idx].image = RCloneImage(image);
258 RImageCache[oldest_idx].last_modif = st.st_mtime;
259 RImageCache[oldest_idx].last_use = time(NULL);
263 return image;
266 char *RGetImageFileFormat(char *file)
268 switch (identFile(file)) {
269 case IM_XPM:
270 return "XPM";
272 #ifdef USE_TIFF
273 case IM_TIFF:
274 return "TIFF";
275 #endif /* USE_TIFF */
277 #ifdef USE_PNG
278 case IM_PNG:
279 return "PNG";
280 #endif /* USE_PNG */
282 #ifdef USE_JPEG
283 case IM_JPEG:
284 return "JPEG";
285 #endif /* USE_JPEG */
287 #ifdef USE_GIF
288 case IM_GIF:
289 return "GIF";
290 #endif /* USE_GIF */
292 case IM_PPM:
293 return "PPM";
295 default:
296 return NULL;
300 static int identFile(char *path)
302 int fd;
303 unsigned char buffer[32];
305 assert(path != NULL);
307 fd = open(path, O_RDONLY | O_BINARY);
308 if (fd < 0) {
309 RErrorCode = RERR_OPEN;
310 return IM_ERROR;
312 if (read(fd, buffer, 32) < 1) {
313 close(fd);
314 RErrorCode = RERR_READ;
315 return IM_ERROR;
317 close(fd);
319 /* check for XPM */
320 if (strncmp((char *)buffer, "/* XPM */", 9) == 0)
321 return IM_XPM;
323 /* check for TIFF */
324 if ((buffer[0] == 'I' && buffer[1] == 'I' && buffer[2] == '*' && buffer[3] == 0)
325 || (buffer[0] == 'M' && buffer[1] == 'M' && buffer[2] == 0 && buffer[3] == '*'))
326 return IM_TIFF;
328 #ifdef USE_PNG
329 /* check for PNG */
330 if (png_check_sig(buffer, 8))
331 return IM_PNG;
332 #endif
334 /* check for raw PPM or PGM */
335 if (buffer[0] == 'P' && (buffer[1] == '5' || buffer[1] == '6'))
336 return IM_PPM;
338 /* check for JPEG */
339 if (buffer[0] == 0xff && buffer[1] == 0xd8)
340 return IM_JPEG;
342 /* check for GIF */
343 if (buffer[0] == 'G' && buffer[1] == 'I' && buffer[2] == 'F')
344 return IM_GIF;
346 return IM_UNKNOWN;