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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
84 changes: 84 additions & 0 deletions src/machmap/jpg/jpgtoxcf.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 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<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())
}
}
4 changes: 4 additions & 0 deletions src/machmap/jpg/mod.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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<dyn InputTo<'a> + '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,
Expand Down
Loading