Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 137 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ clap = { version="4.5", features=['cargo'] }
lopdf = { version="0.33", features=["embed_image"] }
exitcode="1.1.2"
mime="0.3"
mime_guess="2.0"
resvg="0.43"
usvg="0.43"
mime_guess = { git = "https://github.com/mothsART/mime_guess", branch = "feature/xcf-file" }
resvg="0.45"
usvg="0.45"
tiny-skia="0.11"
image = { version="0.25" }
markdown = "0.3"
Expand All @@ -31,6 +31,7 @@ serde_json = { version = "1.0", features = ["preserve_order"] }
serde_yaml = "0.9"
tera = "1.17"
svg2pdf = "0.11"
xcf-rs = "0.5"

[dev-dependencies]
sha1 = "0.10.6"
Expand Down
1 change: 0 additions & 1 deletion src/bin/machmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ fn main() {
));
process::exit(exitcode::DATAERR);
}

for _l in readlines() {
if !Path::new(&_l).exists() {
colored_err!(format!("Input file \"{_l}\" doesn't exist"));
Expand Down
14 changes: 12 additions & 2 deletions src/machmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod macros;
pub mod jpg;
pub mod png;
pub mod webp;
//pub mod xcf;

pub mod markdown;
pub mod svg;
Expand All @@ -34,6 +35,7 @@ impl<'a> InputsFiles<'a> {
let jpg = JPGInputFile::new(input_file, output_file);
let png = PNGInputFile::new(input_file, output_file);
let webp = WebpInputFile::new(input_file, output_file);
//let xcf = XcfInputFile::new(input_file, output_file);
let markdown = MarkdownInputFile::new(input_file, output_file);
let yaml = YamlInputFile::new(input_file, output_file);
let json = JsonInputFile::new(input_file, output_file);
Expand All @@ -42,6 +44,7 @@ impl<'a> InputsFiles<'a> {
map.insert("image/jpeg", Box::new(jpg));
map.insert("image/png", Box::new(png));
map.insert("image/webp", Box::new(webp));
//map.insert("image/x-xcf", Box::new(xcf));
map.insert("text/markdown", Box::new(markdown));
map.insert("text/x-yaml", Box::new(yaml));
map.insert("application/json", Box::new(json));
Expand All @@ -61,7 +64,10 @@ impl<'a> InputsFiles<'a> {
match &input_mime.first_raw() {
Some(i_mime) => match self.map.get(i_mime) {
Some(val) => val.support(),
None => Err(Box::new(e)),
None => {
println!("ppppp");
Err(Box::new(e))
},
},
None => Err(Box::new(e)),
}
Expand All @@ -76,7 +82,10 @@ impl<'a> InputsFiles<'a> {
match &input_mime.first_raw() {
Some(i_mime) => match self.map.get(i_mime) {
Some(val) => val.mime_map(),
None => Err(Box::new(e)),
None => {
println!("yyyy");
Err(Box::new(e))
},
},
None => Err(Box::new(e)),
}
Expand All @@ -91,6 +100,7 @@ create_input!(SVGInputFile, InputTo);
create_input!(JPGInputFile, InputTo);
create_input!(PNGInputFile, InputTo);
create_input!(WebpInputFile, InputTo);
//create_input!(XcfInputFile, InputTo);
create_input!(MarkdownInputFile, InputTo);
create_input!(YamlInputFile, InputTo);
create_input!(JsonInputFile, InputTo);
7 changes: 6 additions & 1 deletion src/machmap/png/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod pngtopdf;
pub mod pngtoxcf;

use colored::Colorize;
use image::{ImageFormat, ImageReader};

use crate::machmap::png::pngtopdf::PngToPdf;
use crate::machmap::png::{pngtopdf::PngToPdf, pngtoxcf::PngToXcf};
use crate::machmap::{Error, HashMap, InputTo, PNGInputFile};

impl<'a> PNGInputFile<'a> {
Expand All @@ -14,12 +15,16 @@ impl<'a> PNGInputFile<'a> {
convert_img!(PngToAvif, "png", "avif");
let avif = PngToAvif::new(input_file, output_file);

let xcf = PngToXcf::new(input_file, output_file);

let pdf = PngToPdf::new(input_file, output_file);

let mut map: HashMap<&'a str, Box<dyn InputTo<'a> + 'a>> = HashMap::new();
map.insert("image/jpeg", Box::new(jpg));
map.insert("image/avif", Box::new(avif));
map.insert("image/x-xcf", Box::new(xcf));
map.insert("application/pdf", Box::new(pdf));

PNGInputFile {
input_file,
output_file,
Expand Down
84 changes: 84 additions & 0 deletions src/machmap/png/pngtoxcf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::error::Error;
use std::path::PathBuf;

use crate::machmap::InputTo;

use image::ImageReader;
use image::GenericImageView;

use xcf_rs::create::XcfCreator;
use xcf_rs::data::color::ColorType;
use xcf_rs::data::rgba::RgbaPixel;
use xcf_rs::data::pixeldata::PixelData;
use xcf_rs::data::layer::Layer;
use xcf_rs::{LayerColorType, LayerColorValue};

pub struct PngToXcf<'a> {
pub input_file: &'a str,
pub output_file: &'a str,
}

impl<'a> PngToXcf<'a> {
pub fn new(input_file: &'a str, output_file: &'a str) -> PngToXcf<'a> {
PngToXcf {
input_file,
output_file,
}
}
}

impl<'a> InputTo<'a> for PngToXcf<'a> {
fn convert(&self) -> Result<String, Box<dyn Error + 'a>> {
let img = ImageReader::open(self.input_file)?.decode()?;
let dimensions = img.dimensions();
let width = dimensions.0;
let height = dimensions.1;


let mut xcf = XcfCreator::new(11, width, height, ColorType::Rgb);
xcf.add_properties(&vec![]);

let mut layers = vec![];
let mut pixels = vec![];

for p in img.pixels() {
let rgba = p.2.0;
pixels.push(RgbaPixel::new(
*rgba.get(0).unwrap(),
*rgba.get(1).unwrap(),
*rgba.get(2).unwrap(),
*rgba.get(3).unwrap()
));
}

let pixels_layer_one: PixelData = PixelData {
width: width,
height: height,
pixels: pixels,
};
let properties_layer_one = vec![];
let mut kind = LayerColorType {
kind: LayerColorValue::Rgb,
alpha: false,
};
if img.color().has_alpha() {
kind = LayerColorType {
kind: LayerColorValue::Rgba,
alpha: true,
};
}
let layer_one = Layer {
width,
height,
kind,
name: "Background".to_string(),
pixels: pixels_layer_one,
properties: properties_layer_one,
};
layers.push(layer_one);
xcf.add_layers(&layers);
let output_path = PathBuf::from(self.output_file);
xcf.save(&output_path)?;
Ok("".to_string())
}
}
Empty file added src/machmap/xcf/mod.rs
Empty file.
Binary file modified tests/datasets/machmap/Rust_programming_language_black_logo.pdf
Binary file not shown.
Loading