pub(crate) const VP8L_MAGIC_BYTE: u8 = 0x2f;
pub(crate) const VP8L_IMAGE_SIZE_BITS: u32 = 14;
pub(crate) const VP8L_VERSION_BITS: u32 = 3;
pub(crate) const NUM_LITERAL_CODES: usize = 256;
pub(crate) const NUM_LENGTH_CODES: usize = 24;
pub(crate) const NUM_DISTANCE_CODES: usize = 40;
pub(crate) const CODE_LENGTH_CODES: usize = 19;
pub(crate) const MAX_ALLOWED_CODE_LENGTH: usize = 15;
pub(crate) const DEFAULT_CODE_LENGTH: u32 = 8;
pub(crate) const HUFFMAN_TABLE_BITS: u32 = 8;
pub(crate) const MAX_CACHE_BITS: u32 = 11;
pub(crate) const HASH_MUL: u32 = 0x1e35_a7bd;
pub(crate) const CODE_TO_PLANE_CODES: usize = 120;
pub(crate) const WINDOW_SIZE: u32 = (1 << 20) - 120;
pub(crate) const MAX_COPY_LENGTH: u32 = 4096;
pub(crate) const MIN_MATCH: u32 = 4;
pub(crate) const MAX_CHAIN: u32 = 64;
pub(crate) const HASH_BITS_LZ77: u32 = 18;
pub(crate) const ARGB_BLACK: u32 = 0xff00_0000;
pub(crate) const PREDICTOR_TRANSFORM: u32 = 0;
pub(crate) const CROSS_COLOR_TRANSFORM: u32 = 1;
pub(crate) const SUBTRACT_GREEN_TRANSFORM: u32 = 2;
pub(crate) const COLOR_INDEXING_TRANSFORM: u32 = 3;
pub(crate) const MIN_TRANSFORM_BITS: u32 = 2;
pub(crate) const NUM_TRANSFORM_BITS: u32 = 3;
#[must_use]
pub(crate) const fn subsample_size(size: u32, bits: u32) -> u32 {
(size + (1 << bits) - 1) >> bits
}
pub(crate) const HUFFMAN_CODES_PER_META_CODE: usize = 5;
pub(crate) const ALPHABET_SIZE: [usize; HUFFMAN_CODES_PER_META_CODE] = [
NUM_LITERAL_CODES + NUM_LENGTH_CODES,
NUM_LITERAL_CODES,
NUM_LITERAL_CODES,
NUM_LITERAL_CODES,
NUM_DISTANCE_CODES,
];
pub(crate) const CODE_LENGTH_CODE_ORDER: [u8; CODE_LENGTH_CODES] = [
17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
];
pub(crate) const CODE_LENGTH_EXTRA_BITS: [u8; 3] = [2, 3, 7];
pub(crate) const CODE_LENGTH_REPEAT_OFFSETS: [u8; 3] = [3, 3, 11];
pub(crate) const CODE_TO_PLANE: [u8; CODE_TO_PLANE_CODES] = [
0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a, 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a, 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b, 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03, 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c, 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e, 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b, 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f, 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b, 0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41, 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f, 0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70, ];
const _: () = assert!(CODE_TO_PLANE.len() == CODE_TO_PLANE_CODES);
const _: () = assert!(CODE_LENGTH_CODE_ORDER.len() == CODE_LENGTH_CODES);
#[cfg(test)]
mod tests {
use crate::MAX_DIMENSION;
use super::{
CODE_LENGTH_CODE_ORDER, CODE_TO_PLANE, HASH_MUL, VP8L_IMAGE_SIZE_BITS, subsample_size,
};
#[test]
fn max_dimension_is_16384() {
assert_eq!(MAX_DIMENSION, 16384);
assert_eq!(1u32 << VP8L_IMAGE_SIZE_BITS, MAX_DIMENSION);
}
#[test]
fn code_to_plane_anchors() {
assert_eq!(CODE_TO_PLANE[0], 0x18); assert_eq!(CODE_TO_PLANE[1], 0x07); assert_eq!(CODE_TO_PLANE.len(), 120);
}
#[test]
fn code_to_plane_table_bytes_are_pinned() {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for &byte in &CODE_TO_PLANE {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3); }
assert_eq!(
hash, 11_597_027_981_418_168_485,
"CODE_TO_PLANE bytes changed"
);
}
#[test]
fn code_length_code_order_is_a_permutation_of_0_18() {
let mut seen = [false; 19];
for &c in &CODE_LENGTH_CODE_ORDER {
seen[c as usize] = true;
}
assert!(
seen.iter().all(|&s| s),
"must cover every code-length code 0..=18"
);
}
#[test]
fn hash_multiplier_is_exact() {
assert_eq!(HASH_MUL, 0x1e35_a7bd);
}
#[test]
fn lz77_window_and_length_limits() {
use super::{MAX_COPY_LENGTH, WINDOW_SIZE};
assert_eq!(WINDOW_SIZE, 1_048_456);
assert_eq!(
u64::from(WINDOW_SIZE) + super::CODE_TO_PLANE_CODES as u64,
1 << 20
);
assert_eq!(MAX_COPY_LENGTH, 4096);
}
#[test]
fn subsample_size_ceil_divides() {
assert_eq!(subsample_size(5, 1), 3);
assert_eq!(subsample_size(1, 3), 1);
assert_eq!(subsample_size(16, 0), 16);
assert_eq!(subsample_size(17, 4), 2);
}
}