-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
193 lines (167 loc) · 7.19 KB
/
build.rs
File metadata and controls
193 lines (167 loc) · 7.19 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
use std::fs;
use std::io::{BufWriter, Write};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=assets/icon_source.png");
fs::create_dir_all("assets").expect("failed to create assets dir");
let (rgba, src_w, _src_h) = load_png("assets/icon_source.png");
// 256x256 base icon used at runtime and on Linux
let icon_256 = scale(&rgba, src_w, 256);
save_png("assets/icon.png", &icon_256, 256, 256);
generate_icns(&rgba, src_w);
generate_ico(&rgba, src_w);
// Windows: embed icon in the executable
#[cfg(target_os = "windows")]
{
let mut res = winres::WindowsResource::new();
res.set_icon("assets/icon.ico");
res.compile().expect("failed to compile Windows resources");
}
}
// ── macOS .icns ──────────────────────────────────────────────────────────────
fn generate_icns(src: &[u8], src_w: u32) {
let iconset = "assets/icon.iconset";
fs::create_dir_all(iconset).expect("failed to create iconset dir");
let entries: &[(u32, &str)] = &[
(16, "icon_16x16.png"),
(32, "icon_32x32.png"),
(128, "icon_128x128.png"),
(256, "icon_256x256.png"),
(512, "icon_512x512.png"),
];
for &(size, name) in entries {
let scaled = scale(src, src_w, size);
save_png(&format!("{iconset}/{name}"), &scaled, size, size);
}
// Run iconutil if building on macOS
let host = std::env::var("HOST").unwrap_or_default();
if host.contains("apple") {
let status = std::process::Command::new("iconutil")
.args(["-c", "icns", iconset, "-o", "assets/icon.icns"])
.status();
if let Ok(s) = status {
if s.success() {
println!("cargo:warning=icon.icns generated successfully");
}
}
}
}
// ── Windows .ico ──────────────────────────────────────────────────────────────
fn generate_ico(src: &[u8], src_w: u32) {
let sizes: &[u32] = &[16, 32, 48, 64, 128, 256];
// Encode each size as PNG bytes (ICO can embed PNG since Vista)
let entries: Vec<(u32, Vec<u8>)> = sizes
.iter()
.map(|&s| {
let scaled = scale(src, src_w, s);
let png = encode_png_to_vec(&scaled, s, s);
(s, png)
})
.collect();
let file = fs::File::create("assets/icon.ico").expect("failed to create icon.ico");
let mut out = BufWriter::new(file);
let count = entries.len() as u16;
// ICONDIR header (6 bytes)
out.write_all(&[0, 0]).unwrap(); // reserved
out.write_all(&[1, 0]).unwrap(); // type = 1 (icon)
out.write_all(&count.to_le_bytes()).unwrap();
// ICONDIRENTRY (16 bytes each) — data starts after header + all entries
let data_start = 6u32 + count as u32 * 16;
let mut offset = data_start;
for (size, png) in &entries {
let w = if *size >= 256 { 0u8 } else { *size as u8 };
out.write_all(&[w, w, 0, 0]).unwrap(); // width, height, color count, reserved
out.write_all(&[1, 0]).unwrap(); // planes
out.write_all(&[32, 0]).unwrap(); // bit depth
out.write_all(&(png.len() as u32).to_le_bytes()).unwrap();
out.write_all(&offset.to_le_bytes()).unwrap();
offset += png.len() as u32;
}
// Image data
for (_, png) in &entries {
out.write_all(png).unwrap();
}
}
// ── PNG helpers ───────────────────────────────────────────────────────────────
fn load_png(path: &str) -> (Vec<u8>, u32, u32) {
let file = fs::File::open(path).unwrap_or_else(|_| panic!("cannot open {path}"));
let decoder = png::Decoder::new(file);
let mut reader = decoder.read_info().unwrap();
let mut buf = vec![0u8; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf).unwrap();
let bytes = buf[..info.buffer_size()].to_vec();
// Ensure RGBA
let rgba = match info.color_type {
png::ColorType::Rgba => bytes,
png::ColorType::Rgb => bytes.chunks(3)
.flat_map(|c| [c[0], c[1], c[2], 255u8])
.collect(),
_ => panic!("unsupported PNG color type"),
};
(rgba, info.width, info.height)
}
fn save_png(path: &str, rgba: &[u8], w: u32, h: u32) {
let file = fs::File::create(path).unwrap_or_else(|_| panic!("cannot create {path}"));
let mut enc = png::Encoder::new(file, w, h);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
enc.write_header().unwrap().write_image_data(rgba).unwrap();
}
fn encode_png_to_vec(rgba: &[u8], w: u32, h: u32) -> Vec<u8> {
let mut buf = Vec::new();
let mut enc = png::Encoder::new(&mut buf, w, h);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
enc.write_header().unwrap().write_image_data(rgba).unwrap();
buf
}
// ── Scaling (box filter down, nearest-neighbor up) ────────────────────────────
fn scale(src: &[u8], src_s: u32, dst_s: u32) -> Vec<u8> {
if src_s == dst_s {
return src.to_vec();
}
let mut dst = vec![0u8; (dst_s * dst_s * 4) as usize];
let ratio = src_s as f32 / dst_s as f32;
for dy in 0..dst_s {
for dx in 0..dst_s {
if ratio > 1.0 {
// Box filter downscale
let x0 = (dx as f32 * ratio) as u32;
let x1 = ((dx + 1) as f32 * ratio).ceil() as u32;
let y0 = (dy as f32 * ratio) as u32;
let y1 = ((dy + 1) as f32 * ratio).ceil() as u32;
let (mut r, mut g, mut b, mut a, mut n) = (0u32, 0, 0, 0, 0);
for sy in y0..y1.min(src_s) {
for sx in x0..x1.min(src_s) {
let i = ((sy * src_s + sx) * 4) as usize;
r += src[i] as u32;
g += src[i + 1] as u32;
b += src[i + 2] as u32;
a += src[i + 3] as u32;
n += 1;
}
}
if n > 0 {
let i = ((dy * dst_s + dx) * 4) as usize;
dst[i] = (r / n) as u8;
dst[i + 1] = (g / n) as u8;
dst[i + 2] = (b / n) as u8;
dst[i + 3] = (a / n) as u8;
}
} else {
// Nearest-neighbor upscale
let sx = (dx as f32 * ratio) as u32;
let sy = (dy as f32 * ratio) as u32;
let si = ((sy * src_s + sx) * 4) as usize;
let di = ((dy * dst_s + dx) * 4) as usize;
dst[di..di + 4].copy_from_slice(&src[si..si + 4]);
}
}
}
dst
}