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#include "./color_cache.h"
18#include "../utils/utils.h"
19
20//------------------------------------------------------------------------------
21// VP8LColorCache.
22
23int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) {
24  const int hash_size = 1 << hash_bits;
25  assert(cc != NULL);
26  assert(hash_bits > 0);
27  cc->colors_ = (uint32_t*)WebPSafeCalloc((uint64_t)hash_size,
28                                          sizeof(*cc->colors_));
29  if (cc->colors_ == NULL) return 0;
30  cc->hash_shift_ = 32 - hash_bits;
31  cc->hash_bits_ = hash_bits;
32  return 1;
33}
34
35void VP8LColorCacheClear(VP8LColorCache* const cc) {
36  if (cc != NULL) {
37    WebPSafeFree(cc->colors_);
38    cc->colors_ = NULL;
39  }
40}
41
42void VP8LColorCacheCopy(const VP8LColorCache* const src,
43                        VP8LColorCache* const dst) {
44  assert(src != NULL);
45  assert(dst != NULL);
46  assert(src->hash_bits_ == dst->hash_bits_);
47  memcpy(dst->colors_, src->colors_,
48         ((size_t)1u << dst->hash_bits_) * sizeof(*dst->colors_));
49}
50