]> icculus.org git repositories - dana/openbox.git/blob - openbox/imageload.c
No \n on ob_debug in this branch.
[dana/openbox.git] / openbox / imageload.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2    imageload.c for the Openbox window manager
3    by Libor Kadlcik (aka KadlSoft)
4 */
5
6 /*
7     All loaded images are cached. There's no separate cache for the images,
8     instead they are simply stored in image cache (RrImageCache) as RrImages,
9     ready to be used.
10     Every RrImage loaded from file is associated with name of the file. This is
11     done by file name table (RrImageCache.file_name_table), which is a simple
12     hash table, where file names are keys to pointers to RrImage.
13     If you request to load file that is already in image cache, nothing will be
14     loaded and you just got the RrImage from cache.
15     When RrImage is destroyed (see RrImageDestroyNotify), the file name - pointer
16     to RrImage pair is removed from the file name table.
17 */
18
19 #include "debug.h"
20 #include "menu.h"
21 #include "openbox.h"
22 #include "gettext.h"
23 #include "obrender/render.h"
24 #include "obrender/image.h"
25 #include "obrender/imagecache.h"
26 #include "imageload.h"
27 #include <Imlib2.h>
28
29 #ifndef USE_IMLIB2
30 RrImage* RrImageFetchFromFile(RrImageCache *cache, const gchar *name)
31 {
32     return NULL;
33 }
34 #else
35
36 static void CreateFileNameTable(RrImageCache *self)
37 {
38     g_assert(self->file_name_table == NULL);
39     self->file_name_table = g_hash_table_new(&g_str_hash, &g_str_equal);
40 }
41
42 static void DestroyFileNameTable(RrImageCache *self)
43 {
44     g_assert(g_hash_table_size(self->file_name_table) == 0);
45     g_hash_table_destroy(self->file_name_table);
46     self->file_name_table = NULL;
47 }
48
49 /*! Return file name from which this image has been loaded. */
50 static gchar* GetFileName(RrImage *image)
51 {
52     GHashTableIter iter;
53     void *key, *value;
54
55     g_hash_table_iter_init(&iter, image->cache->file_name_table);
56     while (g_hash_table_iter_next(&iter, &key, &value)) {
57         if (value == image)
58             return key;
59     }
60     return NULL;
61 }
62
63 /* RrImage is about to be deleted. So remove it from file name table. */
64 static void RrImageDestroyNotify(RrImage *image)
65 {
66     gchar *file_name = GetFileName(image);
67     g_assert(file_name != NULL);
68     ob_debug("Image \"%s\" no longer needed", file_name);
69     g_hash_table_remove(image->cache->file_name_table, file_name);
70     g_free(file_name);
71
72     if (g_hash_table_size(image->cache->file_name_table) == 0) {
73         ob_debug("No RrImage in file_name_table, destroying");
74         DestroyFileNameTable(image->cache);
75     }
76 }
77
78 #if (RrDefaultAlphaOffset != 24 || RrDefaultRedOffset != 16 \
79     || RrDefaultGreenOffset != 8 || RrDefaultBlueOffset != 0)
80 #error RrImageFetchFromFile cannot handle current bit layout of RrPixel32.
81 #endif
82
83 /*! Load image from specified file and create RrImage for it (RrImage will be
84     linked into specified image cache). Reference count of the RrImage will
85     be set to 1.
86     If that image has already been loaded into the image cache, RrImage
87     from the cache will be returned and its reference count will be incremented.
88 */
89 RrImage* RrImageFetchFromFile(RrImageCache *cache, const gchar *name)
90 {
91     RrImage *rr_image, *found_rr_image;
92     gint w, h;
93     DATA32 *ro_data;
94
95     imlib_set_color_usage(128);
96
97     if (cache->file_name_table == NULL)
98         CreateFileNameTable(cache);
99
100     /* Find out if that image has already been loaded to this cache. */
101     rr_image = g_hash_table_lookup(cache->file_name_table, name);
102     if (rr_image && rr_image->cache == cache) {
103         ob_debug("\"%s\" already loaded in this image cache.", name);
104         RrImageRef(rr_image);
105         return rr_image;
106     }
107
108     Imlib_Image imlib_image = imlib_load_image(name);
109     if (imlib_image == NULL) {
110         g_message(_("Cannot load image from file \"%s\""), name);
111         return NULL;
112     }
113
114     /* Get data and dimensions of the image. */
115     imlib_context_set_image(imlib_image);
116     g_message("Alpha = %d\n", imlib_image_has_alpha());
117     ro_data = imlib_image_get_data_for_reading_only();
118     w = imlib_image_get_width();
119     h = imlib_image_get_height();
120     ob_debug("Loaded \"%s\", dimensions %dx%d", name, w, h);
121
122     /* There must not be any duplicated pictures in RrImageCache. */
123     found_rr_image = RrImageCacheFind(cache, ro_data, w, h);
124     if (found_rr_image) {
125         rr_image = found_rr_image;
126         RrImageRef(rr_image);
127         ob_debug("Image \"%s\" is duplicate", name);
128     }
129     else {
130         /* Create RrImage from the image and add it to file name table. */
131         rr_image = RrImageNew(cache);
132         RrImageSetDestroyFunc(rr_image, &RrImageDestroyNotify);
133         /* XXX: Is Imlib2's format of DATA32 always identical to RrPixel32? */
134         RrImageAddPicture(rr_image, ro_data, w, h);
135         g_hash_table_insert(cache->file_name_table, g_strdup(name), rr_image);
136     }
137
138     imlib_free_image();
139
140     return rr_image;
141 }
142
143 #endif