pixsy/src/lib.rs

390 lines
11 KiB
Rust
Raw Normal View History

#![feature(clamp)]
2020-10-30 17:50:03 +00:00
use bitsy_parser::game::Game;
use bitsy_parser::image::Image;
use bitsy_parser::tile::Tile;
2020-11-04 15:38:43 +00:00
use image::{GenericImageView, Pixel, DynamicImage};
use lazy_static::lazy_static;
use std::sync::Mutex;
2020-10-30 17:50:03 +00:00
use wasm_bindgen::prelude::*;
2020-11-05 18:48:22 +00:00
use image::imageops::dither;
2020-10-30 17:50:03 +00:00
const SD: u32 = 8;
2020-11-04 15:38:43 +00:00
struct State {
game: Option<Game>,
image: Option<DynamicImage>,
room_name: Option<String>,
palette: Option<String>,
dither: bool,
brightness: i32,
2020-11-04 15:38:43 +00:00
}
lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(
State {
game: None,
image: None,
room_name: None,
palette: None,
dither: true,
brightness: 0,
2020-11-04 15:38:43 +00:00
}
);
2020-10-30 17:50:03 +00:00
}
2020-11-04 16:11:44 +00:00
fn tile_name(prefix: &Option<String>, index: &u32) -> Option<String> {
if let Some(prefix) = prefix {
2020-10-30 17:50:03 +00:00
Some(format!("{} {}", prefix, index))
} else {
None
}
}
#[wasm_bindgen]
2020-11-04 15:38:43 +00:00
pub fn load_default_game() {
let mut state = STATE.lock().unwrap();
state.game = Some(bitsy_parser::mock::game_default());
2020-11-05 18:48:22 +00:00
state.palette = Some(bitsy_parser::mock::game_default().palette_ids()[0].clone())
2020-11-04 15:38:43 +00:00
}
#[wasm_bindgen]
pub fn load_game(game_data: String) -> String {
let result = Game::from(game_data);
let mut state = STATE.lock().unwrap();
match result {
Ok(game) => {
2020-11-05 18:48:22 +00:00
let palette_id = game.palette_ids()[0].clone();
state.game = Some(game);
state.palette = Some(palette_id);
2020-11-04 15:38:43 +00:00
"".to_string()
},
_ => {
2020-11-05 18:48:22 +00:00
state.game = None;
state.palette = None;
2020-11-04 15:38:43 +00:00
format!("{}", result.err().unwrap())
}
2020-10-30 17:50:03 +00:00
}
2020-11-04 15:38:43 +00:00
}
#[wasm_bindgen]
pub fn load_image(image_base64: String) -> String {
let mut state = STATE.lock().expect("Couldn't lock application state");
let image_base64: Vec<&str> = image_base64.split("base64,").collect();
let image_base64 = image_base64[1];
match base64::decode(image_base64) {
Ok(image) => {
match image::load_from_memory(image.as_ref()) {
Ok(image) => {
state.image = Some(image);
"OK"
},
_ => {
state.image = None;
"Couldn't load image"
}
}
},
_ => {
state.image = None;
"Couldn't decode image"
}
}.to_string()
}
2020-10-30 17:50:03 +00:00
2020-11-05 18:48:22 +00:00
#[wasm_bindgen]
pub fn set_dither(dither: bool) {
let mut state = STATE.lock().unwrap();
state.dither = dither;
}
#[wasm_bindgen]
pub fn set_palette(palette_id: String) {
let mut state = STATE.lock().unwrap();
match palette_id.is_empty() {
true => { state.palette = None },
false => { state.palette = Some(palette_id) },
}
}
2020-11-04 15:38:43 +00:00
#[wasm_bindgen]
2020-11-04 16:11:44 +00:00
pub fn set_room_name(room_name: String) {
2020-11-04 15:38:43 +00:00
let mut state = STATE.lock().unwrap();
2020-10-30 17:50:03 +00:00
2020-11-04 16:11:44 +00:00
match room_name.is_empty() {
true => { state.room_name = None },
false => { state.room_name = Some(room_name) },
}
2020-11-04 15:38:43 +00:00
}
#[wasm_bindgen]
pub fn set_brightness(brightness: i32) {
let mut state = STATE.lock().unwrap();
state.brightness = brightness;
}
2020-11-04 16:18:38 +00:00
#[wasm_bindgen]
2020-11-05 18:48:22 +00:00
pub fn get_palettes() -> String {
let state = STATE.lock().unwrap();
2020-11-04 16:18:38 +00:00
2020-11-05 18:48:22 +00:00
let mut palette_objects = json::JsonValue::new_array();
for palette in &state.game.as_ref().unwrap().palettes {
let mut object = json::JsonValue::new_object();
object.insert("id", palette.id.clone()).unwrap();
object.insert(
"name",
palette.name.clone().unwrap_or(
format!("Palette {}", palette.id))
).unwrap();
palette_objects.push(object).unwrap();
}
json::stringify(palette_objects)
}
fn image_to_base64(image: &DynamicImage) -> String {
let mut bytes: Vec<u8> = Vec::new();
image.write_to(&mut bytes, image::ImageOutputFormat::Png).unwrap();
format!("data:image/png;base64,{}", base64::encode(&bytes))
}
fn colour_difference(compare: image::Rgba<u8>, other: &bitsy_parser::Colour) -> u32 {
let diff_red = (compare[0] as i16 - other.red as i16).abs();
let diff_green= (compare[1] as i16 - other.green as i16).abs();
let diff_blue = (compare[2] as i16 - other.blue as i16).abs();
(diff_red + diff_green + diff_blue) as u32
}
fn closest_colour(compare: image::Rgba<u8>, colours: &[bitsy_parser::Colour]) -> image::Rgba<u8> {
let diff_background = colour_difference(compare, &colours[0]);
let diff_foreground = colour_difference(compare, &colours[1]);
if diff_foreground <= diff_background {
image::Rgba::from([colours[1].red, colours[1].green, colours[1].blue, 255])
} else {
image::Rgba::from([colours[0].red, colours[0].green, colours[0].blue, 255])
}
}
fn adjust_brightness(pixel: &mut image::Rgba<u8>, state: &State) -> image::Rgba<u8> {
pixel[0] = (pixel[0] as i32 + state.brightness).clamp(0, 255) as u8;
pixel[1] = (pixel[1] as i32 + state.brightness).clamp(0, 255) as u8;
pixel[2] = (pixel[2] as i32 + state.brightness).clamp(0, 255) as u8;
*pixel
}
fn render_preview(state: &State) -> DynamicImage {
let image = state.image.as_ref().unwrap().clone();
let mut preview = image.clone();
2020-11-05 18:48:22 +00:00
// todo dither
// todo convert to palette colours
// get background and foreground colours from palette
let colours = &state.game.as_ref().unwrap().palettes
.iter()
.find(|palette| &palette.id == state.palette.as_ref().unwrap())
.unwrap()
.colours[0..2];
for (x, y, mut pixel) in image.pixels() {
// is pixel closer to background or foreground?
preview.put_pixel(x, y, closest_colour(adjust_brightness(&mut pixel, &state), colours));
}
preview
2020-11-05 18:48:22 +00:00
}
#[wasm_bindgen]
pub fn get_preview() -> String {
let state = STATE.lock().unwrap();
match &state.image.is_some() {
true => image_to_base64(&render_preview(&state)),
2020-11-05 18:48:22 +00:00
false => "".to_string(),
2020-11-04 16:18:38 +00:00
}
}
2020-11-04 15:38:43 +00:00
#[wasm_bindgen]
2020-11-04 16:11:44 +00:00
pub fn add_room() -> String {
2020-11-04 15:38:43 +00:00
let mut state = STATE.lock().expect("Couldn't lock application state");
if state.game.is_none() {
return "No game data loaded".to_string();
2020-10-30 17:50:03 +00:00
}
2020-11-04 15:38:43 +00:00
let mut game = state.game.clone().unwrap();
if state.image.is_none() {
return "No image loaded".to_string();
2020-10-30 17:50:03 +00:00
}
2020-11-04 15:38:43 +00:00
let width = state.image.as_ref().unwrap().width();
let height = state.image.as_ref().unwrap().height();
2020-10-30 17:50:03 +00:00
let columns = (width as f64 / SD as f64).floor() as u32;
let rows = (height as f64 / SD as f64).floor() as u32;
let mut tile_index = 1;
for column in 0..columns {
for row in 0..rows {
let mut pixels = Vec::with_capacity(64);
for y in (row * SD)..((row + 1) * SD) {
for x in (column * SD)..((column + 1) * SD) {
2020-11-04 15:38:43 +00:00
let pixel = state.image.as_ref().unwrap().get_pixel(x, y).to_rgb();
2020-10-30 17:50:03 +00:00
let total = pixel[0] as u32 + pixel[1] as u32 + pixel[2] as u32;
// is each channel brighter than 128/255 on average?
pixels.push(if total >= 384 {1} else {0});
}
}
let tile = Tile {
2020-11-04 15:38:43 +00:00
// "0" will get overwritten to a new, safe tile ID
2020-10-30 17:50:03 +00:00
id: "0".to_string(),
2020-11-04 16:11:44 +00:00
name: tile_name(&state.room_name, &tile_index),
2020-10-30 17:50:03 +00:00
wall: None,
animation_frames: vec![Image { pixels }],
colour_id: None
};
if !game.tiles.contains(&tile) {
game.add_tile(tile.clone());
tile_index += 1;
}
}
}
game.dedupe_tiles();
2020-11-04 15:38:43 +00:00
state.game = Some(game.to_owned());
"OK".to_string()
}
#[wasm_bindgen]
pub fn output() -> String {
let state = STATE.lock().unwrap();
match &state.game {
Some(game) => game.to_string(),
None => "No game loaded".to_string(),
}
2020-10-30 17:50:03 +00:00
}
#[cfg(test)]
mod test {
2020-11-05 18:48:22 +00:00
use crate::{add_room, load_image, load_default_game, output, get_preview};
use image::Rgba;
2020-11-05 18:48:22 +00:00
#[test]
fn image_to_base64() {
let image = image::load_from_memory(include_bytes!("test-resources/test.png")).unwrap();
let output = crate::image_to_base64(&image);
let expected = include_str!("test-resources/test.png.base64").trim();
assert_eq!(output, expected);
}
#[test]
fn colour_difference_none() {
let output = crate::colour_difference(
Rgba::from([255; 4]),
&bitsy_parser::Colour {
red: 255,
green: 255,
blue: 255
}
);
assert_eq!(output, 0);
}
#[test]
fn colour_difference_some() {
let output = crate::colour_difference(
Rgba::from([255; 4]),
&bitsy_parser::Colour {
red: 254,
green: 255,
blue: 255
}
);
assert_eq!(output, 1);
}
#[test]
fn colour_difference_some_2() {
let output = crate::colour_difference(
Rgba::from([254; 4]),
&bitsy_parser::Colour {
red: 254,
green: 255,
blue: 254
}
);
assert_eq!(output, 1);
}
#[test]
fn colour_difference_max() {
let expected = 255 * 3;
let output = crate::colour_difference(
Rgba::from([0; 4]),
&bitsy_parser::Colour {
red: 255,
green: 255,
blue: 255
}
);
assert_eq!(output, expected);
let output = crate::colour_difference(
Rgba::from([255; 4]),
&bitsy_parser::Colour {
red: 0,
green: 0,
blue: 0
}
);
assert_eq!(output, expected);
}
2020-11-05 18:48:22 +00:00
#[test]
fn get_palettes() {
load_default_game();
let output = crate::get_palettes();
let expected = "[{\"id\":\"0\",\"name\":\"blueprint\"}]";
assert_eq!(output, expected);
}
#[test]
fn render_preview() {
load_default_game();
load_image(include_str!("test-resources/colour_input.png.base64").trim().to_string());
let output = get_preview();
let expected = include_str!("test-resources/colour_input.png.base64.greyscale").trim();
assert_eq!(output, expected);
}
2020-10-30 17:50:03 +00:00
#[test]
fn example() {
2020-11-04 15:38:43 +00:00
load_default_game();
load_image(include_str!("test-resources/test.png.base64").to_string());
2020-11-05 18:48:22 +00:00
add_room();
2020-11-04 15:38:43 +00:00
assert_eq!(output(), include_str!("test-resources/expected.bitsy"));
2020-10-30 17:50:03 +00:00
}
}