Function fax::decoder::decode_g4

source ·
pub fn decode_g4(
    input: impl Iterator<Item = u8>,
    width: u16,
    height: Option<u16>,
    line_cb: impl FnMut(&[u16])
) -> Option<()>
Expand description

Decode a Group 4 Image

  • width is the width of the image.

  • The callback line_cb is called for each decoded line. The argument is the list of positions of color change, starting with white.

    If height is specified, at most that many lines will be decoded, otherwise data is decoded until the end-of-block marker (or end of data).

To obtain an iterator over the pixel colors, the pels function is provided.

Examples found in repository?
examples/fax2tiff.rs (lines 14-16)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let mut args = std::env::args().skip(1);
    let input: String = args.next().unwrap();
    let width: u16 = args.next().unwrap().parse().unwrap();
    let output = args.next().unwrap();

    let data = fs::read(&input).unwrap();
    let mut height = 0;
    decoder::decode_g4(data.iter().cloned(), width, None,  |transitions| {
        height += 1;
    });

    std::fs::write(output, wrap(&data, width as _, height)).unwrap();
}
More examples
Hide additional examples
examples/fax2pbm.rs (lines 14-24)
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
fn main() {
    let mut args = std::env::args().skip(1);
    let input: String = args.next().unwrap();
    let width: u16 = args.next().unwrap().parse().unwrap();
    let output = args.next().unwrap();

    let data = fs::read(&input).unwrap();
    let mut writer = VecWriter::new();
    let mut height = 0;
    decoder::decode_g4(data.iter().cloned(), width, None,  |transitions| {
        for c in pels(transitions, width) {
            let bit = match c {
                Color::Black => Bits { data: 1, len: 1 },
                Color::White => Bits { data: 0, len: 1 }
            };
            writer.write(bit);
        }
        writer.pad();
        height += 1;
    });
    let data = writer.finish();
    assert_eq!(data.len(), height as usize * ((width as usize + 7) / 8));

    let header = format!("P4\n{} {}\n", width, height);
    let mut out = File::create(&output).unwrap();
    out.write_all(header.as_bytes()).unwrap();
    out.write_all(&data).unwrap();
}