diff --git a/README.md b/README.md index d637a2f..a4e55a2 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ ls *.jpg | machmap -e png ls *.jpg | machmap -e avif ls *.jpg | machmap -e pdf ls *.jpg | machmap -e odt +ls *.jpg | machmap -e xcf ``` Convert markdown files to html : diff --git a/src/machmap/jpg/jpgtoxcf.rs b/src/machmap/jpg/jpgtoxcf.rs new file mode 100644 index 0000000..77bf538 --- /dev/null +++ b/src/machmap/jpg/jpgtoxcf.rs @@ -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 JpgToXcf<'a> { + pub input_file: &'a str, + pub output_file: &'a str, +} + +impl<'a> JpgToXcf<'a> { + pub fn new(input_file: &'a str, output_file: &'a str) -> JpgToXcf<'a> { + JpgToXcf { + input_file, + output_file, + } + } +} + +impl<'a> InputTo<'a> for JpgToXcf<'a> { + fn convert(&self) -> Result> { + 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()) + } +} \ No newline at end of file diff --git a/src/machmap/jpg/mod.rs b/src/machmap/jpg/mod.rs index 6503159..17b05c6 100644 --- a/src/machmap/jpg/mod.rs +++ b/src/machmap/jpg/mod.rs @@ -1,11 +1,13 @@ pub mod jpgtoodt; pub mod jpgtopdf; +pub mod jpgtoxcf; use colored::Colorize; use image::{ImageFormat, ImageReader}; use crate::machmap::jpg::jpgtoodt::JpgToOdt; use crate::machmap::jpg::jpgtopdf::JpgToPdf; +use crate::machmap::jpg::jpgtoxcf::JpgToXcf; use crate::machmap::{Error, HashMap, InputTo, JPGInputFile}; impl<'a> JPGInputFile<'a> { @@ -18,12 +20,14 @@ impl<'a> JPGInputFile<'a> { let pdf = JpgToPdf::new(input_file, output_file); let odt = JpgToOdt::new(input_file, output_file); + let xcf = JpgToXcf::new(input_file, output_file); let mut map: HashMap<&'a str, Box + 'a>> = HashMap::new(); map.insert("image/png", Box::new(png)); map.insert("image/avif", Box::new(avif)); map.insert("application/pdf", Box::new(pdf)); map.insert("application/vnd.oasis.opendocument.text", Box::new(odt)); + map.insert("image/x-xcf", Box::new(xcf)); JPGInputFile { input_file, output_file,