-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): max upscale pixels config (#4765)
* feat(ui): max upscale pixels config Add `maxUpscalePixels: number` to the app config. The number should be the *total* number of pixels eg `maxUpscalePixels: 4096 * 4096`. If not provided, any size image may be upscaled. If the config is provided, users will see be advised if their image is too large for either model, or told to switch to an x2 model if it's only too large for x4. The message is via tooltip in the popover and via toast if the user uses the hotkey to upscale. * feat(ui): "mayUpscale" -> "isAllowedToUpscale"
- Loading branch information
1 parent
208bf68
commit f002ae8
Showing
7 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
invokeai/frontend/web/src/features/parameters/hooks/useIsAllowedToUpscale.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { createSelector } from '@reduxjs/toolkit'; | ||
import { stateSelector } from 'app/store/store'; | ||
import { useAppSelector } from 'app/store/storeHooks'; | ||
import { defaultSelectorOptions } from 'app/store/util/defaultMemoizeOptions'; | ||
import { useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { ImageDTO } from 'services/api/types'; | ||
|
||
const getUpscaledPixels = (imageDTO?: ImageDTO, maxUpscalePixels?: number) => { | ||
if (!imageDTO) { | ||
return; | ||
} | ||
if (!maxUpscalePixels) { | ||
return; | ||
} | ||
const { width, height } = imageDTO; | ||
const x4 = height * 4 * width * 4; | ||
const x2 = height * 2 * width * 2; | ||
return { x4, x2 }; | ||
}; | ||
|
||
const getIsAllowedToUpscale = ( | ||
upscaledPixels?: ReturnType<typeof getUpscaledPixels>, | ||
maxUpscalePixels?: number | ||
) => { | ||
if (!upscaledPixels || !maxUpscalePixels) { | ||
return { x4: true, x2: true }; | ||
} | ||
const isAllowedToUpscale = { x4: false, x2: false }; | ||
if (upscaledPixels.x4 <= maxUpscalePixels) { | ||
isAllowedToUpscale.x4 = true; | ||
} | ||
if (upscaledPixels.x2 <= maxUpscalePixels) { | ||
isAllowedToUpscale.x2 = true; | ||
} | ||
|
||
return isAllowedToUpscale; | ||
}; | ||
|
||
const getDetailTKey = ( | ||
isAllowedToUpscale?: ReturnType<typeof getIsAllowedToUpscale>, | ||
scaleFactor?: number | ||
) => { | ||
if (!isAllowedToUpscale || !scaleFactor) { | ||
return; | ||
} | ||
|
||
if (isAllowedToUpscale.x4 && isAllowedToUpscale.x2) { | ||
return; | ||
} | ||
|
||
if (!isAllowedToUpscale.x2 && !isAllowedToUpscale.x4) { | ||
return 'parameters.isAllowedToUpscale.tooLarge'; | ||
} | ||
|
||
if (!isAllowedToUpscale.x4 && isAllowedToUpscale.x2 && scaleFactor === 4) { | ||
return 'parameters.isAllowedToUpscale.useX2Model'; | ||
} | ||
|
||
return; | ||
}; | ||
|
||
export const createIsAllowedToUpscaleSelector = (imageDTO?: ImageDTO) => | ||
createSelector( | ||
stateSelector, | ||
({ postprocessing, config }) => { | ||
const { esrganModelName } = postprocessing; | ||
const { maxUpscalePixels } = config; | ||
|
||
const upscaledPixels = getUpscaledPixels(imageDTO, maxUpscalePixels); | ||
const isAllowedToUpscale = getIsAllowedToUpscale( | ||
upscaledPixels, | ||
maxUpscalePixels | ||
); | ||
const scaleFactor = esrganModelName.includes('x2') ? 2 : 4; | ||
const detailTKey = getDetailTKey(isAllowedToUpscale, scaleFactor); | ||
return { | ||
isAllowedToUpscale: | ||
scaleFactor === 2 ? isAllowedToUpscale.x2 : isAllowedToUpscale.x4, | ||
detailTKey, | ||
}; | ||
}, | ||
defaultSelectorOptions | ||
); | ||
|
||
export const useIsAllowedToUpscale = (imageDTO?: ImageDTO) => { | ||
const { t } = useTranslation(); | ||
const selectIsAllowedToUpscale = useMemo( | ||
() => createIsAllowedToUpscaleSelector(imageDTO), | ||
[imageDTO] | ||
); | ||
const { isAllowedToUpscale, detailTKey } = useAppSelector( | ||
selectIsAllowedToUpscale | ||
); | ||
|
||
return { | ||
isAllowedToUpscale, | ||
detail: detailTKey ? t(detailTKey) : undefined, | ||
}; | ||
}; |