metadata: Move file info and format to metadata

This commit is contained in:
Sophie Herold 2023-12-04 12:52:40 +01:00
parent c36e7f4eb6
commit c0f435386d
16 changed files with 327 additions and 339 deletions

View file

@ -19,7 +19,7 @@
use super::*;
use crate::decoder::tiling::{self, FrameBufferExt};
use crate::deps::*;
use crate::image_metadata::ImageMetadata;
use crate::metadata::{ImageFormat, Metadata};
use arc_swap::ArcSwap;
use gtk::prelude::*;
@ -62,8 +62,9 @@ impl Glycin {
let image = image_request.request().await?;
if let Some(exif_raw) = image.info().exif.clone().into() {
let mut metadata = ImageMetadata::from_exif_bytes(exif_raw);
metadata.heif_transform = image.info().transformations_applied;
let mut metadata = Metadata::default();
metadata.set_exif_bytes(exif_raw);
metadata.set_transformations_applied(image.info().transformations_applied);
updater.send(DecoderUpdate::Metadata(metadata));
}

View file

@ -23,35 +23,6 @@ pub use svg::{Svg, RSVG_MAX_SIZE};
use super::{DecoderUpdate, UpdateSender};
#[derive(Clone, Debug)]
pub struct ImageFormat {
mime_type: glycin::MimeType,
name: String,
}
impl ImageFormat {
pub fn new(mime_type: glycin::MimeType, name: String) -> Self {
Self { mime_type, name }
}
pub fn is_svg(&self) -> bool {
matches!(
self.mime_type.as_str(),
"image/svg+xml" | "image/svg+xml-compressed"
)
}
pub fn is_potentially_transparent(&self) -> bool {
!matches!(self.mime_type.as_str(), "image/bmp" | "image/jpeg")
}
}
impl std::fmt::Display for ImageFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.name)
}
}
#[derive(Clone, Debug, Default)]
pub enum ImageDimensionDetails {
Svg(String),

View file

@ -20,6 +20,7 @@ use super::*;
use crate::decoder::tiling::{self, FrameBufferExt};
use crate::decoder::TileRequest;
use crate::deps::*;
use crate::metadata::ImageFormat;
use anyhow::Context;
use arc_swap::ArcSwap;

View file

@ -23,18 +23,17 @@ pub mod tiling;
pub use formats::ImageDimensionDetails;
use crate::deps::*;
use crate::image_metadata::ImageMetadata;
use crate::metadata::{ImageFormat, Metadata};
use formats::*;
use tiling::FrameBufferExt;
use anyhow::Result;
use arc_swap::ArcSwap;
use futures::channel::mpsc;
use gio::prelude::*;
use std::sync::Arc;
pub use formats::{ImageFormat, RSVG_MAX_SIZE};
pub use formats::RSVG_MAX_SIZE;
#[derive(Clone, Copy, Debug)]
/// Renderer requests new tiles
@ -58,7 +57,7 @@ pub enum DecoderUpdate {
/// Dimensions of image in `TilingSore` available/updated
Dimensions(ImageDimensionDetails),
/// Metadata available
Metadata(ImageMetadata),
Metadata(Metadata),
/// Image format determined
Format(ImageFormat),
/// Image format not supported or unknown
@ -120,6 +119,7 @@ impl Decoder {
/// The renderer should listen to updates from the returned receiver.
pub async fn new(
file: gio::File,
mime_type: Option<String>,
tiles: Arc<ArcSwap<tiling::FrameBuffer>>,
) -> (Self, mpsc::UnboundedReceiver<DecoderUpdate>) {
let (sender, receiver) = mpsc::unbounded();
@ -127,7 +127,7 @@ impl Decoder {
let update_sender = UpdateSender { sender };
tiles.set_update_sender(update_sender.clone());
let format_decoder = Self::format_decoder(update_sender.clone(), file, tiles);
let format_decoder = Self::format_decoder(update_sender.clone(), file, mime_type, tiles);
let decoder = Self {
decoder: format_decoder,
update_sender,
@ -139,28 +139,19 @@ impl Decoder {
fn format_decoder(
update_sender: UpdateSender,
file: gio::File,
mime_type: Option<String>,
tiles: Arc<ArcSwap<tiling::FrameBuffer>>,
) -> FormatDecoder {
if let Ok(file_info) = file.query_info(
gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
gio::FileQueryInfoFlags::NONE,
gio::Cancellable::NONE,
) {
if let Some(mime_type) = file_info
.content_type()
.as_ref()
.and_then(|x| gio::content_type_get_mime_type(x))
{
// Known things we want to match here are
// - image/svg+xml
// - image/svg+xml-compressed
if mime_type.split('+').next() == Some("image/svg") {
update_sender.send(DecoderUpdate::Format(ImageFormat::new(
mime_type.into(),
"SVG".into(),
)));
return FormatDecoder::Svg(Svg::new(file, update_sender, tiles));
}
if let Some(mime_type) = mime_type {
// Known things we want to match here are
// - image/svg+xml
// - image/svg+xml-compressed
if mime_type.split('+').next() == Some("image/svg") {
update_sender.send(DecoderUpdate::Format(ImageFormat::new(
mime_type,
"SVG".into(),
)));
return FormatDecoder::Svg(Svg::new(file, update_sender, tiles));
}
}