1
2
3
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::core::traits::PixelBound;
use crate::core::*;
use num_traits::cast::{FromPrimitive, NumCast};
use num_traits::{Num, NumAssignOps};
use std::fmt::Display;
use std::fs::{read, File};
use std::io::prelude::*;
use std::path::Path;
pub trait Encoder<T, C>
where
T: Copy + Clone + Num + NumAssignOps + NumCast + PartialOrd + Display + PixelBound,
C: ColourModel,
{
fn encode(&self, image: &Image<T, C>) -> Vec<u8>;
fn encode_file<P: AsRef<Path>>(&self, image: &Image<T, C>, filename: P) -> std::io::Result<()> {
let mut file = File::create(filename)?;
file.write_all(&self.encode(image))?;
Ok(())
}
}
pub trait Decoder<T, C>
where
T: Copy
+ Clone
+ FromPrimitive
+ Num
+ NumAssignOps
+ NumCast
+ PartialOrd
+ Display
+ PixelBound,
C: ColourModel,
{
fn decode(&self, bytes: &[u8]) -> std::io::Result<Image<T, C>>;
fn decode_file<P: AsRef<Path>>(&self, filename: P) -> std::io::Result<Image<T, C>> {
let bytes = read(filename)?;
self.decode(&bytes)
}
}
pub mod netpbm;