]> icculus.org git repositories - dana/openbox.git/blob - obrender/imagecache.c
make control keys work in menus/dialogs/etc with the new obt code, using XLookup...
[dana/openbox.git] / obrender / imagecache.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    imagecache.c for the Openbox window manager
4    Copyright (c) 2008        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "render.h"
20 #include "imagecache.h"
21 #include "image.h"
22
23 static gboolean RrImagePicEqual(const RrImagePic *p1,
24                                 const RrImagePic *p2);
25
26 RrImageCache* RrImageCacheNew(gint max_resized_saved)
27 {
28     RrImageCache *self;
29
30     g_assert(max_resized_saved >= 0);
31
32     self = g_new(RrImageCache, 1);
33     self->ref = 1;
34     self->max_resized_saved = max_resized_saved;
35     self->pic_table = g_hash_table_new((GHashFunc)RrImagePicHash,
36                                        (GEqualFunc)RrImagePicEqual);
37     self->name_table = g_hash_table_new(g_str_hash, g_str_equal);
38     return self;
39 }
40
41 void RrImageCacheRef(RrImageCache *self)
42 {
43     ++self->ref;
44 }
45
46 void RrImageCacheUnref(RrImageCache *self)
47 {
48     if (self && --self->ref == 0) {
49         g_assert(g_hash_table_size(self->pic_table) == 0);
50         g_hash_table_unref(self->pic_table);
51         self->pic_table = NULL;
52
53         g_assert(g_hash_table_size(self->name_table) == 0);
54         g_hash_table_destroy(self->name_table);
55         self->name_table = NULL;
56
57         g_free(self);
58     }
59 }
60
61 RrImage* RrImageCacheFindName(RrImageCache *self, const gchar *name)
62 {
63     return g_hash_table_lookup(self->name_table, name);
64 }
65
66 /*! Finds an image in the cache, if it is already in there */
67 RrImage* RrImageCacheFind(RrImageCache *self,
68                           RrPixel32 *data, gint w, gint h)
69 {
70     RrImagePic pic;
71
72     RrImagePicInit(&pic, NULL, w, h, data);
73     return g_hash_table_lookup(self->pic_table, &pic);
74 }
75
76 #define hashsize(n) ((RrPixel32)1<<(n))
77 #define hashmask(n) (hashsize(n)-1)
78 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
79 /* mix -- mix 3 32-bit values reversibly. */
80 #define mix(a,b,c) \
81 { \
82   a -= c;  a ^= rot(c, 4);  c += b; \
83   b -= a;  b ^= rot(a, 6);  a += c; \
84   c -= b;  c ^= rot(b, 8);  b += a; \
85   a -= c;  a ^= rot(c,16);  c += b; \
86   b -= a;  b ^= rot(a,19);  a += c; \
87   c -= b;  c ^= rot(b, 4);  b += a; \
88 }
89 /* final -- final mixing of 3 32-bit values (a,b,c) into c */
90 #define final(a,b,c) \
91 { \
92   c ^= b; c -= rot(b,14); \
93   a ^= c; a -= rot(c,11); \
94   b ^= a; b -= rot(a,25); \
95   c ^= b; c -= rot(b,16); \
96   a ^= c; a -= rot(c,4);  \
97   b ^= a; b -= rot(a,14); \
98   c ^= b; c -= rot(b,24); \
99 }
100
101 /* This is a fast, reversable hash function called "lookup3", found here:
102    http://burtleburtle.net/bob/c/lookup3.c, by Bob Jenkins
103
104    This hashing algorithm is "reversible", that is, not cryptographically
105    secure at all.  But we don't care about that, we just want something to
106    tell when images are the same or different relatively quickly.
107 */
108 guint32 hashword(const guint32 *key, gint length, guint32 initval)
109 {
110     guint32 a,b,c;
111
112     /* Set up the internal state */
113     a = b = c = 0xdeadbeef + (((guint32)length)<<2) + initval;
114
115     /* handle most of the key */
116     while (length > 3)
117     {
118         a += key[0];
119         b += key[1];
120         c += key[2];
121         mix(a,b,c);
122         length -= 3;
123         key += 3;
124     }
125
126     /* handle the last 3 guint32's */
127     switch(length)      /* all the case statements fall through */
128     { 
129     case 3: c+=key[2];
130     case 2: b+=key[1];
131     case 1: a+=key[0];
132         final(a,b,c);
133     case 0:             /* case 0: nothing left to add */
134         break;
135     }
136     /* report the result */
137     return c;
138 }
139
140 /*! This is some arbitrary initial value for the hashing function.  It's
141   constant so that you get the same result from the same data each time.
142 */
143 #define HASH_INITVAL 0xf00d
144
145 guint RrImagePicHash(const RrImagePic *p)
146 {
147     return hashword(p->data, p->width * p->height, HASH_INITVAL);
148 }
149
150 static gboolean RrImagePicEqual(const RrImagePic *p1,
151                                 const RrImagePic *p2)
152 {
153     return p1->width == p2->width && p1->height == p2->height &&
154         p1->sum == p2->sum;
155 }