]> Dogcows Code - chaz/openbox/blob - render/imagecache.c
Introducing the icon cache.
[chaz/openbox] / render / 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
22 static gboolean RrImagePicEqual(const RrImagePic *p1,
23 const RrImagePic *p2);
24
25 RrImageCache* RrImageCacheNew()
26 {
27 RrImageCache *self;
28
29 self = g_new(RrImageCache, 1);
30 self->ref = 1;
31 self->table = g_hash_table_new((GHashFunc)RrImagePicHash,
32 (GEqualFunc)RrImagePicEqual);
33 return self;
34 }
35
36 void RrImageCacheRef(RrImageCache *self)
37 {
38 ++self->ref;
39 }
40
41 void RrImageCacheUnref(RrImageCache *self)
42 {
43 if (self && --self->ref == 0) {
44 g_assert(g_hash_table_size(self->table) == 0);
45 g_hash_table_unref(self->table);
46
47 g_free(self);
48 }
49 }
50
51 /*! Finds an image in the cache, if it is already in there */
52 RrImage* RrImageCacheFind(RrImageCache *self,
53 RrPixel32 *data, gint w, gint h)
54 {
55 RrImagePic pic;
56 pic.width = w;
57 pic.height = h;
58 pic.data = data;
59 return g_hash_table_lookup(self->table, &pic);
60 }
61
62 /* This is a fast, reversable hash function called "lookup3", found here:
63 http://burtleburtle.net/bob/c/lookup3.c
64 */
65 #define hashsize(n) ((RrPixel32)1<<(n))
66 #define hashmask(n) (hashsize(n)-1)
67 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
68 /* mix -- mix 3 32-bit values reversibly. */
69 #define mix(a,b,c) \
70 { \
71 a -= c; a ^= rot(c, 4); c += b; \
72 b -= a; b ^= rot(a, 6); a += c; \
73 c -= b; c ^= rot(b, 8); b += a; \
74 a -= c; a ^= rot(c,16); c += b; \
75 b -= a; b ^= rot(a,19); a += c; \
76 c -= b; c ^= rot(b, 4); b += a; \
77 }
78 /* final -- final mixing of 3 32-bit values (a,b,c) into c */
79 #define final(a,b,c) \
80 { \
81 c ^= b; c -= rot(b,14); \
82 a ^= c; a -= rot(c,11); \
83 b ^= a; b -= rot(a,25); \
84 c ^= b; c -= rot(b,16); \
85 a ^= c; a -= rot(c,4); \
86 b ^= a; b -= rot(a,14); \
87 c ^= b; c -= rot(b,24); \
88 }
89
90 guint32 hashword(const guint32 *key, gint length, guint32 initval)
91 {
92 guint32 a,b,c;
93
94 /* Set up the internal state */
95 a = b = c = 0xdeadbeef + (((guint32)length)<<2) + initval;
96
97 /* handle most of the key */
98 while (length > 3)
99 {
100 a += key[0];
101 b += key[1];
102 c += key[2];
103 mix(a,b,c);
104 length -= 3;
105 key += 3;
106 }
107
108 /* handle the last 3 guint32's */
109 switch(length) /* all the case statements fall through */
110 {
111 case 3: c+=key[2];
112 case 2: b+=key[1];
113 case 1: a+=key[0];
114 final(a,b,c);
115 case 0: /* case 0: nothing left to add */
116 break;
117 }
118 /* report the result */
119 return c;
120 }
121
122 #define HASH_INITVAL 0xf00d
123
124 guint RrImagePicHash(const RrImagePic *p)
125 {
126 return hashword(p->data, p->width * p->height, HASH_INITVAL);
127 }
128
129 static gboolean RrImagePicEqual(const RrImagePic *p1,
130 const RrImagePic *p2)
131 {
132 guint s1, s2;
133 RrPixel32 *data1, *data2;
134 gint i;
135
136 if (p1->width != p2->width || p1->height != p2->height) return FALSE;
137
138 /* strcmp() would probably suck on 4k of data.. sum all their values and
139 see if they get the same thing. they already matched on their hashes
140 at this point. */
141 s1 = s2 = 0;
142 data1 = p1->data;
143 data2 = p2->data;
144 for (i = 0; i < p1->width * p1->height; ++i, ++data1)
145 s1 += *data1;
146 for (i = 0; i < p2->width * p2->height; ++i, ++data2)
147 s2 += *data2;
148 return s1 == s2;
149 }
This page took 0.038361 seconds and 4 git commands to generate.