mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-02-16 03:45:48 -05:00
39 lines
1.6 KiB
TypeScript
39 lines
1.6 KiB
TypeScript
import { roundToMultiple } from 'common/util/roundDownToMultiple';
|
|
import { CANVAS_GRID_SIZE_FINE } from 'features/controlLayers/konva/constants';
|
|
import type { Dimensions } from 'features/controlLayers/store/types';
|
|
|
|
/**
|
|
* Scales the bounding box dimensions to the optimal dimension. The optimal dimensions should be the trained dimension
|
|
* for the model. For example, 1024 for SDXL or 512 for SD1.5.
|
|
* @param dimensions The un-scaled bbox dimensions
|
|
* @param optimalDimension The optimal dimension to scale the bbox to
|
|
*/
|
|
export const getScaledBoundingBoxDimensions = (dimensions: Dimensions, optimalDimension: number): Dimensions => {
|
|
const { width, height } = dimensions;
|
|
|
|
const scaledDimensions = { width, height };
|
|
const targetArea = optimalDimension * optimalDimension;
|
|
const aspectRatio = width / height;
|
|
let currentArea = width * height;
|
|
let maxDimension = optimalDimension - CANVAS_GRID_SIZE_FINE;
|
|
while (currentArea < targetArea) {
|
|
maxDimension += CANVAS_GRID_SIZE_FINE;
|
|
if (width === height) {
|
|
scaledDimensions.width = optimalDimension;
|
|
scaledDimensions.height = optimalDimension;
|
|
break;
|
|
} else {
|
|
if (aspectRatio > 1) {
|
|
scaledDimensions.width = maxDimension;
|
|
scaledDimensions.height = roundToMultiple(maxDimension / aspectRatio, CANVAS_GRID_SIZE_FINE);
|
|
} else if (aspectRatio < 1) {
|
|
scaledDimensions.height = maxDimension;
|
|
scaledDimensions.width = roundToMultiple(maxDimension * aspectRatio, CANVAS_GRID_SIZE_FINE);
|
|
}
|
|
currentArea = scaledDimensions.width * scaledDimensions.height;
|
|
}
|
|
}
|
|
|
|
return scaledDimensions;
|
|
};
|