Skip to content

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.

feat(canvas): define control pixel edit targets

joshistoast8b1fc7fa22a3
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/editing/controlPixelEdit.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/editing/controlPixelEdit.test.ts
new file mode 100644
index 0000000000..aeddc4a2a1
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/editing/controlPixelEdit.test.ts
@@ -0,0 +1,139 @@
+import type { CanvasControlLayerContract, CanvasLayerContract } from '@workbench/types';
+
+import { createTestStubRasterBackend } from '@workbench/canvas-engine/render/raster.testStub';
+import { describe, expect, it } from 'vitest';
+
+import {
+ bakeControlPixelEditSurface,
+ buildMaterializedControlLayer,
+ decideControlPixelEdit,
+ isLayerPixelEditEligible,
+} 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('decideControlPixelEdit', () => {
+ 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(decideControlPixelEdit({ hasSourceContent, isCacheReady, layer })).toEqual({ reason, status: 'rejected' });
+ });
+
+ it('edits an empty identity paint control directly', () => {
+ expect(decideControlPixelEdit({ 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(decideControlPixelEdit({ hasSourceContent: true, isCacheReady: true, layer })).toEqual({
+ status: 'materialize',
+ });
+ });
+});
+
+describe('isLayerPixelEditEligible', () => {
+ 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],
+ ['control', control(), true],
+ [
+ '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(isLayerPixelEditEligible(layer)).toBe(expected);
+ });
+});
+
+describe('buildMaterializedControlLayer', () => {
+ it('changes only source and transform', () => {
+ 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 = buildMaterializedControlLayer(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');
+ });
+});
+
+describe('bakeControlPixelEditSurface', () => {
+ it('bakes source pixels through the transform into document-space bounds', () => {
+ const backend = createTestStubRasterBackend();
+ const source = backend.createSurface(10, 5);
+ const baked = bakeControlPixelEditSurface({
+ backend,
+ source,
+ sourceRect: { height: 5, width: 10, x: 0, y: 0 },
+ transform: { rotation: 0, scaleX: 2, scaleY: 3, x: 7, y: 11 },
+ });
+
+ expect(baked.rect).toEqual({ height: 15, width: 20, x: 7, y: 11 });
+ expect(baked.surface.width).toBe(20);
+ expect(baked.surface.height).toBe(15);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/editing/controlPixelEdit.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/editing/controlPixelEdit.ts
new file mode 100644
index 0000000000..e2c2d18cdb
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/editing/controlPixelEdit.ts
@@ -0,0 +1,92 @@
+import type { RasterBackend, RasterSurface } from '@workbench/canvas-engine/render/raster';
+import type { Rect } from '@workbench/canvas-engine/types';
+import type { CanvasControlLayerContract, CanvasLayerContract } from '@workbench/types';
+
+import { roundOut, transformBounds } from '@workbench/canvas-engine/math/rect';
+import { bakeMatrix, IDENTITY_TRANSFORM, type LayerTransform } from '@workbench/canvas-engine/transform/transformMath';
+
+export type ControlPixelEditRejectedReason = 'disabled' | 'locked' | 'not-ready' | 'unsupported';
+export type ControlPixelEditDecision =
+ | { status: 'direct' }
+ | { status: 'materialize' }
+ | { status: 'rejected'; reason: ControlPixelEditRejectedReason };
+
+export interface DecideControlPixelEditInput {
+ layer: CanvasControlLayerContract;
+ hasSourceContent: boolean;
+ isCacheReady: boolean;
+}
+
+const isIdentity = (transform: LayerTransform): boolean =>
+ transform.x === 0 &&
+ transform.y === 0 &&
+ transform.scaleX === 1 &&
+ transform.scaleY === 1 &&
+ transform.rotation === 0;
+
+const isRasterizable = (layer: CanvasControlLayerContract): boolean =>
+ layer.source.type !== 'shape' || layer.source.kind !== 'polygon';
+
+export const isLayerPixelEditEligible = (layer: CanvasLayerContract | undefined): boolean =>
+ !!layer &&
+ !layer.isLocked &&
+ layer.isEnabled &&
+ ((layer.type === 'raster' && layer.source.type === 'paint') || layer.type === 'control');
+
+export const decideControlPixelEdit = ({
+ hasSourceContent,
+ isCacheReady,
+ layer,
+}: DecideControlPixelEditInput): ControlPixelEditDecision => {
+ if (layer.isLocked) {
+ return { reason: 'locked', status: 'rejected' };
+ }
+ if (!layer.isEnabled) {
+ return { reason: 'disabled', status: 'rejected' };
+ }
+ if (!isRasterizable(layer)) {
+ return { reason: 'unsupported', status: 'rejected' };
+ }
+ if (hasSourceContent && !isCacheReady) {
+ return { reason: 'not-ready', status: 'rejected' };
+ }
+ if (layer.source.type === 'paint' && isIdentity(layer.transform)) {
+ return { status: 'direct' };
+ }
+ return { status: 'materialize' };
+};
+
+export const buildMaterializedControlLayer = (
+ layer: CanvasControlLayerContract,
+ rect: Rect
+): CanvasControlLayerContract => ({
+ ...structuredClone(layer),
+ source: { bitmap: null, offset: { x: rect.x, y: rect.y }, type: 'paint' },
+ transform: { ...IDENTITY_TRANSFORM },
+});
+
+export interface BakeControlPixelEditSurfaceInput {
+ backend: RasterBackend;
+ source: RasterSurface;
+ sourceRect: Rect;
+ transform: LayerTransform;
+}
+
+export const bakeControlPixelEditSurface = ({
+ backend,
+ source,
+ sourceRect,
+ transform,
+}: BakeControlPixelEditSurfaceInput): { rect: Rect; surface: RasterSurface } => {
+ const matrix = bakeMatrix(transform);
+ const rect = roundOut(transformBounds(matrix, sourceRect));
+ const surface = backend.createSurface(rect.width, rect.height);
+ const ctx = surface.ctx;
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ ctx.clearRect(0, 0, rect.width, rect.height);
+ ctx.imageSmoothingEnabled = true;
+ ctx.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e - rect.x, matrix.f - rect.y);
+ ctx.drawImage(source.canvas, sourceRect.x, sourceRect.y);
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ return { rect, surface };
+};