From 5e3e83a5522eecee9d8dbf6c1284c581ef7a35ea Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 22 Apr 2021 16:59:54 +0530 Subject: add obiv, the obi image viewer --- src/bin/viewer.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/bin/viewer.rs (limited to 'src/bin') 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 @@ +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::>(); + + 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!"); + } + } +} -- cgit v1.2.3