Struct VecWriter

Source
pub struct VecWriter { /* private fields */ }

Implementations§

Source§

impl VecWriter

Source

pub fn new() -> Self

Examples found in repository?
examples/fax2pbm.rs (line 12)
5fn main() {
6    let mut args = std::env::args().skip(1);
7    let input: String = args.next().unwrap();
8    let width: u16 = args.next().unwrap().parse().unwrap();
9    let output = args.next().unwrap();
10
11    let data = fs::read(&input).unwrap();
12    let mut writer = VecWriter::new();
13    let mut height = 0;
14    decoder::decode_g4(data.iter().cloned(), width, None,  |transitions| {
15        for c in pels(transitions, width) {
16            let bit = match c {
17                Color::Black => Bits { data: 1, len: 1 },
18                Color::White => Bits { data: 0, len: 1 }
19            };
20            writer.write(bit);
21        }
22        writer.pad();
23        height += 1;
24    });
25    let data = writer.finish();
26    assert_eq!(data.len(), height as usize * ((width as usize + 7) / 8));
27
28    let header = format!("P4\n{} {}\n", width, height);
29    let mut out = File::create(&output).unwrap();
30    out.write_all(header.as_bytes()).unwrap();
31    out.write_all(&data).unwrap();
32}
More examples
Hide additional examples
examples/pbm2fax.rs (line 16)
4fn main() {
5    let mut args = std::env::args().skip(1);
6    let input: String = args.next().unwrap();
7    let output = args.next().unwrap();
8
9    let data = fs::read(&input).unwrap();
10    let mut parts = data.splitn(3, |&b| b == b'\n');
11    assert_eq!(parts.next().unwrap(), b"P4");
12    let mut size = parts.next().unwrap().splitn(2, |&b| b == b' ');
13    let width: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
14    let height: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
15
16    let writer = VecWriter::new();
17    let mut encoder = Encoder::new(writer);
18    
19    for line in parts.next().unwrap().chunks((width as usize + 7) / 8).take(height as _) {
20        let line = slice_bits(line).take(width as usize)
21        .map(|b| match b {
22            false => Color::White,
23            true => Color::Black
24        });
25        encoder.encode_line(line, width as u16).unwrap();
26    }
27    let data = encoder.finish().unwrap().finish();
28
29    fs::write(&output, &tiff::wrap(&data, width, height)).unwrap();
30}
Source

pub fn with_capacity(n: usize) -> Self

Source

pub fn pad(&mut self)

Pad the output with 0 bits until it is at a byte boundary.

Examples found in repository?
examples/fax2pbm.rs (line 22)
5fn main() {
6    let mut args = std::env::args().skip(1);
7    let input: String = args.next().unwrap();
8    let width: u16 = args.next().unwrap().parse().unwrap();
9    let output = args.next().unwrap();
10
11    let data = fs::read(&input).unwrap();
12    let mut writer = VecWriter::new();
13    let mut height = 0;
14    decoder::decode_g4(data.iter().cloned(), width, None,  |transitions| {
15        for c in pels(transitions, width) {
16            let bit = match c {
17                Color::Black => Bits { data: 1, len: 1 },
18                Color::White => Bits { data: 0, len: 1 }
19            };
20            writer.write(bit);
21        }
22        writer.pad();
23        height += 1;
24    });
25    let data = writer.finish();
26    assert_eq!(data.len(), height as usize * ((width as usize + 7) / 8));
27
28    let header = format!("P4\n{} {}\n", width, height);
29    let mut out = File::create(&output).unwrap();
30    out.write_all(header.as_bytes()).unwrap();
31    out.write_all(&data).unwrap();
32}
Source

pub fn finish(self) -> Vec<u8>

pad and return the accumulated bytes

Examples found in repository?
examples/fax2pbm.rs (line 25)
5fn main() {
6    let mut args = std::env::args().skip(1);
7    let input: String = args.next().unwrap();
8    let width: u16 = args.next().unwrap().parse().unwrap();
9    let output = args.next().unwrap();
10
11    let data = fs::read(&input).unwrap();
12    let mut writer = VecWriter::new();
13    let mut height = 0;
14    decoder::decode_g4(data.iter().cloned(), width, None,  |transitions| {
15        for c in pels(transitions, width) {
16            let bit = match c {
17                Color::Black => Bits { data: 1, len: 1 },
18                Color::White => Bits { data: 0, len: 1 }
19            };
20            writer.write(bit);
21        }
22        writer.pad();
23        height += 1;
24    });
25    let data = writer.finish();
26    assert_eq!(data.len(), height as usize * ((width as usize + 7) / 8));
27
28    let header = format!("P4\n{} {}\n", width, height);
29    let mut out = File::create(&output).unwrap();
30    out.write_all(header.as_bytes()).unwrap();
31    out.write_all(&data).unwrap();
32}
More examples
Hide additional examples
examples/pbm2fax.rs (line 27)
4fn main() {
5    let mut args = std::env::args().skip(1);
6    let input: String = args.next().unwrap();
7    let output = args.next().unwrap();
8
9    let data = fs::read(&input).unwrap();
10    let mut parts = data.splitn(3, |&b| b == b'\n');
11    assert_eq!(parts.next().unwrap(), b"P4");
12    let mut size = parts.next().unwrap().splitn(2, |&b| b == b' ');
13    let width: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
14    let height: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
15
16    let writer = VecWriter::new();
17    let mut encoder = Encoder::new(writer);
18    
19    for line in parts.next().unwrap().chunks((width as usize + 7) / 8).take(height as _) {
20        let line = slice_bits(line).take(width as usize)
21        .map(|b| match b {
22            false => Color::White,
23            true => Color::Black
24        });
25        encoder.encode_line(line, width as u16).unwrap();
26    }
27    let data = encoder.finish().unwrap().finish();
28
29    fs::write(&output, &tiff::wrap(&data, width, height)).unwrap();
30}

Trait Implementations§

Source§

impl BitWriter for VecWriter

Source§

type Error = Infallible

Source§

fn write(&mut self, bits: Bits) -> Result<(), Self::Error>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.