Move painter modules to sub module
This commit is contained in:
48
src/painter/handle.rs
Normal file
48
src/painter/handle.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
extern crate image;
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::thread::JoinHandle;
|
||||
|
||||
use image::DynamicImage;
|
||||
|
||||
use rect::Rect;
|
||||
|
||||
|
||||
|
||||
/// A handle to a painter thread.
|
||||
///
|
||||
/// This also holds a channel to the painter thread,
|
||||
/// to allow image updates to be pushed to the thread.
|
||||
pub struct Handle {
|
||||
#[allow(dead_code)]
|
||||
thread: JoinHandle<u32>,
|
||||
area: Rect,
|
||||
image_sender: Sender<DynamicImage>,
|
||||
}
|
||||
|
||||
impl Handle {
|
||||
/// Create a new handle from the given properties.
|
||||
pub fn new(thread: JoinHandle<u32>, area: Rect, image_sender: Sender<DynamicImage>) -> Handle {
|
||||
Handle {
|
||||
thread,
|
||||
area,
|
||||
image_sender,
|
||||
}
|
||||
}
|
||||
|
||||
/// Push an image update.
|
||||
pub fn update_image(&self, full_image: &mut DynamicImage) {
|
||||
// Crop the image to the area
|
||||
let image = full_image.crop(
|
||||
self.area.x,
|
||||
self.area.y,
|
||||
self.area.w,
|
||||
self.area.h,
|
||||
);
|
||||
|
||||
// Push a new image to the thread
|
||||
// TODO: return this result
|
||||
self.image_sender.send(image)
|
||||
.expect("Failed to send image update to painter");
|
||||
}
|
||||
}
|
||||
3
src/painter/mod.rs
Normal file
3
src/painter/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
// Re-export modules
|
||||
pub mod painter;
|
||||
pub mod handle;
|
||||
82
src/painter/painter.rs
Normal file
82
src/painter/painter.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use std::io::Error;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use image::{DynamicImage, Pixel};
|
||||
|
||||
use app::PAINTER_IMAGE_WAIT_DELAY_MILLIS;
|
||||
use color::Color;
|
||||
use pix::client::Client;
|
||||
use rect::Rect;
|
||||
|
||||
|
||||
|
||||
/// A painter that paints on a pixelflut panel.
|
||||
pub struct Painter {
|
||||
client: Client,
|
||||
area: Rect,
|
||||
offset: (u32, u32),
|
||||
image: Option<DynamicImage>,
|
||||
}
|
||||
|
||||
impl Painter {
|
||||
/// Create a new painter.
|
||||
pub fn new(client: Client, area: Rect, offset: (u32, u32), image: Option<DynamicImage>) -> Painter {
|
||||
Painter {
|
||||
client,
|
||||
area,
|
||||
offset,
|
||||
image,
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform work.
|
||||
/// Paint the whole defined area.
|
||||
pub fn work(&mut self) -> Result<(), Error> {
|
||||
// Make sure there is an image
|
||||
if self.image.is_none() {
|
||||
// Show a warning
|
||||
println!("Painter thread has no image yet to paint, waiting...");
|
||||
|
||||
// Sleep a little
|
||||
thread::sleep(Duration::from_millis(PAINTER_IMAGE_WAIT_DELAY_MILLIS));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Get an RGB image
|
||||
let image = self.image.as_mut().unwrap().to_rgb();
|
||||
|
||||
// Loop through all the pixels, and set their color
|
||||
for x in 0..self.area.w {
|
||||
for y in 0..self.area.h {
|
||||
// Get the pixel at this location
|
||||
let pixel = image.get_pixel(x, y);
|
||||
|
||||
// Get the channels
|
||||
let channels = pixel.channels();
|
||||
|
||||
// Define the color
|
||||
let color = Color::from(
|
||||
channels[0],
|
||||
channels[1],
|
||||
channels[2],
|
||||
);
|
||||
|
||||
// Set the pixel
|
||||
self.client.write_pixel(
|
||||
x + self.area.x + self.offset.0,
|
||||
y + self.area.y + self.offset.1,
|
||||
&color,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Everything seems to be ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Update the image that should be painted
|
||||
pub fn set_image(&mut self, image: DynamicImage) {
|
||||
self.image = Some(image);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user