|
Packit |
9c6abc |
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Use of this source code is governed by a BSD-style license
|
|
Packit |
9c6abc |
// that can be found in the COPYING file in the root of the source
|
|
Packit |
9c6abc |
// tree. An additional intellectual property rights grant can be found
|
|
Packit |
9c6abc |
// in the file PATENTS. All contributing project authors may
|
|
Packit |
9c6abc |
// be found in the AUTHORS file in the root of the source tree.
|
|
Packit |
9c6abc |
// -----------------------------------------------------------------------------
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Color Cache for WebP Lossless
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Authors: Jyrki Alakuijala (jyrki@google.com)
|
|
Packit |
9c6abc |
// Urvang Joshi (urvang@google.com)
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifndef WEBP_UTILS_COLOR_CACHE_UTILS_H_
|
|
Packit |
9c6abc |
#define WEBP_UTILS_COLOR_CACHE_UTILS_H_
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#include <assert.h>
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#include "src/webp/types.h"
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifdef __cplusplus
|
|
Packit |
9c6abc |
extern "C" {
|
|
Packit |
9c6abc |
#endif
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Main color cache struct.
|
|
Packit |
9c6abc |
typedef struct {
|
|
Packit |
9c6abc |
uint32_t *colors_; // color entries
|
|
Packit |
9c6abc |
int hash_shift_; // Hash shift: 32 - hash_bits_.
|
|
Packit |
9c6abc |
int hash_bits_;
|
|
Packit |
9c6abc |
} VP8LColorCache;
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static const uint64_t kHashMul = 0x1e35a7bdull;
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE int VP8LHashPix(uint32_t argb, int shift) {
|
|
Packit |
9c6abc |
return (int)(((argb * kHashMul) & 0xffffffffu) >> shift);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE uint32_t VP8LColorCacheLookup(
|
|
Packit |
9c6abc |
const VP8LColorCache* const cc, uint32_t key) {
|
|
Packit |
9c6abc |
assert((key >> cc->hash_bits_) == 0u);
|
|
Packit |
9c6abc |
return cc->colors_[key];
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE void VP8LColorCacheSet(const VP8LColorCache* const cc,
|
|
Packit |
9c6abc |
uint32_t key, uint32_t argb) {
|
|
Packit |
9c6abc |
assert((key >> cc->hash_bits_) == 0u);
|
|
Packit |
9c6abc |
cc->colors_[key] = argb;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc,
|
|
Packit |
9c6abc |
uint32_t argb) {
|
|
Packit |
9c6abc |
const int key = VP8LHashPix(argb, cc->hash_shift_);
|
|
Packit |
9c6abc |
cc->colors_[key] = argb;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc,
|
|
Packit |
9c6abc |
uint32_t argb) {
|
|
Packit |
9c6abc |
return VP8LHashPix(argb, cc->hash_shift_);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Return the key if cc contains argb, and -1 otherwise.
|
|
Packit |
9c6abc |
static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc,
|
|
Packit |
9c6abc |
uint32_t argb) {
|
|
Packit |
9c6abc |
const int key = VP8LHashPix(argb, cc->hash_shift_);
|
|
Packit |
9c6abc |
return (cc->colors_[key] == argb) ? key : -1;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
//------------------------------------------------------------------------------
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Initializes the color cache with 'hash_bits' bits for the keys.
|
|
Packit |
9c6abc |
// Returns false in case of memory error.
|
|
Packit |
9c6abc |
int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
void VP8LColorCacheCopy(const VP8LColorCache* const src,
|
|
Packit |
9c6abc |
VP8LColorCache* const dst);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Delete the memory associated to color cache.
|
|
Packit |
9c6abc |
void VP8LColorCacheClear(VP8LColorCache* const color_cache);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
//------------------------------------------------------------------------------
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifdef __cplusplus
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
#endif
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#endif // WEBP_UTILS_COLOR_CACHE_UTILS_H_
|