viewer/
viewer.rs

1#![deny(warnings)]
2
3extern crate orbclient;
4extern crate orbimage;
5
6use std::cmp::max;
7use std::env;
8
9use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
10use orbimage::Image;
11
12fn find_scale(image: &Image, width: u32, height: u32) -> (u32, u32, f64) {
13    let d_w = width as f64;
14    let d_h = height as f64;
15    let i_w = image.width() as f64;
16    let i_h = image.height() as f64;
17
18    let scale = if d_w / d_h > i_w / i_h {
19        d_h / i_h
20    } else {
21        d_w / i_w
22    };
23
24    if scale < 1.0 {
25        ((i_w * scale) as u32, (i_h * scale) as u32, scale)
26    } else {
27        (i_w as u32, i_h as u32, 1.0)
28    }
29}
30
31fn draw_image(window: &mut Window, image: &Image) {
32    window.set(Color::rgb(0, 0, 0));
33    /*
34    let box_size = 4;
35    for box_y in 0..window.height()/box_size {
36        for box_x in 0..window.width()/box_size {
37            let color = if box_x % 2 == box_y % 2 {
38                Color::rgb(102, 102, 102)
39            }else{
40                Color::rgb(53, 53, 53)
41            };
42
43            window.rect((box_x * box_size) as i32, (box_y * box_size) as i32, box_size, box_size, color);
44        }
45    }
46    */
47
48    let x = (window.width() - image.width())/2;
49    let y = (window.height() - image.height())/2;
50    image.draw(window, x as i32, y as i32);
51    window.sync();
52}
53
54fn main() {
55    let path = match env::args().nth(1) {
56        Some(arg) => arg,
57        None => "/ui/background.png".to_string(),
58    };
59
60    match Image::from_path(&path) {
61        Ok(image) => {
62            let (display_width, display_height) = orbclient::get_display_size().expect("viewer: failed to get display size");
63
64            let (width, height, scale) = find_scale(&image, display_width * 4/5, display_height * 4/5);
65
66            let mut window = Window::new_flags(
67                -1, -1, max(320, width), max(240, height),
68                &format!("{} - {:.1}% - Viewer", path, scale * 100.0),
69                &[WindowFlag::Resizable]
70            ).unwrap();
71
72            let mut scaled_image = image.clone();
73            let mut resize = Some((window.width(), window.height()));
74            loop {
75                if let Some((w, h)) = resize.take() {
76                    let (width, height, scale) = find_scale(&image, w, h);
77
78                    if width == scaled_image.width() && height == scaled_image.height() {
79                        // Do not resize scaled image
80                    } else if width == image.width() && height == image.height() {
81                        scaled_image = image.clone();
82                    } else {
83                        scaled_image = image.resize(width, height, orbimage::ResizeType::Lanczos3).unwrap();
84                    }
85
86                    window.set_title(&format!("{} - {:.1}% - Viewer", path, scale * 100.0));
87
88                    draw_image(&mut window, &scaled_image);
89                }
90
91                for event in window.events() {
92                    match event.to_option() {
93                        EventOption::Resize(resize_event) => {
94                            resize = Some((resize_event.width, resize_event.height));
95                        },
96                        EventOption::Quit(_) => return,
97                        _ => ()
98                    }
99                }
100            }
101        },
102        Err(err) => {
103            let msg = format!("{}", err);
104
105            let mut window = Window::new(-1, -1, max(320, msg.len() as u32 * 8), 32,
106                                         &format!("{} - Viewer", path)).unwrap();
107
108            window.set(Color::rgb(255, 255, 255));
109
110            let mut x = 0;
111            for c in msg.chars() {
112                window.char(x, 0, c, Color::rgb(0, 0, 0));
113                x += 8;
114            }
115
116            window.sync();
117
118            loop {
119                for event in window.events() {
120                    if let EventOption::Quit(_) = event.to_option() {
121                        return;
122                    }
123                }
124            }
125        }
126    }
127}