aboutsummaryrefslogtreecommitdiff
path: root/lvc/src/main.rs
blob: 413e4a40d4e10c9b5b1855dec04c565511dd742d (plain)
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
use lvc::{debug::draw_debug, decode::decode, encode::encode, Frame, Pixel, PixelValue, View, P2};
use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write};

fn main() {
    let size = P2 { x: 1920, y: 1080 };

    let mut last_frame = Frame::new(size);
    let mut debug_frame = Some(Frame::new(size));

    let mut stdin = BufReader::new(stdin());
    let mut stdout = BufWriter::new(stdout());

    loop {
        let mut frame = read_frame(&mut stdin, size);

        let b = encode(&last_frame, &frame, View::all(size));
        decode(&last_frame, &mut frame, View::all(size), &b);

        if let Some(debug_frame) = &mut debug_frame {
            debug_frame.pixels.copy_from_slice(&frame.pixels);
            draw_debug(debug_frame, View::all(size), &b);
            write_frame(&mut stdout, &debug_frame);
        } else {
            write_frame(&mut stdout, &frame);
        }

        last_frame = frame;
    }
}

fn read_frame(inp: &mut impl Read, size: P2) -> Frame {
    let mut f = Frame::new(size);

    for y in 0..size.y {
        for x in 0..size.x {
            let mut cc = [0u8; 3];
            inp.read_exact(&mut cc).unwrap();
            f[P2 { x, y }] = Pixel {
                r: cc[0] as PixelValue,
                g: cc[1] as PixelValue,
                b: cc[2] as PixelValue,
            };
        }
    }
    f
}

fn write_frame(out: &mut impl Write, frame: &Frame) {
    for y in 0..frame.size.y {
        for x in 0..frame.size.x {
            let p = frame[P2 { x, y }];
            let mut cc = [0u8; 3];
            cc[0] = p.r as u8;
            cc[1] = p.g as u8;
            cc[2] = p.b as u8;
            out.write_all(&mut cc).unwrap();
        }
    }
}