diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 104 |
1 files changed, 66 insertions, 38 deletions
diff --git a/src/main.rs b/src/main.rs index fed8090..912d0cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -use std::{fs, io::Result, os::unix}; +use std::{fs, io::Result, os::unix, sync::{Arc, RwLock}}; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use serde::{Deserialize, Serialize}; use tiny_skia::Pixmap; use xcursor::{ImageInfo, XCursorEncoder}; @@ -102,40 +103,65 @@ fn process_xcursor( desired_sizes: Vec<u16>, time_scale: u32, ) -> Vec<u8> { - let mut image_info_vec: Vec<ImageInfo> = Vec::new(); - for cursor_frame in cursor.frames { - for desired_size in &desired_sizes { - let svg_data = apply_colours( - resize_cursvg(&cursor_frame.svg, desired_size), - &desired_colours, - ); - let pixmap = vector_to_pixmap(svg_data.as_bytes()); - - let pixels = { - let pixels = pixmap.pixels(); - let mut otherpixels: Vec<u8> = Vec::new(); - for elem in pixels.iter() { - otherpixels.push(elem.red()); - otherpixels.push(elem.green()); - otherpixels.push(elem.blue()); - otherpixels.push(elem.alpha()); + let size = cursor.frames.len() * desired_sizes.len(); + let image_info_vec: Arc<RwLock<Vec<ImageInfo>>> = Arc::new(RwLock::new(vec![ImageInfo { + r#type: 0, + subtype: 0, + width: 0, + height: 0, + xhot: 0, + yhot: 0, + data: vec![], + delay: 0, + };size])); + let desired_colours = Arc::new(desired_colours); + let desired_sizes = Arc::new(desired_sizes); + rayon::scope(|s| { + let mut i=0; + for cursor_frame in cursor.frames { + let ouridx = i; + i+=1; + let image_info_vec = image_info_vec.clone(); + let desired_colours = desired_colours.clone(); + let desired_sizes = desired_sizes.clone(); + s.spawn(move |_| { + for desired_size in &desired_sizes.to_vec() { + let svg_data = apply_colours( + resize_cursvg(&cursor_frame.svg, desired_size), + &desired_colours, + ); + let pixmap = vector_to_pixmap(svg_data.as_bytes()); + + let pixels = { + let pixels = pixmap.pixels(); + let mut otherpixels: Vec<u8> = Vec::new(); + for elem in pixels.iter() { + otherpixels.push(elem.red()); + otherpixels.push(elem.green()); + otherpixels.push(elem.blue()); + otherpixels.push(elem.alpha()); + } + otherpixels + }; + let image_info = ImageInfo { + r#type: *desired_size as u32, + subtype: *desired_size as u32, + width: pixmap.width().try_into().unwrap(), + height: pixmap.height().try_into().unwrap(), + xhot: (cursor.hot.x * pixmap.width() / 256).try_into().unwrap(), + yhot: (cursor.hot.y * pixmap.height() / 256).try_into().unwrap(), + data: pixels.try_into().unwrap(), + delay: (time_scale / cursor_frame.delay).try_into().unwrap(), + }; + { + let mut image_info_vec = image_info_vec.write().unwrap(); + image_info_vec[ouridx] = image_info; + } } - otherpixels - }; - let image_info = ImageInfo { - r#type: *desired_size as u32, - subtype: *desired_size as u32, - width: pixmap.width().try_into().unwrap(), - height: pixmap.height().try_into().unwrap(), - xhot: (cursor.hot.x * pixmap.width() / 256).try_into().unwrap(), - yhot: (cursor.hot.y * pixmap.height() / 256).try_into().unwrap(), - data: pixels.try_into().unwrap(), - delay: (time_scale / cursor_frame.delay).try_into().unwrap(), - }; - image_info_vec.push(image_info); + }); } - } - let mut encoder = XCursorEncoder::new(image_info_vec); + }); + let mut encoder = XCursorEncoder::new(image_info_vec.read().unwrap().to_vec()); encoder.pack() } @@ -150,7 +176,7 @@ fn main() -> Result<()> { fs::create_dir("xcursor-out")?; for style in cursor_style { fs::create_dir(format!("xcursor-out/{}", style.kind))?; - for cursor in style.cursors { + let c: usize = style.cursors.par_iter().map(|cursor|{ println!("xcursor-out/{}/{}", style.kind, cursor.name.clone()); // shove these files in a dir named `cursors` inside the theme dir // the theme dir should have these 2 files in it: @@ -185,14 +211,16 @@ fn main() -> Result<()> { ], 750, ), - )?; - for alias in cursor.aliases { + ).unwrap(); + for alias in cursor.aliases.clone() { unix::fs::symlink( cursor.name.clone(), format!("xcursor-out/{}/{}", style.kind, alias), - )?; + ).unwrap(); } - } + 1 + }).sum(); + println!("Generated {c} cursors"); } Ok(()) |