1use std::io;
2
3use io::BufRead;
4use io::Seek;
5
6use io::Read;
7
8use io::Cursor;
9
10use io::BufWriter;
11use io::Write;
12
13use image_webp::WebPDecoder;
14
15pub fn reader2exif<R>(webp_rdr: R) -> Result<Option<Vec<u8>>, io::Error>
16where
17 R: BufRead + Seek,
18{
19 let mut dec: WebPDecoder<_> = WebPDecoder::new(webp_rdr).map_err(io::Error::other)?;
20 dec.exif_metadata().map_err(io::Error::other)
21}
22
23pub fn bytes2exif(bytes: &[u8]) -> Result<Option<Vec<u8>>, io::Error> {
24 let cur = Cursor::new(bytes);
25 reader2exif(cur)
26}
27
28pub fn exif2wtr<W>(mut wtr: W, ebytes: &[u8]) -> Result<(), io::Error>
29where
30 W: Write,
31{
32 wtr.write_all(ebytes)?;
33 wtr.flush()
34}
35
36pub fn exif2stdout(ebytes: &[u8]) -> Result<(), io::Error> {
37 let o = io::stdout();
38 let mut ol = o.lock();
39 exif2wtr(BufWriter::new(&mut ol), ebytes)?;
40 ol.flush()
41}
42
43pub fn stdin2exif(limit: u64) -> Result<Option<Vec<u8>>, io::Error> {
44 let i = io::stdin();
45 let il = i.lock();
46 let mut limited = il.take(limit);
47 let mut buf: Vec<u8> = vec![];
48 limited.read_to_end(&mut buf)?;
49 bytes2exif(&buf)
50}
51
52pub fn stdin2exif2stdout(limit: u64) -> Result<(), io::Error> {
53 let oexif: Option<Vec<u8>> = stdin2exif(limit)?;
54 match oexif {
55 None => Ok(()),
56 Some(ebytes) => exif2stdout(&ebytes),
57 }
58}