Skip to main content

simp_rexif/
lib.rs

1#![allow(clippy::upper_case_acronyms)]
2
3//! RExif is a native Rust create, written to extract EXIF data from JPEG and TIFF images.
4//!
5//! Note that it is in very early stages of development. Any sort of feedback is welcome!
6//!
7//! The crate contains a
8//! sample binary called 'rexiftool' that accepts files as arguments and prints the EXIF data. It gives
9//! a rough idea on how to use the crate. Get some sample images and run
10//!
11//!
12//! `cargo run [image file 1] [image file 2] ...`
13//!
14//!
15//! To learn to use this crate, start by the documentation of function `parse_file()`,
16//! and the struct `ExifData` that is returned by the parser. The rest falls more or less into place.
17//!
18//! Code sample lightly edited from src/bin.rs:
19//!
20//! ```
21//! use std::error::Error;
22//!
23//! let file_name = "foo.jpg";
24//! match rexif::parse_file(&file_name) {
25//!     Ok(exif) => {
26//!         println!("{} {} exif entries: {}", file_name, exif.mime, exif.entries.len());
27//!         for entry in &exif.entries {
28//!             println!("\t{}: {}", entry.tag, entry.value_more_readable);
29//!         }
30//!     },
31//!     Err(e) => {
32//!         eprintln!("Error in {}: {}", &file_name, e)
33//!     },
34//! }
35//! ```
36
37use std::fs::File;
38use std::io::{Read, Seek, SeekFrom};
39use std::path::Path;
40
41mod lowlevel;
42mod rational;
43pub use self::rational::*;
44mod types;
45pub use self::types::*;
46mod types_impl;
47pub use self::types_impl::*;
48mod image;
49use self::image::*;
50mod ifdformat;
51mod tiff;
52use self::tiff::parse_tiff;
53mod exif;
54mod exifpost;
55mod exifreadable;
56
57/// Parse a byte buffer that should contain a TIFF or JPEG image.
58/// Tries to detect format and parse EXIF data.
59///
60/// Prints warnings to stderr.
61pub fn parse_buffer(contents: &[u8]) -> ExifResult {
62    let (res, _warnings) = parse_buffer_quiet(contents);
63    res
64}
65
66/// Parse a byte buffer that should contain a TIFF or JPEG image.
67/// Tries to detect format and parse EXIF data.
68///
69/// Returns warnings alongside result.
70pub fn parse_buffer_quiet(contents: &[u8]) -> (ExifResult, Vec<String>) {
71    let mime = detect_type(contents);
72    let mut warnings = vec![];
73    let (entries, le) = match mime {
74        FileType::Unknown => return (Err(ExifError::FileTypeUnknown), warnings),
75        FileType::TIFF => parse_tiff(contents, &mut warnings),
76        FileType::JPEG => {
77            match find_embedded_tiff_in_jpeg(contents)
78                .map(|(offset, size)| parse_tiff(&contents[offset..offset + size], &mut warnings))
79            {
80                Ok(r) => r,
81                Err(e) => return (Err(e), warnings),
82            }
83        }
84    };
85
86    (
87        entries.map(|entries| ExifData {
88            mime: mime.as_str(),
89            entries,
90            le,
91        }),
92        warnings,
93    )
94}
95
96/// Try to read and parse an open file that is expected to contain an image
97pub fn read_file(f: &mut File) -> ExifResult {
98    f.seek(SeekFrom::Start(0))?;
99
100    // TODO: should read only the relevant parts of a file,
101    // and pass a StringIO-like object instead of a Vec buffer
102
103    let mut contents: Vec<u8> = Vec::new();
104    f.read_to_end(&mut contents)?;
105    parse_buffer(&contents)
106}
107
108/// Opens an image (passed as a file name), tries to read and parse it.
109pub fn parse_file<P: AsRef<Path>>(fname: P) -> ExifResult {
110    read_file(&mut File::open(fname)?)
111}