Skip to main content

ob_engine/
palette.rs

1//! Palettes and lookup tables.
2//!
3//! * `.PAL`  – 256 RGB triplets, 6 bits per channel.
4//! * `.PLU`  – palookup: 64 shade levels x 256 colors (shade 0 = brightest).
5//! * `.TLU`  – 256x256 translucency table, `tlu[a * 256 + b]`.
6
7pub const NUM_SHADES: usize = 64;
8
9#[derive(Clone)]
10pub struct Palette {
11    /// 0x00RRGGBB, expanded to 8 bits per channel.
12    pub colors: [u32; 256],
13}
14
15impl Palette {
16    /// Parses 768 bytes of RGB. Classic VGA palettes use 6 bits per channel; Blood's
17    /// palettes are stored with full 8-bit values, so the depth is auto-detected.
18    pub fn from_vga(b: &[u8]) -> Self {
19        let six_bit = b.iter().take(768).all(|&v| v < 64);
20        let mut colors = [0u32; 256];
21        for (i, c) in colors.iter_mut().enumerate() {
22            let e = |v: u8| -> u32 {
23                if six_bit {
24                    let v = v as u32;
25                    (v << 2) | (v >> 4)
26                } else {
27                    v as u32
28                }
29            };
30            if i * 3 + 2 < b.len() {
31                *c = (e(b[i * 3]) << 16) | (e(b[i * 3 + 1]) << 8) | e(b[i * 3 + 2]);
32            }
33        }
34        Palette { colors }
35    }
36
37    pub fn rgb(&self, idx: u8) -> (u8, u8, u8) {
38        let c = self.colors[idx as usize];
39        ((c >> 16) as u8, (c >> 8) as u8, c as u8)
40    }
41}
42
43/// Full set of colour tables used by the renderer.
44#[derive(Clone)]
45pub struct ColorTables {
46    pub palettes: Vec<Palette>,
47    /// Indexed by sprite/wall `pal` value. `None` falls back to table 0.
48    pub lookups: Vec<Option<Vec<u8>>>,
49    pub trans: Vec<u8>,
50}
51
52impl ColorTables {
53    pub fn new(base: Palette) -> Self {
54        // Identity shade table: shade simply darkens toward index 0.
55        ColorTables {
56            palettes: vec![base],
57            lookups: vec![None; 256],
58            trans: Vec::new(),
59        }
60    }
61
62    pub fn lookup(&self, pal: u8) -> &[u8] {
63        match &self.lookups[pal as usize] {
64            Some(t) => t,
65            None => self.lookups[0].as_deref().unwrap_or(&[]),
66        }
67    }
68
69    /// Builds a true-colour shade table for one palookup and base palette:
70    /// `out[shade * 256 + index]` = 0x00RRGGBB.
71    pub fn build_rgb_lut(&self, pal: u8, base: usize) -> Vec<u32> {
72        let p = &self.palettes[base.min(self.palettes.len() - 1)];
73        let lk = self.lookup(pal);
74        let mut out = vec![0u32; NUM_SHADES * 256];
75        for s in 0..NUM_SHADES {
76            for c in 0..256 {
77                let idx = if lk.len() >= NUM_SHADES * 256 {
78                    lk[s * 256 + c]
79                } else {
80                    c as u8
81                };
82                out[s * 256 + c] = p.colors[idx as usize];
83            }
84        }
85        out
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn eight_bit_detect() {
95        let mut b = vec![0u8; 768];
96        b[3] = 200;
97        assert_eq!(Palette::from_vga(&b).rgb(1).0, 200);
98    }
99
100    #[test]
101    fn vga_expand() {
102        let mut b = vec![0u8; 768];
103        b[3] = 63;
104        b[4] = 32;
105        b[5] = 1;
106        let p = Palette::from_vga(&b);
107        assert_eq!(p.rgb(1), (255, 130, 4));
108        assert_eq!(p.rgb(0), (0, 0, 0));
109    }
110}