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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! # ufb
//!
//! Just quickly show or draw a framebuffer in a window, nothing else!
//! Why use ufb:
//! - Uses hardware-acceleration via OpenGL.
//! - To quickly debug image or framebuffer output, instead of writing to files.
//! - Supports L8, La8, Rgb8 and Rgba8 `&[u8]` buffers.
//! - Fast to build.
//! - Doesn't need vulkan drivers.
//! - Minimal interface.
//!
//!
//! ## Usage
//! ```toml
//! [dependencies]
//! ufb = "0.2"
//! ```
//!
//! ```no_run
//! use ufb::{ColorDepth, Window};
//!
//! const WIDTH: u32 = 768;
//! const HEIGHT: u32 = 768;
//!
//! let mut win = Window::new(WIDTH, HEIGHT, ColorDepth::Rgb8, "My Framebuffer").unwrap();
//! for (iter, pixel) in win.get_frame().chunks_exact_mut(3).enumerate() {
//!     let x = iter % WIDTH as usize;
//!     let y = iter / WIDTH as usize;
//!     let val = x ^ y;
//!     let hex = format!("{:06x}", val);
//!     let r = u8::from_str_radix(&hex[0..2], 16).unwrap();
//!     let g = u8::from_str_radix(&hex[2..4], 16).unwrap();
//!     let b = u8::from_str_radix(&hex[4..6], 16).unwrap();
//!     pixel.copy_from_slice(&[r, g, b]);
//! }
//! win.show();
//! ```
//!
//! Using the image crate:
//! ```no_run
//! use ufb::{ColorDepth, Window};
//! use image::GenericImageView;
//!
//! let img = image::open("screenshots/image.jpg").unwrap();
//! let (w, h) = img.dimensions();
//! let mut win = Window::new(w, h, ColorDepth::Rgba8, "image.jpg").unwrap();
//! win.get_frame().copy_from_slice(&img.to_rgba8());
//! win.show();
//! ```
//!

extern crate glfw;
use glu_sys::glu::*;

use glfw::{Action, Context, Key};

/// ufb error types
#[derive(Debug)]
pub enum UfbError {
    /// glfw init error
    InitError(glfw::InitError),
    /// Internal error
    Internal(UfbErrorKind),
}

/// ufb error kinds
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum UfbErrorKind {
    /// Invalid ufb format
    InvalidFormat,
}

impl std::error::Error for UfbError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

impl std::fmt::Display for UfbError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            UfbError::InitError(ref err) => write!(f, "A glfw init error occured {:?}", err),
            UfbError::Internal(ref err) => write!(f, "An internal error occured {:?}", err),
        }
    }
}

impl From<glfw::InitError> for UfbError {
    fn from(err: glfw::InitError) -> UfbError {
        UfbError::InitError(err)
    }
}

/// Supported color depths
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum ColorDepth {
    L8 = 1,
    La8 = 2,
    Rgb8 = 3,
    Rgba8 = 4,
}

/// Wrapper around a glfw window
pub struct Window {
    glfw: glfw::Glfw,
    win: glfw::Window,
    w: u32,
    h: u32,
    visual: ColorDepth,
    events: std::sync::mpsc::Receiver<(f64, glfw::WindowEvent)>,
    frame: Vec<u8>,
}

impl Window {
    /// Instantiate a window
    pub fn new(w: u32, h: u32, visual: ColorDepth, title: &str) -> Result<Self, UfbError> {
        let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS)?;
        let (mut window, events) = glfw
            .create_window(w, h, title, glfw::WindowMode::Windowed)
            .expect("Failed to create GLFW window.");
        window.set_resizable(false);
        window.make_current();
        glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
        Ok(Self {
            glfw,
            win: window,
            w,
            h,
            visual,
            events,
            frame: vec![0u8; (w * h * visual as u32) as usize],
        })
    }

    /// Get the internal buffer
    pub fn get_frame(&mut self) -> &mut [u8] {
        &mut self.frame
    }

    /// Get the pixels at `x, y`
    pub fn buffer_index_at(&self, x: u32, y: u32) -> Option<usize> {
        if x >= self.w || y >= self.h {
            None
        } else {
            Some((y as usize * self.w as usize + x as usize) * self.visual as usize)
        }
    }

    /// Show the window
    pub fn show(&mut self) {
        while !self.win.should_close() {
            let gl_enum = match self.visual {
                ColorDepth::L8 => GL_LUMINANCE,
                ColorDepth::La8 => GL_LUMINANCE_ALPHA,
                ColorDepth::Rgb8 => GL_RGB,
                ColorDepth::Rgba8 => GL_RGBA,
            };
            unsafe {
                glRasterPos2i(-1, 1);
                glPixelZoom(1., -1.);
                glDrawPixels(
                    self.w as _,
                    self.h as _,
                    gl_enum,
                    GL_UNSIGNED_BYTE,
                    self.frame.as_ptr() as _,
                );
            }
            self.win.swap_buffers();
            self.glfw.poll_events();
            for (_, event) in glfw::flush_messages(&self.events) {
                if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event {
                    self.win.set_should_close(true)
                }
            }
        }
    }

    /// Add logic while the window is shown
    pub fn shown(&mut self) -> bool {
        let gl_enum = match self.visual {
            ColorDepth::L8 => GL_LUMINANCE,
            ColorDepth::La8 => GL_LUMINANCE_ALPHA,
            ColorDepth::Rgb8 => GL_RGB,
            ColorDepth::Rgba8 => GL_RGBA,
        };
        unsafe {
            glRasterPos2i(-1, 1);
            glPixelZoom(1., -1.);
            glDrawPixels(
                self.w as _,
                self.h as _,
                gl_enum,
                GL_UNSIGNED_BYTE,
                self.frame.as_ptr() as _,
            );
        }
        self.win.swap_buffers();
        self.glfw.poll_events();
        for (_, event) in glfw::flush_messages(&self.events) {
            if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event {
                self.win.set_should_close(true)
            }
        }
        !self.win.should_close()
    }
}