OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Color Cache for WebP Lossless | 10 // Color Cache for WebP Lossless |
11 // | 11 // |
12 // Authors: Jyrki Alakuijala (jyrki@google.com) | 12 // Authors: Jyrki Alakuijala (jyrki@google.com) |
13 // Urvang Joshi (urvang@google.com) | 13 // Urvang Joshi (urvang@google.com) |
14 | 14 |
15 #ifndef WEBP_UTILS_COLOR_CACHE_H_ | 15 #ifndef WEBP_UTILS_COLOR_CACHE_H_ |
16 #define WEBP_UTILS_COLOR_CACHE_H_ | 16 #define WEBP_UTILS_COLOR_CACHE_H_ |
17 | 17 |
18 #include "../webp/types.h" | 18 #include "../webp/types.h" |
19 | 19 |
20 #ifdef __cplusplus | 20 #ifdef __cplusplus |
21 extern "C" { | 21 extern "C" { |
22 #endif | 22 #endif |
23 | 23 |
24 // Main color cache struct. | 24 // Main color cache struct. |
25 typedef struct { | 25 typedef struct { |
26 uint32_t *colors_; // color entries | 26 uint32_t *colors_; // color entries |
27 int hash_shift_; // Hash shift: 32 - hash_bits. | 27 int hash_shift_; // Hash shift: 32 - hash_bits_. |
| 28 int hash_bits_; |
28 } VP8LColorCache; | 29 } VP8LColorCache; |
29 | 30 |
30 static const uint32_t kHashMul = 0x1e35a7bd; | 31 static const uint32_t kHashMul = 0x1e35a7bd; |
31 | 32 |
32 static WEBP_INLINE uint32_t VP8LColorCacheLookup( | 33 static WEBP_INLINE uint32_t VP8LColorCacheLookup( |
33 const VP8LColorCache* const cc, uint32_t key) { | 34 const VP8LColorCache* const cc, uint32_t key) { |
34 assert(key <= (~0U >> cc->hash_shift_)); | 35 assert((key >> cc->hash_bits_) == 0u); |
35 return cc->colors_[key]; | 36 return cc->colors_[key]; |
36 } | 37 } |
37 | 38 |
| 39 static WEBP_INLINE void VP8LColorCacheSet(const VP8LColorCache* const cc, |
| 40 uint32_t key, uint32_t argb) { |
| 41 assert((key >> cc->hash_bits_) == 0u); |
| 42 cc->colors_[key] = argb; |
| 43 } |
| 44 |
38 static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc, | 45 static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc, |
39 uint32_t argb) { | 46 uint32_t argb) { |
40 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; | 47 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; |
41 cc->colors_[key] = argb; | 48 cc->colors_[key] = argb; |
42 } | 49 } |
43 | 50 |
44 static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc, | 51 static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc, |
45 uint32_t argb) { | 52 uint32_t argb) { |
46 return (kHashMul * argb) >> cc->hash_shift_; | 53 return (kHashMul * argb) >> cc->hash_shift_; |
47 } | 54 } |
48 | 55 |
49 static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, | 56 static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, |
50 uint32_t argb) { | 57 uint32_t argb) { |
51 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; | 58 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; |
52 return cc->colors_[key] == argb; | 59 return (cc->colors_[key] == argb); |
53 } | 60 } |
54 | 61 |
55 //------------------------------------------------------------------------------ | 62 //------------------------------------------------------------------------------ |
56 | 63 |
57 // Initializes the color cache with 'hash_bits' bits for the keys. | 64 // Initializes the color cache with 'hash_bits' bits for the keys. |
58 // Returns false in case of memory error. | 65 // Returns false in case of memory error. |
59 int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits); | 66 int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits); |
60 | 67 |
| 68 void VP8LColorCacheCopy(const VP8LColorCache* const src, |
| 69 VP8LColorCache* const dst); |
| 70 |
61 // Delete the memory associated to color cache. | 71 // Delete the memory associated to color cache. |
62 void VP8LColorCacheClear(VP8LColorCache* const color_cache); | 72 void VP8LColorCacheClear(VP8LColorCache* const color_cache); |
63 | 73 |
64 //------------------------------------------------------------------------------ | 74 //------------------------------------------------------------------------------ |
65 | 75 |
66 #ifdef __cplusplus | 76 #ifdef __cplusplus |
67 } | 77 } |
68 #endif | 78 #endif |
69 | 79 |
70 #endif // WEBP_UTILS_COLOR_CACHE_H_ | 80 #endif // WEBP_UTILS_COLOR_CACHE_H_ |
OLD | NEW |