Skip to content

Commit

Permalink
feat(ui): use 64 as grid for auto-scaled bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
psychedelicious committed Sep 8, 2024
1 parent 2301b38 commit 5eb919f
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import { roundToMultiple } from 'common/util/roundDownToMultiple';
import type { Dimensions } from 'features/controlLayers/store/types';

const CANVAS_GRID_SIZE_FINE = 8;

/**
* 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 => {
export const getScaledBoundingBoxDimensions = (
dimensions: Dimensions,
optimalDimension: number,
gridSize: number = 64
): 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;
let maxDimension = optimalDimension - gridSize;
while (currentArea < targetArea) {
maxDimension += CANVAS_GRID_SIZE_FINE;
maxDimension += gridSize;
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);
scaledDimensions.height = roundToMultiple(maxDimension / aspectRatio, gridSize);
} else if (aspectRatio < 1) {
scaledDimensions.height = maxDimension;
scaledDimensions.width = roundToMultiple(maxDimension * aspectRatio, CANVAS_GRID_SIZE_FINE);
scaledDimensions.width = roundToMultiple(maxDimension * aspectRatio, gridSize);
}
currentArea = scaledDimensions.width * scaledDimensions.height;
}
Expand Down

0 comments on commit 5eb919f

Please sign in to comment.