Skip to content

Commit

Permalink
Added option in lasso inpaint to maintain original selection ratio in…
Browse files Browse the repository at this point in the history
…stead of squaring.
  • Loading branch information
AbdullahAlfaraj committed Nov 21, 2023
1 parent 0a6d412 commit f039b38
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
36 changes: 31 additions & 5 deletions selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ async function channelToSelectionExe(channel_name = 'mask') {
}
}

function keepRatio(selectionInfo, offset) {
// Calculate the current width and height
let width = selectionInfo.right - selectionInfo.left
let height = selectionInfo.bottom - selectionInfo.top

// Calculate the new coordinates with the offset
selectionInfo.right += offset
selectionInfo.left -= offset
selectionInfo.bottom += offset
selectionInfo.top -= offset

// Update width and height
selectionInfo.width = width + 2 * offset
selectionInfo.height = height + 2 * offset

return selectionInfo
}

function makeSquare(selectionInfo, offset) {
// Calculate the current width and height
let width = selectionInfo.right - selectionInfo.left
Expand All @@ -349,12 +367,20 @@ function makeSquare(selectionInfo, offset) {
return selectionInfo
}

async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
async function inpaintLassoInitImageAndMask(
channel_name = 'mask',
offset = 0,
make_square = true
) {
const selectionInfo = await psapi.getSelectionInfoExe()
//convert the selection box into square box so that you have best output results
const squareSelection = makeSquare(selectionInfo, offset)

const newSelection = make_square
? makeSquare(selectionInfo, offset)
: keepRatio(selectionInfo, offset)

//correct width and height sliders, since this is lasso mode.
await calcWidthHeightFromSelection(squareSelection)
await calcWidthHeightFromSelection(newSelection)

async function getImageFromCanvas() {
const width = html_manip.getWidth()
Expand All @@ -363,7 +389,7 @@ async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
const base64 = await io.IO.getSelectionFromCanvasAsBase64Interface_New(
width,
height,
squareSelection,
newSelection,
true
)
return base64
Expand Down Expand Up @@ -396,7 +422,7 @@ async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
synchronousExecution: true,
})
// const selection_info = await psapi.getSelectionInfoExe()
mask_base64 = await fillSelectionWhiteOutsideBlack(squareSelection)
mask_base64 = await fillSelectionWhiteOutsideBlack(newSelection)
})

//save laso selection to channel
Expand Down
16 changes: 16 additions & 0 deletions typescripts/sd_tab/sd_tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ const Modes = observer(() => {
>
Lasso Mode
</SpCheckBox>
<SpCheckBox
style={{
marginLeft: '10px',
display: store.data.is_lasso_mode
? void 0
: 'none',
}}
onChange={() => {
helper_store.data.make_square =
!helper_store.data.make_square
}}
checked={helper_store.data.make_square}
// id={`chEnableControlNet_${this.props.index}`}
>
Make Square
</SpCheckBox>

<SpSlider
show-value="false"
Expand Down
1 change: 1 addition & 0 deletions typescripts/sd_tab/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export const helper_store = new AStore({
native_presets: {},
base_size: 512 as number,
lasso_offset: 10 as number,
make_square: true as boolean,
})
export async function refreshModels() {
let b_result = false
Expand Down
3 changes: 2 additions & 1 deletion typescripts/session/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ export class LassoInpaintMode extends Img2ImgMode {
}
const [init_image, mask] = await selection.inpaintLassoInitImageAndMask(
'mask',
sd_tab_util.helper_store.data.lasso_offset
sd_tab_util.helper_store.data.lasso_offset,
sd_tab_util.helper_store.data.make_square
)

const selectionInfo = await psapi.getSelectionInfoExe()
Expand Down

0 comments on commit f039b38

Please sign in to comment.