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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![cfg(not(doctest))]
use crate::error::{ImgIoError, ImgIoResult};
use crate::image::{Image, BaseImage};
use image::io::Reader;
use image::{GenericImageView, ColorType, ImageBuffer};
fn from_color_type(color: ColorType) -> ImgIoResult<(u8, bool)> {
match color {
ColorType::L8 => Ok((1, false)),
ColorType::La8 => Ok((2, true)),
ColorType::Rgb8 | ColorType::Bgr8 => Ok((3, false)),
ColorType::Rgba8 | ColorType::Bgra8 => Ok((4, true)),
_ => Err(ImgIoError::UnsupportedColorTypeError("unsupported color type".to_string()))
}
}
pub fn read(filename: &str) -> ImgIoResult<Image<u8>> {
let img = Reader::open(filename)?.decode()?;
let (width, height) = img.dimensions();
let (channels, alpha) = from_color_type(img.color())?;
Ok(Image::from_slice(width, height, channels, alpha, img.as_bytes()))
}
pub fn write(input: &Image<u8>, filename: &str) -> ImgIoResult<()> {
let (width, height, channels, alpha) = input.info().whca();
if alpha {
match channels {
2 => {
let img_buf: ImageBuffer<image::LumaA<u8>, &[u8]> = ImageBuffer::from_raw(width, height, input.data())
.ok_or_else(|| ImgIoError::ImageWriteError("ImageBuffer Container is not big enough".to_string()))?;
img_buf.save(filename)?;
},
4 => {
let img_buf: ImageBuffer<image::Rgba<u8>, &[u8]> = ImageBuffer::from_raw(width, height, input.data())
.ok_or_else(|| ImgIoError::ImageWriteError("ImageBuffer Container is not big enough".to_string()))?;
img_buf.save(filename)?;
},
_ => return Err(ImgIoError::UnsupportedColorTypeError("unsupported color type".to_string()))
}
} else {
match channels {
1 => {
let img_buf: ImageBuffer<image::Luma<u8>, &[u8]> = ImageBuffer::from_raw(width, height, input.data())
.ok_or_else(|| ImgIoError::ImageWriteError("ImageBuffer Container is not big enough".to_string()))?;
img_buf.save(filename)?;
},
3 => {
let img_buf: ImageBuffer<image::Rgb<u8>, &[u8]> = ImageBuffer::from_raw(width, height, input.data())
.ok_or_else(|| ImgIoError::ImageWriteError("ImageBuffer Container is not big enough".to_string()))?;
img_buf.save(filename)?;
},
_ => return Err(ImgIoError::UnsupportedColorTypeError("unsupported color type".to_string()))
}
}
Ok(())
}