Compare commits

...

4 Commits

3 changed files with 36 additions and 11 deletions

View File

@ -56,14 +56,14 @@ html(lang="en-gb")
label label
| brightness | brightness
input#brightness(type="range" min=-64 max=64 value=0) input#brightness(type="range" min=-96 max=96 value=0)
td td(style="vertical-align: top;")
label label
| palette | palette
select#palette select#palette
label label
input#dither(type="checkbox") input#dither(type="checkbox" checked=true)
| dither | dither
br br

View File

@ -93,7 +93,7 @@ async function run() {
} }
function new_game() { function new_game() {
load_default_game() console.debug(load_default_game());
textareaGameDataInput.value = output(); textareaGameDataInput.value = output();
checkGameData(); checkGameData();
// we don't need to look at the default game data, so skip ahead to the image page // we don't need to look at the default game data, so skip ahead to the image page
@ -121,7 +121,8 @@ async function run() {
}); });
function setPaletteDropdown() { function setPaletteDropdown() {
let palettes = JSON.parse(get_palettes()); const palettes = JSON.parse(get_palettes());
console.debug(palettes);
selectPalette.innerHTML = ""; selectPalette.innerHTML = "";

View File

@ -4,6 +4,8 @@ use bitsy_parser::game::Game;
use bitsy_parser::image::Image; use bitsy_parser::image::Image;
use bitsy_parser::tile::Tile; use bitsy_parser::tile::Tile;
use image::{GenericImageView, Pixel, DynamicImage}; use image::{GenericImageView, Pixel, DynamicImage};
use image::imageops::ColorMap;
use image::imageops::FilterType::CatmullRom;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::sync::Mutex; use std::sync::Mutex;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -62,7 +64,7 @@ pub fn load_game(game_data: String) -> String {
let palette_id = game.palette_ids()[0].clone(); let palette_id = game.palette_ids()[0].clone();
state.game = Some(game); state.game = Some(game);
state.palette = Some(palette_id); state.palette = Some(palette_id);
"".to_string() format!("Loaded game")
}, },
_ => { _ => {
state.game = None; state.game = None;
@ -83,6 +85,7 @@ pub fn load_image(image_base64: String) -> String {
Ok(image) => { Ok(image) => {
match image::load_from_memory(image.as_ref()) { match image::load_from_memory(image.as_ref()) {
Ok(image) => { Ok(image) => {
// todo get rid of magic numbers! what about Bitsy HD?
let image = image.resize(128, 128, CatmullRom); let image = image.resize(128, 128, CatmullRom);
state.image = Some(image); state.image = Some(image);
"OK" "OK"
@ -162,19 +165,40 @@ fn image_to_base64(image: &DynamicImage) -> String {
} }
fn render_preview(state: &State) -> DynamicImage { fn render_preview(state: &State) -> DynamicImage {
let mut buffer = state.image.as_ref().unwrap().clone().into_rgba(); let mut buffer = state.image.as_ref().unwrap().clone().into_rgba();
// todo get actual chosen palette // todo get actual chosen palette
let palette_id = state.palette.as_ref().unwrap(); let palette_id = state.palette.as_ref().unwrap();
let palette = &state.game.as_ref().unwrap().get_palette(palette_id).unwrap(); let palette = *&state.game.as_ref().unwrap().get_palette(palette_id).unwrap();
let colour_map = crate::ColourMap::from(palette); let colour_map = crate::ColourMap::from(palette);
image::imageops::brighten(&mut buffer, state.brightness); let mut buffer = image::imageops::brighten(&mut buffer, state.brightness);
if state.dither { if state.dither {
image::imageops::dither(&mut buffer, &colour_map); image::imageops::dither(&mut buffer, &colour_map);
} else { } else {
image::imageops::colorops::index_colors(&mut buffer, &colour_map); // 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);
colour_map
.lookup(p.0[0] as usize)
.expect("indexed color out-of-range")
.into()
});
let mut indexed = buffer.clone();
for (i, pixel) in buffer.pixels().enumerate() {
// todo get rid of magic numbers! what about Bitsy HD?
let mut pixel = image::Rgba::from(pixel.0);
colour_map.map_color(&mut pixel);
indexed.put_pixel(
(i % 128) as u32,
(i / 128) as u32,
pixel
);
}
} }
image::DynamicImage::ImageRgba8(buffer) image::DynamicImage::ImageRgba8(buffer)