fix dithering and indexing for image

This commit is contained in:
Max Bradbury 2020-11-07 13:48:00 +00:00
parent 3a8e349aac
commit 45ef41c366
2 changed files with 30 additions and 6 deletions

View File

@ -63,7 +63,7 @@ html(lang="en-gb")
select#palette
label
input#dither(type="checkbox")
input#dither(type="checkbox" checked=true)
| dither
br

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::*;
@ -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)