r300: Fixed (hopefully) a problem where ImLib support failed on multi-depth
[rox-filer/ma.git] / ROX-Filer / src / pixmaps.c
blobb557de8343fbda7e7f580caef0934370ce9f2c64
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* pixmaps.c - code for handling pixmaps */
25 * 2000/04/11 Imlib support added
26 * Christiansen Merel <c.merel@wanadoo.fr>
29 #include "config.h"
30 #define PIXMAPS_C
32 /* Remove pixmaps from the cache when they haven't been accessed for
33 * this period of time (seconds).
36 #define PIXMAP_PURGE_TIME 1200
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <errno.h>
42 #include <gtk/gtk.h>
43 #include <gdk/gdkprivate.h> /* XXX - find another way to do this */
44 #include "collection.h"
45 #ifdef HAVE_IMLIB
46 # include <gdk_imlib.h>
47 #endif
49 #include "fscache.h"
50 #include "support.h"
51 #include "gui_support.h"
52 #include "pixmaps.h"
54 GFSCache *pixmap_cache = NULL;
56 static char * bad_xpm[] = {
57 "12 12 3 1",
58 " c #000000000000",
59 ". c #FFFF00000000",
60 "X c #FFFFFFFFFFFF",
61 " ",
62 " ..XXXXXX.. ",
63 " ...XXXX... ",
64 " X...XX...X ",
65 " XX......XX ",
66 " XXX....XXX ",
67 " XXX....XXX ",
68 " XX......XX ",
69 " X...XX...X ",
70 " ...XXXX... ",
71 " ..XXXXXX.. ",
72 " "};
74 MaskedPixmap *im_error;
75 MaskedPixmap *im_unknown;
76 MaskedPixmap *im_symlink;
78 MaskedPixmap *im_unmounted;
79 MaskedPixmap *im_mounted;
80 MaskedPixmap *im_multiple;
81 MaskedPixmap *im_exec_file;
82 MaskedPixmap *im_appdir;
84 MaskedPixmap *im_up_icon;
85 MaskedPixmap *im_home_icon;
86 MaskedPixmap *im_refresh_icon;
87 MaskedPixmap *im_help;
89 /* Static prototypes */
91 static MaskedPixmap *load_pixmap(char *name);
92 static void load_default_pixmaps(void);
93 static MaskedPixmap *load(char *pathname, gpointer data);
94 static void ref(MaskedPixmap *mp, gpointer data);
95 static void unref(MaskedPixmap *mp, gpointer data);
96 static int getref(MaskedPixmap *mp);
97 static gint purge(gpointer data);
98 static MaskedPixmap *image_from_file(char *path);
99 static MaskedPixmap *get_bad_image(void);
100 #ifdef HAVE_IMLIB
101 static GdkImlibImage *make_half_size(GdkImlibImage *big);
102 #endif
105 /****************************************************************
106 * EXTERNAL INTERFACE *
107 ****************************************************************/
109 void pixmaps_init(void)
111 #ifdef HAVE_IMLIB
112 gdk_imlib_init();
114 gtk_widget_push_visual(gdk_imlib_get_visual());
115 gtk_widget_push_colormap(gdk_imlib_get_colormap());
116 #endif
118 pixmap_cache = g_fscache_new((GFSLoadFunc) load,
119 (GFSRefFunc) ref,
120 (GFSRefFunc) unref,
121 (GFSGetRefFunc) getref,
122 NULL, /* Update func */
123 NULL);
125 gtk_timeout_add(10000, purge, NULL);
127 load_default_pixmaps();
130 /* 'name' is relative to APP_DIR. Always returns with a valid image. */
131 MaskedPixmap *load_pixmap(char *name)
133 MaskedPixmap *retval;
135 retval = image_from_file(make_path(getenv("APP_DIR"), name)->str);
136 if (!retval)
137 retval = get_bad_image();
138 return retval;
141 /* Load all the standard pixmaps */
142 static void load_default_pixmaps(void)
144 im_error = load_pixmap("pixmaps/error.xpm");
145 im_unknown = load_pixmap("pixmaps/unknown.xpm");
146 im_symlink = load_pixmap("pixmaps/symlink.xpm");
148 im_unmounted = load_pixmap("pixmaps/mount.xpm");
149 im_mounted = load_pixmap("pixmaps/mounted.xpm");
150 im_multiple = load_pixmap("pixmaps/multiple.xpm");
151 im_exec_file = load_pixmap("pixmaps/exec.xpm");
152 im_appdir = load_pixmap("pixmaps/application.xpm");
154 im_up_icon = load_pixmap("pixmaps/up.xpm");
155 im_home_icon = load_pixmap("pixmaps/home.xpm");
156 im_refresh_icon = load_pixmap("pixmaps/refresh.xpm");
157 im_help = load_pixmap("pixmaps/help.xpm");
160 void pixmap_ref(MaskedPixmap *mp)
162 ref(mp, pixmap_cache->user_data);
165 void pixmap_unref(MaskedPixmap *mp)
167 unref(mp, pixmap_cache->user_data);
170 void pixmap_make_small(MaskedPixmap *mp)
172 if (mp->sm_pixmap)
173 return;
175 #ifdef HAVE_IMLIB
176 if (mp->image)
178 GdkImlibImage *small;
180 small = make_half_size(mp->image);
182 if (small && gdk_imlib_render(small,
183 small->rgb_width, small->rgb_height))
185 mp->sm_pixmap = gdk_imlib_move_image(small);
186 mp->sm_mask = gdk_imlib_move_mask(small);
187 mp->sm_width = small->width;
188 mp->sm_height = small->height;
191 if (small)
192 gdk_imlib_kill_image(small);
193 if (mp->sm_pixmap)
194 return;
196 #endif
197 gdk_pixmap_ref(mp->pixmap);
198 if (mp->mask)
199 gdk_bitmap_ref(mp->mask);
200 mp->sm_pixmap = mp->pixmap;
201 mp->sm_mask = mp->mask;
202 mp->sm_width = mp->width;
203 mp->sm_height = mp->height;
206 /****************************************************************
207 * INTERNAL FUNCTIONS *
208 ****************************************************************/
210 /* Load the image 'path' and return a pointer to the resulting
211 * MaskedPixmap. NULL on failure.
213 static MaskedPixmap *image_from_file(char *path)
215 MaskedPixmap *mp;
216 GdkPixmap *pixmap;
217 GdkBitmap *mask;
218 int width;
219 int height;
220 #ifdef HAVE_IMLIB
221 GdkImlibImage *image;
223 image = gdk_imlib_load_image(path);
224 if (!image)
225 return NULL;
227 /* Avoid ImLib cache - ours is better! */
228 gdk_imlib_changed_image(image);
230 if (!gdk_imlib_render(image, image->rgb_width, image->rgb_height))
232 gdk_imlib_kill_image(image);
233 return NULL;
236 pixmap = image->pixmap;
237 mask = image->shape_mask;
238 width = image->width;
239 height = image->height;
240 #else
241 pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
242 gtk_widget_get_default_colormap(),
243 &mask,
245 path);
247 if (!pixmap)
248 return NULL;
249 width = ((GdkPixmapPrivate *) pixmap)->width;
250 height = ((GdkPixmapPrivate *) pixmap)->height;
251 #endif
253 mp = g_new(MaskedPixmap, 1);
254 mp->ref = 1;
255 mp->pixmap = pixmap;
256 mp->mask = mask;
257 mp->width = width;
258 mp->height = height;
259 #ifdef HAVE_IMLIB
260 mp->image = image;
261 #endif
262 mp->sm_pixmap = NULL;
263 mp->sm_mask = NULL;
265 return mp;
268 /* Return a pointer to the (static) bad image. The ref counter will ensure
269 * that the image is never freed.
271 static MaskedPixmap *get_bad_image(void)
273 static MaskedPixmap *image = NULL;
275 if (!image)
277 image = g_new(MaskedPixmap, 1);
278 image->ref = 1;
280 image->pixmap= gdk_pixmap_colormap_create_from_xpm_d(NULL,
281 gtk_widget_get_default_colormap(),
282 &image->mask, NULL, bad_xpm);
283 #ifdef HAVE_IMLIB
284 image->image = NULL;
285 #endif
288 image->ref++;
289 image->width = ((GdkPixmapPrivate *) image->pixmap)->width;
290 image->height = ((GdkPixmapPrivate *) image->pixmap)->height;
292 return image;
295 static MaskedPixmap *load(char *pathname, gpointer user_data)
297 return image_from_file(pathname);
300 static void ref(MaskedPixmap *mp, gpointer data)
302 /* printf("[ ref %p %d->%d ]\n", mp, mp->ref, mp->ref + 1); */
304 if (mp)
305 mp->ref++;
308 static void unref(MaskedPixmap *mp, gpointer data)
310 /* printf("[ unref %p %d->%d ]\n", mp, mp->ref, mp->ref - 1); */
312 if (mp && --mp->ref == 0)
314 #ifdef HAVE_IMLIB
315 if (mp->image)
317 gdk_imlib_kill_image(mp->image);
319 else
320 #endif
322 gdk_pixmap_unref(mp->pixmap);
323 if (mp->mask)
324 gdk_bitmap_unref(mp->mask);
326 if (mp->sm_pixmap)
327 gdk_pixmap_unref(mp->sm_pixmap);
328 if (mp->sm_mask)
329 gdk_bitmap_unref(mp->sm_mask);
330 g_free(mp);
334 static int getref(MaskedPixmap *mp)
336 return mp->ref;
339 /* Called now and then to clear out old pixmaps */
340 static gint purge(gpointer data)
342 g_fscache_purge(pixmap_cache, PIXMAP_PURGE_TIME);
344 return TRUE;
347 #ifdef HAVE_IMLIB
349 /* Returns data to make an 1/4 size image of 'big'. g_free() the result. */
350 static GdkImlibImage *make_half_size(GdkImlibImage *big)
352 int line_size = big->width * 3;
353 int sw = big->width >> 1;
354 int sh = big->height >> 1;
355 GdkImlibColor tr; /* Mask colour */
356 unsigned char *small_data, *in, *out;
357 GdkImlibImage *small;
358 int x, y;
359 GtkStyle *style = gtk_widget_get_default_style();
360 GdkColor *bg = &style->bg[GTK_STATE_INSENSITIVE];
362 gdk_imlib_get_image_shape(big, &tr);
363 small_data = g_malloc(sw * sh * 3);
365 out = small_data;
367 for (y = 0; y < sh; y++)
369 in = big->rgb_data + y * line_size * 2;
371 for (x = 0; x < sw; x++)
373 int r1 = in[0], r2 = in[3];
374 int r3 = in[0 + line_size], r4 = in[3 + line_size];
375 int g1 = in[1], g2 = in[4];
376 int g3 = in[1 + line_size], g4 = in[4 + line_size];
377 int b1 = in[2], b2 = in[5];
378 int b3 = in[2 + line_size], b4 = in[5 + line_size];
379 int m = 0; /* No. trans pixels */
381 if (r1 == tr.r && g1 == tr.g && b1 == tr.b)
383 r1 = bg->red;
384 g1 = bg->green;
385 b1 = bg->blue;
386 m++;
388 if (r2 == tr.r && g2 == tr.g && b2 == tr.b)
390 r2 = bg->red;
391 g2 = bg->green;
392 b2 = bg->blue;
393 m++;
395 if (r3 == tr.r && g3 == tr.g && b3 == tr.b)
397 r3 = bg->red;
398 g3 = bg->green;
399 b3 = bg->blue;
400 m++;
402 if (r4 == tr.r && g4 == tr.g && b4 == tr.b)
404 r4 = bg->red;
405 g4 = bg->green;
406 b4 = bg->blue;
407 m++;
410 if (m < 3)
412 out[0] = (r1 + r2 + r3 + r4) >> 2;
413 out[1] = (g1 + g2 + g3 + g4) >> 2;
414 out[2] = (b1 + b2 + b3 + b4) >> 2;
416 else
418 out[0] = tr.r;
419 out[1] = tr.g;
420 out[2] = tr.b;
423 in += 6;
424 out += 3;
428 small = gdk_imlib_create_image_from_data(small_data, NULL, sw, sh);
429 g_free(small_data);
431 if (small)
432 gdk_imlib_set_image_shape(small, &tr);
434 return small;
436 #endif