pixsy/src/lib.rs

396 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-06 16:16:17 +00:00
use image::{GenericImageView, Pixel, DynamicImage};
2020-11-07 13:48:00 +00:00
use image::imageops::ColorMap;
use image::imageops::FilterType::CatmullRom;
2020-11-04 15:38:43 +00:00
use lazy_static::lazy_static;
use std::sync::Mutex;
2020-10-30 17:50:03 +00:00
use wasm_bindgen::prelude::*;
mod colour_map;
use colour_map::ColourMap;
2020-10-30 17:50:03 +00:00
const SD: u32 = 8;
enum SelectedPalette {
None,
Existing {
id: String,
},
New {
background: bitsy_parser::Colour,
foreground: bitsy_parser::Colour,
}
}
2020-11-04 15:38:43 +00:00
struct State {
game: Option<Game>,
image: Option<DynamicImage>,
room_name: Option<String>,
palette: SelectedPalette,
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: SelectedPalette::None,
dither: true,
brightness: 0,
2020-11-04 15:38:43 +00:00
}
);
2020-10-30 17:50:03 +00:00
}
2020-11-08 15:37:38 +00:00
fn tile_name(prefix: &Option<String>, x: &u32, y: &u32) -> Option<String> {
2020-11-04 16:11:44 +00:00
if let Some(prefix) = prefix {
2020-11-08 15:37:38 +00:00
Some(format!("{} ({},{})", prefix, x, y))
2020-10-30 17:50:03 +00:00
} else {
None
}
}
#[wasm_bindgen]
2020-11-04 15:38:43 +00:00
pub fn load_default_game() {
let mut state = STATE.lock().unwrap();
2020-11-04 15:38:43 +00:00
state.game = Some(bitsy_parser::mock::game_default());
// yes, this will probably always just be "0", but to be safe…
state.palette = SelectedPalette::Existing {
id: 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 mut state = STATE.lock().unwrap();
let result = Game::from(game_data);
2020-11-04 15:38:43 +00:00
match result {
Ok((game, _errs)) => {
2020-11-05 18:48:22 +00:00
let palette_id = game.palette_ids()[0].clone();
state.game = Some(game);
state.palette = SelectedPalette::Existing { id: palette_id };
2020-11-07 11:10:27 +00:00
format!("Loaded game")
2020-11-04 15:38:43 +00:00
},
_ => {
2020-11-05 18:48:22 +00:00
state.game = None;
state.palette = SelectedPalette::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();
2020-11-08 17:34:48 +00:00
if image_base64.len() < 2 {
return format!("Error: Badly-formatted base64: {}", image_base64.join(""));
}
2020-11-04 15:38:43 +00:00
let image_base64 = image_base64[1];
match base64::decode(image_base64) {
Ok(image) => {
match image::load_from_memory(image.as_ref()) {
Ok(image) => {
let size = format!("{}×{}", image.width(), image.height());
2020-11-07 13:48:00 +00:00
// todo get rid of magic numbers! what about Bitsy HD?
let image = image.resize(128, 128, CatmullRom);
2020-11-04 15:38:43 +00:00
state.image = Some(image);
size
2020-11-04 15:38:43 +00:00
},
_ => {
state.image = None;
2020-11-08 17:34:48 +00:00
"Error: Couldn't load image".to_string()
2020-11-04 15:38:43 +00:00
}
}
},
_ => {
state.image = None;
2020-11-08 17:34:48 +00:00
"Error: Couldn't decode image".to_string()
2020-11-04 15:38:43 +00:00
}
}
2020-11-04 15:38:43 +00:00
}
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: &str, background: String, foreground: String) {
2020-11-05 18:48:22 +00:00
let mut state = STATE.lock().unwrap();
state.palette = match palette_id {
"NEW_PALETTE" => SelectedPalette::New {
background: bitsy_parser::Colour::from_hex(&background).unwrap(),
foreground: bitsy_parser::Colour::from_hex(&foreground).unwrap(),
},
"" => SelectedPalette::None,
_ => SelectedPalette::Existing { id: palette_id.to_string() },
2020-11-05 18:48:22 +00:00
}
}
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))
}
2020-11-08 17:33:15 +00:00
fn palette_from(bg: &bitsy_parser::Colour, fg: &bitsy_parser::Colour) -> bitsy_parser::Palette {
bitsy_parser::Palette {
id: "0".to_string(),
name: None,
colours: vec![
bg.clone(), fg.clone(), bitsy_parser::Colour { red: 0, green: 0, blue: 0 }
],
}
}
fn render_preview(state: &State) -> DynamicImage {
2020-11-07 13:48:00 +00:00
let mut buffer = state.image.as_ref().unwrap().clone().into_rgba();
2020-11-05 18:48:22 +00:00
let palette = match &state.palette {
SelectedPalette::None => bitsy_parser::mock::game_default().palettes[0].clone(),
SelectedPalette::Existing { id } => state.game.as_ref().unwrap().get_palette(id).unwrap().clone(),
2020-11-08 17:33:15 +00:00
SelectedPalette::New { background, foreground } => palette_from(background, foreground),
};
let colour_map = crate::ColourMap::from(&palette);
// adjust brightness
2020-11-07 13:48:00 +00:00
let mut buffer = image::imageops::brighten(&mut buffer, state.brightness);
if state.dither {
image::imageops::dither(&mut buffer, &colour_map);
} else {
2020-11-07 13:48:00 +00:00
// just do colour indexing
let indices = image::imageops::colorops::index_colors(&mut buffer, &colour_map);
// todo get rid of magic numbers! what about Bitsy HD?
buffer = image::ImageBuffer::from_fn(128, 128, |x, y| -> image::Rgba<u8> {
let p = indices.get_pixel(x, y);
2020-11-07 16:31:09 +00:00
2020-11-07 13:48:00 +00:00
colour_map
.lookup(p.0[0] as usize)
2020-11-07 16:31:09 +00:00
.expect("indexed colour out-of-range")
2020-11-07 13:48:00 +00:00
.into()
});
}
image::DynamicImage::ImageRgba8(buffer)
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
2020-11-08 12:30:46 +00:00
match &state.palette {
SelectedPalette::None => { return "No palette selected".to_string(); },
_ => {}
};
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-08 12:30:46 +00:00
let palette_id = Some(match &state.palette {
SelectedPalette::None => bitsy_parser::mock::game_default().palettes[0].id.clone(),
SelectedPalette::Existing { id } => id.clone(),
SelectedPalette::New { background, foreground } => {
2020-11-08 17:33:15 +00:00
game.add_palette(palette_from(background, foreground))
2020-11-08 12:30:46 +00:00
},
});
2020-11-08 14:54:49 +00:00
let foreground = &game.palettes
.iter()
.find(|&palette| &palette.id == palette_id.as_ref().unwrap())
.unwrap().colours[1].clone();
let preview = render_preview(&state);
let width = 128;
let height = 128;
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;
2020-11-08 12:30:46 +00:00
let mut tile_ids = Vec::new();
2020-11-08 14:54:49 +00:00
let initial_tile_count = game.tiles.len();
2020-10-30 17:50:03 +00:00
2020-11-08 14:54:49 +00:00
for row in 0..rows {
for column in 0..columns {
2020-10-30 17:50:03 +00:00
let mut pixels = Vec::with_capacity(64);
2020-11-08 14:54:49 +00:00
fn colour_match(a: &image::Rgb<u8>, b: &bitsy_parser::Colour) -> u8 {
if a[0] == b.red && a[1] == b.green && a[2] == b.blue { 1 } else { 0 }
};
2020-10-30 17:50:03 +00:00
for y in (row * SD)..((row + 1) * SD) {
for x in (column * SD)..((column + 1) * SD) {
2020-11-08 14:54:49 +00:00
let pixel = preview.get_pixel(x, y).to_rgb();
pixels.push(colour_match(&pixel, foreground));
2020-10-30 17:50:03 +00:00
}
}
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-08 16:59:04 +00:00
name: tile_name(&state.room_name, &column, &row),
2020-10-30 17:50:03 +00:00
wall: None,
animation_frames: vec![Image { pixels }],
colour_id: None
};
2020-11-08 14:54:49 +00:00
let tile_id = if game.tiles.contains(&tile) {
game.tiles.iter().find(|&t| t == &tile).unwrap().id.clone()
} else {
game.add_tile(tile)
};
tile_ids.push(tile_id);
2020-10-30 17:50:03 +00:00
}
}
2020-11-08 12:30:46 +00:00
game.add_room(bitsy_parser::Room {
id: "0".to_string(),
palette_id,
name: state.room_name.clone(),
2020-11-08 12:30:46 +00:00
tiles: tile_ids,
items: vec![],
exits: vec![],
endings: vec![],
walls: None
});
2020-10-30 17:50:03 +00:00
game.dedupe_tiles();
2020-11-04 15:38:43 +00:00
2020-11-08 14:54:49 +00:00
let new_tile_count = game.tiles.len();
2020-11-04 15:38:43 +00:00
state.game = Some(game.to_owned());
2020-11-08 12:30:46 +00:00
format!(
"Added room \"{}\" with {} new tiles",
&state.room_name.as_ref().unwrap_or(&"untitled".to_string()),
2020-11-08 14:54:49 +00:00
new_tile_count - initial_tile_count
2020-11-08 12:30:46 +00:00
)
2020-11-04 15:38:43 +00:00
}
#[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 {
use crate::{add_room, load_image, load_default_game, output, get_preview, set_room_name};
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);
}
2020-11-08 11:23:44 +00:00
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();
2020-11-08 14:54:49 +00:00
load_image(include_str!("test-resources/test.png.base64").trim().to_string());
set_room_name("test".to_string());
2020-11-08 14:54:49 +00:00
println!("add_room(): {}", add_room());
// todo what? why are extraneous pixels appearing in the output tiles?
assert_eq!(output(), include_str!("test-resources/expected.bitsy"));
2020-10-30 17:50:03 +00:00
}
}