aboutsummaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-22 12:29:54 +0100
committerAkshay <[email protected]>2021-04-22 12:29:54 +0100
commit5e3e83a5522eecee9d8dbf6c1284c581ef7a35ea (patch)
treecb1e587dcbf17138ef6ef4d417f6787d2064adae /src/bin
parenteba6e3376490f2b0693f75fda7c0b6ab5e68da6c (diff)
add obiv, the obi image viewer
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/viewer.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/bin/viewer.rs b/src/bin/viewer.rs
new file mode 100644
index 0000000..e600aee
--- /dev/null
+++ b/src/bin/viewer.rs
@@ -0,0 +1,79 @@
1use std::env;
2use std::fs::{File, OpenOptions};
3use std::io::{self, prelude, Cursor, Read};
4
5const HELP_TEXT: &'static str = r#"
6OBI Viewer
7"#;
8
9use obi::Image;
10use sdl2::{event::Event, keyboard::Keycode, pixels::Color, rect::Rect};
11
12fn main() {
13 let args = env::args().collect::<Vec<_>>();
14
15 let sdl_context = sdl2::init().unwrap();
16 let video_subsystem = sdl_context.video().unwrap();
17
18 match &args[..] {
19 [_, path] => {
20 eprintln!("{}", path);
21 let image_src = OpenOptions::new()
22 .read(true)
23 .write(false)
24 .create(false)
25 .open(path);
26 if let Ok(mut image) = image_src {
27 let mut buf = Vec::new();
28 image.read_to_end(&mut buf);
29 let img = Image::decode(&mut (Cursor::new(buf))).unwrap();
30
31 let window = video_subsystem
32 .window("OBI Viewer", img.width(), img.height())
33 .position_centered()
34 .opengl()
35 .build()
36 .map_err(|e| e.to_string())
37 .unwrap();
38
39 let mut canvas = window
40 .into_canvas()
41 .build()
42 .map_err(|e| e.to_string())
43 .unwrap();
44
45 let mut event_pump = sdl_context.event_pump().unwrap();
46
47 'running: loop {
48 for event in event_pump.poll_iter() {
49 match event {
50 Event::Quit { .. }
51 | Event::KeyDown {
52 keycode: Some(Keycode::Escape),
53 ..
54 } => break 'running,
55 _ => {}
56 }
57 }
58
59 canvas.set_draw_color(Color::RGB(0, 0, 0));
60 canvas.clear();
61 canvas.set_draw_color(Color::RGB(255, 255, 255));
62
63 for x in 0..img.width() {
64 for y in 0..img.height() {
65 if img.data[img.index(x, y).unwrap()] {
66 canvas.fill_rect(Rect::new(x as i32, y as i32, 1, 1));
67 }
68 }
69 }
70
71 canvas.present();
72 }
73 }
74 }
75 _ => {
76 eprintln!("requires and argument!");
77 }
78 }
79}