1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
use std::{
io::{self, stdin, stdout, Read, Write},
ops::{Index, IndexMut},
time::Instant,
};
use vector::{UVec2, Vec2};
pub mod common;
pub mod vector;
#[derive(Clone)]
pub struct CodecParams {
pub width: usize,
pub height: usize,
pub debug: usize,
pub mode: CodecMode,
}
#[derive(Clone)]
pub enum CodecMode {
Encode,
Decode,
}
pub struct Framework {
process_start: Instant,
params: CodecParams,
}
impl Framework {
pub fn init() -> (Self, CodecParams) {
let width = std::env::var("V_WIDTH").unwrap().parse().unwrap();
let height = std::env::var("V_HEIGHT").unwrap().parse().unwrap();
let debug = std::env::var("V_DEBUG").unwrap().parse().unwrap();
let mode = match std::env::var("V_MODE").unwrap().as_str() {
"encode" => CodecMode::Encode,
"decode" => CodecMode::Decode,
_ => panic!("invalid mode"),
};
let params = CodecParams {
height,
width,
debug,
mode,
};
(
Self {
params: params.clone(),
process_start: Instant::now(),
},
params,
)
}
pub fn next_frame(&mut self) -> Option<Frame> {
self.process_start = Instant::now();
Some(Frame::read(self.params.width, self.params.height, stdin()).unwrap())
}
pub fn encode_done(&mut self, output: &[u8]) {
let el = self.process_start.elapsed();
eprintln!(
"size={}KB ratio={:.02} t={el:?}",
output.len() / 1000,
(self.params.width * self.params.height * 3) / output.len()
)
}
pub fn next_chunk(&mut self, _buffer: &mut Vec<u8>) -> bool {
true
}
pub fn decode_done(&mut self, output: &Frame) {
output.write(stdout()).unwrap();
}
}
#[derive(Debug, Clone)]
pub struct Frame {
pub width: usize,
pub height: usize,
pub pixels: Vec<Pixel<u8>>,
}
#[derive(Debug, Clone, Copy)]
pub struct Pixel<T> {
pub r: T,
pub g: T,
pub b: T,
}
pub type EPixel = Pixel<u8>;
impl Frame {
pub fn new(width: usize, height: usize) -> Self {
Self {
height,
width,
pixels: vec![Pixel::BLACK; width * height],
}
}
pub fn read(width: usize, height: usize, mut r: impl Read) -> io::Result<Self> {
let mut f = Frame::new(width, height);
let p = unsafe {
std::slice::from_raw_parts_mut(
f.pixels.as_mut_slice().as_mut_ptr() as *mut u8,
width * height * 3,
)
};
r.read_exact(p)?;
Ok(f)
}
pub fn write(&self, mut w: impl Write) -> io::Result<()> {
let p = unsafe {
std::slice::from_raw_parts(
self.pixels.as_slice().as_ptr() as *const u8,
self.width * self.height * 3,
)
};
w.write_all(p)
}
}
impl Index<(usize, usize)> for Frame {
type Output = Pixel<u8>;
#[inline]
fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
&self.pixels[x + y * self.width]
}
}
impl IndexMut<(usize, usize)> for Frame {
#[inline]
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output {
&mut self.pixels[x + y * self.width]
}
}
impl Index<UVec2> for Frame {
type Output = Pixel<u8>;
#[inline]
fn index(&self, Vec2 { x, y }: UVec2) -> &Self::Output {
&self.pixels[x + y * self.width]
}
}
impl IndexMut<UVec2> for Frame {
#[inline]
fn index_mut(&mut self, Vec2 { x, y }: UVec2) -> &mut Self::Output {
&mut self.pixels[x + y * self.width]
}
}
impl Pixel<u8> {
pub const BLACK: Pixel<u8> = Pixel { r: 0, g: 0, b: 0 };
}
|