fast image operations
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
#[test]
fn b64() {
    fn t(i: &'static str, o: &'static str) {
        let mut x = vec![];
        encode(i.as_bytes(), &mut x).unwrap();
        assert_eq!(x, o.as_bytes());
    }

    t("Hello World!", "SGVsbG8gV29ybGQh");
    t("Hello World", "SGVsbG8gV29ybGQ=");
}

pub fn encode(mut input: &[u8], output: &mut impl std::io::Write) -> std::io::Result<()> {
    const Α: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    while let [a, b, c, rest @ ..] = input {
        let α = ((*a as usize) << 16) | ((*b as usize) << 8) | *c as usize;
        output.write_all(&[
            Α[α >> 18],
            Α[(α >> 12) & 0x3F],
            Α[(α >> 6) & 0x3F],
            Α[α & 0x3F],
        ])?;
        input = rest;
    }
    if !input.is_empty() {
        let mut α = (input[0] as usize) << 16;
        if input.len() > 1 {
            α |= (input[1] as usize) << 8;
        }
        output.write_all(&[Α[α >> 18], Α[α >> 12 & 0x3F]])?;
        if input.len() > 1 {
            output.write_all(&[Α[α >> 6 & 0x3f]])?;
        } else {
            output.write_all(&[b'='])?;
        }
        output.write_all(&[b'='])?;
    }
    Ok(())
}

pub const fn size(of: &[u8]) -> usize {
    let use_pad = of.len() % 3 != 0;
    if use_pad {
        4 * (of.len() / 3 + 1)
    } else {
        4 * (of.len() / 3)
    }
}