Skip to main content

ai_image/
io.rs

1//! Input and output of images.
2
3#[cfg(any(feature = "tga", feature = "dds", feature = "bmp"))]
4use alloc::vec::Vec;
5#[cfg(any(feature = "tga", feature = "dds", feature = "bmp"))]
6use no_std_io::io;
7
8/// The decoder traits.
9pub(crate) mod decoder;
10/// The encoder traits.
11pub(crate) mod encoder;
12
13pub(crate) mod format;
14pub(crate) mod free_functions;
15#[cfg(feature = "std")]
16pub(crate) mod image_reader_type;
17pub(crate) mod limits;
18
19#[cfg(feature = "std")]
20#[deprecated(note = "this type has been moved and renamed to ai_image::ImageReader")]
21/// Deprecated re-export of `ImageReader` as `Reader`
22pub type Reader<R> = ImageReader<R>;
23#[deprecated(note = "this type has been moved to ai_image::Limits")]
24/// Deprecated re-export of `Limits`
25pub type Limits = limits::Limits;
26#[deprecated(note = "this type has been moved to ai_image::LimitSupport")]
27/// Deprecated re-export of `LimitSupport`
28pub type LimitSupport = limits::LimitSupport;
29
30#[cfg(feature = "std")]
31pub(crate) use self::image_reader_type::ImageReader;
32
33/// Adds `read_exact_vec`
34#[cfg(any(feature = "tga", feature = "dds", feature = "bmp"))]
35pub(crate) trait ReadExt {
36    fn read_exact_vec(&mut self, vec: &mut Vec<u8>, len: usize) -> io::Result<()>;
37}
38
39#[cfg(any(feature = "tga", feature = "dds", feature = "bmp"))]
40impl<R: io::Read> ReadExt for R {
41    fn read_exact_vec(&mut self, vec: &mut Vec<u8>, len: usize) -> io::Result<()> {
42        let initial_len = vec.len();
43        // no_std_io::Error doesn't provide other()
44        #[allow(clippy::io_other_error)]
45        vec.try_reserve(len)
46            .map_err(|_| io::Error::new(io::ErrorKind::Other, "allocation failed"))?;
47        vec.resize(initial_len + len, 0);
48        match self.read_exact(&mut vec[initial_len..]) {
49            Ok(()) => Ok(()),
50            Err(e) => {
51                vec.truncate(initial_len);
52                Err(e)
53            }
54        }
55    }
56}