15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2012 Google Inc. All Rights Reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license
4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// that can be found in the COPYING file in the root of the source
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// tree. An additional intellectual property rights grant can be found
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// in the file PATENTS. All contributing project authors may
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// be found in the AUTHORS file in the root of the source tree.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// -----------------------------------------------------------------------------
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Color Cache for WebP Lossless
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: Jyrki Alakuijala (jyrki@google.com)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <assert.h>
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h>
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "./color_cache.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "../utils/utils.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// VP8LColorCache.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const int hash_size = 1 << hash_bits;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(cc != NULL);
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(hash_bits > 0);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cc->colors_ = (uint32_t*)WebPSafeCalloc((uint64_t)hash_size,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          sizeof(*cc->colors_));
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cc->colors_ == NULL) return 0;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cc->hash_shift_ = 32 - hash_bits;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return 1;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void VP8LColorCacheClear(VP8LColorCache* const cc) {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cc != NULL) {
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    WebPSafeFree(cc->colors_);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cc->colors_ = NULL;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
40