metadata: Use gufo instead of kamadak exif
A bunch of metadata code is now provided by gufo.
This commit is contained in:
parent
51b4ddbc5c
commit
5409203ec9
12 changed files with 212 additions and 438 deletions
315
src/metadata.rs
315
src/metadata.rs
|
|
@ -1,5 +1,4 @@
|
|||
// Copyright (c) 2022-2024 Sophie Herold
|
||||
// Copyright (c) 2023 FineFindus
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -22,59 +21,38 @@
|
|||
*/
|
||||
|
||||
mod file;
|
||||
mod gps;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use chrono::prelude::*;
|
||||
pub use file::FileInfo;
|
||||
use glib::TimeZone;
|
||||
use glycin::{Frame, FrameDetails, ImageInfoDetails, MemoryFormat};
|
||||
pub use gps::GPSLocation;
|
||||
use gufo::common::datetime::DateTime;
|
||||
use gufo::common::orientation::Orientation;
|
||||
|
||||
use crate::deps::*;
|
||||
use crate::util;
|
||||
use crate::util::gettext::*;
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Metadata {
|
||||
mime_type: Option<String>,
|
||||
exif: Option<exif::Exif>,
|
||||
exif: gufo::Metadata,
|
||||
file_info: Option<FileInfo>,
|
||||
image_info: Option<glycin::ImageInfoDetails>,
|
||||
frame_info: Option<FrameDetails>,
|
||||
memory_format: Option<MemoryFormat>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Metadata {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
if let Some(exif) = &self.exif {
|
||||
let list = exif.fields().map(|f| {
|
||||
let mut value = f.display_value().to_string();
|
||||
// Remove long values
|
||||
if value.len() > 100 {
|
||||
value = String::from("…");
|
||||
}
|
||||
|
||||
(f.ifd_num.to_string(), f.tag.to_string(), value)
|
||||
});
|
||||
fmt.write_str("Exif")?;
|
||||
fmt.debug_list().entries(list).finish()
|
||||
} else {
|
||||
fmt.write_str("Empty")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
fn set_exif_bytes(&mut self, bytes: Vec<u8>) {
|
||||
let reader = exif::Reader::new();
|
||||
let exif = reader.read_raw(bytes);
|
||||
let result = self.exif.add_raw_exif(bytes);
|
||||
|
||||
if let Err(err) = &exif {
|
||||
if let Err(err) = &result {
|
||||
log::warn!("Failed to decode EXIF bytes: {err}");
|
||||
}
|
||||
|
||||
self.exif = exif.ok();
|
||||
}
|
||||
|
||||
pub fn set_image_info(&mut self, image_info: ImageInfoDetails) {
|
||||
|
|
@ -98,7 +76,7 @@ impl Metadata {
|
|||
}
|
||||
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
if self.exif.is_none() {
|
||||
if self.exif.is_empty() {
|
||||
self.exif = other.exif;
|
||||
}
|
||||
if self.file_info.is_none() {
|
||||
|
|
@ -165,18 +143,9 @@ impl Metadata {
|
|||
pub fn orientation(&self) -> Orientation {
|
||||
if self.transformations_applied() {
|
||||
// HEIF library already does it's transformations on its own
|
||||
Orientation::default()
|
||||
} else if let Some(exif) = &self.exif {
|
||||
if let Some(orientation) = exif
|
||||
.get_field(exif::Tag::Orientation, exif::In::PRIMARY)
|
||||
.and_then(|x| x.value.get_uint(0))
|
||||
{
|
||||
Orientation::from(orientation)
|
||||
} else {
|
||||
Orientation::default()
|
||||
}
|
||||
Orientation::Id
|
||||
} else {
|
||||
Orientation::default()
|
||||
self.exif.orientation().unwrap_or(Orientation::Id)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,44 +193,41 @@ impl Metadata {
|
|||
}
|
||||
|
||||
pub fn originally_created(&self) -> Option<String> {
|
||||
if let Some(exif) = &self.exif {
|
||||
if let Some(field) = exif
|
||||
.get_field(exif::Tag::DateTimeOriginal, exif::In::PRIMARY)
|
||||
.or_else(|| exif.get_field(exif::Tag::DateTime, exif::In::PRIMARY))
|
||||
{
|
||||
if let exif::Value::Ascii(ascii_vec) = &field.value {
|
||||
if let Some(ascii) = ascii_vec.first() {
|
||||
if let Ok(dt) = exif::DateTime::from_ascii(ascii) {
|
||||
if let Ok(datetime) = glib::DateTime::from_local(
|
||||
dt.year.into(),
|
||||
dt.month.into(),
|
||||
dt.day.into(),
|
||||
dt.hour.into(),
|
||||
dt.minute.into(),
|
||||
dt.second.into(),
|
||||
) {
|
||||
return util::datetime_fmt(&datetime);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(date_time) = &self.exif.date_time_original() {
|
||||
let glib_date_time = match date_time {
|
||||
DateTime::Naive(naive) => {
|
||||
glib::DateTime::from_unix_local(naive.and_utc().timestamp()).ok()
|
||||
}
|
||||
DateTime::FixedOffset(fixed) => {
|
||||
let naive = fixed.naive_utc();
|
||||
glib::DateTime::new(
|
||||
&TimeZone::from_offset(fixed.timezone().local_minus_utc() * 60),
|
||||
naive.year(),
|
||||
naive.month() as i32,
|
||||
naive.day() as i32,
|
||||
naive.hour() as i32,
|
||||
naive.minute() as i32,
|
||||
naive.second() as f64,
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
};
|
||||
|
||||
return Some(field.display_value().to_string());
|
||||
if let Some(glib_date_time) = glib_date_time {
|
||||
util::datetime_fmt(&glib_date_time)
|
||||
} else {
|
||||
Some(date_time.to_string())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn f_number(&self) -> Option<String> {
|
||||
if let Some(field) = self
|
||||
.exif
|
||||
.as_ref()
|
||||
.and_then(|exif| exif.get_field(exif::Tag::FNumber, exif::In::PRIMARY))
|
||||
{
|
||||
if let Some(f_number) = self.exif.f_number() {
|
||||
return Some(
|
||||
// Translators: Display of the f-number <https://en.wikipedia.org/wiki/F-number>. {} will be replaced with the number
|
||||
gettext_f(r"\u{192}\u{2215}{}", [field.display_value().to_string()]),
|
||||
gettext_f(r"\u{192}\u{2215}{}", [f_number.to_string()]),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -269,53 +235,34 @@ impl Metadata {
|
|||
}
|
||||
|
||||
pub fn exposure_time(&self) -> Option<String> {
|
||||
if let Some(exif) = &self.exif {
|
||||
let field = exif.get_field(exif::Tag::ExposureTime, exif::In::PRIMARY)?;
|
||||
if let exif::Value::Rational(rational) = &field.value {
|
||||
let speed = rational.first()?.to_f32();
|
||||
|
||||
if speed <= 0.5 {
|
||||
let exposure = format!("{:.0}", 1. / speed);
|
||||
// Translators: Fractional exposure time (photography) in seconds
|
||||
return Some(gettext_f(r"1\u{2215}{}\u{202F}s", [exposure]));
|
||||
if let Some((num, denom)) = self.exif.exposure_time() {
|
||||
let speed = num as f64 / denom as f64;
|
||||
if speed <= 0.5 {
|
||||
let exposure = format!("{:.0}", 1. / speed);
|
||||
// Translators: Fractional exposure time (photography) in seconds
|
||||
Some(gettext_f(r"1\u{2215}{}\u{202F}s", [exposure]))
|
||||
} else {
|
||||
let exposure = if speed < 5. {
|
||||
format!("{:.1}", speed)
|
||||
} else {
|
||||
let exposure = if speed < 5. {
|
||||
format!("{:.1}", speed)
|
||||
} else {
|
||||
format!("{:.0}", speed)
|
||||
};
|
||||
// Translators: Exposure time (photography) in seconds
|
||||
return Some(gettext_f(r"{}\u{202F}s", [exposure]));
|
||||
}
|
||||
format!("{:.0}", speed)
|
||||
};
|
||||
// Translators: Exposure time (photography) in seconds
|
||||
Some(gettext_f(r"{}\u{202F}s", [exposure]))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn iso(&self) -> Option<String> {
|
||||
if let Some(exif) = &self.exif {
|
||||
if let Some(field) =
|
||||
exif.get_field(exif::Tag::PhotographicSensitivity, exif::In::PRIMARY)
|
||||
{
|
||||
return Some(field.display_value().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
self.exif.iso_speed_rating().map(|iso| iso.to_string())
|
||||
}
|
||||
|
||||
pub fn focal_length(&self) -> Option<String> {
|
||||
if let Some(exif) = &self.exif {
|
||||
let field = exif.get_field(exif::Tag::FocalLength, exif::In::PRIMARY)?;
|
||||
if let exif::Value::Rational(rational) = &field.value {
|
||||
let length = format!("{:.0}", rational.first()?.to_f32());
|
||||
// Translators: Unit for focal length in millimeters
|
||||
return Some(gettext_f(r"{}\u{202F}mm", [length]));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
self.exif
|
||||
.focal_length()
|
||||
.map(|focal_length| gettext_f(r"{}\u{202F}mm", [focal_length.to_string()]))
|
||||
}
|
||||
|
||||
/// Combined maker and model info
|
||||
|
|
@ -337,99 +284,77 @@ impl Metadata {
|
|||
}
|
||||
|
||||
pub fn model(&self) -> Option<String> {
|
||||
if let Some(exif) = &self.exif {
|
||||
if let Some(field) = exif.get_field(exif::Tag::Model, exif::In::PRIMARY) {
|
||||
if let exif::Value::Ascii(value) = &field.value {
|
||||
if let Some(entry) = value.first() {
|
||||
return Some(String::from_utf8_lossy(entry).to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
self.exif.model()
|
||||
}
|
||||
|
||||
pub fn maker(&self) -> Option<String> {
|
||||
if let Some(exif) = &self.exif {
|
||||
if let Some(field) = exif.get_field(exif::Tag::Make, exif::In::PRIMARY) {
|
||||
if let exif::Value::Ascii(value) = &field.value {
|
||||
if let Some(entry) = value.first() {
|
||||
return Some(String::from_utf8_lossy(entry).to_string());
|
||||
self.exif.make()
|
||||
}
|
||||
|
||||
pub fn gps_location(&self) -> Option<gufo::common::geography::Location> {
|
||||
self.exif.gps_location()
|
||||
}
|
||||
|
||||
pub fn gps_location_display(&self) -> Option<String> {
|
||||
if let Some(location) = self.gps_location() {
|
||||
let (lat_ref, (lat_deg, lat_min, lat_sec)) = location.lat_ref_deg_min_sec();
|
||||
|
||||
let lat_ref = match lat_ref {
|
||||
gufo::common::geography::LatRef::North => {
|
||||
// Translators: short for "north" in GPS coordinate
|
||||
gettext("N")
|
||||
}
|
||||
gufo::common::geography::LatRef::South => {
|
||||
// Translators: short for "south" in GPS coordinate
|
||||
gettext("S")
|
||||
}
|
||||
};
|
||||
|
||||
let (lon_ref, (lon_deg, lon_min, lon_sec)) = location.lon_ref_deg_min_sec();
|
||||
|
||||
let lon_ref = match lon_ref {
|
||||
gufo::common::geography::LonRef::East => {
|
||||
// Translators: short for "east" in GPS coordinate
|
||||
gettext("E")
|
||||
}
|
||||
gufo::common::geography::LonRef::West => {
|
||||
// Translators: short for "west" in GPS coordinate
|
||||
gettext("W")
|
||||
}
|
||||
};
|
||||
|
||||
let lat = format!("{lat_deg}° {lat_min}′ {lat_sec:05.2}″ {lat_ref}");
|
||||
let lon = format!("{lon_deg}° {lon_min}′ {lon_sec:05.2}″ {lon_ref}");
|
||||
Some(format!("{lat}\n{lon}"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Outputs location as "City, County" if possible
|
||||
///
|
||||
/// Falls back to coordinates
|
||||
pub fn gps_location_place(&self) -> Option<String> {
|
||||
if let Some(location) = self.gps_location() {
|
||||
if let Some(world) = libgweather::Location::world() {
|
||||
let lat = location.lat.0;
|
||||
let lon = location.lon.0;
|
||||
let nearest_city = world.find_nearest_city(lat, lon);
|
||||
let location_exact = libgweather::Location::new_detached("", None, lat, lon);
|
||||
|
||||
// do not use city if more than 15 km away
|
||||
if nearest_city.distance(&location_exact) < 15. {
|
||||
if let (Some(city), Some(country)) =
|
||||
(nearest_city.city_name(), nearest_city.country_name())
|
||||
{
|
||||
return Some(format!("{city}, {country}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn gps_location(&self) -> Option<GPSLocation> {
|
||||
if let Some(exif) = &self.exif {
|
||||
if let (Some(latitude), Some(latitude_ref), Some(longitude), Some(longitude_ref)) = (
|
||||
exif.get_field(exif::Tag::GPSLatitude, exif::In::PRIMARY),
|
||||
exif.get_field(exif::Tag::GPSLatitudeRef, exif::In::PRIMARY),
|
||||
exif.get_field(exif::Tag::GPSLongitude, exif::In::PRIMARY),
|
||||
exif.get_field(exif::Tag::GPSLongitudeRef, exif::In::PRIMARY),
|
||||
) {
|
||||
if let (
|
||||
exif::Value::Rational(latitude),
|
||||
exif::Value::Ascii(latitude_ref),
|
||||
exif::Value::Rational(longitude),
|
||||
exif::Value::Ascii(longitude_ref),
|
||||
) = (
|
||||
&latitude.value,
|
||||
&latitude_ref.value,
|
||||
&longitude.value,
|
||||
&longitude_ref.value,
|
||||
) {
|
||||
return GPSLocation::for_exif(latitude, latitude_ref, longitude, longitude_ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
pub struct Orientation {
|
||||
pub rotation: f64,
|
||||
pub mirrored: bool,
|
||||
}
|
||||
|
||||
impl From<u32> for Orientation {
|
||||
fn from(number: u32) -> Self {
|
||||
match number {
|
||||
8 => Self {
|
||||
rotation: 90.,
|
||||
mirrored: false,
|
||||
},
|
||||
3 => Self {
|
||||
rotation: 180.,
|
||||
mirrored: false,
|
||||
},
|
||||
6 => Self {
|
||||
rotation: 270.,
|
||||
mirrored: false,
|
||||
},
|
||||
2 => Self {
|
||||
rotation: 0.,
|
||||
mirrored: true,
|
||||
},
|
||||
5 => Self {
|
||||
rotation: 90.,
|
||||
mirrored: true,
|
||||
},
|
||||
4 => Self {
|
||||
rotation: 180.,
|
||||
mirrored: true,
|
||||
},
|
||||
7 => Self {
|
||||
rotation: 270.,
|
||||
mirrored: true,
|
||||
},
|
||||
_ => Self::default(),
|
||||
self.gps_location_display()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue