aboutsummaryrefslogtreecommitdiff
path: root/evc/src/block.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2022-12-05 20:29:05 +0100
committermetamuffin <metamuffin@disroot.org>2022-12-05 20:29:05 +0100
commit96e316ea16b7b915e02735457d5ac7495d3db305 (patch)
treee2ef2957009dd96c452fb8a80020ed83caa70993 /evc/src/block.rs
parent75b87762196c461069fa087f8aa3c978d7407549 (diff)
downloadvideo-codec-experiments-96e316ea16b7b915e02735457d5ac7495d3db305.tar
video-codec-experiments-96e316ea16b7b915e02735457d5ac7495d3db305.tar.bz2
video-codec-experiments-96e316ea16b7b915e02735457d5ac7495d3db305.tar.zst
serialization
Diffstat (limited to 'evc/src/block.rs')
-rw-r--r--evc/src/block.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/evc/src/block.rs b/evc/src/block.rs
new file mode 100644
index 0000000..29dd4ba
--- /dev/null
+++ b/evc/src/block.rs
@@ -0,0 +1,69 @@
+use crate::ser::{Ser, Sink, Source};
+
+#[derive(Copy, Clone, Debug)]
+pub struct Pixel {
+ pub r: u8,
+ pub g: u8,
+ pub b: u8,
+}
+
+#[derive(Clone, Debug)]
+pub struct Block {
+ size: (usize, usize),
+ inner: BlockInner,
+}
+
+#[derive(Clone, Debug)]
+pub enum BlockInner {
+ Literal(Vec<Pixel>),
+ Split(Box<[Block; 2]>),
+ Reference { translation: (usize, usize) },
+}
+
+impl Block {
+ pub fn write(&self, sink: &mut impl std::io::Write) -> std::io::Result<()> {
+ match &self.inner {
+ BlockInner::Literal(pixels) => {
+ sink.put(0u8)?;
+ pixels.write(sink)?;
+ }
+ BlockInner::Split(box [a, b]) => {
+ sink.put(1u8)?;
+ a.write(sink)?;
+ b.write(sink)?;
+ }
+ BlockInner::Reference { translation } => {
+ sink.put(2u8)?;
+ }
+ }
+ Ok(())
+ }
+
+ pub fn read(source: &mut impl std::io::Read, size: (usize, usize)) -> std::io::Result<Self> {
+ let inner = match source.get::<u8>()? {
+ 0 => BlockInner::Literal(source.get()?),
+ 1 => BlockInner::Split(Box::new({
+ let subsize = if size.0 > size.1 {
+ (size.0 / 2, size.1)
+ } else {
+ (size.0, size.1 / 2)
+ };
+ [Block::read(source, subsize)?, Block::read(source, subsize)?]
+ })),
+ 2 => todo!(),
+ _ => panic!("file corrupt"),
+ };
+
+ Ok(Self { size, inner })
+ }
+}
+impl Ser for Pixel {
+ fn write(&self, sink: &mut impl std::io::Write) -> std::io::Result<()> {
+ sink.put((self.r, self.g, self.b))
+ }
+
+ fn read(source: &mut impl std::io::Read) -> std::io::Result<Self> {
+ let (r, g, b) = source.get()?;
+ Ok(Self { r, g, b })
+ }
+}