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