1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
8 // Color Cache for WebP Lossless
10 // Authors: Jyrki Alakuijala (jyrki@google.com)
11 // Urvang Joshi (urvang@google.com)
13 #ifndef WEBP_UTILS_COLOR_CACHE_H_
14 #define WEBP_UTILS_COLOR_CACHE_H_
16 #include "../webp/types.h"
18 #if defined(__cplusplus) || defined(c_plusplus)
22 // Main color cache struct.
24 uint32_t *colors_; // color entries
25 int hash_shift_; // Hash shift: 32 - hash_bits.
28 static const uint32_t kHashMul = 0x1e35a7bd;
30 static WEBP_INLINE uint32_t VP8LColorCacheLookup(
31 const VP8LColorCache* const cc, uint32_t key) {
32 assert(key <= (~0U >> cc->hash_shift_));
33 return cc->colors_[key];
36 static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc,
38 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_;
39 cc->colors_[key] = argb;
42 static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc,
44 return (kHashMul * argb) >> cc->hash_shift_;
47 static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc,
49 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_;
50 return cc->colors_[key] == argb;
53 //------------------------------------------------------------------------------
55 // Initializes the color cache with 'hash_bits' bits for the keys.
56 // Returns false in case of memory error.
57 int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits);
59 // Delete the memory associated to color cache.
60 void VP8LColorCacheClear(VP8LColorCache* const color_cache);
62 //------------------------------------------------------------------------------
64 #if defined(__cplusplus) || defined(c_plusplus)
68 #endif // WEBP_UTILS_COLOR_CACHE_H_