image-rs / jpeg-decoder

JPEG decoder written in Rust
Apache License 2.0
149 stars 87 forks source link

Add support for extracting Exif data #182

Closed fseegraeber closed 3 years ago

fseegraeber commented 3 years ago

Implements #42.

Also a first step towards image#1045.

Exposes Exif data as an Option<Vec<u8>> that can be parsed by client code e.g. using kamadak-exif (or simply to pass it on when saving the file again):

    let path = Path::new("tests")
        .join("reftest")
        .join("images")
        .join("ycck.jpg");

    let mut decoder = jpeg::Decoder::new(File::open(&path).unwrap());
    decoder.decode().unwrap();

    let exif_data = decoder.exif_data().unwrap();
    let exif_reader = exif::Reader::new();
    let exif = exif_reader.read_raw(exif_data).unwrap();
    for field in exif.fields() {
        println!("{}", field.display_value());
    }

Not 100% sure about the exif_data() method. Since Exif might contain up to 64kb a copy might be a bit much. Perhaps:

pub fn take_exif_data(&mut self) -> Option<Vec<u8>> {
    self.exif_data.take()
}

or

pub fn exif_data(&self) -> Option<&Vec<u8>> {
    self.exif_data.as_ref()
}

I guess exif data might be inspected multiple times e.g. to determine orientation?

HeroicKatora commented 3 years ago

I must have missed the force pushed commit addressing the review comment. @fseegraeber Pinging a maintainer when a PR is in limbo is absolutely encouraged :slightly_smiling_face:

fseegraeber commented 3 years ago

Will do! 😄