Commit 51b722e3 authored by Jeremy Soller's avatar Jeremy Soller

Cleanup, remove bmp

parent 0e9fc5d0
......@@ -16,10 +16,6 @@ path = "src/lib.rs"
name = "data"
path = "examples/data.rs"
[[bin]]
name = "redox_icon_display"
path = "examples/redox_icon_display.rs"
[target.i686-unknown-linux-gnu.dependencies]
sdl2 = "0.9"
......
extern crate orbclient;
use orbclient::{Color, Window};
use orbclient::{Color, Window, EventOption};
fn main() {
......@@ -15,7 +15,17 @@ fn main() {
.unwrap();
window.set(Color::rgb(255, 255, 255));
window.sync();
println!("Data: {}", window.data().len());
println!("Mut Data: {}", window.data_mut().len());
'events: loop {
for event in window.events() {
match event.to_option() {
EventOption::Quit(_quit_event) => break 'events,
event_option => println!("{:?}", event_option)
}
}
}
}
extern crate orbclient;
use std::cmp::max;
use std::env;
use orbclient::{BmpFile, Color, EventOption, Window};
fn main() {
let path = match env::args().nth(1) {
Some(arg) => arg,
None => "res/redox.bmp".to_string(),
};
let bmp = BmpFile::from_path(&path);
let mut window = Window::new(-1,
-1,
max(32, bmp.width() as u32),
max(32, bmp.height() as u32),
&path)
.unwrap();
window.set(Color::rgb(0, 0, 0));
window.image(0, 0, bmp.width() as u32, bmp.height() as u32, &bmp);
window.sync();
loop {
for event in window.events() {
println!("{:?}", event.to_option());
if let EventOption::Quit(_) = event.to_option() {
return;
}
}
}
}
use super::Color;
use std::fs::File;
use std::io::Read;
use std::ops::Deref;
// TODO: Follow naming convention
/// A bitmap
pub struct BmpFile {
/// The bitmap width
w: usize,
/// The bitmap height
h: usize,
/// The data of the bitmap
data: Vec<Color>,
}
impl BmpFile {
/// Create a new bitmap
pub fn new(width: usize, height: usize) -> Self {
BmpFile {
w: width,
h: height,
data: Vec::new(),
}
}
/// Create a new empty bitmap
pub fn default() -> Self {
Self::new(0, 0)
}
/// Load bitmap from given path
pub fn from_path(path: &str) -> Self {
let mut data: Vec<u8> = Vec::new();
if let Ok(mut file) = File::open(path) {
let _ = file.read_to_end(&mut data); // TODO handle error case
}
Self::from_data(&data)
}
/// Create a bitmap from some data
pub fn from_data(file_data: &[u8]) -> Self {
let get = |i: usize| -> u8 {
match file_data.get(i) {
Some(byte) => *byte,
None => 0,
}
};
let getw = |i: usize| -> u16 { (get(i) as u16) + ((get(i + 1) as u16) << 8) };
let getd = |i: usize| -> u32 {
(get(i) as u32) + ((get(i + 1) as u32) << 8) + ((get(i + 2) as u32) << 16) +
((get(i + 3) as u32) << 24)
};
let gets = |start: usize, len: usize| -> String {
(start..start + len).map(|i| get(i) as char).collect::<String>()
};
let mut ret: BmpFile;
if gets(0, 2) == "BM" {
// let file_size = getd(2);
let offset = getd(0xA);
// let header_size = getd(0xE);
let width = getd(0x12);
let height = getd(0x16);
let depth = getw(0x1C) as u32;
let bytes = (depth + 7) / 8;
let row_bytes = (depth * width + 31) / 32 * 4;
let mut blue_mask = 0xFF;
let mut green_mask = 0xFF00;
let mut red_mask = 0xFF0000;
let mut alpha_mask = 0xFF000000;
if getd(0x1E) == 3 {
red_mask = getd(0x36);
green_mask = getd(0x3A);
blue_mask = getd(0x3E);
alpha_mask = getd(0x42);
}
let mut blue_shift = 0;
while blue_mask > 0 && blue_shift < 32 && (blue_mask >> blue_shift) & 1 == 0 {
blue_shift += 1;
}
let mut green_shift = 0;
while green_mask > 0 && green_shift < 32 && (green_mask >> green_shift) & 1 == 0 {
green_shift += 1;
}
let mut red_shift = 0;
while red_mask > 0 && red_shift < 32 && (red_mask >> red_shift) & 1 == 0 {
red_shift += 1;
}
let mut alpha_shift = 0;
while alpha_mask > 0 && alpha_shift < 32 && (alpha_mask >> alpha_shift) & 1 == 0 {
alpha_shift += 1;
}
ret = Self::new(width as usize, height as usize);
for y in 0..height {
for x in 0..width {
let pixel_offset = offset + (height - y - 1) * row_bytes + x * bytes;
let pixel_data = getd(pixel_offset as usize);
let red = ((pixel_data & red_mask) >> red_shift) as u8;
let green = ((pixel_data & green_mask) >> green_shift) as u8;
let blue = ((pixel_data & blue_mask) >> blue_shift) as u8;
let alpha = ((pixel_data & alpha_mask) >> alpha_shift) as u8;
if bytes == 3 {
ret.data.push(Color::rgb(red, green, blue));
} else if bytes == 4 {
ret.data.push(Color::rgba(red, green, blue, alpha));
}
}
}
} else {
ret = Self::default();
}
ret
}
/// Indicate if data exists
pub fn has_data(&self) -> bool {
!self.data.is_empty()
}
pub fn width(&self) -> usize {
self.w
}
pub fn height(&self) -> usize {
self.h
}
}
impl Deref for BmpFile {
type Target = [Color];
/// Convert to slice for drawing
fn deref(&self) -> &Self::Target {
&self.data
}
}
#![crate_name="orbclient"]
#![crate_type="lib"]
#![feature(associated_consts)]
#![feature(const_fn)]
#![feature(box_syntax)]
#![deny(warnings)]
......@@ -10,12 +8,10 @@ extern crate core;
pub static FONT: &'static [u8] = include_bytes!("../res/unifont.font");
pub use bmp::BmpFile;
pub use color::Color;
pub use event::*;
pub use window::Window;
pub mod bmp;
pub mod color;
pub mod event;
......
......@@ -212,18 +212,6 @@ impl Window {
}
}
/// Display an image stored in a bmp::BmpFile
pub fn image_bmp(&mut self, start_x: i32, start_y: i32, bmp: &super::bmp::BmpFile) {
use std::ops::Deref;
let w = bmp.width() as u32;
let h = bmp.height() as u32;
let data = bmp.deref();
self.image(start_x, start_y, w, h, data);
}
fn convert_scancode(&self, scancode_option: Option<sdl2::keyboard::Scancode>, shift: bool) -> Option<(char, u8)> {
if let Some(scancode) = scancode_option {
match scancode {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment