2023-04-19 20:02:01 +02:00
|
|
|
// Copyright (c) 2023 Sophie Herold
|
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
///! Decodes several image formats
|
2023-04-17 22:38:52 +02:00
|
|
|
pub mod formats;
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
pub mod tiling;
|
|
|
|
|
|
2023-03-10 19:08:26 +01:00
|
|
|
pub use formats::ImageDimensionDetails;
|
|
|
|
|
|
2023-03-14 00:10:14 +01:00
|
|
|
use crate::deps::*;
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
use crate::image_metadata::ImageMetadata;
|
2023-04-11 00:02:28 +02:00
|
|
|
use crate::util::gettext::*;
|
2023-04-12 04:13:30 +02:00
|
|
|
use crate::util::ToBufRead;
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
use formats::*;
|
2023-03-15 22:35:59 +01:00
|
|
|
use tiling::FrameBufferExt;
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
|
2023-04-12 04:13:30 +02:00
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
use arc_swap::ArcSwap;
|
|
|
|
|
use futures::channel::mpsc;
|
|
|
|
|
use gio::prelude::*;
|
|
|
|
|
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2023-03-09 22:10:08 +01:00
|
|
|
pub use formats::{ImageFormat, RSVG_MAX_SIZE};
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
/// Renderer requests new tiles
|
|
|
|
|
///
|
|
|
|
|
/// This happens for initial loading or when zoom level or viewing area changes.
|
|
|
|
|
pub struct TileRequest {
|
|
|
|
|
pub viewport: graphene::Rect,
|
2023-04-16 01:36:38 +02:00
|
|
|
pub area: graphene::Rect,
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
pub zoom: f64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
/// Signals update to the renderer
|
|
|
|
|
pub struct UpdateSender {
|
|
|
|
|
sender: mpsc::UnboundedSender<DecoderUpdate>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-03-14 00:10:14 +01:00
|
|
|
/// Signals for renderer (LpImage)
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
pub enum DecoderUpdate {
|
|
|
|
|
/// Dimensions of image in `TilingSore` available/updated
|
2023-03-10 19:08:26 +01:00
|
|
|
Dimensions(ImageDimensionDetails),
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
/// Metadata available
|
|
|
|
|
Metadata(ImageMetadata),
|
|
|
|
|
/// Image format determined
|
|
|
|
|
Format(ImageFormat),
|
|
|
|
|
/// New image data available, redraw
|
|
|
|
|
Redraw,
|
|
|
|
|
/// And error occured during decoding
|
|
|
|
|
Error(anyhow::Error),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpdateSender {
|
|
|
|
|
pub fn send(&self, update: DecoderUpdate) {
|
|
|
|
|
let result = self.sender.unbounded_send(update);
|
|
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
|
log::error!("Failed to send update: {err}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send occuring errors to renderer
|
|
|
|
|
pub fn spawn_error_handled<F>(&self, f: F) -> std::thread::JoinHandle<()>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce() -> Result<(), anyhow::Error> + Send + 'static,
|
|
|
|
|
{
|
|
|
|
|
let update_sender = self.clone();
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
if let Err(err) = f() {
|
|
|
|
|
update_sender.send(DecoderUpdate::Error(err));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Decoder {
|
|
|
|
|
decoder: FormatDecoder,
|
2023-04-10 17:53:15 +02:00
|
|
|
update_sender: UpdateSender,
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum FormatDecoder {
|
|
|
|
|
ImageRsOther(ImageRsOther),
|
|
|
|
|
Svg(Svg),
|
|
|
|
|
Heif(Heif),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Decoder {
|
|
|
|
|
/// Get new image decoder
|
|
|
|
|
///
|
2023-03-15 22:35:59 +01:00
|
|
|
/// The textures will be stored in the passed `TiledImage`.
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
/// The renderer should listen to updates from the returned receiver.
|
2023-03-14 00:10:14 +01:00
|
|
|
pub async fn new(
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
file: gio::File,
|
2023-03-15 22:35:59 +01:00
|
|
|
tiles: Arc<ArcSwap<tiling::FrameBuffer>>,
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
) -> anyhow::Result<(Self, mpsc::UnboundedReceiver<DecoderUpdate>)> {
|
|
|
|
|
let (sender, receiver) = mpsc::unbounded();
|
|
|
|
|
|
|
|
|
|
let update_sender = UpdateSender { sender };
|
|
|
|
|
tiles.set_update_sender(update_sender.clone());
|
|
|
|
|
|
2023-04-10 17:53:15 +02:00
|
|
|
let decoder = gio::spawn_blocking(
|
|
|
|
|
glib::clone!(@strong update_sender => move || Self::format_decoder(
|
|
|
|
|
update_sender,
|
|
|
|
|
file,
|
|
|
|
|
tiles
|
|
|
|
|
)),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| anyhow!("Constructing the FormatDecoder failed unexpectedly"))??;
|
|
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
Self {
|
|
|
|
|
decoder,
|
|
|
|
|
update_sender,
|
|
|
|
|
},
|
|
|
|
|
receiver,
|
|
|
|
|
))
|
2023-03-14 00:10:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn format_decoder(
|
|
|
|
|
update_sender: UpdateSender,
|
2023-04-12 04:13:30 +02:00
|
|
|
file: gio::File,
|
2023-03-15 22:35:59 +01:00
|
|
|
tiles: Arc<ArcSwap<tiling::FrameBuffer>>,
|
2023-03-14 00:10:14 +01:00
|
|
|
) -> anyhow::Result<FormatDecoder> {
|
2023-04-12 04:13:30 +02:00
|
|
|
update_sender.send(DecoderUpdate::Metadata(ImageMetadata::load(&file)));
|
2023-03-14 00:10:14 +01:00
|
|
|
|
2023-04-12 04:13:30 +02:00
|
|
|
let format = Self::guess_format(&file)?;
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
|
2023-04-12 04:13:30 +02:00
|
|
|
if let Some(format) = format {
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
match format {
|
|
|
|
|
image_rs::ImageFormat::Avif => {
|
|
|
|
|
update_sender.send(DecoderUpdate::Format(ImageFormat::Heif));
|
2023-04-12 04:13:30 +02:00
|
|
|
return Ok(FormatDecoder::Heif(Heif::new(file, update_sender, tiles)));
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
}
|
|
|
|
|
format => {
|
|
|
|
|
update_sender.send(DecoderUpdate::Format(ImageFormat::ImageRs(format)));
|
2023-03-14 00:10:14 +01:00
|
|
|
return Ok(FormatDecoder::ImageRsOther(ImageRsOther::new(
|
2023-04-12 04:13:30 +02:00
|
|
|
file,
|
2023-03-14 00:10:14 +01:00
|
|
|
format,
|
|
|
|
|
update_sender,
|
|
|
|
|
tiles,
|
|
|
|
|
)));
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-04-12 04:13:30 +02:00
|
|
|
let file_info = file
|
2023-03-14 00:10:14 +01:00
|
|
|
.query_info(
|
|
|
|
|
gio::FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
|
|
|
|
|
gio::FileQueryInfoFlags::NONE,
|
|
|
|
|
gio::Cancellable::NONE,
|
|
|
|
|
)
|
|
|
|
|
.context("Could not read content type information")?;
|
|
|
|
|
if let Some(content_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 content_type.split('+').next() == Some("image/svg") {
|
|
|
|
|
update_sender.send(DecoderUpdate::Format(ImageFormat::Svg));
|
2023-04-12 04:13:30 +02:00
|
|
|
return Ok(FormatDecoder::Svg(Svg::new(file, update_sender, tiles)));
|
2023-04-06 22:01:36 +02:00
|
|
|
} else if ["image/avif", "image/heif", "image/heic"]
|
|
|
|
|
.contains(&content_type.as_str())
|
|
|
|
|
{
|
|
|
|
|
update_sender.send(DecoderUpdate::Format(ImageFormat::Heif));
|
2023-04-12 04:13:30 +02:00
|
|
|
return Ok(FormatDecoder::Heif(Heif::new(file, update_sender, tiles)));
|
2023-03-14 00:10:14 +01:00
|
|
|
} else {
|
2023-04-11 00:02:28 +02:00
|
|
|
bail!(gettext_f(
|
|
|
|
|
"Unknown image format: {}",
|
|
|
|
|
&[content_type.as_str()]
|
|
|
|
|
));
|
2023-03-14 00:10:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
|
2023-04-11 00:02:28 +02:00
|
|
|
bail!(gettext("Unknown image format"))
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Request missing tiles
|
|
|
|
|
pub fn request(&self, tile_request: TileRequest) {
|
|
|
|
|
match &self.decoder {
|
2023-04-10 17:53:15 +02:00
|
|
|
FormatDecoder::Svg(svg) => {
|
|
|
|
|
if let Err(err) = svg.request(tile_request) {
|
|
|
|
|
self.update_sender.send(DecoderUpdate::Error(err));
|
|
|
|
|
}
|
|
|
|
|
}
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
FormatDecoder::Heif(_) => {}
|
|
|
|
|
_ => {}
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-03-15 22:35:59 +01:00
|
|
|
|
|
|
|
|
pub fn fill_frame_buffer(&self) {
|
|
|
|
|
if let FormatDecoder::ImageRsOther(decoder) = &self.decoder {
|
|
|
|
|
decoder.fill_frame_buffer();
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("Trying to fill frame buffer for decoder without animation support");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-12 04:13:30 +02:00
|
|
|
|
|
|
|
|
fn guess_format(file: &gio::File) -> anyhow::Result<Option<image_rs::ImageFormat>> {
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
let fs_file = file
|
|
|
|
|
.to_buf_read()
|
|
|
|
|
.context(gettext("Could not open image"))?;
|
|
|
|
|
fs_file
|
|
|
|
|
.take(64)
|
|
|
|
|
.read_to_end(&mut buf)
|
|
|
|
|
.context(gettext("Could not read image"))?;
|
|
|
|
|
|
|
|
|
|
// Try magic bytes first and than file name extension
|
|
|
|
|
|
|
|
|
|
if let Ok(format) = image_rs::guess_format(&buf) {
|
|
|
|
|
Ok(Some(format))
|
|
|
|
|
} else if let Some(basename) = file.basename() {
|
|
|
|
|
Ok(image_rs::ImageFormat::from_path(basename).ok())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
decoder: Use image-rs, libheif-rs, librsvg
This adds proper support for a lot of image formats including SVGs.
It will be the basis for many features to come. The technical core
is an asynchronous renderer that can arrange textures in smaller tiles
which might be generated on demand.
Closes #34, #40, #41, #49
Basis for fixing the following issues #7, #27, #36, #43, #68, #71
2023-03-04 03:14:52 +01:00
|
|
|
}
|