From 337914a036c7c9183377c8e190c01d72469bc71c Mon Sep 17 00:00:00 2001 From: James Zern Date: Tue, 10 Apr 2012 17:23:38 -0700 Subject: [PATCH] add utils/color_cache.[hc] from experimental Pulled from the current HEAD (218c32e). The history of this and related files is a bit entangled so rather trying to split the changes and introduce some noise in master's history we'll start with a fresh snapshot. The file progression is still available in the experimental branch. Change-Id: Id879be453a94d9f44ec8d47747823ca7297ae008 --- src/utils/color_cache.c | 45 +++++++++++++++++++++++++++ src/utils/color_cache.h | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/utils/color_cache.c create mode 100644 src/utils/color_cache.h diff --git a/src/utils/color_cache.c b/src/utils/color_cache.c new file mode 100644 index 00000000..06e8a718 --- /dev/null +++ b/src/utils/color_cache.c @@ -0,0 +1,45 @@ +// Copyright 2012 Google Inc. All Rights Reserved. +// +// This code is licensed under the same terms as WebM: +// Software License Agreement: http://www.webmproject.org/license/software/ +// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ +// ----------------------------------------------------------------------------- +// +// Color Cache for WebP Lossless +// +// Author: jyrki@google.com (Jyrki Alakuijala) + +#include +#include +#include "./color_cache.h" + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +//------------------------------------------------------------------------------ +// VP8LColorCache. + +int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) { + int hash_size; + assert(cc != NULL); + + if (hash_bits == 0) hash_bits = 1; + hash_size = 1 << hash_bits; + cc->colors_ = (uint32_t*)calloc(hash_size, sizeof(*cc->colors_)); + if (cc->colors_ == NULL) return 0; + cc->hash_shift_ = 32 - hash_bits; + return 1; +} + +void VP8LColorCacheDelete(VP8LColorCache* const cc) { + if (cc != NULL) { + free(cc->colors_); + free(cc); + } +} + +#if defined(__cplusplus) || defined(c_plusplus) +} +#endif + diff --git a/src/utils/color_cache.h b/src/utils/color_cache.h new file mode 100644 index 00000000..aece1453 --- /dev/null +++ b/src/utils/color_cache.h @@ -0,0 +1,68 @@ +// Copyright 2012 Google Inc. All Rights Reserved. +// +// This code is licensed under the same terms as WebM: +// Software License Agreement: http://www.webmproject.org/license/software/ +// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ +// ----------------------------------------------------------------------------- +// +// Color Cache for WebP Lossless +// +// Authors: jyrki@google.com (Jyrki Alakuijala) +// urvang@google.com (Urvang Joshi) + +#ifndef WEBP_UTILS_COLOR_CACHE_H_ +#define WEBP_UTILS_COLOR_CACHE_H_ + +#include "../webp/types.h" + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +// Main color cache struct. +typedef struct { + uint32_t *colors_; // color entries + int hash_shift_; // Hash shift: 32 - hash_bits. +} VP8LColorCache; + +static const uint32_t kHashMul = 0x1e35a7bd; + +static WEBP_INLINE uint32_t VP8LColorCacheLookup( + const VP8LColorCache* const cc, uint32_t key) { + assert(key <= (~0U >> cc->hash_shift_)); + return cc->colors_[key]; +} + +static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc, + uint32_t argb) { + const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; + cc->colors_[key] = argb; +} + +static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc, + uint32_t argb) { + return (kHashMul * argb) >> cc->hash_shift_; +} + +static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, + uint32_t argb) { + const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; + return cc->colors_[key] == argb; +} + +//------------------------------------------------------------------------------ + +// Initializes the color cache with 'hash_bits' bits for the keys. +// Returns false in case of memory error. +int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits); + +// Delete the color cache. +void VP8LColorCacheDelete(VP8LColorCache* const color_cache); + +//------------------------------------------------------------------------------ + +#if defined(__cplusplus) || defined(c_plusplus) +} +#endif + +#endif // WEBP_UTILS_COLOR_CACHE_H_