Skip to content

Commit 5f33fd2

Browse files
authored
Ports some minor iconforge optimizations from iconforge-rs repo (#257)
1 parent 09c7880 commit 5f33fd2

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

src/iconforge/icon_operations.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,7 @@ impl Transform {
897897
"Invalid bounds {x1} {y1} to {x2} {y2} in DrawBox transform"
898898
));
899899
}
900-
let hex = color.clone().unwrap_or_else(|| String::from("#00000000"));
901-
let rgba = hex_to_rgba(&hex)?;
900+
let rgba = color.as_ref().map(hex_to_rgba).unwrap_or(Ok([0; 4]))?;
902901
images =
903902
image_data.map_cloned_images(|image| draw_box(image, rgba, x1, y1, x2, y2));
904903
}

src/iconforge/image_cache.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl UniversalIcon {
165165
frame_offset = 0;
166166
}
167167

168-
let mut images: Vec<RgbaImage> = Vec::new();
168+
let mut images: Vec<RgbaImage> = Vec::with_capacity(frames * dirs);
169169

170170
for frame_index in frame_offset..(frame_offset + frames) {
171171
for dir_offset in dir_index..(dir_index + dirs) {
@@ -239,7 +239,7 @@ pub fn filepath_to_dmi(icon_path: &str) -> Result<Arc<Icon>, String> {
239239

240240
cell.get_or_try_init(|| {
241241
zone!("open_dmi_file");
242-
let icon_file = File::open(&full_path).map_err(|err| {
242+
let reader = File::open(&full_path).map(BufReader::new).map_err(|err| {
243243
format!(
244244
"Failed to open DMI '{}' (resolved to '{}') - {}",
245245
icon_path,
@@ -248,12 +248,10 @@ pub fn filepath_to_dmi(icon_path: &str) -> Result<Arc<Icon>, String> {
248248
)
249249
})?;
250250

251-
let reader = BufReader::new(icon_file);
252-
253251
zone!("parse_dmi");
254-
Ok(Arc::new(Icon::load(reader).map_err(|err| {
255-
format!("DMI '{icon_path}' failed to parse - {err}")
256-
})?))
252+
Icon::load(reader)
253+
.map(Arc::new)
254+
.map_err(|err| format!("DMI '{icon_path}' failed to parse - {err}"))
257255
})
258256
.cloned()
259257
}

src/iconforge/spritesheet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ pub fn generate_spritesheet(
476476
}
477477
};
478478
let mut no_transforms = Option::<&UniversalIcon>::None;
479-
let unique_icons = DashSet::<&UniversalIcon>::new();
479+
let unique_icons = DashSet::<&UniversalIcon>::with_capacity(icons.len());
480480
{
481481
zone!("map_unique");
482482
icons.iter().for_each(|(_, icon)| {

0 commit comments

Comments
 (0)