Skip to content

Commit d5010d8

Browse files
authored
Bump dmi to 0.5.0, optimize dmi_read_metadata (#238)
1 parent 1ecc5c4 commit d5010d8

File tree

9 files changed

+66
-52
lines changed

9 files changed

+66
-52
lines changed

Cargo.lock

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ rayon = { version = "1.10", optional = true }
5959
dbpnoise = { version = "0.1.2", optional = true }
6060
pathfinding = { version = "4.14", optional = true }
6161
num-integer = { version = "0.1.46", optional = true }
62-
dmi = { version = "0.4.0", optional = true }
62+
dmi = { version = "0.5.0", optional = true }
6363
tracy_full = { version = "1.12.0", optional = true }
6464
ammonia = { version = "4.1", optional = true }
6565
fast_poisson = { version = "1.0.2", optional = true, features = [

src/dmi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ struct DmiMetadata {
212212
}
213213

214214
fn read_metadata(path: &str) -> Result<String> {
215-
let dmi = Icon::load(File::open(path).map(BufReader::new)?)?;
215+
let dmi = Icon::load_meta(File::open(path).map(BufReader::new)?)?;
216216
let metadata = DmiMetadata {
217217
width: dmi.width,
218218
height: dmi.height,

src/iconforge/gags.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{blending, icon_operations, image_cache::filepath_to_dmi};
22
use crate::error::Error;
33
use dashmap::DashMap;
44
use dmi::icon::{DmiVersion, Icon, IconState};
5-
use image::DynamicImage;
5+
use image::RgbaImage;
66
use once_cell::sync::Lazy;
77
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
88
use serde::{Deserialize, Serialize};
@@ -217,10 +217,10 @@ fn gags_internal(
217217
config_path: &str,
218218
colors_vec: &Vec<String>,
219219
icon_state: &String,
220-
last_external_images: Option<Vec<DynamicImage>>,
220+
last_external_images: Option<Vec<RgbaImage>>,
221221
first_matched_state: &mut Option<IconState>,
222222
last_matched_state: &mut Option<IconState>,
223-
) -> Result<Vec<DynamicImage>, String> {
223+
) -> Result<Vec<RgbaImage>, String> {
224224
zone!("gags_internal");
225225
let gags_data = match GAGS_CACHE.get(config_path) {
226226
Some(config) => config,
@@ -270,12 +270,12 @@ fn generate_layer_groups_for_iconstate(
270270
colors: &Vec<String>,
271271
layer_groups: &Vec<GAGSLayerGroupOption>,
272272
gags_data: &GAGSData,
273-
last_external_images: Option<Vec<DynamicImage>>,
273+
last_external_images: Option<Vec<RgbaImage>>,
274274
first_matched_state: &mut Option<IconState>,
275275
last_matched_state: &mut Option<IconState>,
276-
) -> Result<Vec<DynamicImage>, String> {
276+
) -> Result<Vec<RgbaImage>, String> {
277277
zone!("generate_layer_groups_for_iconstate");
278-
let mut new_images: Option<Vec<DynamicImage>> = None;
278+
let mut new_images: Option<Vec<RgbaImage>> = None;
279279
for option in layer_groups {
280280
zone!("process_gags_layergroup_option");
281281
let (layer_images, blend_mode_result) = match option {
@@ -342,12 +342,12 @@ fn generate_layer_for_iconstate(
342342
colors: &[String],
343343
layer: &GAGSLayer,
344344
gags_data: &GAGSData,
345-
new_images: Option<Vec<DynamicImage>>,
345+
new_images: Option<Vec<RgbaImage>>,
346346
first_matched_state: &mut Option<IconState>,
347347
last_matched_state: &mut Option<IconState>,
348-
) -> Result<Vec<DynamicImage>, String> {
348+
) -> Result<Vec<RgbaImage>, String> {
349349
zone!("generate_layer_for_iconstate");
350-
let images_result: Option<Vec<DynamicImage>> = match layer {
350+
let images_result: Option<Vec<RgbaImage>> = match layer {
351351
GAGSLayer::IconState {
352352
icon_state,
353353
blend_mode: _,
@@ -459,16 +459,16 @@ fn generate_layer_for_iconstate(
459459
}
460460
}
461461

462-
pub fn map_cloned_images<F>(images: &Vec<DynamicImage>, do_fn: F) -> Vec<DynamicImage>
462+
pub fn map_cloned_images<F>(images: &Vec<RgbaImage>, do_fn: F) -> Vec<RgbaImage>
463463
where
464-
F: Fn(&mut image::ImageBuffer<image::Rgba<u8>, Vec<u8>>) + Send + Sync,
464+
F: Fn(&mut RgbaImage) + Send + Sync,
465465
{
466466
images
467467
.par_iter()
468468
.map(|image| {
469-
let mut new_image = image.clone().into_rgba8();
469+
let mut new_image = image.clone();
470470
do_fn(&mut new_image);
471-
DynamicImage::ImageRgba8(new_image)
471+
new_image
472472
})
473473
.collect()
474474
}

src/iconforge/icon_operations.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
};
55
use crate::error::Error;
66
use dmi::{dirs::Dirs, icon::IconState};
7-
use image::{imageops, DynamicImage, Rgba, RgbaImage};
7+
use image::{imageops, Rgba, RgbaImage};
88
use ordered_float::OrderedFloat;
99
use rayon::{
1010
iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator},
@@ -95,7 +95,7 @@ pub fn crop(image: &mut RgbaImage, x1: i32, y1: i32, x2: i32, y2: i32) {
9595
height_inc += y1.unsigned_abs();
9696
y1 = 0;
9797
}
98-
let mut blank_img: image::ImageBuffer<image::Rgba<u8>, Vec<u8>> =
98+
let mut blank_img: RgbaImage =
9999
RgbaImage::from_fn(i_width + width_inc, i_height + height_inc, |_x, _y| {
100100
image::Rgba([0, 0, 0, 0])
101101
});
@@ -571,26 +571,26 @@ pub fn blend_images_other_universal(
571571
)));
572572
}
573573
}
574-
let images_out: Vec<DynamicImage> = if images_other.len() == 1 {
574+
let images_out: Vec<RgbaImage> = if images_other.len() == 1 {
575575
// This is useful in the case where the something with 4+ dirs blends with 1dir
576-
let first_image = images_other.first().unwrap().clone().into_rgba8();
576+
let first_image = images_other.first().unwrap().clone();
577577
images
578578
.into_par_iter()
579579
.map(|image| {
580580
zone!("blend_image_other_simple");
581-
let mut new_image = image.clone().into_rgba8();
581+
let mut new_image = image.clone();
582582
blend_icon(&mut new_image, &first_image, blend_mode, position);
583-
DynamicImage::ImageRgba8(new_image)
583+
new_image
584584
})
585585
.collect()
586586
} else {
587587
(images, images_other)
588588
.into_par_iter()
589589
.map(|(image, image2)| {
590590
zone!("blend_image_other");
591-
let mut new_image = image.clone().into_rgba8();
592-
blend_icon(&mut new_image, &image2.into_rgba8(), blend_mode, position);
593-
DynamicImage::ImageRgba8(new_image)
591+
let mut new_image = image.clone();
592+
blend_icon(&mut new_image, &image2, blend_mode, position);
593+
new_image
594594
})
595595
.collect()
596596
};
@@ -611,12 +611,12 @@ pub fn blend_images_other_universal(
611611
/// Blends a set of images with another set of images.
612612
/// The frame and dir counts of first_matched_state are mutated to match the new icon.
613613
pub fn blend_images_other(
614-
images: Vec<DynamicImage>,
615-
images_other: Vec<DynamicImage>,
614+
images: Vec<RgbaImage>,
615+
images_other: Vec<RgbaImage>,
616616
blend_mode: &blending::BlendMode,
617617
first_matched_state: &mut Option<IconState>,
618618
last_matched_state: &mut Option<IconState>,
619-
) -> Result<Vec<DynamicImage>, Error> {
619+
) -> Result<Vec<RgbaImage>, Error> {
620620
zone!("blend_images_other");
621621
let base_icon_state = match first_matched_state {
622622
Some(state) => state,
@@ -709,26 +709,26 @@ pub fn blend_images_other(
709709
)));
710710
}
711711
}
712-
let images_out: Vec<DynamicImage> = if images_other.len() == 1 {
712+
let images_out: Vec<RgbaImage> = if images_other.len() == 1 {
713713
// This is useful in the case where the something with 4+ dirs blends with 1dir
714-
let first_image = images_other.first().unwrap().clone().into_rgba8();
714+
let first_image = images_other.first().unwrap().clone();
715715
images
716716
.into_par_iter()
717717
.map(|image| {
718718
zone!("blend_image_other_simple");
719-
let mut new_image = image.clone().into_rgba8();
719+
let mut new_image = image.clone();
720720
blend_icon(&mut new_image, &first_image, blend_mode, None);
721-
DynamicImage::ImageRgba8(new_image)
721+
new_image
722722
})
723723
.collect()
724724
} else {
725725
(images, images_other)
726726
.into_par_iter()
727727
.map(|(image, image2)| {
728728
zone!("blend_image_other");
729-
let mut new_image = image.clone().into_rgba8();
730-
blend_icon(&mut new_image, &image2.into_rgba8(), blend_mode, None);
731-
DynamicImage::ImageRgba8(new_image)
729+
let mut new_image = image.clone();
730+
blend_icon(&mut new_image, &image2, blend_mode, None);
731+
new_image
732732
})
733733
.collect()
734734
};
@@ -747,7 +747,7 @@ impl Transform {
747747
flatten: bool,
748748
) -> Result<UniversalIconData, String> {
749749
zone!("transform_apply");
750-
let images: Vec<DynamicImage>;
750+
let images: Vec<RgbaImage>;
751751
let mut frames = image_data.frames;
752752
let mut dirs = image_data.dirs;
753753
let mut delay = image_data.delay.to_owned();

src/iconforge/image_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use dmi::{
44
dirs::{Dirs, ALL_DIRS, CARDINAL_DIRS},
55
icon::{dir_to_dmi_index, Icon, IconState},
66
};
7-
use image::DynamicImage;
7+
use image::RgbaImage;
88
use once_cell::sync::Lazy;
99
use std::{fs::File, hash::BuildHasherDefault, io::BufReader, sync::Arc};
1010
use tracy_full::zone;
@@ -128,7 +128,7 @@ impl UniversalIcon {
128128
frame_offset = 0;
129129
}
130130

131-
let mut images: Vec<DynamicImage> = Vec::new();
131+
let mut images: Vec<RgbaImage> = Vec::new();
132132

133133
for frame_index in frame_offset..(frame_offset + frames) {
134134
for dir_offset in dir_index..(dir_index + dirs) {

src/iconforge/spritesheet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ fn create_png_image(
346346
base_width: u32,
347347
base_height: u32,
348348
sprite_entries: &Vec<(&String, &UniversalIcon)>,
349-
) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, String> {
349+
) -> Result<RgbaImage, String> {
350350
zone!("create_png_image");
351351
let mut final_image = RgbaImage::new(base_width * sprite_entries.len() as u32, base_height);
352352
for (idx, sprite_entry) in sprite_entries.iter().enumerate() {
@@ -361,7 +361,7 @@ fn create_png_image(
361361
if image_data.images.len() > 1 {
362362
return Err(format!("More than one image (non-flattened) sprite {sprite_name} in PNG spritesheet for icon {icon}!"));
363363
}
364-
let image = image_data.images.first().unwrap().to_rgba8();
364+
let image = image_data.images.first().unwrap();
365365
let base_x: u32 = base_width * idx as u32;
366366
for x in 0..image.width() {
367367
for y in 0..image.height() {

src/iconforge/universal_icon.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use dmi::icon::Looping;
2-
use image::DynamicImage;
2+
use image::RgbaImage;
33
use ordered_float::OrderedFloat;
44
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
55
use serde::{Deserialize, Serialize};
@@ -112,7 +112,7 @@ pub enum Transform {
112112

113113
#[derive(Clone)]
114114
pub struct UniversalIconData {
115-
pub images: Vec<DynamicImage>,
115+
pub images: Vec<RgbaImage>,
116116
pub frames: u32,
117117
pub dirs: u8,
118118
pub delay: Option<Vec<f32>>,
@@ -121,16 +121,16 @@ pub struct UniversalIconData {
121121
}
122122

123123
impl UniversalIconData {
124-
pub fn map_cloned_images<F>(&self, do_fn: F) -> Vec<DynamicImage>
124+
pub fn map_cloned_images<F>(&self, do_fn: F) -> Vec<RgbaImage>
125125
where
126-
F: Fn(&mut image::ImageBuffer<image::Rgba<u8>, Vec<u8>>) + Send + Sync,
126+
F: Fn(&mut RgbaImage) + Send + Sync,
127127
{
128128
self.images
129129
.par_iter()
130130
.map(|image| {
131-
let mut new_image = image.clone().into_rgba8();
131+
let mut new_image = image.clone();
132132
do_fn(&mut new_image);
133-
DynamicImage::ImageRgba8(new_image)
133+
new_image
134134
})
135135
.collect()
136136
}

tests/iconforge/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use dmi::{
33
error::DmiError,
44
icon::{Icon, IconState},
55
};
6-
use image::{DynamicImage, GenericImageView};
6+
use image::RgbaImage;
77
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
88
use std::{
99
fs::{read_dir, File},
@@ -211,8 +211,8 @@ fn compare_states(dm_state: &IconState, rustg_state: &IconState) -> Option<Strin
211211

212212
fn compare_images(
213213
differences: &mut Vec<String>,
214-
dm_images: &Vec<DynamicImage>,
215-
rustg_images: &Vec<DynamicImage>,
214+
dm_images: &Vec<RgbaImage>,
215+
rustg_images: &Vec<RgbaImage>,
216216
dirs: u8,
217217
) {
218218
let safe_diffs = Arc::new(Mutex::new(Vec::<String>::new()));

0 commit comments

Comments
 (0)