termimage/ops/
no_ansi.rs

1pub use image::DynamicImage;
2#[cfg(target_os = "windows")]
3use self::imports::*;
4
5#[cfg(target_os = "windows")]
6mod imports {
7    pub use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFOEX, SMALL_RECT, COORD, GetConsoleScreenBufferInfoEx, FillConsoleOutputAttribute};
8    pub use self::super::super::super::util::{closest_colour, mul_str};
9    pub use winapi::um::winbase::STD_OUTPUT_HANDLE;
10    pub use self::super::super::create_colourtable;
11    pub use image::{GenericImageView, Pixel, Rgb};
12    pub use winapi::um::processenv::GetStdHandle;
13    pub use std::mem;
14}
15
16
17/// Display the specified image in the default console using WinAPI.
18#[cfg(target_os = "windows")]
19pub fn write_no_ansi(img: &DynamicImage) {
20    let (width, height) = img.dimensions();
21    let term_h = height / 2;
22    print!("{}", mul_str(&format!("{}\n", mul_str("\u{2580}", width as usize)), term_h as usize)); // ▀
23
24    let console_h = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
25    let mut console_info = CONSOLE_SCREEN_BUFFER_INFOEX {
26        cbSize: mem::size_of::<CONSOLE_SCREEN_BUFFER_INFOEX>() as u32,
27        dwSize: COORD { X: 0, Y: 0 },
28        dwCursorPosition: COORD { X: 0, Y: 0 },
29        wAttributes: 0,
30        srWindow: SMALL_RECT {
31            Left: 0,
32            Top: 0,
33            Right: 0,
34            Bottom: 0,
35        },
36        dwMaximumWindowSize: COORD { X: 0, Y: 0 },
37        wPopupAttributes: 0,
38        bFullscreenSupported: 0,
39        ColorTable: [0; 16],
40    };
41    unsafe { GetConsoleScreenBufferInfoEx(console_h, &mut console_info) };
42    let colours =
43        console_info.ColorTable.iter().map(|cr| Rgb([(cr & 0xFF) as u8, ((cr & 0xFF00) >> 8) as u8, ((cr & 0xFF0000) >> 16) as u8])).collect::<Vec<_>>();
44
45    for (y, line) in create_colourtable(img, &colours, &colours).into_iter().enumerate() {
46        for (x, (upper_clr, lower_clr)) in line.into_iter().enumerate() {
47            unsafe {
48                FillConsoleOutputAttribute(console_h,
49                                           (console_info.wAttributes & 0xFF00) | ((lower_clr as u16) << 4) | (upper_clr as u16),
50                                           1,
51                                           COORD {
52                                               X: x as i16,
53                                               Y: console_info.dwCursorPosition.Y - (term_h as i16 - y as i16),
54                                           },
55                                           &mut 0);
56            }
57        }
58    }
59}
60
61/// Display the specified image in the default console using WinAPI.
62///
63/// Or, actually, don't. This is Linux, after all...
64#[cfg(not(target_os = "windows"))]
65pub fn write_no_ansi(_: &DynamicImage) {}