1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// libwebp v1.6.0, src/utils/color_cache_utils.c:
//
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Color Cache for WebP Lossless
//
// Author: Jyrki Alakuijala (jyrki@google.com)
// libwebp v1.6.0, src/utils/color_cache_utils.h:
//
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Color Cache for WebP Lossless
//
// Authors: Jyrki Alakuijala (jyrki@google.com)
// Urvang Joshi (urvang@google.com)
//! Translation of `src/utils/color_cache_utils.c` and `src/utils/color_cache_utils.h` from libwebp v1.6.0.
//!
//! Work package: WP3 (lossless shared).
//!
//! # Deviations from upstream
//!
//! Changes of representation that the ADRs allow, or behavior that the Rust
//! side cannot reproduce exactly. Each records the reason.
//!
//! 1. **`VP8LColorCache`.** `colors` is a `Vec<u32>`, empty where C has `NULL`.
//! `Set` and `Insert` take `&mut`, because C writes through the cache's
//! pointer from a `const` struct pointer. Keys index after a checked
//! conversion.
//!
//! # Excluded undefined behavior
//!
//! Inputs that are undefined behavior (or assertion failures) in C and are
//! therefore excluded from the oracle comparison (PORTING rule 8):
//!
//! 1. `VP8LHashPix` with a shift of 32 or more, `VP8LColorCacheInit` with hash
//! bits outside 1 to 30, `VP8LSubSampleSize` with 31 or more sampling bits,
//! `RightShiftRound` with a shift of 64 or more, `DivRound` by zero or with
//! an overflowing quotient, and the prefix encodings for distances below 3:
//! shifts out of range, a zero argument to `BitsLog2Floor` or signed
//! overflow in C. (`VP8LSubSampleSize`, `RightShiftRound`, `DivRound` and
//! the prefix encodings are in `src/dsp/lossless_common.rs`.)
//!
//! # Checks not applicable
//!
//! These checks do not apply, or cannot be made, with the reason:
//!
//! 1. **`VP8LColorCacheInit` failure.** The format allows 1 to 11 hash bits,
//! which always pass the size test; the failure branch keeps C's partial
//! state (see the documentation of `vp8l_color_cache_init`) but is not
//! reached.
use cratewebp_safe_calloc;
// upstream: src/utils/color_cache_utils.h VP8LColorCache
/// Main color cache struct.
// upstream: src/utils/color_cache_utils.h kHashMul
pub const HASH_MUL: u32 = 0x1e35_a7bd;
// upstream: src/utils/color_cache_utils.h VP8LHashPix
/// The hash of `argb`: the 32-bit product with [`HASH_MUL`], which wraps as
/// in C, shifted right by `shift`.
/// Converts a cache key, which is never negative, to an index.
// upstream: src/utils/color_cache_utils.h VP8LColorCacheLookup
// upstream: src/utils/color_cache_utils.h VP8LColorCacheSet
// upstream: src/utils/color_cache_utils.h VP8LColorCacheInsert
// upstream: src/utils/color_cache_utils.h VP8LColorCacheGetIndex
// upstream: src/utils/color_cache_utils.h VP8LColorCacheContains
/// Return the key if cc contains argb, and -1 otherwise.
// upstream: src/utils/color_cache_utils.c VP8LColorCacheInit
/// Initializes the color cache with `hash_bits` bits for the keys.
/// Returns false in case of memory error; `colors` is then empty, and the
/// other fields keep their values, as in C.
// upstream: src/utils/color_cache_utils.c VP8LColorCacheClear
/// Delete the memory associated to color cache.
// upstream: src/utils/color_cache_utils.c VP8LColorCacheCopy
/// Copies the `1 << dst.hash_bits` colors of `src` into `dst`.
///
/// # Panics
///
/// Panics when either cache holds fewer colors, where C copies outside them.