pub struct VecWriter { /* private fields */ }
Implementations§
source§impl VecWriter
impl VecWriter
sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/fax2pbm.rs (line 12)
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();
}
More examples
examples/pbm2fax.rs (line 16)
4 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
fn main() {
let mut args = std::env::args().skip(1);
let input: String = args.next().unwrap();
let output = args.next().unwrap();
let data = fs::read(&input).unwrap();
let mut parts = data.splitn(3, |&b| b == b'\n');
assert_eq!(parts.next().unwrap(), b"P4");
let mut size = parts.next().unwrap().splitn(2, |&b| b == b' ');
let width: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
let height: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
let writer = VecWriter::new();
let mut encoder = Encoder::new(writer);
for line in parts.next().unwrap().chunks((width as usize + 7) / 8).take(height as _) {
let line = slice_bits(line).take(width as usize)
.map(|b| match b {
false => Color::White,
true => Color::Black
});
encoder.encode_line(line, width as u16).unwrap();
}
let data = encoder.finish().unwrap().finish();
fs::write(&output, &tiff::wrap(&data, width, height)).unwrap();
}
pub fn with_capacity(n: usize) -> Self
sourcepub fn pad(&mut self)
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)
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();
}
sourcepub fn finish(self) -> Vec<u8>
pub fn finish(self) -> Vec<u8>
pad and return the accumulated bytes
Examples found in repository?
examples/fax2pbm.rs (line 25)
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();
}
More examples
examples/pbm2fax.rs (line 27)
4 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
fn main() {
let mut args = std::env::args().skip(1);
let input: String = args.next().unwrap();
let output = args.next().unwrap();
let data = fs::read(&input).unwrap();
let mut parts = data.splitn(3, |&b| b == b'\n');
assert_eq!(parts.next().unwrap(), b"P4");
let mut size = parts.next().unwrap().splitn(2, |&b| b == b' ');
let width: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
let height: u32 = std::str::from_utf8(size.next().unwrap()).unwrap().parse().unwrap();
let writer = VecWriter::new();
let mut encoder = Encoder::new(writer);
for line in parts.next().unwrap().chunks((width as usize + 7) / 8).take(height as _) {
let line = slice_bits(line).take(width as usize)
.map(|b| match b {
false => Color::White,
true => Color::Black
});
encoder.encode_line(line, width as u16).unwrap();
}
let data = encoder.finish().unwrap().finish();
fs::write(&output, &tiff::wrap(&data, width, height)).unwrap();
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for VecWriter
impl RefUnwindSafe for VecWriter
impl Send for VecWriter
impl Sync for VecWriter
impl Unpin for VecWriter
impl UnwindSafe for VecWriter
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more