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
extern crate proc_macro;
use image::open;
use proc_macro::TokenStream;
use quote::quote;
use syn::{Expr, Lit};
#[proc_macro]
pub fn include_rgba(input: TokenStream) -> TokenStream {
let path = match syn::parse_macro_input!(input as Expr) {
Expr::Lit(lit) => match lit.lit {
Lit::Str(lit) => lit.value(),
_ => panic!("expected a string literal"),
},
_ => panic!("expected a string literal"),
};
let image = match open(&path) {
Ok(i) => i,
Err(e) => {
panic!("Can't open image {:?}: {}", path, e);
}
};
let image = image.into_rgba();
let (x, y) = image.dimensions();
let bytes = image.into_raw();
TokenStream::from(quote! {{
let _ = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", #path));
(
(#x, #y),
&[
#( #bytes, )*
]
)
}})
}