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
//! # JFIF Dump
//!
//! A crate for reading the content of a JFIF file without decoding JPEG image data.
//!
//! ## Example: Print image dimensions
//!
//! ```no_run
//! # use jfifdump::JfifError;
//! # fn main() -> Result<(), JfifError> {
//!
//! use jfifdump::{Reader, Segment};
//! use std::fs::File;
//! use std::io::BufReader;
//!
//! let file = File::open("some.jpeg")?;
//!
//! let mut reader = Reader::new(BufReader::new(file))?;
//!
//! loop {
//!     match reader.next_segment()? {
//!         Segment::Eoi => break,
//!         Segment::Frame(frame) => {
//!             println!("{}x{}", frame.dimension_x, frame.dimension_y);
//!             break;
//!         }
//!         _ => {
//!             // Ignore other segments
//!         }
//!     }
//! }
//!
//! # Ok(())
//! }
//! ```

use std::io::Read;

pub use error::JfifError;
pub use handler::Handler;
pub use reader::{App0Jfif, Dac, Dht, Dqt, Frame, FrameComponent, Reader, Rst, Scan, ScanComponent, Segment};
pub use text::TextFormat;

pub use crate::json::JsonFormat;
use crate::reader::SegmentKind;

mod error;
mod reader;
mod handler;
mod json;
mod text;

/// Read JFIF input and call handler for all segments
pub fn read<H: Handler, R: Read>(input: R, handler: &mut H) -> Result<(), JfifError> {
    let mut reader = Reader::new(input)?;

    loop {
        let segment = reader.next_segment()?;
        match segment.kind {
            SegmentKind::Eoi => break,
            SegmentKind::App { nr, data } => handler.handle_app(segment.position, nr, &data),
            SegmentKind::App0Jfif(jfif) => handler.handle_app0_jfif(segment.position, &jfif),
            SegmentKind::Dqt(tables) => handler.handle_dqt(segment.position, &tables),
            SegmentKind::Dht(tables) => handler.handle_dht(segment.position, &tables),
            SegmentKind::Dac(dac) => handler.handle_dac(segment.position, &dac),
            SegmentKind::Frame(frame) => handler.handle_frame(segment.position, &frame),
            SegmentKind::Scan(scan) => handler.handle_scan(segment.position, &scan),
            SegmentKind::Dri(restart) => handler.handle_dri(segment.position, restart),
            SegmentKind::Rst(rst) => handler.handle_rst(segment.position, &rst),
            SegmentKind::Comment(data) => handler.handle_comment(segment.position, &data),
            SegmentKind::Unknown { marker, data } => handler.handle_unknown(segment.position, marker, &data),
        };
    }

    Ok(())
}