refactor: move kra to crate
in the interest of reusing that code in a gallery app
This commit is contained in:
parent
db457fc4e7
commit
603678ba34
4 changed files with 14 additions and 43 deletions
40
src/kra.rs
40
src/kra.rs
|
|
@ -1,40 +0,0 @@
|
|||
use std::{
|
||||
ffi::OsString,
|
||||
io::{self, Read},
|
||||
};
|
||||
|
||||
use image::{
|
||||
ImageDecoder, ImageError, ImageReader, ImageResult,
|
||||
error::DecodingError,
|
||||
hooks::{self, GenericReader},
|
||||
};
|
||||
use zip::ZipArchive;
|
||||
|
||||
pub fn register() -> bool {
|
||||
hooks::register_decoding_hook(OsString::from("kra"), Box::new(hook))
|
||||
}
|
||||
|
||||
fn hook<'a>(reader: GenericReader<'a>) -> ImageResult<Box<dyn ImageDecoder + 'a>> {
|
||||
let mut zip = ZipArchive::new(reader).map_err(to_image_error)?;
|
||||
|
||||
let mut reader = zip.by_name("mergedimage.png").map_err(to_image_error)?;
|
||||
|
||||
// reading it all and wrapping it with a Cursor
|
||||
// is the only way I know to give it Seek
|
||||
// (which is required by ImageReader)
|
||||
let mut buf = Vec::new();
|
||||
reader.read_to_end(&mut buf)?;
|
||||
|
||||
let image_reader = ImageReader::with_format(io::Cursor::new(buf), image::ImageFormat::Png);
|
||||
|
||||
Ok(Box::new(image_reader.into_decoder()?))
|
||||
}
|
||||
|
||||
fn to_image_error(
|
||||
e: impl Into<Box<dyn std::error::Error + std::marker::Send + Sync + 'static>>,
|
||||
) -> ImageError {
|
||||
ImageError::Decoding(DecodingError::new(
|
||||
image::error::ImageFormatHint::PathExtension("kra".into()),
|
||||
e,
|
||||
))
|
||||
}
|
||||
|
|
@ -15,7 +15,6 @@ use iced::{
|
|||
use image::{EncodableLayout, RgbaImage, imageops};
|
||||
use size::Size;
|
||||
|
||||
mod kra;
|
||||
mod utils;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -302,7 +301,7 @@ impl State {
|
|||
|
||||
fn main() -> Result<(), iced::Error> {
|
||||
jxl_oxide::integration::register_image_decoding_hook();
|
||||
kra::register();
|
||||
kra_image_integration::register();
|
||||
|
||||
iced::application(State::new, State::update, State::view)
|
||||
.subscription(State::subscription)
|
||||
|
|
|
|||
Loading…
Reference in a new issue