Skip to content

Commit

Permalink
feat(gui): add fill with white, toggle for outpainting
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 15, 2023
1 parent 09c9b2c commit 0d53fdf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions gui/src/api/client.ts
Expand Up @@ -46,6 +46,8 @@ export interface InpaintParams extends BaseImgParams {
}

export interface OutpaintPixels {
enabled: boolean;

left: number;
right: number;
top: number;
Expand Down
2 changes: 1 addition & 1 deletion gui/src/components/Inpaint.tsx
Expand Up @@ -27,7 +27,7 @@ export function Inpaint(props: InpaintProps) {
async function uploadSource(): Promise<void> {
const outpaint = state.getState().outpaint; // TODO: seems shady

if (outpaint.bottom > 0 || outpaint.left > 0 || outpaint.right > 0 || outpaint.top > 0) {
if (outpaint.enabled) {
const output = await client.outpaint({
...params,
...outpaint,
Expand Down
24 changes: 19 additions & 5 deletions gui/src/components/MaskCanvas.tsx
Expand Up @@ -233,8 +233,8 @@ export function MaskCanvas(props: MaskCanvasProps) {
<Stack direction='row' spacing={4}>
<NumericField
label='Brush Color'
min={0}
max={255}
min={COLORS.black}
max={COLORS.white}
step={1}
value={brush.color}
onChange={(color) => {
Expand All @@ -243,7 +243,7 @@ export function MaskCanvas(props: MaskCanvasProps) {
/>
<NumericField
label='Brush Size'
min={4}
min={1}
max={64}
step={1}
value={brush.size}
Expand Down Expand Up @@ -282,6 +282,16 @@ export function MaskCanvas(props: MaskCanvasProps) {
}}>
Fill with black
</Button>
<Button
variant='outlined'
startIcon={<FormatColorFill />}
onClick={() => {
floodCanvas(bufferRef, floodWhite);
drawBuffer();
save();
}}>
Fill with white
</Button>
<Button
variant='outlined'
startIcon={<Gradient />}
Expand Down Expand Up @@ -333,7 +343,11 @@ export function floodAbove(n: number): number {
}

export function floodBlack(): number {
return 0;
return COLORS.black;
}

export function floodWhite(): number {
return COLORS.white;
}

export function grayToRGB(n: number, o = 1.0): string {
Expand All @@ -355,7 +369,7 @@ function floodCanvas(ref: RefObject<HTMLCanvasElement>, flood: FloodFn) {
pixels[i + 1] = final;
pixels[i + 2] = final;
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
pixels[i + 3] = 255;
pixels[i + 3] = COLORS.white; // fully opaque
}
}

Expand Down
6 changes: 5 additions & 1 deletion gui/src/components/NumericField.tsx
Expand Up @@ -12,6 +12,7 @@ export function parseNumber(num: string, decimal = false): number {

export interface ImageControlProps {
decimal?: boolean;
disabled?: boolean;
label: string;
min: number;
max: number;
Expand All @@ -22,10 +23,12 @@ export interface ImageControlProps {
}

export function NumericField(props: ImageControlProps) {
const { decimal, label, min, max, step, value } = props;
const { decimal = false, disabled = false, label, min, max, step, value } = props;

return <Stack spacing={2}>
<TextField
label={label}
disabled={disabled}
variant='outlined'
type='number'
inputProps={{ min, max, step }}
Expand All @@ -37,6 +40,7 @@ export function NumericField(props: ImageControlProps) {
}}
/>
<Slider
disabled={disabled}
min={min}
max={max}
step={step}
Expand Down
20 changes: 19 additions & 1 deletion gui/src/components/OutpaintControl.tsx
@@ -1,5 +1,6 @@
import { mustExist } from '@apextoaster/js-utils';
import { Stack } from '@mui/material';
import { Check } from '@mui/icons-material';
import { Stack, ToggleButton } from '@mui/material';
import * as React from 'react';
import { useContext } from 'react';
import { useStore } from 'zustand';
Expand All @@ -21,8 +22,22 @@ export function OutpaintControl(props: OutpaintControlProps) {
const setOutpaint = useStore(state, (s) => s.setOutpaint);

return <Stack direction='row' spacing={4}>
<ToggleButton
color='primary'
selected={params.enabled}
value='check'
onChange={(event) => {
setOutpaint({
enabled: params.enabled === false,
});
}}
>
<Check />
Outpainting
</ToggleButton>
<NumericField
label='Left'
disabled={params.enabled === false}
min={0}
max={config.width.max}
step={config.width.step}
Expand All @@ -35,6 +50,7 @@ export function OutpaintControl(props: OutpaintControlProps) {
/>
<NumericField
label='Right'
disabled={params.enabled === false}
min={0}
max={config.width.max}
step={config.width.step}
Expand All @@ -47,6 +63,7 @@ export function OutpaintControl(props: OutpaintControlProps) {
/>
<NumericField
label='Top'
disabled={params.enabled === false}
min={0}
max={config.height.max}
step={config.height.step}
Expand All @@ -59,6 +76,7 @@ export function OutpaintControl(props: OutpaintControlProps) {
/>
<NumericField
label='Bottom'
disabled={params.enabled === false}
min={0}
max={config.height.max}
step={config.height.step}
Expand Down
1 change: 1 addition & 0 deletions gui/src/state.ts
Expand Up @@ -184,6 +184,7 @@ export function createStateSlices(base: ConfigParams) {

const createOutpaintSlice: StateCreator<OnnxState, [], [], OutpaintSlice> = (set) => ({
outpaint: {
enabled: false,
left: 0,
right: 0,
top: 0,
Expand Down

0 comments on commit 0d53fdf

Please sign in to comment.