forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmi.rs
More file actions
205 lines (175 loc) · 5.95 KB
/
dmi.rs
File metadata and controls
205 lines (175 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! DMI metadata and image composition.
//!
//! Includes re-exports from `dreammaker::dmi`.
use std::io;
use std::path::Path;
use bytemuck::Pod;
use lodepng::{self, RGBA, Decoder, ColorType};
use ndarray::Array2;
pub use dm::dmi::*;
use std::ops::{Index, IndexMut};
type Rect = (u32, u32, u32, u32);
// ----------------------------------------------------------------------------
// Icon file and metadata handling
/// An image with associated DMI metadata.
pub struct IconFile {
/// The icon's metadata.
pub metadata: Metadata,
/// The icon's image.
pub image: Image,
}
impl IconFile {
pub fn from_file(path: &Path) -> io::Result<IconFile> {
let (bitmap, metadata) = Metadata::from_file(path)?;
Ok(IconFile {
metadata,
image: Image::from_rgba(bitmap),
})
}
#[inline]
pub fn rect_of(&self, icon_state: &str, dir: Dir) -> Option<Rect> {
self.metadata.rect_of(self.image.width, icon_state, dir, 0)
}
pub fn rect_of_index(&self, icon_index: u32) -> Rect {
let icon_count = self.image.width / self.metadata.width;
let (icon_x, icon_y) = (icon_index % icon_count, icon_index / icon_count);
(
icon_x * self.metadata.width,
icon_y * self.metadata.height,
self.metadata.width,
self.metadata.height,
)
}
}
#[derive(Default, Clone, Copy, Pod, Zeroable, Eq, PartialEq)]
#[repr(C)]
pub struct Rgba8 {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Rgba8 {
pub fn new(r: u8, g: u8, b: u8, a: u8) -> Rgba8 {
Rgba8 { r, g, b, a }
}
pub fn as_bytes(&self) -> &[u8; 4] {
bytemuck::cast_ref(self)
}
pub fn as_bytes_mut(&mut self) -> &mut [u8; 4] {
bytemuck::cast_mut(self)
}
}
impl Index<u8> for Rgba8 {
type Output = u8;
fn index(&self, index: u8) -> &Self::Output {
&self.as_bytes()[index as usize]
}
}
impl IndexMut<u8> for Rgba8 {
fn index_mut(&mut self, index: u8) -> &mut Self::Output {
&mut self.as_bytes_mut()[index as usize]
}
}
// ----------------------------------------------------------------------------
// Image manipulation
/// A two-dimensional RGBA image.
pub struct Image {
pub width: u32,
pub height: u32,
pub data: Array2<Rgba8>,
}
impl Image {
pub fn new_rgba(width: u32, height: u32) -> Image {
Image {
width,
height,
data: {
Array2::default((width as usize, height as usize))
},
}
}
fn from_rgba(bitmap: lodepng::Bitmap<RGBA>) -> Image {
Image {
width: bitmap.width as u32,
height: bitmap.height as u32,
data: {
let cast_input = bytemuck::cast_slice(bitmap.buffer.as_slice());
let mut arr = Array2::default((bitmap.width, bitmap.height));
arr.as_slice_mut().unwrap().copy_from_slice(cast_input);
arr
},
}
}
/// Read an `Image` from a file.
///
/// Prefer to call `IconFile::from_file`, which can read both metadata and
/// image contents at one time.
pub fn from_file(path: &Path) -> io::Result<Image> {
let path = &::dm::fix_case(path);
let mut decoder = Decoder::new();
decoder.info_raw_mut().colortype = ColorType::RGBA;
decoder.info_raw_mut().set_bitdepth(8);
decoder.read_text_chunks(false);
decoder.remember_unknown_chunks(false);
let bitmap = match decoder.decode_file(path) {
Ok(::lodepng::Image::RGBA(bitmap)) => bitmap,
Ok(_) => return Err(io::Error::new(io::ErrorKind::InvalidData, "not RGBA")),
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e)),
};
Ok(Image::from_rgba(bitmap))
}
#[cfg(feature = "png")]
pub fn to_file(&self, path: &Path) -> io::Result<()> {
use std::fs::File;
let mut encoder = png::Encoder::new(File::create(path)?, self.width, self.height);
encoder.set_color(::png::ColorType::RGBA);
encoder.set_depth(::png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
// TODO: metadata with write_chunk()
writer.write_image_data(bytemuck::cast_slice(self.data.as_slice().unwrap()))?;
Ok(())
}
pub fn composite(&mut self, other: &Image, pos: (u32, u32), crop: Rect, color: [u8; 4]) {
let other_dat = other.data.as_slice().unwrap();
let self_dat = self.data.as_slice_mut().unwrap();
let mut sy = crop.1;
for y in pos.1..(pos.1 + crop.3) {
let mut sx = crop.0;
for x in pos.0..(pos.0 + crop.2) {
let src = other_dat[(sy * other.width + sx) as usize];
macro_rules! tint {
($i:expr) => {
mul255(
src[$i],
color[$i],
)
};
}
let mut dst = &mut self_dat[(y * self.width + x) as usize];
let src_tint = Rgba8::new(tint!(0), tint!(1), tint!(2), tint!(3));
// out_A = src_A + dst_A (1 - src_A)
// out_RGB = (src_RGB src_A + dst_RGB dst_A (1 - src_A)) / out_A
let out_a = src_tint.a + mul255(dst.a, 255 - src_tint.a);
if out_a != 0 {
for i in 0..3 {
dst[i] = ((src_tint[i] as u32 * src_tint.a as u32
+ dst[i] as u32 * dst.a as u32 * (255 - src_tint.a as u32) / 255)
/ out_a as u32) as u8;
}
} else {
for i in 0..3 {
dst[i] = 0;
}
}
dst.a = out_a as u8;
sx += 1;
}
sy += 1;
}
}
}
#[inline]
fn mul255(x: u8, y: u8) -> u8 {
(x as u16 * y as u16 / 255) as u8
}