go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

color_cache_utils.h (2917B)


      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_UTILS_H_
     16 #define WEBP_UTILS_COLOR_CACHE_UTILS_H_
     17 
     18 #include <assert.h>
     19 
     20 #include "src/dsp/cpu.h"
     21 #include "src/dsp/dsp.h"
     22 #include "src/webp/types.h"
     23 
     24 #ifdef __cplusplus
     25 extern "C" {
     26 #endif
     27 
     28 // Main color cache struct.
     29 typedef struct {
     30   uint32_t* colors;  // color entries
     31   int hash_shift;    // Hash shift: 32 - 'hash_bits'.
     32   int hash_bits;
     33 } VP8LColorCache;
     34 
     35 static const uint32_t kHashMul = 0x1e35a7bdu;
     36 
     37 static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
     38 int VP8LHashPix(uint32_t argb, int shift) {
     39   return (int)((argb * kHashMul) >> shift);
     40 }
     41 
     42 static WEBP_INLINE uint32_t VP8LColorCacheLookup(
     43     const VP8LColorCache* const cc, uint32_t key) {
     44   assert((key >> cc->hash_bits) == 0u);
     45   return cc->colors[key];
     46 }
     47 
     48 static WEBP_INLINE void VP8LColorCacheSet(const VP8LColorCache* const cc,
     49                                           uint32_t key, uint32_t argb) {
     50   assert((key >> cc->hash_bits) == 0u);
     51   cc->colors[key] = argb;
     52 }
     53 
     54 static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc,
     55                                              uint32_t argb) {
     56   const int key = VP8LHashPix(argb, cc->hash_shift);
     57   cc->colors[key] = argb;
     58 }
     59 
     60 static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc,
     61                                               uint32_t argb) {
     62   return VP8LHashPix(argb, cc->hash_shift);
     63 }
     64 
     65 // Return the key if cc contains argb, and -1 otherwise.
     66 static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc,
     67                                               uint32_t argb) {
     68   const int key = VP8LHashPix(argb, cc->hash_shift);
     69   return (cc->colors[key] == argb) ? key : -1;
     70 }
     71 
     72 //------------------------------------------------------------------------------
     73 
     74 // Initializes the color cache with 'hash_bits' bits for the keys.
     75 // Returns false in case of memory error.
     76 int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits);
     77 
     78 void VP8LColorCacheCopy(const VP8LColorCache* const src,
     79                         VP8LColorCache* const dst);
     80 
     81 // Delete the memory associated to color cache.
     82 void VP8LColorCacheClear(VP8LColorCache* const color_cache);
     83 
     84 //------------------------------------------------------------------------------
     85 
     86 #ifdef __cplusplus
     87 }
     88 #endif
     89 
     90 #endif  // WEBP_UTILS_COLOR_CACHE_UTILS_H_