| 1 |
use crate::{Color, BitWriter, Transitions, maps::{Mode, mode, black, white, EDFB_HALF}}; |
| 2 |
|
| 3 |
fn absdiff(a: u16, b: u16) -> u16 { |
| 4 |
if a > b { |
| 5 |
a - b |
| 6 |
} else { |
| 7 |
b - a |
| 8 |
} |
| 9 |
} |
| 10 |
|
| 11 |
pub struct Encoder<W> { |
| 12 |
writer: W, |
| 13 |
reference: Vec<u16>, |
| 14 |
current: Vec<u16>, |
| 15 |
} |
| 16 |
fn encode_color<W: BitWriter>(writer: &mut W, color: Color, mut n: u16) -> Result<(), W::Error> { |
| 17 |
let table = match color { |
| 18 |
Color::White => &white::ENTRIES, |
| 19 |
Color::Black => &black::ENTRIES, |
| 20 |
}; |
| 21 |
let mut write = |n: u16| { |
| 22 |
let idx = if n >= 64 { 63 + n / 64 } else { n } as usize; |
| 23 |
let (v, bits) = table[idx]; |
| 24 |
assert_eq!(v, n); |
| 25 |
//debug!("{}", n); |
| 26 |
writer.write(bits) |
| 27 |
}; |
| 28 |
|
| 29 |
while n >= 2560 { |
| 30 |
write(2560)?; |
| 31 |
n -= 2560; |
| 32 |
} |
| 33 |
if n >= 64 { |
| 34 |
let d = n & !63; |
| 35 |
write(d)?; |
| 36 |
n -= d; |
| 37 |
} |
| 38 |
|
| 39 |
write(n) |
| 40 |
} |
| 41 |
impl<W: BitWriter> Encoder<W> { |
| 42 |
pub fn new(writer: W) -> Self { |
| 43 |
Encoder { |
| 44 |
writer, |
| 45 |
reference: vec![], |
| 46 |
current: vec![], |
| 47 |
} |
| 48 |
} |
| 49 |
pub fn encode_line(&mut self, pels: impl Iterator<Item=Color>, width: u16) -> Result<(), W::Error> { |
| 50 |
let mut color = Color::White; |
| 51 |
let mut transitions = Transitions::new(&self.reference); |
| 52 |
let mut a0 = 0; |
| 53 |
let mut start_of_line = true; |
| 54 |
let mut pels = pels.enumerate() |
| 55 |
.scan(Color::White, |state, (i, c)| { |
| 56 |
Some(if c != *state { |
| 57 |
debug!(" {i} {c:?}"); |
| 58 |
*state = c; |
| 59 |
Some(i as u16) |
| 60 |
} else { |
| 61 |
None |
| 62 |
}) |
| 63 |
}).filter_map(|x| x); |
| 64 |
let writer = &mut self.writer; |
| 65 |
self.current.clear(); |
| 66 |
|
| 67 |
while a0 < width { |
| 68 |
let a1; |
| 69 |
if let Some(a1_) = pels.next() { |
| 70 |
self.current.push(a1_); |
| 71 |
a1 = a1_; |
| 72 |
} else { |
| 73 |
a1 = width; |
| 74 |
} |
| 75 |
loop { |
| 76 |
transitions.seek_back(a0); |
| 77 |
let b1 = transitions.next_color(a0, !color, start_of_line).unwrap_or(width); |
| 78 |
let b2 = transitions.peek(); |
| 79 |
start_of_line = false; |
| 80 |
debug!(" a0={a0}, a1={a1}, b1={:?}, b2={:?}", b1, b2); |
| 81 |
match (b1, b2) { |
| 82 |
(_b1, Some(b2)) if b2 < a1 => { |
| 83 |
debug!(" Pass"); |
| 84 |
let bits = mode::encode(Mode::Pass).unwrap(); |
| 85 |
writer.write(bits)?; |
| 86 |
transitions.skip(1); |
| 87 |
a0 = b2; |
| 88 |
continue; |
| 89 |
} |
| 90 |
(b1, _) if absdiff(a1, b1) <= 3 => { |
| 91 |
let delta = a1 as i16 - b1 as i16; |
| 92 |
debug!(" Vertical({})", delta); |
| 93 |
let bits = mode::encode(Mode::Vertical(delta as i8)).unwrap(); |
| 94 |
writer.write(bits)?; |
| 95 |
a0 = a1; |
| 96 |
color = !color; |
| 97 |
} |
| 98 |
_ => { |
| 99 |
let a2 = match pels.next() { |
| 100 |
Some(a2) => { |
| 101 |
self.current.push(a2); |
| 102 |
a2 |
| 103 |
}, |
| 104 |
None => width |
| 105 |
}; |
| 106 |
let a0a1 = a1 - a0; |
| 107 |
let a1a2 = a2 - a1; |
| 108 |
debug!(" Horizontal({}, {}) color={color:?}", a0a1, a1a2); |
| 109 |
let bits = mode::encode(Mode::Horizontal).unwrap(); |
| 110 |
writer.write(bits)?; |
| 111 |
let c = if a0 + a1 == 0 { |
| 112 |
Color::White |
| 113 |
} else { |
| 114 |
color |
| 115 |
}; |
| 116 |
encode_color(writer, c, a0a1)?; |
| 117 |
encode_color(writer, !c, a1a2)?; |
| 118 |
a0 = a2; |
| 119 |
} |
| 120 |
} |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
std::mem::swap(&mut self.reference, &mut self.current); |
| 126 |
debug!("next line"); |
| 127 |
Ok(()) |
| 128 |
} |
| 129 |
pub fn finish(mut self) -> Result<W, W::Error> { |
| 130 |
self.writer.write(EDFB_HALF)?; |
| 131 |
self.writer.write(EDFB_HALF)?; |
| 132 |
Ok(self.writer) |
| 133 |
} |
| 134 |
} |