Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
/** Layer fits snap inward; mask fits pad by blur plus `MASK_FIT_MARGIN` and snap outward. Null when empty. */
import {
compileDocumentLeaves,
getSourceBounds,
isEmpty,
isRenderableLayer,
type CanvasDocumentContractV3,
type CanvasLayerContract,
type Rect,
union,
} from '@workbench/canvas-engine/api';
/** Outward margin (document px) beyond the mask blur, so the feathered edge lands inside the region. */
const MASK_FIT_MARGIN = 8;
/** Snap inward: round origins up and sizes down. gridSize <= 1 uses integer rounding, matching legacy fitRectToGrid. */
export const fitRectToGrid = (rect: Rect, gridSize: number): Rect => {
const g = gridSize > 1 ? gridSize : 1;
const x = Math.ceil(rect.x / g) * g;
const y = Math.ceil(rect.y / g) * g;
const width = Math.floor((rect.width - (x - rect.x)) / g) * g;
const height = Math.floor((rect.height - (y - rect.y)) / g) * g;
return { height, width, x, y };
};
/** Snap outward: round origins down and far edges up, so the result contains `rect`. */
const expandRectToGrid = (rect: Rect, gridSize: number): Rect => {
const g = gridSize > 1 ? gridSize : 1;
const x = Math.floor(rect.x / g) * g;
const y = Math.floor(rect.y / g) * g;
const width = Math.ceil((rect.x + rect.width) / g) * g - x;
const height = Math.ceil((rect.y + rect.height) / g) * g - y;
return { height, width, x, y };
};
/** Union document bounds of enabled, renderable, nonempty layers matching predicate; return null when none qualify. */
export const unionRenderableBounds = (
doc: CanvasDocumentContractV3,
predicate: (layer: CanvasLayerContract) => boolean
): Rect | null => {
let bounds: Rect | null = null;
for (const leaf of compileDocumentLeaves(doc)) {
const layer = leaf.layer;
if (!leaf.contributionEnabled || !isRenderableLayer(layer) || !predicate(layer)) {
continue;
}
const layerBounds = getSourceBounds(layer, doc);
if (isEmpty(layerBounds)) {
continue;
}
bounds = bounds ? union(bounds, layerBounds) : layerBounds;
}
return bounds;
};
/** The grid-snapped bbox that tightly fits all visible content, or `null` when there is none. */
export const computeFitBboxToLayers = (doc: CanvasDocumentContractV3, gridSize: number): Rect | null => {
const bounds = unionRenderableBounds(doc, () => true);
if (!bounds) {
return null;
}
const fitted = fitRectToGrid(bounds, gridSize);
return isEmpty(fitted) ? null : fitted;
};
/** The outward-snapped frame around visible inpaint masks padded by `maskBlur` plus the margin, or null. */
export const computeFitBboxToMasks = (
doc: CanvasDocumentContractV3,
gridSize: number,
maskBlur: number
): Rect | null => {
const bounds = unionRenderableBounds(doc, (layer) => layer.type === 'inpaint_mask');
if (!bounds) {
return null;
}
const padding = maskBlur + MASK_FIT_MARGIN;
const expanded: Rect = {
height: bounds.height + padding * 2,
width: bounds.width + padding * 2,
x: bounds.x - padding,
y: bounds.y - padding,
};
return expandRectToGrid(expanded, gridSize);
};