From 0dc582bdd1984ccb4a6bd48f067e625f53cf353c Mon Sep 17 00:00:00 2001 From: CrispyBaguette Date: Mon, 13 Dec 2021 22:56:54 +0100 Subject: [PATCH 1/5] Support dithering with other palettes --- client/src/App.tsx | 7 ++-- client/src/ImageInput.tsx | 26 ++++++++++---- client/src/ImageOutput.tsx | 10 +++--- client/src/ImagePreview.tsx | 10 +++--- client/src/Palette.ts | 68 +++++++++++++++++++++++++++++++++++++ client/src/lib/Ditherer.ts | 9 +++-- src/wasm.go | 42 ++++++++++------------- 7 files changed, 126 insertions(+), 46 deletions(-) create mode 100644 client/src/Palette.ts diff --git a/client/src/App.tsx b/client/src/App.tsx index 6567fe3..2e3e458 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -4,6 +4,7 @@ import ImageOutput from "./ImageOutput"; import Ditherer from "./lib/Ditherer"; import ImagePreview from "./ImagePreview"; import Header from "./Header"; +import Palette from "./Palette"; enum AppState { NO_IMAGE, @@ -16,13 +17,13 @@ function App() { const [ditheredImage, setDitheredImage] = React.useState(); const [appState, setAppState] = React.useState(AppState.NO_IMAGE); - const handleImageSubmit = async (data: Blob) => { + const handleImageSubmit = async (data: Blob, palette: Palette) => { setBaseImage(data); setAppState(AppState.IMAGE_LOADED); try { const imageArray = new Uint8ClampedArray(await data.arrayBuffer()); - const ditheredImage = await new Ditherer().dither(imageArray); + const ditheredImage = await new Ditherer().dither(imageArray, palette); setDitheredImage(new Blob([ditheredImage], { type: "image/png" })); setAppState(AppState.IMAGE_PROCESSED); } catch (e) { @@ -34,7 +35,7 @@ function App() { return (
-
+

Go+Wasm image dithering tool diff --git a/client/src/ImageInput.tsx b/client/src/ImageInput.tsx index abe2c6c..f0d2997 100644 --- a/client/src/ImageInput.tsx +++ b/client/src/ImageInput.tsx @@ -1,11 +1,13 @@ import React, { FormEventHandler } from "react"; +import Palette, { palettes } from "./Palette"; interface Props { - onImageSubmit: (image: Blob) => void; + onImageSubmit: (image: Blob, palette: Palette) => void; } function ImageInput({ onImageSubmit }: Props) { const fileInputRef = React.useRef(null); + const [paletteIndex, setPaletteIndex] = React.useState(0); const handleSubmit: FormEventHandler = (e) => { e.preventDefault(); @@ -17,7 +19,11 @@ function ImageInput({ onImageSubmit }: Props) { return; } - onImageSubmit(fileInputRef.current.files[0]); + onImageSubmit(fileInputRef.current.files[0], palettes[paletteIndex]); + }; + + const handlePaletteChange = (event: React.ChangeEvent) => { + setPaletteIndex(parseInt(event.target.value)); }; return ( @@ -34,12 +40,20 @@ function ImageInput({ onImageSubmit }: Props) { className="block w-full mt-1 text-sm text-nord-0 file:mr-4 file:py-2 file:px-4 file:border-0 file:text-sm file:font-semibold file:bg-nord-4 file:text-nord-0 hover:file:bg-nord-5" /> - {/*