Skip to content

Commit f039b38

Browse files
Added option in lasso inpaint to maintain original selection ratio instead of squaring.
1 parent 0a6d412 commit f039b38

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

selection.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,24 @@ async function channelToSelectionExe(channel_name = 'mask') {
324324
}
325325
}
326326

327+
function keepRatio(selectionInfo, offset) {
328+
// Calculate the current width and height
329+
let width = selectionInfo.right - selectionInfo.left
330+
let height = selectionInfo.bottom - selectionInfo.top
331+
332+
// Calculate the new coordinates with the offset
333+
selectionInfo.right += offset
334+
selectionInfo.left -= offset
335+
selectionInfo.bottom += offset
336+
selectionInfo.top -= offset
337+
338+
// Update width and height
339+
selectionInfo.width = width + 2 * offset
340+
selectionInfo.height = height + 2 * offset
341+
342+
return selectionInfo
343+
}
344+
327345
function makeSquare(selectionInfo, offset) {
328346
// Calculate the current width and height
329347
let width = selectionInfo.right - selectionInfo.left
@@ -349,12 +367,20 @@ function makeSquare(selectionInfo, offset) {
349367
return selectionInfo
350368
}
351369

352-
async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
370+
async function inpaintLassoInitImageAndMask(
371+
channel_name = 'mask',
372+
offset = 0,
373+
make_square = true
374+
) {
353375
const selectionInfo = await psapi.getSelectionInfoExe()
354376
//convert the selection box into square box so that you have best output results
355-
const squareSelection = makeSquare(selectionInfo, offset)
377+
378+
const newSelection = make_square
379+
? makeSquare(selectionInfo, offset)
380+
: keepRatio(selectionInfo, offset)
381+
356382
//correct width and height sliders, since this is lasso mode.
357-
await calcWidthHeightFromSelection(squareSelection)
383+
await calcWidthHeightFromSelection(newSelection)
358384

359385
async function getImageFromCanvas() {
360386
const width = html_manip.getWidth()
@@ -363,7 +389,7 @@ async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
363389
const base64 = await io.IO.getSelectionFromCanvasAsBase64Interface_New(
364390
width,
365391
height,
366-
squareSelection,
392+
newSelection,
367393
true
368394
)
369395
return base64
@@ -396,7 +422,7 @@ async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
396422
synchronousExecution: true,
397423
})
398424
// const selection_info = await psapi.getSelectionInfoExe()
399-
mask_base64 = await fillSelectionWhiteOutsideBlack(squareSelection)
425+
mask_base64 = await fillSelectionWhiteOutsideBlack(newSelection)
400426
})
401427

402428
//save laso selection to channel

typescripts/sd_tab/sd_tab.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ const Modes = observer(() => {
105105
>
106106
Lasso Mode
107107
</SpCheckBox>
108+
<SpCheckBox
109+
style={{
110+
marginLeft: '10px',
111+
display: store.data.is_lasso_mode
112+
? void 0
113+
: 'none',
114+
}}
115+
onChange={() => {
116+
helper_store.data.make_square =
117+
!helper_store.data.make_square
118+
}}
119+
checked={helper_store.data.make_square}
120+
// id={`chEnableControlNet_${this.props.index}`}
121+
>
122+
Make Square
123+
</SpCheckBox>
108124

109125
<SpSlider
110126
show-value="false"

typescripts/sd_tab/util.ts

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export const helper_store = new AStore({
206206
native_presets: {},
207207
base_size: 512 as number,
208208
lasso_offset: 10 as number,
209+
make_square: true as boolean,
209210
})
210211
export async function refreshModels() {
211212
let b_result = false

typescripts/session/modes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ export class LassoInpaintMode extends Img2ImgMode {
588588
}
589589
const [init_image, mask] = await selection.inpaintLassoInitImageAndMask(
590590
'mask',
591-
sd_tab_util.helper_store.data.lasso_offset
591+
sd_tab_util.helper_store.data.lasso_offset,
592+
sd_tab_util.helper_store.data.make_square
592593
)
593594

594595
const selectionInfo = await psapi.getSelectionInfoExe()

0 commit comments

Comments
 (0)