refactor: restructure project into a workspace by introducing a plugin API crate and modularizing individual plugin definitions.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "convertis-native-image"
|
||||
build = "../../plugin-build.rs"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
convertis-plugin-api.workspace = true
|
||||
image.workspace = true
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright (C) 2026 Elias Wendland <eliaswendland@pm.me>
|
||||
//
|
||||
// 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, version 3 exclusively.
|
||||
//
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
use convertis_plugin_api::{Conversion, ConversionRequest, Plugin, PluginMetadata};
|
||||
use image::{ImageFormat, ImageReader};
|
||||
use std::{fs::File, io::BufReader};
|
||||
|
||||
const FORMATS: &[&str] = &["png", "jpeg", "gif", "webp", "bmp", "tiff", "ico"];
|
||||
|
||||
struct NativeImage;
|
||||
|
||||
fn image_format(name: &str) -> Result<ImageFormat, String> {
|
||||
ImageFormat::from_extension(name).ok_or_else(|| format!("unsupported image format '{name}'"))
|
||||
}
|
||||
|
||||
impl Plugin for NativeImage {
|
||||
fn metadata(&self) -> PluginMetadata {
|
||||
PluginMetadata {
|
||||
id: "native-image".into(),
|
||||
package: "convertis-native-image".into(),
|
||||
description: "Native common image conversion".into(),
|
||||
conversions: FORMATS
|
||||
.iter()
|
||||
.flat_map(|from| {
|
||||
FORMATS
|
||||
.iter()
|
||||
.filter(move |to| to != &from)
|
||||
.map(move |to| Conversion::file(from, to, (255, 230, 240)))
|
||||
})
|
||||
.collect(),
|
||||
options: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn convert(&self, request: &ConversionRequest) -> Result<(), String> {
|
||||
let reader = ImageReader::with_format(
|
||||
BufReader::new(File::open(&request.input).map_err(|error| error.to_string())?),
|
||||
image_format(&request.from)?,
|
||||
);
|
||||
let mut image = reader.decode().map_err(|error| error.to_string())?;
|
||||
if request.to == "ico" && (image.width() > 256 || image.height() > 256) {
|
||||
image = image.resize(256, 256, image::imageops::FilterType::Lanczos3);
|
||||
}
|
||||
image
|
||||
.save_with_format(&request.output, image_format(&request.to)?)
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
convertis_plugin_api::export_plugin!(NativeImage, "native-image");
|
||||
Reference in New Issue
Block a user