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
This commit is contained in:
James Zern 2012-04-10 17:23:38 -07:00
parent b3bf8fe7a1
commit 337914a036
2 changed files with 113 additions and 0 deletions

45
src/utils/color_cache.c Normal file
View File

@ -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 <assert.h>
#include <stdlib.h>
#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

68
src/utils/color_cache.h Normal file
View File

@ -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_