Compare commits

...

4 Commits

3 changed files with 36 additions and 11 deletions

View File

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

View File

@ -93,7 +93,7 @@ async function run() {
}
function new_game() {
load_default_game()
console.debug(load_default_game());
textareaGameDataInput.value = output();
checkGameData();
// 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() {
let palettes = JSON.parse(get_palettes());
const palettes = JSON.parse(get_palettes());
console.debug(palettes);
selectPalette.innerHTML = "";

View File

@ -4,6 +4,8 @@ use bitsy_parser::game::Game;
use bitsy_parser::image::Image;
use bitsy_parser::tile::Tile;
use image::{GenericImageView, Pixel, DynamicImage};
use image::imageops::ColorMap;
use image::imageops::FilterType::CatmullRom;
use lazy_static::lazy_static;
use std::sync::Mutex;
use wasm_bindgen::prelude::*;
@ -62,7 +64,7 @@ pub fn load_game(game_data: String) -> String {
let palette_id = game.palette_ids()[0].clone();
state.game = Some(game);
state.palette = Some(palette_id);
"".to_string()
format!("Loaded game")
},
_ => {
state.game = None;
@ -83,6 +85,7 @@ pub fn load_image(image_base64: String) -> String {
Ok(image) => {
match image::load_from_memory(image.as_ref()) {
Ok(image) => {
// todo get rid of magic numbers! what about Bitsy HD?
let image = image.resize(128, 128, CatmullRom);
state.image = Some(image);
"OK"
@ -166,15 +169,36 @@ fn render_preview(state: &State) -> DynamicImage {
// todo get actual chosen palette
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);
image::imageops::brighten(&mut buffer, state.brightness);
let mut buffer = image::imageops::brighten(&mut buffer, state.brightness);
if state.dither {
image::imageops::dither(&mut buffer, &colour_map);
} 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)