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.

8.2 KB
Raw Blame
import type {
  CanvasAdjustmentEntry,
  CanvasDocumentContractV3,
  CanvasRasterLayerContractV2,
} from '@workbench/canvas-engine/contracts';
import type { RasterSurface } from '@workbench/canvas-engine/render/raster';
import type { StubRasterSurface } from '@workbench/canvas-engine/render/raster.testStub';

import { stacksFrom } from '@workbench/canvas-engine/document-model/documentFixtures.testStub';
import { applyAdjustments } from '@workbench/canvas-engine/render/adjustments';
import { createLayerCacheStore } from '@workbench/canvas-engine/render/layerCache';
import { createTestStubRasterBackend } from '@workbench/canvas-engine/render/raster.testStub';
import { describe, expect, it, vi } from 'vitest';

import { ThumbnailController } from './thumbnailController';

describe('ThumbnailController budget', () => {
  it('returns over-budget before rasterizing a missing thumbnail cache', async () => {
    const layer = {
      blendMode: 'normal' as const,
      id: 'layer',
      isEnabled: true,
      isLocked: false,
      name: 'Layer',
      opacity: 1,
      source: { image: { height: 100, imageName: 'layer.png', width: 100 }, type: 'image' as const },
      transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
      type: 'raster' as const,
    };
    const document: CanvasDocumentContractV3 = {
      background: 'transparent',
      bbox: { height: 100, width: 100, x: 0, y: 0 },
      height: 100,
      stacks: stacksFrom([layer]),
      selectedLayerId: null,
      version: 3,
      width: 100,
    };
    const rasterize = vi.fn();
    const controller = new ThumbnailController({
      backend: createTestStubRasterBackend(),
      getActiveProjectId: () => 'p',
      getCheckerboard: vi.fn(),
      getDocument: () => document,
      getEntry: () => undefined,
      getMaskPattern: () => null,
      isDisposed: () => false,
      isSupportedSource: () => true,
      peekDisplayedSurface: () => null,
      projectId: 'p',
      rasterize,
      reportError: vi.fn(),
      reserve: () => ({ availableBytes: 0, requestedBytes: 80_000, status: 'over-budget' }),
      setStatus: vi.fn(),
    });

    await expect(controller.request('layer')).resolves.toBe('over-budget');
    expect(rasterize).not.toHaveBeenCalled();
  });
});

