aboutsummaryrefslogtreecommitdiff
path: root/src/bin/viewer.rs
blob: e600aeed17245fcff4819a1a5d9ca7143ee3e90f (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::env;
use std::fs::{File, OpenOptions};
use std::io::{self, prelude, Cursor, Read};

const HELP_TEXT: &'static str = r#"
OBI Viewer
"#;

use obi::Image;
use sdl2::{event::Event, keyboard::Keycode, pixels::Color, rect::Rect};

fn main() {
    let args = env::args().collect::<Vec<_>>();

    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();

    match &args[..] {
        [_, path] => {
            eprintln!("{}", path);
            let image_src = OpenOptions::new()
                .read(true)
                .write(false)
                .create(false)
                .open(path);
            if let Ok(mut image) = image_src {
                let mut buf = Vec::new();
                image.read_to_end(&mut buf);
                let img = Image::decode(&mut (Cursor::new(buf))).unwrap();

                let window = video_subsystem
                    .window("OBI Viewer", img.width(), img.height())
                    .position_centered()
                    .opengl()
                    .build()
                    .map_err(|e| e.to_string())
                    .unwrap();

                let mut canvas = window
                    .into_canvas()
                    .build()
                    .map_err(|e| e.to_string())
                    .unwrap();

                let mut event_pump = sdl_context.event_pump().unwrap();

                'running: loop {
                    for event in event_pump.poll_iter() {
                        match event {
                            Event::Quit { .. }
                            | Event::KeyDown {
                                keycode: Some(Keycode::Escape),
                                ..
                            } => break 'running,
                            _ => {}
                        }
                    }

                    canvas.set_draw_color(Color::RGB(0, 0, 0));
                    canvas.clear();
                    canvas.set_draw_color(Color::RGB(255, 255, 255));

                    for x in 0..img.width() {
                        for y in 0..img.height() {
                            if img.data[img.index(x, y).unwrap()] {
                                canvas.fill_rect(Rect::new(x as i32, y as i32, 1, 1));
                            }
                        }
                    }

                    canvas.present();
                }
            }
        }
        _ => {
            eprintln!("requires and argument!");
        }
    }
}