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