blob: 0f824ed457e6fad49579fc7d2ef76f09a787ebac [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// 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.
Vikas Aroraa2415722012-08-09 16:18:58 -07008// -----------------------------------------------------------------------------
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
James Zern9e80ee92015-03-17 18:54:21 -070018#include "../webp/types.h"
Vikas Aroraa2415722012-08-09 16:18:58 -070019
Vikas Arora8b720222014-01-02 16:48:02 -080020#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -070021extern "C" {
22#endif
23
24// Main color cache struct.
25typedef struct {
26 uint32_t *colors_; // color entries
27 int hash_shift_; // Hash shift: 32 - hash_bits.
28} VP8LColorCache;
29
30static const uint32_t kHashMul = 0x1e35a7bd;
31
32static WEBP_INLINE uint32_t VP8LColorCacheLookup(
33 const VP8LColorCache* const cc, uint32_t key) {
34 assert(key <= (~0U >> cc->hash_shift_));
35 return cc->colors_[key];
36}
37
38static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc,
39 uint32_t argb) {
40 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_;
41 cc->colors_[key] = argb;
42}
43
44static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc,
45 uint32_t argb) {
46 return (kHashMul * argb) >> cc->hash_shift_;
47}
48
49static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc,
50 uint32_t argb) {
51 const uint32_t key = (kHashMul * argb) >> cc->hash_shift_;
52 return cc->colors_[key] == argb;
53}
54
55//------------------------------------------------------------------------------
56
57// Initializes the color cache with 'hash_bits' bits for the keys.
58// Returns false in case of memory error.
59int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits);
60
61// Delete the memory associated to color cache.
62void VP8LColorCacheClear(VP8LColorCache* const color_cache);
63
64//------------------------------------------------------------------------------
65
Vikas Arora8b720222014-01-02 16:48:02 -080066#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -070067}
68#endif
69
70#endif // WEBP_UTILS_COLOR_CACHE_H_