mappings: split coord mapping from pixel mapping
This commit is contained in:
parent
d28c2a1a4c
commit
9289a829be
@ -4,55 +4,16 @@ use crate::lib8::interpolate::scale8;
|
|||||||
|
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
use std::fmt::{Formatter, Debug};
|
use std::fmt::{Formatter, Debug};
|
||||||
use ansi_term::Color;
|
|
||||||
use rgb::Rgb;
|
|
||||||
|
|
||||||
pub trait CoordinateView: Debug {
|
pub trait CoordinateView<'a>: Debug {
|
||||||
type Space: CoordinateSpace;
|
type Space: CoordinateSpace;
|
||||||
fn next(&mut self) -> Option<(Coordinates<Virtual>, Coordinates<Self::Space>)>;
|
fn next(&mut self) -> Option<(Coordinates<Virtual>, Coordinates<Self::Space>)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PixelMapping {
|
pub trait Select<'a> {
|
||||||
type Space: CoordinateSpace;
|
type Space: CoordinateSpace;
|
||||||
fn select(&self, rect: &Rectangle<Virtual>) -> impl CoordinateView<Space = Self::Space>;
|
type View: CoordinateView<'a>;
|
||||||
fn to_idx(&self, coords: &Coordinates<Self::Space>) -> usize;
|
fn select(&'a self, rect: &Rectangle<Virtual>) -> Self::View;
|
||||||
}
|
|
||||||
|
|
||||||
pub trait DisplayDump<T: PixelMapping> {
|
|
||||||
fn dump(&self, map: &T);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const PIXEL_NUM: usize> DisplayDump<LinearPixelMapping> for [Rgb<u8>; PIXEL_NUM] {
|
|
||||||
fn dump(&self, _map: &LinearPixelMapping) {
|
|
||||||
for ref pixel in self {
|
|
||||||
print!("{}", Color::RGB(pixel.r, pixel.g, pixel.b).paint("█"));
|
|
||||||
}
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const PIXEL_NUM: usize> DisplayDump<StrideMapping> for [Rgb<u8>; PIXEL_NUM] {
|
|
||||||
fn dump(&self, map: &StrideMapping) {
|
|
||||||
for y in 0..map.stride_count {
|
|
||||||
let stride = &map.strides[y];
|
|
||||||
for x in 0..stride.length {
|
|
||||||
let idx = if stride.reverse {
|
|
||||||
stride.physical_idx + stride.length as usize - x as usize
|
|
||||||
} else {
|
|
||||||
stride.physical_idx + x as usize
|
|
||||||
};
|
|
||||||
if idx >= self.len() {
|
|
||||||
println!();
|
|
||||||
println!("frame!!!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let pixel = &self[idx];
|
|
||||||
print!("{}", Color::RGB(pixel.r, pixel.g, pixel.b).paint("█"));
|
|
||||||
}
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
println!("frame!!!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -68,7 +29,7 @@ impl CoordinateSpace for LinearSpace {
|
|||||||
|
|
||||||
pub type LinearCoords = Coordinates<LinearSpace>;
|
pub type LinearCoords = Coordinates<LinearSpace>;
|
||||||
|
|
||||||
impl CoordinateView for LinearCoordView {
|
impl<'a> CoordinateView<'a> for LinearCoordView {
|
||||||
type Space = LinearSpace;
|
type Space = LinearSpace;
|
||||||
fn next(&mut self) -> Option<(VirtualCoordinates, LinearCoords)> {
|
fn next(&mut self) -> Option<(VirtualCoordinates, LinearCoords)> {
|
||||||
if self.idx as u8 == self.rect.bottom_right.x {
|
if self.idx as u8 == self.rect.bottom_right.x {
|
||||||
@ -94,18 +55,15 @@ impl LinearPixelMapping {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PixelMapping for LinearPixelMapping {
|
impl<'a> Select<'a> for LinearPixelMapping {
|
||||||
type Space = LinearSpace;
|
type Space = LinearSpace;
|
||||||
fn select(&self, rect: &Rectangle<Virtual>) -> impl CoordinateView<Space = Self::Space> {
|
type View = LinearCoordView;
|
||||||
|
fn select(&'a self, rect: &Rectangle<Virtual>) -> Self::View {
|
||||||
LinearCoordView {
|
LinearCoordView {
|
||||||
rect: rect.clone(),
|
rect: rect.clone(),
|
||||||
idx: 0,
|
idx: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_idx(&self, coords: &Coordinates<LinearSpace>) -> usize {
|
|
||||||
coords.x
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
@ -190,7 +148,7 @@ impl<const STRIDE_NUM: usize> StrideMapping<STRIDE_NUM> {
|
|||||||
let mut strides = [Stride::default(); STRIDE_NUM];
|
let mut strides = [Stride::default(); STRIDE_NUM];
|
||||||
let stride_count = stride_json.len();
|
let stride_count = stride_json.len();
|
||||||
let mut physical_idx = 0;
|
let mut physical_idx = 0;
|
||||||
let mut size: Option<Rectangle<StrideSpace>> = None;//Rectangle::new(Coordinates::new(usize::MAX, usize::MAX), Coordinates::new(0, 0));
|
let mut size: Option<Rectangle<StrideSpace>> = None;
|
||||||
for stride_idx in 0..stride_count {
|
for stride_idx in 0..stride_count {
|
||||||
let json_data = stride_json[stride_idx];
|
let json_data = stride_json[stride_idx];
|
||||||
let x = json_data.0;
|
let x = json_data.0;
|
||||||
@ -237,15 +195,12 @@ impl<const STRIDE_NUM: usize> StrideMapping<STRIDE_NUM> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PixelMapping for StrideMapping {
|
impl<'a> Select<'a> for StrideMapping {
|
||||||
type Space = StrideSpace;
|
type Space = StrideSpace;
|
||||||
fn select(&self, rect: &Rectangle<Virtual>) -> impl CoordinateView<Space = StrideSpace> {
|
type View = StrideView<'a>;
|
||||||
|
fn select(&'a self, rect: &Rectangle<Virtual>) -> Self::View {
|
||||||
StrideView::new(self, rect)
|
StrideView::new(self, rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_idx(&self, coords: &Coordinates<StrideSpace>) -> usize {
|
|
||||||
self.strides[coords.x as usize].pixel_idx_for_offset(coords.y)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@ -255,8 +210,8 @@ impl CoordinateSpace for StrideSpace {
|
|||||||
}
|
}
|
||||||
pub type StrideCoords = Coordinates<StrideSpace>;
|
pub type StrideCoords = Coordinates<StrideSpace>;
|
||||||
|
|
||||||
struct StrideView<'a> {
|
pub struct StrideView<'a> {
|
||||||
map: &'a StrideMapping,
|
pub map: &'a StrideMapping,
|
||||||
range: Rectangle<StrideSpace>,
|
range: Rectangle<StrideSpace>,
|
||||||
cur: StrideCoords,
|
cur: StrideCoords,
|
||||||
step_size: VirtualCoordinates
|
step_size: VirtualCoordinates
|
||||||
@ -310,7 +265,7 @@ impl<'a> StrideView<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CoordinateView for StrideView<'a> {
|
impl<'a> CoordinateView<'a> for StrideView<'a> {
|
||||||
type Space = StrideSpace;
|
type Space = StrideSpace;
|
||||||
fn next(&mut self) -> Option<(VirtualCoordinates, StrideCoords)> {
|
fn next(&mut self) -> Option<(VirtualCoordinates, StrideCoords)> {
|
||||||
// Keep scanning until we reach the far right of the range
|
// Keep scanning until we reach the far right of the range
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use smart_leds_trait::SmartLedsWrite;
|
use smart_leds_trait::SmartLedsWrite;
|
||||||
|
|
||||||
use crate::lib8::Rgb8Blend;
|
|
||||||
use crate::lib8::interpolate::Fract8Ops;
|
use crate::lib8::interpolate::Fract8Ops;
|
||||||
use crate::render::{Surface, Display, Surfaces};
|
use crate::render::{Display, HardwarePixel, PixelView, Sample, Surface, Surfaces};
|
||||||
use crate::buffers::SurfacePool;
|
use crate::buffers::SurfacePool;
|
||||||
use crate::power::{brightness_for_mw, AsMilliwatts};
|
use crate::power::{brightness_for_mw, AsMilliwatts};
|
||||||
use crate::geometry::*;
|
use crate::geometry::*;
|
||||||
@ -10,13 +9,8 @@ use crate::mappings::*;
|
|||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use rgb::Rgb;
|
|
||||||
use std::ops::IndexMut;
|
use std::ops::IndexMut;
|
||||||
|
|
||||||
pub trait HardwarePixel: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
|
||||||
impl<T> HardwarePixel for T where T: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
|
||||||
|
|
||||||
pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
|
pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
|
||||||
type Pixel: HardwarePixel;
|
type Pixel: HardwarePixel;
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
@ -24,6 +18,38 @@ pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
|
|||||||
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = Self::Pixel> + Send;
|
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = Self::Pixel> + Send;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct StrideSampler<'a, P: Pixbuf> {
|
||||||
|
pixbuf: &'a mut P,
|
||||||
|
selection: StrideView<'a>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, P: Pixbuf> PixelView for StrideSampler<'a, P> {
|
||||||
|
type Pixel = P::Pixel;
|
||||||
|
fn next(&mut self) -> Option<(Coordinates<Virtual>, &mut Self::Pixel)> {
|
||||||
|
if let Some((virt, coords)) = self.selection.next() {
|
||||||
|
let idx = self.selection.map.strides[coords.x as usize].pixel_idx_for_offset(coords.y);
|
||||||
|
Some((virt, &mut self.pixbuf[idx]))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StrideOutput<P: Pixbuf> {
|
||||||
|
pixbuf: P,
|
||||||
|
stride_map: StrideMapping
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Pixbuf> Sample for StrideOutput<P> {
|
||||||
|
type Pixel = P::Pixel;
|
||||||
|
fn sample(&mut self, rect: &Rectangle<Virtual>) -> impl PixelView<Pixel = Self::Pixel> {
|
||||||
|
StrideSampler {
|
||||||
|
pixbuf: &mut self.pixbuf,
|
||||||
|
selection: self.stride_map.select(rect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
|
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
|
||||||
type Pixel = T;
|
type Pixel = T;
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
@ -41,9 +67,8 @@ impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
|
|||||||
|
|
||||||
struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
|
struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
|
||||||
surfaces : Option<SurfacePool<S>>,
|
surfaces : Option<SurfacePool<S>>,
|
||||||
pixmap: StrideMapping,
|
output: StrideOutput<P>,
|
||||||
target: T,
|
target: T,
|
||||||
pixbuf: P,
|
|
||||||
max_mw: u32,
|
max_mw: u32,
|
||||||
frame: usize
|
frame: usize
|
||||||
}
|
}
|
||||||
@ -51,7 +76,7 @@ struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
|
|||||||
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> Debug for SmartLedDisplay<T, S, P> {
|
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> Debug for SmartLedDisplay<T, S, P> {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
|
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
|
||||||
f.debug_struct("SmartLedDisplay")
|
f.debug_struct("SmartLedDisplay")
|
||||||
.field("total_mw", &self.pixbuf.as_milliwatts())
|
.field("total_mw", &self.output.pixbuf.as_milliwatts())
|
||||||
.field("surfaces", &self.surfaces)
|
.field("surfaces", &self.surfaces)
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
@ -60,11 +85,10 @@ impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> Debug for SmartLedDi
|
|||||||
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> SmartLedDisplay<T, S, P> {
|
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> SmartLedDisplay<T, S, P> {
|
||||||
fn new(target: T, max_mw: u32, pixmap: StrideMapping, pixbuf: P) -> Self {
|
fn new(target: T, max_mw: u32, pixmap: StrideMapping, pixbuf: P) -> Self {
|
||||||
SmartLedDisplay {
|
SmartLedDisplay {
|
||||||
pixbuf,
|
output: StrideOutput { pixbuf: pixbuf, stride_map: pixmap },
|
||||||
surfaces: Some(SurfacePool::new()),
|
surfaces: Some(SurfacePool::new()),
|
||||||
target,
|
target,
|
||||||
max_mw,
|
max_mw,
|
||||||
pixmap,
|
|
||||||
frame: 0
|
frame: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,20 +112,19 @@ T: FastWrite,
|
|||||||
S: Surface,
|
S: Surface,
|
||||||
P: Pixbuf<Pixel = T::Color> {
|
P: Pixbuf<Pixel = T::Color> {
|
||||||
fn start_frame(&mut self) {
|
fn start_frame(&mut self) {
|
||||||
self.pixbuf.blank();
|
self.output.pixbuf.blank();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_frame(&mut self) {
|
fn render_frame(&mut self) {
|
||||||
let surfaces = self.surfaces.take().unwrap();
|
let surfaces = self.surfaces.take().unwrap();
|
||||||
for surface in surfaces.iter() {
|
for surface in surfaces.iter() {
|
||||||
let rect = surface.rect().clone();
|
let rect = surface.rect();
|
||||||
let mut sel = self.pixmap.select(&rect);
|
|
||||||
let opacity = surface.opacity();
|
let opacity = surface.opacity();
|
||||||
if opacity > 0 {
|
if opacity > 0 {
|
||||||
|
let mut sample = self.output.sample(&rect);
|
||||||
surface.with_shader(|shader| {
|
surface.with_shader(|shader| {
|
||||||
while let Some((virt_coords, phys_coords)) = sel.next() {
|
while let Some((virt_coords, pixel)) = sample.next() {
|
||||||
let idx = self.pixmap.to_idx(&phys_coords);
|
*pixel = pixel.blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
|
||||||
self.pixbuf[idx] = self.pixbuf[idx].blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -110,10 +133,10 @@ P: Pixbuf<Pixel = T::Color> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn end_frame(&mut self) {
|
fn end_frame(&mut self) {
|
||||||
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
|
let b = brightness_for_mw(self.output.pixbuf.as_milliwatts(), 255, self.max_mw);
|
||||||
if let Err(_) = self.target.fast_write(self.pixbuf.iter_with_brightness(b)) {
|
if self.target.fast_write(self.output.pixbuf.iter_with_brightness(b)).is_err() {
|
||||||
panic!("Could not write frame!");
|
panic!("Could not write frame!");
|
||||||
}
|
};
|
||||||
self.frame += 1;
|
self.frame += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,30 @@ use std::io;
|
|||||||
use rgb::Rgb;
|
use rgb::Rgb;
|
||||||
|
|
||||||
use crate::geometry::*;
|
use crate::geometry::*;
|
||||||
|
use crate::lib8::Rgb8Blend;
|
||||||
|
use crate::lib8::interpolate::Fract8Ops;
|
||||||
|
use crate::power::AsMilliwatts;
|
||||||
use crate::task::Task;
|
use crate::task::Task;
|
||||||
use crate::time::Periodically;
|
use crate::time::Periodically;
|
||||||
use running_average::RealTimeRunningAverage;
|
use running_average::RealTimeRunningAverage;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
pub trait HardwarePixel: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
||||||
|
impl<T> HardwarePixel for T where T: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
||||||
|
|
||||||
|
pub trait PixelView {
|
||||||
|
type Pixel: HardwarePixel;
|
||||||
|
fn next(&mut self) -> Option<(Coordinates<Virtual>, &mut Self::Pixel)>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Sample {
|
||||||
|
type Pixel: HardwarePixel;
|
||||||
|
|
||||||
|
fn sample(&mut self, rect: &Rectangle<Virtual>) -> impl PixelView<Pixel = Self::Pixel>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub trait Shader: Send + Debug {
|
pub trait Shader: Send + Debug {
|
||||||
fn draw(&self, surface_coords: &VirtualCoordinates, frame: usize) -> Rgb<u8>;
|
fn draw(&self, surface_coords: &VirtualCoordinates, frame: usize) -> Rgb<u8>;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user