color_cache_utils.c (1701B)
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 // Author: Jyrki Alakuijala (jyrki@google.com) 13 14 #include <assert.h> 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include "src/utils/color_cache_utils.h" 19 #include "src/webp/types.h" 20 #include "src/utils/utils.h" 21 22 //------------------------------------------------------------------------------ 23 // VP8LColorCache. 24 25 int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits) { 26 const int hash_size = 1 << hash_bits; 27 assert(color_cache != NULL); 28 assert(hash_bits > 0); 29 color_cache->colors = (uint32_t*)WebPSafeCalloc( 30 (uint64_t)hash_size, sizeof(*color_cache->colors)); 31 if (color_cache->colors == NULL) return 0; 32 color_cache->hash_shift = 32 - hash_bits; 33 color_cache->hash_bits = hash_bits; 34 return 1; 35 } 36 37 void VP8LColorCacheClear(VP8LColorCache* const color_cache) { 38 if (color_cache != NULL) { 39 WebPSafeFree(color_cache->colors); 40 color_cache->colors = NULL; 41 } 42 } 43 44 void VP8LColorCacheCopy(const VP8LColorCache* const src, 45 VP8LColorCache* const dst) { 46 assert(src != NULL); 47 assert(dst != NULL); 48 assert(src->hash_bits == dst->hash_bits); 49 memcpy(dst->colors, src->colors, 50 ((size_t)1u << dst->hash_bits) * sizeof(*dst->colors)); 51 }