Commit f9e8df2c authored by robbycerantola's avatar robbycerantola

fast_image()

parent bb3fb09e
extern crate orbclient;
extern crate time;
use orbclient::{Color, Window, Renderer, EventOption, GraphicsPath};
use orbclient::{Color, Window, Renderer, EventOption};
const TIMES:i32 = 10;
fn main() {
let (width, height) = orbclient::get_display_size().unwrap();
......@@ -10,33 +12,41 @@ fn main() {
(height as i32)/4,
width/2,
height/2,
"TITLE")
"IMAGE BENCHMARK")
.unwrap();
let (win_w, win_h) = (width/2, height/2);
window.set(Color::rgb(255,255,255));
//create image data : a green square
let data = vec![Color::rgb(100,200,10);360000];
let data = vec![Color::rgba(100,200,10,2);200000];
let data2 = vec![Color::rgba(200,100,10,2);200000];
//draw image sequentially
let mut t = time::now();
for i in 1..100 {
window.image(10,10,600,600, &data[..]);
for _i in 1..TIMES {
window.image(10,10,500,400, &data[..]);
}
println!("{:?}", time::now()-t);
println!("image {:?}", time::now()-t);
//draw image parallelizing
t = time::now();
for i in 1..100 {
window.image_par(30,30,600,600, &data[..]);
for _i in 1..TIMES {
window.image_par(20,20,500,400, &data[..]);
}
println!("{:?}", time::now()-t);
println!("image_par {:?}", time::now()-t);
t = time::now();
for i in 1..100 {
window.image_blit(30,30,600,600, &data[..]);
for _i in 1..TIMES {
window.image_fast(30,30,500,400, &data[..]);
}
println!("{:?}", time::now()-t);
println!("image_fast {:?}", time::now()-t);
t = time::now();
for _i in 1..TIMES {
window.image_veryfast(40,40,500,400, &data2[..]);
}
println!("image_veryfast{:?}", time::now()-t);
window.sync();
'events: loop {
......
......@@ -354,10 +354,84 @@ pub trait Renderer {
}
fn image_blit (&mut self, _start_x: i32, _start_y: i32, _w: u32, _h: u32, _image_data: &[Color]) {
//let window_data = self.data_mut();
// *window_data = image_data[..];
return
fn image_fast (&mut self, start_x: i32, start_y: i32, w: u32, _h: u32, image_data: &[Color]) {
let window_w = self.width() as usize;
let window_len = self.data().len();
let data = self.data_mut();
let w = w as usize;
(0..image_data.len())
.map(|i| {
let y0 = i/w;
let y = y0 + start_y as usize ;
let x = start_x as usize + i-(y0*w) ;
let window_index = y * window_w + x;
if window_index <= window_len {
let new = image_data[i].data;
let alpha = (new >> 24) & 0xFF;
if alpha > 0 {
let old = unsafe{ &mut data[window_index].data};
if alpha >= 255 {
*old = new;
} else {
let n_r = (((new >> 16) & 0xFF) * alpha) >> 8;
let n_g = (((new >> 8) & 0xFF) * alpha) >> 8;
let n_b = ((new & 0xFF) * alpha) >> 8;
let n_alpha = 255 - alpha;
let o_a = (((*old >> 24) & 0xFF) * n_alpha) >> 8;
let o_r = (((*old >> 16) & 0xFF) * n_alpha) >> 8;
let o_g = (((*old >> 8) & 0xFF) * n_alpha) >> 8;
let o_b = ((*old & 0xFF) * n_alpha) >> 8;
*old = ((o_a << 24) | (o_r << 16) | (o_g << 8) | o_b) + ((alpha << 24) | (n_r << 16) | (n_g << 8) | n_b);
}
}
}
})
.count();
}
fn image_veryfast (&mut self, start_x: i32, start_y: i32, w: u32, _h: u32, image_data: &[Color]) {
let window_w = self.width() as usize;
let window_len = self.data().len();
let data = self.data_mut();
let w = w as usize;
let start_x = start_x as usize;
let start_y = start_y as usize;
for i in 0..image_data.len() {
let y0 = i/w;
let y = y0 + start_y;
let x = start_x + i-(y0*w) ;
let window_index = y * window_w + x;
if window_index <= window_len {
let new = image_data[i].data;
let alpha = (new >> 24) & 0xFF;
if alpha > 0 {
let old = unsafe{ &mut data[window_index].data};
if alpha >= 255 {
*old = new;
} else {
let n_r = (((new >> 16) & 0xFF) * alpha) >> 8;
let n_g = (((new >> 8) & 0xFF) * alpha) >> 8;
let n_b = ((new & 0xFF) * alpha) >> 8;
let n_alpha = 255 - alpha;
let o_a = (((*old >> 24) & 0xFF) * n_alpha) >> 8;
let o_r = (((*old >> 16) & 0xFF) * n_alpha) >> 8;
let o_g = (((*old >> 8) & 0xFF) * n_alpha) >> 8;
let o_b = ((*old & 0xFF) * n_alpha) >> 8;
*old = ((o_a << 24) | (o_r << 16) | (o_g << 8) | o_b) + ((alpha << 24) | (n_r << 16) | (n_g << 8) | n_b);
}
}
}
}
}
/// Draw a linear gradient in a rectangular region
......
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