Skip to main content

webp/utils/
random_utils.rs

1// libwebp v1.6.0, src/utils/random_utils.c:
2//
3// Copyright 2013 Google Inc. All Rights Reserved.
4//
5// Use of this source code is governed by a BSD-style license
6// that can be found in the COPYING file in the root of the source
7// tree. An additional intellectual property rights grant can be found
8// in the file PATENTS. All contributing project authors may
9// be found in the AUTHORS file in the root of the source tree.
10// -----------------------------------------------------------------------------
11//
12// Pseudo-random utilities
13//
14// Author: Skal (pascal.massimino@gmail.com)
15
16// libwebp v1.6.0, src/utils/random_utils.h:
17//
18// Copyright 2013 Google Inc. All Rights Reserved.
19//
20// Use of this source code is governed by a BSD-style license
21// that can be found in the COPYING file in the root of the source
22// tree. An additional intellectual property rights grant can be found
23// in the file PATENTS. All contributing project authors may
24// be found in the AUTHORS file in the root of the source tree.
25// -----------------------------------------------------------------------------
26//
27// Pseudo-random utilities
28//
29// Author: Skal (pascal.massimino@gmail.com)
30
31//! Translation of `src/utils/random_utils.c` and `src/utils/random_utils.h` from libwebp v1.6.0.
32//!
33//! Work package: WP1 (types, memory, workers).
34
35// upstream: src/utils/random_utils.h VP8_RANDOM_DITHER_FIX
36/// Fixed-point precision of the dithering amplitude.
37pub const VP8_RANDOM_DITHER_FIX: i32 = 8;
38// upstream: src/utils/random_utils.h VP8_RANDOM_TABLE_SIZE
39/// Number of entries of the Knuth generator table.
40pub const VP8_RANDOM_TABLE_SIZE: usize = 55;
41
42// upstream: src/utils/random_utils.c kRandomTable
43/// Initial table of the generator, 31-bit values.
44pub const RANDOM_TABLE: [u32; VP8_RANDOM_TABLE_SIZE] = [
45    0x0de1_5230,
46    0x03b3_1886,
47    0x775f_accb,
48    0x1c88_626a,
49    0x6838_5c55,
50    0x14b3_b828,
51    0x4a85_fef8,
52    0x49dd_b84b,
53    0x64fc_f397,
54    0x5c55_0289,
55    0x4a29_0000,
56    0x0d7e_c1da,
57    0x5940_b7ab,
58    0x5492_577d,
59    0x4e19_ca72,
60    0x38d3_8c69,
61    0x0c01_ee65,
62    0x32a1_755f,
63    0x5437_f652,
64    0x5abb_2c32,
65    0x0faa_57b1,
66    0x73f5_33e7,
67    0x685f_eeda,
68    0x7563_cce2,
69    0x6e99_0e83,
70    0x4730_a7ed,
71    0x4fc0_d9c6,
72    0x496b_153c,
73    0x4f14_03fa,
74    0x541a_fb0c,
75    0x7399_0b32,
76    0x26d7_cb1c,
77    0x6fcc_3706,
78    0x2cbb_77d8,
79    0x7576_2f2a,
80    0x6425_ccdd,
81    0x24b3_5461,
82    0x0a7d_8715,
83    0x2204_14a8,
84    0x141e_bf67,
85    0x56b4_1583,
86    0x73e5_02e3,
87    0x44ca_b16f,
88    0x2826_4d42,
89    0x73ba_aefb,
90    0x0a50_ebed,
91    0x1d6a_b6fb,
92    0x0d3a_d40b,
93    0x35db_3b68,
94    0x2b08_1e83,
95    0x77ce_6b95,
96    0x5181_e5f0,
97    0x7885_3bbc,
98    0x009f_9494,
99    0x27e5_ed3c,
100];
101
102// upstream: src/utils/random_utils.h VP8Random
103/// State of the dithering generator (C: `VP8Random`).
104///
105/// `index1` and `index2` are positions in `tab`; C keeps them as `int`, and they
106/// are only ever in `0..VP8_RANDOM_TABLE_SIZE`.
107#[derive(Clone, Copy, Debug, PartialEq, Eq)]
108pub struct VP8Random {
109    pub index1: usize,
110    pub index2: usize,
111    pub tab: [u32; VP8_RANDOM_TABLE_SIZE],
112    pub amp: i32,
113}
114
115// upstream: src/utils/random_utils.c VP8InitRandom
116/// Initializes the generator with a dithering amplitude in `[0, 1]`; values
117/// outside that range are clamped, and the amplitude is kept in fixed point.
118pub fn vp8_init_random(rg: &mut VP8Random, dithering: f32) {
119    rg.tab = RANDOM_TABLE;
120    rg.index1 = 0;
121    rg.index2 = 31;
122    rg.amp = if dithering < 0.0 {
123        0
124    } else if dithering > 1.0 {
125        1 << VP8_RANDOM_DITHER_FIX
126    } else {
127        scaled_amplitude(dithering)
128    };
129}
130
131/// `(uint32_t)((1 << VP8_RANDOM_DITHER_FIX) * dithering)` for a value in `[0, 1]`.
132#[expect(
133    clippy::cast_possible_truncation,
134    clippy::cast_sign_loss,
135    reason = "the value is in [0, 256] and the C conversion truncates toward zero"
136)]
137#[must_use]
138fn scaled_amplitude(dithering: f32) -> i32 {
139    ((256.0_f32 * dithering) as u32).cast_signed()
140}
141
142// upstream: src/utils/random_utils.h VP8RandomBits2
143/// Returns a centered pseudo-random number with `num_bits` of amplitude, scaled
144/// by `amp` in `VP8_RANDOM_DITHER_FIX` fixed point (Knuth's difference generator).
145///
146/// `num_bits + VP8_RANDOM_DITHER_FIX` must not exceed 31.
147#[must_use]
148pub fn vp8_random_bits2(rg: &mut VP8Random, num_bits: i32, amp: i32) -> i32 {
149    debug_assert!(num_bits + VP8_RANDOM_DITHER_FIX <= 31);
150    let mut diff = rg.tab[rg.index1]
151        .wrapping_sub(rg.tab[rg.index2])
152        .cast_signed();
153    if diff < 0 {
154        diff = diff.cast_unsigned().wrapping_add(1 << 31).cast_signed();
155    }
156    rg.tab[rg.index1] = diff.cast_unsigned();
157    rg.index1 += 1;
158    if rg.index1 == VP8_RANDOM_TABLE_SIZE {
159        rg.index1 = 0;
160    }
161    rg.index2 += 1;
162    if rg.index2 == VP8_RANDOM_TABLE_SIZE {
163        rg.index2 = 0;
164    }
165    // sign-extend, 0-center
166    diff = (diff.cast_unsigned() << 1).cast_signed() >> (32 - num_bits);
167    diff = (diff * amp) >> VP8_RANDOM_DITHER_FIX; // restrict range
168    diff += 1 << (num_bits - 1); // shift back to 0.5-center
169    diff
170}
171
172// upstream: src/utils/random_utils.h VP8RandomBits
173/// Returns a centered pseudo-random number with `num_bits` of amplitude, scaled
174/// by the amplitude that the generator was initialized with.
175#[must_use]
176pub fn vp8_random_bits(rg: &mut VP8Random, num_bits: i32) -> i32 {
177    let amp = rg.amp;
178    vp8_random_bits2(rg, num_bits, amp)
179}