diff options
Diffstat (limited to 'evc/src')
-rw-r--r-- | evc/src/bin/decode.rs | 17 | ||||
-rw-r--r-- | evc/src/block.rs | 30 | ||||
-rw-r--r-- | evc/src/codec/decode.rs | 14 | ||||
-rw-r--r-- | evc/src/codec/encode.rs | 21 |
4 files changed, 28 insertions, 54 deletions
diff --git a/evc/src/bin/decode.rs b/evc/src/bin/decode.rs index 7089de5..01729ab 100644 --- a/evc/src/bin/decode.rs +++ b/evc/src/bin/decode.rs @@ -2,13 +2,8 @@ use anyhow::Context; use clap::Parser; use evc::{ - block::{Block, BlockInner}, - codec::decode::decode_block, - frame::Frame, - header::Header, - pixel::Pixel, - ser::Source, - view::View, + block::Block, codec::decode::decode_block, frame::Frame, header::Header, pixel::Pixel, + ser::Source, view::View, }; use log::info; use std::io::{BufReader, BufWriter}; @@ -54,16 +49,16 @@ fn main() -> anyhow::Result<()> { } fn draw_debug(block: &Block, mut target: View<&mut Frame>) { - match &block.inner { - BlockInner::Literal(_) => { + match &block { + Block::Literal(_) => { target.draw_box(Pixel::GREEN); } - BlockInner::Split(box [a, b]) => { + Block::Split(box [a, b]) => { let [at, bt] = target.split_mut_unsafe(); draw_debug(a, at); draw_debug(b, bt); } - BlockInner::Reference { translation: _ } => { + Block::Reference { translation: _ } => { target.draw_box(Pixel::BLUE); } } diff --git a/evc/src/block.rs b/evc/src/block.rs index dae538c..8daba5c 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -6,13 +6,7 @@ use crate::{ }; #[derive(Clone, Debug)] -pub struct Block { - pub size: (usize, usize), - pub inner: BlockInner, -} - -#[derive(Clone, Debug)] -pub enum BlockInner { +pub enum Block { Literal(Vec<Pixel>), Split(Box<[Block; 2]>), Reference { translation: (usize, usize) }, @@ -20,17 +14,17 @@ pub enum BlockInner { impl Block { pub fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { - match &self.inner { - BlockInner::Literal(pixels) => { + match &self { + Block::Literal(pixels) => { sink.put(0u8)?; pixels.write(sink)?; } - BlockInner::Split(box [a, b]) => { + Block::Split(box [a, b]) => { sink.put(1u8)?; a.write(sink)?; b.write(sink)?; } - BlockInner::Reference { translation: _ } => { + Block::Reference { translation: _ } => { sink.put(2u8)?; // sink.put(*translation)?; } @@ -39,9 +33,9 @@ impl Block { } pub fn read(source: &mut impl std::io::Read, size: (usize, usize)) -> anyhow::Result<Self> { - let inner = match source.get::<u8>()? { - 0 => BlockInner::Literal(source.get()?), - 1 => BlockInner::Split(Box::new({ + Ok(match source.get::<u8>()? { + 0 => Block::Literal(source.get()?), + 1 => Block::Split(Box::new({ let vert = size.0 > size.1; let asize = if vert { (size.0 / 2, size.1) @@ -58,18 +52,16 @@ impl Block { let b = Block::read(source, bsize)?; [a, b] })), - 2 => BlockInner::Reference { + 2 => Block::Reference { translation: (0, 0), //source.get()?, }, x => bail!("corrupt block type ({})", x), - }; - - Ok(Self { size, inner }) + }) } } impl Block { pub fn is_literal(&self) -> bool { - matches!(self.inner, BlockInner::Literal(..)) + matches!(self, Block::Literal(..)) } } diff --git a/evc/src/codec/decode.rs b/evc/src/codec/decode.rs index 9dc6a69..f4559c8 100644 --- a/evc/src/codec/decode.rs +++ b/evc/src/codec/decode.rs @@ -1,18 +1,14 @@ -use crate::{ - block::{Block, BlockInner}, - frame::Frame, - view::View, -}; +use crate::{block::Block, frame::Frame, view::View}; pub fn decode_block(block: &Block, mut target: View<&mut Frame>, prev: View<&Frame>) { - match &block.inner { - BlockInner::Literal(pixels) => target.set_pixels(pixels), - BlockInner::Split(box [a, b]) => { + match &block { + Block::Literal(pixels) => target.set_pixels(pixels), + Block::Split(box [a, b]) => { let [at, bt] = target.split_mut_unsafe(); let [ap, bp] = prev.split(); decode_block(a, at, ap); decode_block(b, bt, bp); } - BlockInner::Reference { translation: _ } => target.copy_from(&prev), + Block::Reference { translation: _ } => target.copy_from(&prev), } } diff --git a/evc/src/codec/encode.rs b/evc/src/codec/encode.rs index 1729e94..43a2783 100644 --- a/evc/src/codec/encode.rs +++ b/evc/src/codec/encode.rs @@ -1,34 +1,25 @@ -use crate::{ - block::{Block, BlockInner}, - frame::Frame, - view::View, -}; +use crate::{block::Block, frame::Frame, view::View}; pub 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.9 { - BlockInner::Reference { + if diff < 0.9 { + Block::Reference { translation: (0, 0), } } else { if view.size.0 < 16 { - BlockInner::Literal(view.pixels()) + Block::Literal(view.pixels()) } else { let [av, bv] = view.split(); let [ap, bp] = prev.split(); let a = encode_block(av, ap); let b = encode_block(bv, bp); if a.is_literal() && b.is_literal() { - BlockInner::Literal(view.pixels()) + Block::Literal(view.pixels()) } else { - BlockInner::Split(Box::new([a, b])) + Block::Split(Box::new([a, b])) } } - }; - - Block { - size: view.size, - inner, } } |