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
mod utils;
#[macro_use]
mod log;
use wasm_bindgen::prelude::*;
use image::{ImageBuffer, Rgb, png::PngDecoder, ImageFormat, RgbImage};
use std::io::{Cursor, Seek, SeekFrom, Read};
use wasm_bindgen::__rt::IntoJsResult;
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, rust-wasm-dev-helper!");
}
#[wasm_bindgen]
pub fn create_img(width: u32, height: u32, r: u8, g: u8, b: u8) -> Vec<u8> {
let width = match width {
0 => 100,
_ => width,
};
let height = match height {
0 => 100,
_ => height,
};
let img_path = format!("gen-png/gen-{}_{}-{}_{}_{}.png", width, height, r, g, b);
std::fs::read(img_path.clone()).unwrap_or_else(|_| {
let mut img: RgbImage = RgbImage::new(width, height);
for mut px in img.enumerate_pixels_mut().enumerate() {
(((px.1).2).0)[0] = r;
(((px.1).2).0)[1] = g;
(((px.1).2).0)[2] = b;
}
std::fs::create_dir("gen-png").unwrap_or_default();
img.save(img_path.clone()).unwrap();
std::fs::read(img_path).unwrap_or_default()
})
}
#[cfg(test)]
mod tests {
use crate::create_img;
#[test]
pub fn test() {
println!("{:?}", create_img(375, 250, 0, 0, 0))
}
}