From 86eda32f9d8fed77136ffd3495e93c4c37e7671b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Dec 2022 09:06:30 +0100 Subject: debug draw --- evc/src/bin/decode.rs | 33 ++++++++++++++++++++++++++++++--- evc/src/bin/encode.rs | 12 +++++++++--- evc/src/block.rs | 6 ++++++ evc/src/debug.rs | 21 +++++++++++++++++++++ evc/src/frame.rs | 1 + evc/src/lib.rs | 1 + 6 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 evc/src/debug.rs (limited to 'evc') diff --git a/evc/src/bin/decode.rs b/evc/src/bin/decode.rs index af6edc6..44fcbe4 100644 --- a/evc/src/bin/decode.rs +++ b/evc/src/bin/decode.rs @@ -5,6 +5,7 @@ use evc::{ block::{Block, BlockInner}, frame::Frame, header::Header, + pixel::Pixel, ser::Source, view::View, }; @@ -12,10 +13,13 @@ use std::io::{BufReader, BufWriter}; #[derive(Parser)] #[clap(about, version)] -pub struct DecodeArgs {} +pub struct DecodeArgs { + #[arg(long)] + debug: bool, +} fn main() -> anyhow::Result<()> { - let _args = DecodeArgs::parse(); + let args = DecodeArgs::parse(); let mut input = BufReader::new(std::io::stdin()); let mut output = BufWriter::new(std::io::stdout()); @@ -36,7 +40,14 @@ fn main() -> anyhow::Result<()> { prev.view((0, 0), size), ); - frame.write(&mut output).context("writing raw frame")?; + if args.debug { + let mut f2 = frame.clone(); + draw_debug(&block, f2.view_mut((0, 0), size)); + f2.write(&mut output).context("writing raw frame")?; + } else { + frame.write(&mut output).context("writing raw frame")?; + } + prev = frame; } drop(input); @@ -55,3 +66,19 @@ fn blit_block(block: &Block, mut target: View<&mut Frame>, prev: View<&Frame>) { BlockInner::Reference { translation: _ } => target.copy_from(&prev), } } + +fn draw_debug(block: &Block, mut target: View<&mut Frame>) { + match &block.inner { + BlockInner::Literal(_) => { + target.draw_box(Pixel::GREEN); + } + BlockInner::Split(box [a, b]) => { + let [at, bt] = target.split_mut_unsafe(); + draw_debug(a, at); + draw_debug(b, bt); + } + BlockInner::Reference { translation: _ } => { + target.draw_box(Pixel::BLUE); + } + } +} diff --git a/evc/src/bin/encode.rs b/evc/src/bin/encode.rs index 1934e26..3e34a3c 100644 --- a/evc/src/bin/encode.rs +++ b/evc/src/bin/encode.rs @@ -54,7 +54,7 @@ fn main() -> anyhow::Result<()> { fn encode_block(view: View<&Frame>, prev: View<&Frame>) -> Block { let diff = View::diff(&view, &prev) / view.area() as f64; // eprintln!("{:?} {diff}", view.size); - let inner = if diff < 0.5 { + let inner = if diff < 0.9 { BlockInner::Reference { translation: (0, 0), } @@ -62,9 +62,15 @@ fn encode_block(view: View<&Frame>, prev: View<&Frame>) -> Block { if view.size.0 < 32 { BlockInner::Literal(view.pixels()) } else { - let [a, b] = view.split(); + let [av, bv] = view.split(); let [ap, bp] = prev.split(); - BlockInner::Split(Box::new([encode_block(a, ap), encode_block(b, bp)])) + let a = encode_block(av, ap); + let b = encode_block(bv, bp); + if a.is_literal() && b.is_literal() { + BlockInner::Literal(view.pixels()) + } else { + BlockInner::Split(Box::new([a, b])) + } } }; diff --git a/evc/src/block.rs b/evc/src/block.rs index a3d040d..dae538c 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -67,3 +67,9 @@ impl Block { Ok(Self { size, inner }) } } + +impl Block { + pub fn is_literal(&self) -> bool { + matches!(self.inner, BlockInner::Literal(..)) + } +} diff --git a/evc/src/debug.rs b/evc/src/debug.rs new file mode 100644 index 0000000..44b51cf --- /dev/null +++ b/evc/src/debug.rs @@ -0,0 +1,21 @@ +use crate::{frame::Frame, pixel::Pixel, view::View}; + +impl View<&mut Frame> { + pub fn draw_box(&mut self, color: Pixel) { + let w = self.size.0; + let h = self.size.1; + for x in 0..w { + self[(x, 0)] = color; + self[(x, h - 1)] = color; + } + for y in 0..h { + self[(0, y)] = color; + self[(w - 1, y)] = color; + } + } +} +impl Pixel { + pub const RED: Pixel = Pixel { r: 255, g: 0, b: 0 }; + pub const GREEN: Pixel = Pixel { r: 0, g: 255, b: 0 }; + pub const BLUE: Pixel = Pixel { r: 0, g: 0, b: 255 }; +} diff --git a/evc/src/frame.rs b/evc/src/frame.rs index 16acb96..5c2adfb 100644 --- a/evc/src/frame.rs +++ b/evc/src/frame.rs @@ -5,6 +5,7 @@ use crate::{ }; use std::ops::{Index, IndexMut}; +#[derive(Debug, Clone)] pub struct Frame { pub size: (usize, usize), buffer: Vec>, diff --git a/evc/src/lib.rs b/evc/src/lib.rs index 4c504ef..65e8e2b 100644 --- a/evc/src/lib.rs +++ b/evc/src/lib.rs @@ -6,3 +6,4 @@ pub mod header; pub mod frame; pub mod pixel; pub mod view; +pub mod debug; -- cgit v1.2.3-70-g09d2