Skip to content

Commit c52969e

Browse files
committed
feat(ccitt): finish white codes, add fill order
1 parent 4ec0e4d commit c52969e

File tree

2 files changed

+156
-10
lines changed

2 files changed

+156
-10
lines changed

crates/ccitt/src/g42d/decode/bits.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ pub enum Bits {
2626
}
2727

2828
impl Bits {
29-
pub fn from_u8(input: u8) -> [Bits; 3] {
30-
TABLE[input as usize]
29+
pub fn from_u8_msb_to_lsb(input: u8) -> [Bits; 3] {
30+
MSB_TO_LSB[input as usize]
31+
}
32+
33+
pub fn from_u8_lsb_to_msb(input: u8) -> [Bits; 3] {
34+
LSB_TO_MSB[input as usize]
3135
}
3236

3337
pub fn off(state: u16, rem: Rem) -> Self {
@@ -60,7 +64,7 @@ impl Bits {
6064
}
6165

6266
#[rustfmt::skip]
63-
const TABLE: [[Bits; 3]; 256] = {
67+
const MSB_TO_LSB: [[Bits; 3]; 256] = {
6468
use Bits::*;
6569
[
6670
[B000, B000, B00], [B000, B000, B01], [B000, B000, B10], [B000, B000, B11],
@@ -129,3 +133,74 @@ const TABLE: [[Bits; 3]; 256] = {
129133
[B111, B111, B00], [B111, B111, B01], [B111, B111, B10], [B111, B111, B11],
130134
]
131135
};
136+
137+
#[rustfmt::skip]
138+
const LSB_TO_MSB: [[Bits; 3]; 256] = {
139+
use Bits::*;
140+
[
141+
[B000, B000, B00],[B100, B000, B00],[B010, B000, B00],[B110, B000, B00],
142+
[B001, B000, B00],[B101, B000, B00],[B011, B000, B00],[B111, B000, B00],
143+
[B000, B100, B00],[B100, B100, B00],[B010, B100, B00],[B110, B100, B00],
144+
[B001, B100, B00],[B101, B100, B00],[B011, B100, B00],[B111, B100, B00],
145+
[B000, B010, B00],[B100, B010, B00],[B010, B010, B00],[B110, B010, B00],
146+
[B001, B010, B00],[B101, B010, B00],[B011, B010, B00],[B111, B010, B00],
147+
[B000, B110, B00],[B100, B110, B00],[B010, B110, B00],[B110, B110, B00],
148+
[B001, B110, B00],[B101, B110, B00],[B011, B110, B00],[B111, B110, B00],
149+
[B000, B001, B00],[B100, B001, B00],[B010, B001, B00],[B110, B001, B00],
150+
[B001, B001, B00],[B101, B001, B00],[B011, B001, B00],[B111, B001, B00],
151+
[B000, B101, B00],[B100, B101, B00],[B010, B101, B00],[B110, B101, B00],
152+
[B001, B101, B00],[B101, B101, B00],[B011, B101, B00],[B111, B101, B00],
153+
[B000, B011, B00],[B100, B011, B00],[B010, B011, B00],[B110, B011, B00],
154+
[B001, B011, B00],[B101, B011, B00],[B011, B011, B00],[B111, B011, B00],
155+
[B000, B111, B00],[B100, B111, B00],[B010, B111, B00],[B110, B111, B00],
156+
[B001, B111, B00],[B101, B111, B00],[B011, B111, B00],[B111, B111, B00],
157+
[B000, B000, B10],[B100, B000, B10],[B010, B000, B10],[B110, B000, B10],
158+
[B001, B000, B10],[B101, B000, B10],[B011, B000, B10],[B111, B000, B10],
159+
[B000, B100, B10],[B100, B100, B10],[B010, B100, B10],[B110, B100, B10],
160+
[B001, B100, B10],[B101, B100, B10],[B011, B100, B10],[B111, B100, B10],
161+
[B000, B010, B10],[B100, B010, B10],[B010, B010, B10],[B110, B010, B10],
162+
[B001, B010, B10],[B101, B010, B10],[B011, B010, B10],[B111, B010, B10],
163+
[B000, B110, B10],[B100, B110, B10],[B010, B110, B10],[B110, B110, B10],
164+
[B001, B110, B10],[B101, B110, B10],[B011, B110, B10],[B111, B110, B10],
165+
[B000, B001, B10],[B100, B001, B10],[B010, B001, B10],[B110, B001, B10],
166+
[B001, B001, B10],[B101, B001, B10],[B011, B001, B10],[B111, B001, B10],
167+
[B000, B101, B10],[B100, B101, B10],[B010, B101, B10],[B110, B101, B10],
168+
[B001, B101, B10],[B101, B101, B10],[B011, B101, B10],[B111, B101, B10],
169+
[B000, B011, B10],[B100, B011, B10],[B010, B011, B10],[B110, B011, B10],
170+
[B001, B011, B10],[B101, B011, B10],[B011, B011, B10],[B111, B011, B10],
171+
[B000, B111, B10],[B100, B111, B10],[B010, B111, B10],[B110, B111, B10],
172+
[B001, B111, B10],[B101, B111, B10],[B011, B111, B10],[B111, B111, B10],
173+
[B000, B000, B01],[B100, B000, B01],[B010, B000, B01],[B110, B000, B01],
174+
[B001, B000, B01],[B101, B000, B01],[B011, B000, B01],[B111, B000, B01],
175+
[B000, B100, B01],[B100, B100, B01],[B010, B100, B01],[B110, B100, B01],
176+
[B001, B100, B01],[B101, B100, B01],[B011, B100, B01],[B111, B100, B01],
177+
[B000, B010, B01],[B100, B010, B01],[B010, B010, B01],[B110, B010, B01],
178+
[B001, B010, B01],[B101, B010, B01],[B011, B010, B01],[B111, B010, B01],
179+
[B000, B110, B01],[B100, B110, B01],[B010, B110, B01],[B110, B110, B01],
180+
[B001, B110, B01],[B101, B110, B01],[B011, B110, B01],[B111, B110, B01],
181+
[B000, B001, B01],[B100, B001, B01],[B010, B001, B01],[B110, B001, B01],
182+
[B001, B001, B01],[B101, B001, B01],[B011, B001, B01],[B111, B001, B01],
183+
[B000, B101, B01],[B100, B101, B01],[B010, B101, B01],[B110, B101, B01],
184+
[B001, B101, B01],[B101, B101, B01],[B011, B101, B01],[B111, B101, B01],
185+
[B000, B011, B01],[B100, B011, B01],[B010, B011, B01],[B110, B011, B01],
186+
[B001, B011, B01],[B101, B011, B01],[B011, B011, B01],[B111, B011, B01],
187+
[B000, B111, B01],[B100, B111, B01],[B010, B111, B01],[B110, B111, B01],
188+
[B001, B111, B01],[B101, B111, B01],[B011, B111, B01],[B111, B111, B01],
189+
[B000, B000, B11],[B100, B000, B11],[B010, B000, B11],[B110, B000, B11],
190+
[B001, B000, B11],[B101, B000, B11],[B011, B000, B11],[B111, B000, B11],
191+
[B000, B100, B11],[B100, B100, B11],[B010, B100, B11],[B110, B100, B11],
192+
[B001, B100, B11],[B101, B100, B11],[B011, B100, B11],[B111, B100, B11],
193+
[B000, B010, B11],[B100, B010, B11],[B010, B010, B11],[B110, B010, B11],
194+
[B001, B010, B11],[B101, B010, B11],[B011, B010, B11],[B111, B010, B11],
195+
[B000, B110, B11],[B100, B110, B11],[B010, B110, B11],[B110, B110, B11],
196+
[B001, B110, B11],[B101, B110, B11],[B011, B110, B11],[B111, B110, B11],
197+
[B000, B001, B11],[B100, B001, B11],[B010, B001, B11],[B110, B001, B11],
198+
[B001, B001, B11],[B101, B001, B11],[B011, B001, B11],[B111, B001, B11],
199+
[B000, B101, B11],[B100, B101, B11],[B010, B101, B11],[B110, B101, B11],
200+
[B001, B101, B11],[B101, B101, B11],[B011, B101, B11],[B111, B101, B11],
201+
[B000, B011, B11],[B100, B011, B11],[B010, B011, B11],[B110, B011, B11],
202+
[B001, B011, B11],[B101, B011, B11],[B011, B011, B11],[B111, B011, B11],
203+
[B000, B111, B11],[B100, B111, B11],[B010, B111, B11],[B110, B111, B11],
204+
[B001, B111, B11],[B101, B111, B11],[B011, B111, B11],[B111, B111, B11],
205+
]
206+
};

crates/ccitt/src/g42d/decode/mod.rs

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! that consumes bytes in a 3-3-2 (bits) pattern and updates the
55
//! internal state accordingly.
66
7-
use crate::{Color, ColorLine, Store};
7+
use crate::{bits::FillOrder, Color, ColorLine, Store};
88
use bits::Bits;
99
use thiserror::Error;
1010

@@ -65,6 +65,7 @@ pub struct Decoder<S: Store> {
6565
reference: S::Row,
6666
current: S::Row,
6767
color: Color,
68+
bit_loader: fn(u8) -> [Bits; 3],
6869
a0: usize,
6970
}
7071

@@ -79,18 +80,27 @@ impl<S: Store> Decoder<S> {
7980
store: S::new(),
8081
reference: S::new_row(width),
8182
current: S::new_row(width),
83+
bit_loader: Bits::from_u8_msb_to_lsb,
8284
color: Color::White,
8385
a0: 0,
8486
#[cfg(feature = "debug")]
8587
debug: false,
8688
}
8789
}
8890

91+
/// Set a different [`FillOrder`]
92+
pub fn set_fill_order(&mut self, fill_order: FillOrder) {
93+
self.bit_loader = match fill_order {
94+
FillOrder::MsbToLsb => Bits::from_u8_msb_to_lsb,
95+
FillOrder::LsbToMsb => Bits::from_u8_lsb_to_msb,
96+
}
97+
}
98+
8999
fn next_bits<'a>(&mut self, input: &'a [u8]) -> Result<(&'a [u8], Bits), Err> {
90100
match self.next_bits {
91101
NextBits::None => {
92102
if let Some((first, rest)) = input.split_first() {
93-
let [b1, b2, b3] = Bits::from_u8(*first);
103+
let [b1, b2, b3] = (self.bit_loader)(*first);
94104
self.next_bits = NextBits::A2(b2, b3);
95105
Ok((rest, b1))
96106
} else {
@@ -172,13 +182,21 @@ impl<S: Store> Decoder<S> {
172182
bits: Bits,
173183
mut input: &'a [u8],
174184
) -> Result<(&'a [u8], u16, Bits), Err> {
185+
let mut sum = 0;
175186
let mut off = 0;
176187
let mut state = 0;
177188
bits.push(&mut state, &mut off);
178189
loop {
179190
if let Some((val, rem)) = func(state, off) {
180191
let bits = Bits::off(state, rem);
181-
return Ok((input, val, bits));
192+
sum += val;
193+
if val < 64 {
194+
return Ok((input, sum, bits));
195+
} else {
196+
off = 0;
197+
state = 0;
198+
bits.push(&mut state, &mut off);
199+
}
182200
} else {
183201
let (rest, bits) = self.next_bits(input)?;
184202
bits.push(&mut state, &mut off);
@@ -1069,7 +1087,9 @@ fn white(state: u16, off: u8) -> Option<(u16, Rem)> {
10691087
9 => white9(state),
10701088
10 => white10(state),
10711089
11 => white11(state),
1072-
_ => todo!(),
1090+
12 => white12(state),
1091+
13 => white13(state),
1092+
_ => todo!("white{off} {:016b}", state),
10731093
}
10741094
}
10751095

@@ -1201,6 +1221,7 @@ fn white8(state: u16) -> Option<(u16, Rem)> {
12011221
0b01010110 | 0b01010111 => Some((25, Rem::R1)),
12021222
0b01101110 | 0b01101111 => Some((256, Rem::R1)),
12031223
// 8 bit
1224+
0b00000010 => Some((29, Rem::R0)),
12041225
0b00000011 => Some((30, Rem::R0)),
12051226
0b00000100 => Some((45, Rem::R0)),
12061227
0b00000101 => Some((46, Rem::R0)),
@@ -1263,6 +1284,7 @@ fn white9(state: u16) -> Option<(u16, Rem)> {
12631284
0b010101100..=0b010101111 => Some((25, Rem::R2)),
12641285
0b011011100..=0b011011111 => Some((256, Rem::R2)),
12651286
// 8 bit
1287+
0b000000100 | 0b000000101 => Some((29, Rem::R1)),
12661288
0b000000110 | 0b000000111 => Some((30, Rem::R1)),
12671289
0b000001000 | 0b000001001 => Some((45, Rem::R1)),
12681290
0b000001010 | 0b000001011 => Some((46, Rem::R1)),
@@ -1329,6 +1351,7 @@ fn white9(state: u16) -> Option<(u16, Rem)> {
13291351
fn white10(state: u16) -> Option<(u16, Rem)> {
13301352
match state {
13311353
// 8 bit
1354+
0b0000001000..=0b0000001011 => Some((29, Rem::R2)),
13321355
0b0000001100..=0b0000001111 => Some((30, Rem::R2)),
13331356
0b0000010000..=0b0000010011 => Some((45, Rem::R2)),
13341357
0b0000010100..=0b0000010111 => Some((46, Rem::R2)),
@@ -1387,8 +1410,6 @@ fn white10(state: u16) -> Option<(u16, Rem)> {
13871410
0b0110110010 | 0b0110110011 => Some((1280, Rem::R1)),
13881411
0b0110110100 | 0b0110110101 => Some((1344, Rem::R1)),
13891412
0b0110110110 | 0b0110110111 => Some((1408, Rem::R1)),
1390-
// 10 bit
1391-
// TODO
13921413
// rest
13931414
_ => None,
13941415
}
@@ -1414,7 +1435,57 @@ fn white11(state: u16) -> Option<(u16, Rem)> {
14141435
0b01101101000..=0b01101101011 => Some((1344, Rem::R2)),
14151436
0b01101101100..=0b01101101111 => Some((1408, Rem::R2)),
14161437
// 11 bit
1417-
// TODO
1438+
0b00000001000 => Some((1792, Rem::R0)),
1439+
0b00000001100 => Some((1856, Rem::R0)),
1440+
0b00000001101 => Some((1920, Rem::R0)),
1441+
// rest
1442+
_ => None,
1443+
}
1444+
}
1445+
1446+
fn white12(state: u16) -> Option<(u16, Rem)> {
1447+
match state {
1448+
// 11 bit
1449+
0b000000010000 | 0b000000010001 => Some((1792, Rem::R1)),
1450+
0b000000011000 | 0b000000011001 => Some((1856, Rem::R1)),
1451+
0b000000011010 | 0b000000011011 => Some((1920, Rem::R1)),
1452+
1453+
// 12 bit
1454+
0b000000010010 => Some((1984, Rem::R0)),
1455+
0b000000010011 => Some((2048, Rem::R0)),
1456+
0b000000010100 => Some((2112, Rem::R0)),
1457+
0b000000010101 => Some((2176, Rem::R0)),
1458+
0b000000010110 => Some((2240, Rem::R0)),
1459+
0b000000010111 => Some((2304, Rem::R0)),
1460+
0b000000011100 => Some((2368, Rem::R0)),
1461+
0b000000011101 => Some((2432, Rem::R0)),
1462+
0b000000011110 => Some((2496, Rem::R0)),
1463+
0b000000011111 => Some((2560, Rem::R0)),
1464+
1465+
// rest
1466+
_ => None,
1467+
}
1468+
}
1469+
1470+
fn white13(state: u16) -> Option<(u16, Rem)> {
1471+
match state {
1472+
// 11 bit
1473+
0b0000000100000..=0b0000000100011 => Some((1792, Rem::R2)),
1474+
0b0000000110000..=0b0000000110011 => Some((1856, Rem::R2)),
1475+
0b0000000110100..=0b0000000110111 => Some((1920, Rem::R2)),
1476+
1477+
// 12 bit
1478+
0b0000000100100 | 0b0000000100101 => Some((1984, Rem::R1)),
1479+
0b0000000100110 | 0b0000000100111 => Some((2048, Rem::R1)),
1480+
0b0000000101000 | 0b0000000101001 => Some((2112, Rem::R1)),
1481+
0b0000000101010 | 0b0000000101011 => Some((2176, Rem::R1)),
1482+
0b0000000101100 | 0b0000000101101 => Some((2240, Rem::R1)),
1483+
0b0000000101110 | 0b0000000101111 => Some((2304, Rem::R1)),
1484+
0b0000000111000 | 0b0000000111001 => Some((2368, Rem::R1)),
1485+
0b0000000111010 | 0b0000000111011 => Some((2432, Rem::R1)),
1486+
0b0000000111100 | 0b0000000111101 => Some((2496, Rem::R1)),
1487+
0b0000000111110 | 0b0000000111111 => Some((2560, Rem::R1)),
1488+
14181489
// rest
14191490
_ => None,
14201491
}

0 commit comments

Comments
 (0)