pub fn from_file<P: AsRef<Path>>(&self, filename: P) -> Result<TgaImage, TgaError> {
        // Open file and read into buffer
        let mut file = File::open(filename).map_err(|e| {FileOpen(e)})?;
        let mut buf = vec![];
        let size = file.read_to_end(&mut buf).map_err(|e| {FileRead(e)})?;
        if size < HEADER_SIZE {
            return Err(InvalidSize);
        }

        // Copy header from file
        let header_buf: [u8; HEADER_SIZE] = buf[0..HEADER_SIZE].try_into().map_err(|_| {InvalidSize})?;
        let header = TgaHeader::from_buf(header_buf)?;

        // Ensure file size is large enough to contain all data specified in the header
        if size < header.file_size() {
            return Err(InvalidSize);
        }

        // Ensure the pixel depth is valid
        if !header.image_type.valid_depth(header.image_bit_depth) {
            return Err(InvalidPixelDepth);
        }

        // Read image id, color map, and image data
        let mut idx = HEADER_SIZE;
        let id = buf[idx..idx + header.id_size as usize].to_vec().into_boxed_slice();
        idx += header.id_size as usize;
        let color_map = buf[idx..idx + header.color_map_size as usize].to_vec().into_boxed_slice();
        idx += header.color_map_size as usize;
        let data = buf[idx..idx + image_size(header.width, header.height, header.image_bit_depth)].to_vec().into_boxed_slice();

        Ok(TgaImage {
            header,
            state: TgaImageState::Uncompressed,
            id,
            color_map,
            data
        })
    }
/// Tries to read a TGA image from a file.
/// 
/// # Errors
/// If the file could not be opened, returns `FileOpen` error.
/// 
/// If the file could not be read, returns `FileRead` error.
/// 
/// If the file is not large enough to contain a TGA header, returns `InvalidSize` error.
/// 
/// If the file is not large enough to contain the TGA image size read from the header, returns `InvalidSize` error.
/// 
/// If the bit depth is invalid for the image type, returns `InvalidPixelDepth` error.
Tries to read a TGA image from a file.
Tries to read a TGA image from a file .
pub fn from_file<P: AsRef<Path>>(&self, filename: P) -> Result<TgaImage, TgaError> {
        // Open file and read into buffer
        let mut file = File::open(filename).map_err(|e| {FileOpen(e)})?;
        let mut buf = vec![];
        let size = file.read_to_end(&mut buf).map_err(|e| {FileRead(e)})?;
        if size < HEADER_SIZE {
            return Err(InvalidSize);
        }
        // Copy header from file
        let header_buf: [u8; HEADER_SIZE] = buf[0..HEADER_SIZE].try_into().map_err(|_| {InvalidSize})?;
        let header = TgaHeader::from_buf(header_buf)?;
        // Ensure file size is large enough to contain all data specified in the header
        if size < header.file_size() {
            return Err(InvalidSize);
        }
        // Ensure the pixel depth is valid
        if !header.image_type.valid_depth(header.image_bit_depth) {
            return Err(InvalidPixelDepth);
        }
        // Read image id, color map, and image data
        let mut idx = HEADER_SIZE;
        let id = buf[idx..idx + header.id_size as usize].to_vec().into_boxed_slice();
        idx += header.id_size as usize;
        let color_map = buf[idx..idx + header.color_map_size as usize].to_vec().into_boxed_slice();
        idx += header.color_map_size as usize;
        let data = buf[idx..idx + image_size(header.width, header.height, header.image_bit_depth)].to_vec().into_boxed_slice();
        Ok(TgaImage {
            header,
            state: TgaImageState::Uncompressed,
            id,
            color_map,
            data
        })
    }
pub fn from_file < P : AsRef < Path > > ( & self , filename : P ) -> Result < TgaImage , TgaError > { // Open file and read into buffer let mut file = File :: open ( filename ) . map_err ( | e | { FileOpen ( e ) } ) ? ; let mut buf = vec ! [ ] ; let size = file . read_to_end ( & mut buf ) . map_err ( | e | { FileRead ( e ) } ) ? ; if size < HEADER_SIZE { return Err ( InvalidSize ) ; } // Copy header from file let header_buf : [ u8 ; HEADER_SIZE ] = buf [ 0 .. HEADER_SIZE ] . try_into ( ) . map_err ( | _ | { InvalidSize } ) ? ; let header = TgaHeader :: from_buf ( header_buf ) ? ; // Ensure file size is large enough to contain all data specified in the header if size < header . file_size ( ) { return Err ( InvalidSize ) ; } // Ensure the pixel depth is valid if ! header . image_type . valid_depth ( header . image_bit_depth ) { return Err ( InvalidPixelDepth ) ; } // Read image id, color map, and image data let mut idx = HEADER_SIZE ; let id = buf [ idx .. idx + header . id_size as usize ] . to_vec ( ) . into_boxed_slice ( ) ; idx += header . id_size as usize ; let color_map = buf [ idx .. idx + header . color_map_size as usize ] . to_vec ( ) . into_boxed_slice ( ) ; idx += header . color_map_size as usize ; let data = buf [ idx .. idx + image_size ( header . width , header . height , header . image_bit_depth ) ] . to_vec ( ) . into_boxed_slice ( ) ; Ok ( TgaImage { header , state : TgaImageState :: Uncompressed , id , color_map , data } ) }
Tries to read a TGA image from a file.
Tries to read a TGA image from a file .
&self
filename
P
&self
filename
P
errors
If the file could not be opened, returns `FileOpen` error.

If the file could not be read, returns `FileRead` error.

If the file is not large enough to contain a TGA header, returns `InvalidSize` error.

If the file is not large enough to contain the TGA image size read from the header, returns `InvalidSize` error.

If the bit depth is invalid for the image type, returns `InvalidPixelDepth` error.
If the file could not be opened returns ` FileOpen ` error . If the file could not be read returns ` FileRead ` error . If the file is not large enough to contain a TGA header returns ` InvalidSize ` error . If the file is not large enough to contain the TGA image size read from the header returns ` InvalidSize ` error . If the bit depth is invalid for the image type returns ` InvalidPixelDepth ` error .