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
import type {
CanvasControlLayerContract,
CanvasLayerContract,
CanvasNodeContract,
} from '@workbench/canvas-engine/contracts';
import {
documentFrom,
groupContract,
layerContract,
} from '@workbench/canvas-engine/document-model/documentFixtures.testStub';
import { compileDocumentLeaves } from '@workbench/canvas-engine/document-model/documentModel';
import { leafEditRefusal } from '@workbench/canvas-engine/document/layerEligibility';
import { createTestStubRasterBackend, type StubRasterSurface } from '@workbench/canvas-engine/render/raster.testStub';
import { describe, expect, it } from 'vitest';
import {
bakePixelEditSurface,
buildMaterializedPixelLayer,
decidePixelEdit,
isLeafPixelEditEligible,
leafPaintRefusal,
} from './controlPixelEdit';
const control = (overrides: Partial<CanvasControlLayerContract> = {}): CanvasControlLayerContract => ({
adapter: { beginEndStepPct: [0.1, 0.9], controlMode: 'more_control', kind: 'controlnet', model: 'm', weight: 0.7 },
blendMode: 'screen',
filter: { settings: { low: 10 }, type: 'canny' },
id: 'control',
isEnabled: true,
isLocked: false,
name: 'Control',
opacity: 0.6,
source: { bitmap: null, type: 'paint' },
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'control',
withTransparencyEffect: true,
...overrides,
});
describe('decidePixelEdit', () => {
it.each([
['locked', control({ isLocked: true }), true, true, 'locked'],
['disabled', control({ isEnabled: false }), true, true, 'disabled'],
[
'unsupported polygon',
control({
source: {
fill: '#fff',
height: 10,
kind: 'polygon',
points: [],
stroke: null,
strokeWidth: 0,
type: 'shape',
width: 10,
},
}),
true,
true,
'unsupported',
],
[
'stale image',
control({ source: { image: { height: 10, imageName: 'image', width: 10 }, type: 'image' } }),
true,
false,
'not-ready',
],
] as const)('rejects a %s control', (_scenario, layer, hasSourceContent, isCacheReady, reason) => {
expect(decidePixelEdit({ hasSourceContent, isCacheReady, layer })).toEqual({ reason, status: 'rejected' });
});
it('edits an empty identity paint control directly', () => {
expect(decidePixelEdit({ hasSourceContent: false, isCacheReady: true, layer: control() })).toEqual({
status: 'direct',
});
});
it.each([
control({ source: { image: { height: 10, imageName: 'image', width: 10 }, type: 'image' } }),
control({ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 12, y: 8 } }),
])('materializes a ready non-direct control', (layer) => {
expect(decidePixelEdit({ hasSourceContent: true, isCacheReady: true, layer })).toEqual({
status: 'materialize',
});
});
it('materializes a ready raster image and rejects a stale one', () => {
const layer: CanvasLayerContract = {
blendMode: 'normal',
id: 'image',
isEnabled: true,
isLocked: false,
name: 'Image',
opacity: 1,
source: { image: { height: 10, imageName: 'image', width: 10 }, type: 'image' },
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'raster',
};
expect(decidePixelEdit({ hasSourceContent: true, isCacheReady: true, layer })).toEqual({
status: 'materialize',
});
expect(decidePixelEdit({ hasSourceContent: true, isCacheReady: false, layer })).toEqual({
reason: 'not-ready',
status: 'rejected',
});
});
});
describe('isLeafPixelEditEligible', () => {
const rasterPaint: CanvasLayerContract = {
blendMode: 'normal',
id: 'raster',
isEnabled: true,
isLocked: false,
name: 'Raster',
opacity: 1,
source: { bitmap: null, type: 'paint' },
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'raster',
};
it.each([
['raster paint', rasterPaint, true],
['paint control', control(), true],
[
'image control',
control({ source: { image: { height: 10, imageName: 'control-image', width: 10 }, type: 'image' } }),
true,
],
[
'rectangle control',
control({
source: { fill: '#fff', height: 10, kind: 'rect', stroke: null, strokeWidth: 0, type: 'shape', width: 10 },
}),
true,
],
[
'polygon control',
control({
source: {
fill: '#fff',
height: 10,
kind: 'polygon',
points: [],
stroke: null,
strokeWidth: 0,
type: 'shape',
width: 10,
},
}),
false,
],
[
'raster image',
{ ...rasterPaint, source: { image: { height: 10, imageName: 'i', width: 10 }, type: 'image' } },
false,
],
['locked control', control({ isLocked: true }), false],
['disabled control', control({ isEnabled: false }), false],
['missing layer', undefined, false],
] as const)('returns %s eligibility for %s', (_scenario, layer, expected) => {
expect(isLeafPixelEditEligible(layer ? compileDocumentLeaves(documentFrom([layer]))[0] : null)).toBe(expected);
});
it('refuses a leaf whose ancestor is disabled or locked', () => {
const gated = compileDocumentLeaves(documentFrom([groupContract('g', [control()], { isEnabled: false })]));
expect(isLeafPixelEditEligible(gated[0])).toBe(false);
const locked = compileDocumentLeaves(documentFrom([groupContract('g', [control()], { isLocked: true })]));
expect(isLeafPixelEditEligible(locked[0])).toBe(false);
});
});
describe('buildMaterializedPixelLayer', () => {
it('changes only source and transform for a control layer', () => {
const before = control({
source: { image: { height: 10, imageName: 'image', width: 10 }, type: 'image' },
transform: { rotation: Math.PI / 2, scaleX: 2, scaleY: 1, x: 30, y: 40 },
});
const after = buildMaterializedPixelLayer(before, { height: 20, width: 10, x: 20, y: 40 });
expect(after).toEqual({
...before,
source: { bitmap: null, offset: { x: 20, y: 40 }, type: 'paint' },
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
});
expect(before.source.type).toBe('image');
});
it("moves a control layer's kept original with the baked transform", () => {
const depth = { settings: {}, type: 'depth_anything_depth_estimation' };
const before = control({
controlType: 'depth',
detection: depth,
filter: depth,
original: { image: { height: 10, imageName: 'photo', width: 10 }, type: 'image' },
originalTransform: { rotation: 0, scaleX: 1, scaleY: 1, x: 5, y: 0 },
transform: { rotation: 0, scaleX: 2, scaleY: 2, x: 10, y: 20 },
});
const after = buildMaterializedPixelLayer(before, { height: 20, width: 20, x: 10, y: 20 });
expect(after).toMatchObject({ controlType: 'depth', detection: depth, original: before.original });
expect(after.type === 'control' && after.originalTransform).toEqual({
rotation: 0,
scaleX: 2,
scaleY: 2,
x: 20,
y: 20,
});
});
it('detaches a control layer whose original the bake would shear', () => {
const depth = { settings: {}, type: 'depth_anything_depth_estimation' };
const before = control({
controlType: 'depth',
detection: depth,
filter: depth,
original: { image: { height: 10, imageName: 'photo', width: 10 }, type: 'image' },
originalTransform: { rotation: Math.PI / 4, scaleX: 1, scaleY: 1, x: 0, y: 0 },
transform: { rotation: 0, scaleX: 2, scaleY: 1, x: 0, y: 0 },
});
const after = buildMaterializedPixelLayer(before, { height: 10, width: 20, x: 0, y: 0 });
expect(after).toMatchObject({ controlType: 'depth', detection: null, filter: depth });
expect(after).not.toHaveProperty('original');
expect(after).not.toHaveProperty('originalTransform');
});
it('clears raster adjustments after baking them into a paint source', () => {
const before: CanvasLayerContract = {
adjustments: [
{ brightness: 0.2, contrast: -0.1, id: 'adj-bc', isEnabled: true, type: 'brightness-contrast' as const },
{ id: 'adj-hsl', isEnabled: true, saturation: 0.3, type: 'hsl' as const },
],
blendMode: 'normal',
id: 'image',
isEnabled: true,
isLocked: false,
name: 'Image',
opacity: 1,
source: { image: { height: 10, imageName: 'image', width: 10 }, type: 'image' },
transform: { rotation: 0, scaleX: 1.5, scaleY: 1, x: 3, y: 4 },
type: 'raster',
};
const after = buildMaterializedPixelLayer(before, { height: 10, width: 15, x: 3, y: 4 });
expect(after).toEqual({
blendMode: 'normal',
id: 'image',
isEnabled: true,
isLocked: false,
name: 'Image',
opacity: 1,
source: { bitmap: null, offset: { x: 3, y: 4 }, type: 'paint' },
transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
type: 'raster',
});
expect(before).toHaveProperty('adjustments');
});
});
describe('bakePixelEditSurface', () => {
it('bakes offset source pixels through the complete translated, rotated, and scaled transform', () => {
const backend = createTestStubRasterBackend();
const source = backend.createSurface(10, 5);
const sourceRect = { height: 5, width: 10, x: 5, y: -4 };
const baked = bakePixelEditSurface({
backend,
source,
sourceRect,
transform: { rotation: Math.PI / 2, scaleX: 2, scaleY: 3, x: 7, y: 11 },
});
expect(baked.rect).toEqual({ height: 20, width: 15, x: 4, y: 21 });
expect(baked.surface.width).toBe(15);
expect(baked.surface.height).toBe(20);
expect((baked.surface as StubRasterSurface).callLog).toEqual([
{ args: [1, 0, 0, 1, 0, 0], op: 'setTransform' },
{ args: [0, 0, 15, 20], op: 'clearRect' },
{ args: ['imageSmoothingEnabled', false], op: 'set' },
{
args: [2 * Math.cos(Math.PI / 2), 2, -3, 3 * Math.cos(Math.PI / 2), 3, -10],
op: 'setTransform',
},
{ args: [source.canvas, sourceRect.x, sourceRect.y], op: 'drawImage' },
{ args: [1, 0, 0, 1, 0, 0], op: 'setTransform' },
]);
});
});
describe('leafPaintRefusal', () => {
const leafOf = (node: CanvasNodeContract) => compileDocumentLeaves(documentFrom([node]))[0]!;
it.each([
['an editable mask', {}, null],
['a disabled mask', { isEnabled: false }, 'layer-hidden'],
['a mask hidden on the canvas', { isHidden: true }, 'layer-display-hidden'],
['a locked mask', { isLocked: true }, 'layer-locked'],
['a disabled, hidden, locked mask', { isEnabled: false, isHidden: true, isLocked: true }, 'layer-hidden'],
['a hidden, locked mask', { isHidden: true, isLocked: true }, 'layer-display-hidden'],
] as const)('answers %s with %s', (_, overrides, expected) => {
expect(leafPaintRefusal(leafOf(layerContract('m', 'inpaint_mask', overrides)))).toBe(expected);
});
it('folds a group that hides its children on the canvas, which edit refusals ignore', () => {
const leaf = leafOf(groupContract('g', [layerContract('m', 'inpaint_mask')], { isHidden: true }));
expect(leafPaintRefusal(leaf)).toBe('layer-display-hidden');
expect(leafEditRefusal(leaf)).toBeNull();
});
});