]>
Dogcows Code - chaz/openbox/blob - obrender/imagecache.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 imagecache.c for the Openbox window manager
4 Copyright (c) 2008 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
20 #include "imagecache.h"
23 static gboolean
RrImagePicEqual(const RrImagePic
*p1
,
24 const RrImagePic
*p2
);
26 RrImageCache
* RrImageCacheNew(gint max_resized_saved
)
30 g_assert(max_resized_saved
>= 0);
32 self
= g_slice_new(RrImageCache
);
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
);
41 void RrImageCacheRef(RrImageCache
*self
)
46 void RrImageCacheUnref(RrImageCache
*self
)
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
;
53 g_assert(g_hash_table_size(self
->name_table
) == 0);
54 g_hash_table_destroy(self
->name_table
);
55 self
->name_table
= NULL
;
57 g_slice_free(RrImageCache
, self
);
61 #define hashsize(n) ((RrPixel32)1<<(n))
62 #define hashmask(n) (hashsize(n)-1)
63 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
64 /* mix -- mix 3 32-bit values reversibly. */
67 a -= c; a ^= rot(c, 4); c += b; \
68 b -= a; b ^= rot(a, 6); a += c; \
69 c -= b; c ^= rot(b, 8); b += a; \
70 a -= c; a ^= rot(c,16); c += b; \
71 b -= a; b ^= rot(a,19); a += c; \
72 c -= b; c ^= rot(b, 4); b += a; \
74 /* final -- final mixing of 3 32-bit values (a,b,c) into c */
75 #define final(a,b,c) \
77 c ^= b; c -= rot(b,14); \
78 a ^= c; a -= rot(c,11); \
79 b ^= a; b -= rot(a,25); \
80 c ^= b; c -= rot(b,16); \
81 a ^= c; a -= rot(c,4); \
82 b ^= a; b -= rot(a,14); \
83 c ^= b; c -= rot(b,24); \
86 /* This is a fast, reversable hash function called "lookup3", found here:
87 http://burtleburtle.net/bob/c/lookup3.c, by Bob Jenkins
89 This hashing algorithm is "reversible", that is, not cryptographically
90 secure at all. But we don't care about that, we just want something to
91 tell when images are the same or different relatively quickly.
93 guint32
hashword(const guint32
*key
, gint length
, guint32 initval
)
97 /* Set up the internal state */
98 a
= b
= c
= 0xdeadbeef + (((guint32
)length
)<<2) + initval
;
100 /* handle most of the key */
111 /* handle the last 3 guint32's */
112 switch(length
) /* all the case statements fall through */
118 case 0: /* case 0: nothing left to add */
121 /* report the result */
125 /*! This is some arbitrary initial value for the hashing function. It's
126 constant so that you get the same result from the same data each time.
128 #define HASH_INITVAL 0xf00d
130 guint
RrImagePicHash(const RrImagePic
*p
)
132 return hashword(p
->data
, p
->width
* p
->height
, HASH_INITVAL
);
135 static gboolean
RrImagePicEqual(const RrImagePic
*p1
,
136 const RrImagePic
*p2
)
138 return p1
->width
== p2
->width
&& p1
->height
== p2
->height
&&
This page took 0.039064 seconds and 4 git commands to generate.