describe('ThumbnailController adjusted rasters', () => {
  const grain: CanvasAdjustmentEntry = {
    amount: 0.4,
    distribution: 'gaussian',
    id: 'n',
    isEnabled: true,
    grain: 'color',
    response: 'flat',
    size: 1,
    seed: 3,
    type: 'noise',
  };
  const invert: CanvasAdjustmentEntry = { id: 'i', isEnabled: true, type: 'invert' };

  const drawThumbnail = (
    peek: (surface: RasterSurface) => RasterSurface | null,
    noise: CanvasAdjustmentEntry = grain
  ) => {
    const backend = createTestStubRasterBackend();
    const created: StubRasterSurface[] = [];
    const layers = createLayerCacheStore(backend);
    const entry = layers.getOrCreate('layer', 200, 100);
    entry.hasPublishedPixels = true;
    const layer: CanvasRasterLayerContractV2 = {
      adjustments: [invert, noise],
      blendMode: 'normal',
      id: 'layer',
      isEnabled: true,
      isLocked: false,
      name: 'Layer',
      opacity: 1,
      source: { image: { height: 100, imageName: 'layer.png', width: 200 }, type: 'image' },
      transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
      type: 'raster',
    };
    const document: CanvasDocumentContractV3 = {
      background: 'transparent',
      bbox: { height: 100, width: 200, x: 0, y: 0 },
      height: 100,
      stacks: stacksFrom([layer]),
      selectedLayerId: null,
      version: 3,
      width: 200,
    };
    const controller = new ThumbnailController({
      backend: {
        ...backend,
        createSurface: (width, height) => {
          const surface = backend.createSurface(width, height);
          created.push(surface);
          return surface;
        },
      },
      getActiveProjectId: () => 'p',
      getCheckerboard: () => backend.createSurface(8, 8),
      getDocument: () => document,
      getEntry: () => entry,
      getMaskPattern: () => null,
      isDisposed: () => false,
      isSupportedSource: () => true,
      peekDisplayedSurface: (_layer, peeked) => peek(peeked.surface),
      projectId: 'p',
      rasterize: vi.fn(),
      reportError: vi.fn(),
      setStatus: vi.fn(),
    });
    const ctx = { clearRect: vi.fn(), createPattern: () => null, drawImage: vi.fn(), globalAlpha: 1 };
    const target = { getContext: () => ctx, height: 0, width: 0 } as unknown as HTMLCanvasElement;
    expect(controller.draw('layer', target, 50)).toBe(true);
    const thumbnail = created.find((surface) => surface.width === 50 && surface.height === 25)!;
    return { cache: entry.surface, thumbnail };
  };

  it("downscales the display's current adjusted copy without applying the stack again", () => {
    const displayed = createTestStubRasterBackend().createSurface(200, 100);
    const { thumbnail } = drawThumbnail(() => displayed);

    const drawn = thumbnail.callLog.find((call) => call.op === 'drawImage');
    expect(drawn?.args).toEqual([displayed.canvas, 0, 0, 50, 25]);
    expect(thumbnail.callLog.some((call) => call.op === 'getImageData' || call.op === 'putImageData')).toBe(false);
  });

  it('without a display copy adjusts the thumbnail itself, scaling noise σ by the thumbnail scale', () => {
    const { cache, thumbnail } = drawThumbnail(() => null);

    expect(thumbnail.callLog.find((call) => call.op === 'drawImage')?.args[0]).toBe(cache.canvas);
    const written = thumbnail.callLog.find((call) => call.op === 'putImageData')!.args[0] as ImageData;
    // The stub reads back opaque black; a quarter-size thumbnail carries a quarter of the grain's σ.
    const expected = { data: new Uint8ClampedArray(50 * 25 * 4), height: 25, width: 50 } as ImageData;
    for (let i = 3; i < expected.data.length; i += 4) {
      expected.data[i] = 255;
    }
    applyAdjustments(expected, [invert, { ...grain, amount: 0.1 } as CanvasAdjustmentEntry], { x: 0, y: 0 });
    expect([...written.data]).toEqual([...expected.data]);
  });

  it("weakens grain narrower than the downscale blocks by the scale times the grain's span", () => {
    // The grain kernel by its definition: a Gaussian of σ √(size² − 1) / 2 truncated at 3σ. Box-averaging B×B pixels
    // leaves σ × (Σ taps)² / (Σ taps²) / B: about 3.07 / 4 for size 2 at a quarter scale.
    const size = 2;
    const sigma = Math.sqrt(size * size - 1) / 2;
    const radius = Math.ceil(3 * sigma);
    const taps = Array.from({ length: 2 * radius + 1 }, (_, i) => Math.exp(-((i - radius) ** 2) / (2 * sigma * sigma)));
    const span = taps.reduce((sum, tap) => sum + tap, 0) ** 2 / taps.reduce((sum, tap) => sum + tap * tap, 0);
    const { thumbnail } = drawThumbnail(() => null, { ...grain, size } as CanvasAdjustmentEntry);

    const written = thumbnail.callLog.find((call) => call.op === 'putImageData')!.args[0] as ImageData;
    const expected = { data: new Uint8ClampedArray(50 * 25 * 4), height: 25, width: 50 } as ImageData;
    for (let i = 3; i < expected.data.length; i += 4) {
      expected.data[i] = 255;
    }
    const scaled = { ...grain, amount: 0.4 * (span / 4), size: 1 } as CanvasAdjustmentEntry;
    applyAdjustments(expected, [invert, scaled], { x: 0, y: 0 });
    expect(span / 4).toBeLessThan(1);
    expect([...written.data]).toEqual([...expected.data]);
  });

  it('keeps the σ of grain wider than the downscale blocks and shrinks the grain to thumbnail pixels', () => {
    // Size 4 grain (blur σ √15 / 2 ≈ 1.9) spans about seven source pixels, wider than the 4×4 blocks a quarter-size
    // thumbnail averages, so averaging barely weakens it; on the thumbnail grid it is one pixel across.
    const coarse = { ...grain, size: 4 } as CanvasAdjustmentEntry;
    const { thumbnail } = drawThumbnail(() => null, coarse);

    const written = thumbnail.callLog.find((call) => call.op === 'putImageData')!.args[0] as ImageData;
    const expected = { data: new Uint8ClampedArray(50 * 25 * 4), height: 25, width: 50 } as ImageData;
    for (let i = 3; i < expected.data.length; i += 4) {
      expected.data[i] = 255;
    }
    applyAdjustments(expected, [invert, { ...grain, size: 1 } as CanvasAdjustmentEntry], { x: 0, y: 0 });
    expect([...written.data]).toEqual([...expected.data]);
  });
});