Skip to content
Snippets Groups Projects
Commit f5236d32 authored by John Hodge's avatar John Hodge
Browse files

Usermode libwtk - Clean up some old code

parent 515528d6
No related merge requests found
...@@ -135,11 +135,18 @@ impl_conv! { ...@@ -135,11 +135,18 @@ impl_conv! {
fn get_4_bytes<F: ::std::io::Read>(f: &mut F) -> Result<[u8; 4], ::std::io::Error> { fn get_4_bytes<F: ::std::io::Read>(f: &mut F) -> Result<[u8; 4], ::std::io::Error> {
let mut rv = [0; 4]; let mut rv = [0; 4];
if try!(f.read(&mut rv)) != 4 { if f.read(&mut rv)? != 4 {
todo!("Handle unexpected EOF in get_4_bytes"); todo!("Handle unexpected EOF in get_4_bytes");
} }
Ok( rv ) Ok( rv )
} }
fn get_fixed_vec<F: ::std::io::Read>(f: &mut F, size: usize) -> Result<Vec<u8>, ::std::io::Error> {
let mut data: Vec<u8> = (0 .. size).map(|_| 0u8).collect();
if f.read(&mut data)? != size {
todo!("Handle unexpected EOF in get_fixed_vec");
}
Ok( data )
}
/// Full-colour raster image /// Full-colour raster image
pub struct RasterRGB pub struct RasterRGB
...@@ -156,17 +163,16 @@ impl RasterRGB ...@@ -156,17 +163,16 @@ impl RasterRGB
use ::byteorder::{LittleEndian,ReadBytesExt}; use ::byteorder::{LittleEndian,ReadBytesExt};
use std::io::Read; use std::io::Read;
let path = path.as_ref(); let path = path.as_ref();
let mut file = ::std::io::BufReader::new( try!( ::std::fs::File::open(path) ) ); let mut file = ::std::io::BufReader::new( ::std::fs::File::open(path)? );
// - Check magic // - Check magic
let magic = try!(get_4_bytes(&mut file)); let magic = get_4_bytes(&mut file)?;
if &magic == b"\x7FR24" { if &magic == b"\x7FR24" {
// - Read dimensions // - Read dimensions
let w = try!( file.read_u16::<LittleEndian>() ) as usize; let w = file.read_u16::<LittleEndian>()? as usize;
let h = try!( file.read_u16::<LittleEndian>() ) as usize; let h = file.read_u16::<LittleEndian>()? as usize;
kernel_log!("w = {}, h = {}", w, h); kernel_log!("w = {}, h = {}", w, h);
// - Read data // - Read data
let mut data: Vec<u8> = (0 .. w*h*3).map(|_| 0u8).collect(); let data = get_fixed_vec(&mut file, w*h*3)?;
try!(file.read(&mut data));
Ok(RasterRGB { Ok(RasterRGB {
width: w, width: w,
...@@ -175,8 +181,8 @@ impl RasterRGB ...@@ -175,8 +181,8 @@ impl RasterRGB
} }
else if &magic == b"\x7FR\x18R" { else if &magic == b"\x7FR\x18R" {
// - Read dimensions // - Read dimensions
let w = try!( file.read_u16::<LittleEndian>() ) as usize; let w = file.read_u16::<LittleEndian>()? as usize;
let h = try!( file.read_u16::<LittleEndian>() ) as usize; let h = file.read_u16::<LittleEndian>()? as usize;
kernel_log!("w = {}, h = {}", w, h); kernel_log!("w = {}, h = {}", w, h);
let size = w*h*3; let size = w*h*3;
let mut data: Vec<u8> = (0 .. size).map(|_| 0u8).collect(); let mut data: Vec<u8> = (0 .. size).map(|_| 0u8).collect();
...@@ -184,10 +190,12 @@ impl RasterRGB ...@@ -184,10 +190,12 @@ impl RasterRGB
while pos < size while pos < size
{ {
let count_u8 = try!(file.read_u8()); let count_u8 = file.read_u8()?;
let px_buf = { let px_buf = {
let mut buf = [0; 3]; let mut buf = [0; 3];
try!( file.read(&mut buf) ) == 3; if file.read(&mut buf)? != 3 {
panic!("TODO: Handle unexpected EOF when parsing RLE");
}
buf buf
}; };
for _ in 0 .. count_u8 { for _ in 0 .. count_u8 {
...@@ -239,19 +247,17 @@ impl RasterMonoA ...@@ -239,19 +247,17 @@ impl RasterMonoA
} }
pub fn new<P: AsRef<::std::fs::Path>>(path: P, fg: ::surface::Colour) -> Result<RasterMonoA,LoadError> { pub fn new<P: AsRef<::std::fs::Path>>(path: P, fg: ::surface::Colour) -> Result<RasterMonoA,LoadError> {
use ::byteorder::{LittleEndian,ReadBytesExt}; use ::byteorder::{LittleEndian,ReadBytesExt};
use std::io::Read;
let path = path.as_ref(); let path = path.as_ref();
let mut file = try!( ::std::fs::File::open(path) ); let mut file = ::std::fs::File::open(path)?;
// - Check magic // - Check magic
if &try!(get_4_bytes(&mut file)) != b"\x7FR8M" { if &get_4_bytes(&mut file)? != b"\x7FR8M" {
return Err(LoadError::Malformed); return Err(LoadError::Malformed);
} }
// - Read dimensions // - Read dimensions
let w = try!( file.read_u16::<LittleEndian>() ) as usize; let w = file.read_u16::<LittleEndian>()? as usize;
let h = try!( file.read_u16::<LittleEndian>() ) as usize; let h = file.read_u16::<LittleEndian>()? as usize;
// - Read data (directly) // - Read data (directly)
let mut alpha: Vec<u8> = (0 .. w*h).map(|_| 0u8).collect(); let alpha = get_fixed_vec(&mut file, w*h)?;
try!(file.read(&mut alpha));
Ok(RasterMonoA { Ok(RasterMonoA {
fg: fg, fg: fg,
width: w, width: w,
...@@ -292,20 +298,20 @@ impl RasterBiA ...@@ -292,20 +298,20 @@ impl RasterBiA
pub fn new<P: AsRef<::std::fs::Path>>(path: P, fg: ::surface::Colour, bg: ::surface::Colour) -> Result<RasterBiA,LoadError> { pub fn new<P: AsRef<::std::fs::Path>>(path: P, fg: ::surface::Colour, bg: ::surface::Colour) -> Result<RasterBiA,LoadError> {
use ::byteorder::{LittleEndian,ReadBytesExt}; use ::byteorder::{LittleEndian,ReadBytesExt};
let path = path.as_ref(); let path = path.as_ref();
let mut file = try!( ::std::fs::File::open(path) ); let mut file = ::std::fs::File::open(path)?;
// - Check magic // - Check magic
if &try!(get_4_bytes(&mut file)) != b"\x7FR8B" { if &get_4_bytes(&mut file)? != b"\x7FR8B" {
return Err(LoadError::Malformed); return Err(LoadError::Malformed);
} }
// - Read dimensions // - Read dimensions
let w = try!( file.read_u16::<LittleEndian>() ) as usize; let w = file.read_u16::<LittleEndian>()? as usize;
let h = try!( file.read_u16::<LittleEndian>() ) as usize; let h = file.read_u16::<LittleEndian>()? as usize;
let mut data = Vec::with_capacity(w * h); let mut data = Vec::with_capacity(w * h);
let mut alpha = Vec::with_capacity(w * h); let mut alpha = Vec::with_capacity(w * h);
for _ in 0 .. w * h for _ in 0 .. w * h
{ {
let v = try!( file.read_u8() ); let v = file.read_u8()?;
data.push( v >= 128 ); data.push( v >= 128 );
alpha.push( (v & 0x7F) * 2 | ((v >> 6) & 1) ); alpha.push( (v & 0x7F) * 2 | ((v >> 6) & 1) );
} }
......
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