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.

6.0 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { RasterBackend, RasterSurface } from './raster';

import { applyLightnessToAlpha, renderControlTransparency } from './controlTransparency';

const pixels = (...rgba: [number, number, number, number][]): Uint8ClampedArray => new Uint8ClampedArray(rgba.flat());

describe('applyLightnessToAlpha', () => {
  it('sets alpha to the HSL lightness of each pixel, clamped to the existing alpha', () => {
    const data = pixels(
      [255, 255, 255, 255], // white: lightness 255 -> alpha stays 255
      [0, 0, 0, 255], // black: lightness 0 -> alpha becomes 0
      [128, 128, 128, 255], // mid-gray: lightness 128 -> alpha 128
      [100, 50, 200, 255], // colored: min 50, max 200 -> lightness 125 -> alpha 125
      [255, 255, 255, 100] // opaque-white but a=100: lightness 255, min(100, 255) keeps 100
    );

    applyLightnessToAlpha(data);

    expect(data[3]).toBe(255);
    expect(data[7]).toBe(0);
    expect(data[11]).toBe(128);
    expect(data[15]).toBe(125);
    expect(data[19]).toBe(100);
  });

  it('leaves the RGB channels untouched', () => {
    const data = pixels(
      [255, 255, 255, 255],
      [0, 0, 0, 255],
      [128, 128, 128, 255],
      [100, 50, 200, 255],
      [255, 255, 255, 100]
    );

    applyLightnessToAlpha(data);

    expect([data[0], data[1], data[2]]).toEqual([255, 255, 255]);
    expect([data[4], data[5], data[6]]).toEqual([0, 0, 0]);
    expect([data[8], data[9], data[10]]).toEqual([128, 128, 128]);
    expect([data[12], data[13], data[14]]).toEqual([100, 50, 200]);
    expect([data[16], data[17], data[18]]).toEqual([255, 255, 255]);
  });

  it('mutates the buffer in place and returns void', () => {
    const data = pixels([0, 0, 0, 255]);
    const same = data;

    const result = applyLightnessToAlpha(data);

    expect(result).toBeUndefined();
    expect(data).toBe(same);
    expect(same[3]).toBe(0);
  });

  it('handles an empty buffer without throwing', () => {
    const data = new Uint8ClampedArray(0);
    expect(() => applyLightnessToAlpha(data)).not.toThrow();
    expect(data).toHaveLength(0);
  });
});

interface FakeSurface extends RasterSurface {
  readonly drawImageArgs: unknown[][];
  readonly getImageDataArgs: unknown[][];
  readonly putImageDataCalls: ImageData[];
}

const createFakeBackend = (sourcePixels: Uint8ClampedArray): RasterBackend & { readonly created: FakeSurface[] } => {
  const created: FakeSurface[] = [];

  const createSurface = (width: number, height: number): FakeSurface => {
    const drawImageArgs: unknown[][] = [];
    const getImageDataArgs: unknown[][] = [];
    const putImageDataCalls: ImageData[] = [];

    const ctx = {
      clearRect: () => {},
      drawImage: (...args: unknown[]) => {
        drawImageArgs.push(args);
      },
      getImageData: (...args: unknown[]): ImageData => {
        getImageDataArgs.push(args);
        const data = new Uint8ClampedArray(sourcePixels);
        return { colorSpace: 'srgb', data, height, width } as unknown as ImageData;
      },
      globalAlpha: 1,
      globalCompositeOperation: 'source-over' as GlobalCompositeOperation,
      putImageData: (imageData: ImageData) => {
        putImageDataCalls.push(imageData);
      },
      setTransform: () => {},
    } as unknown as OffscreenCanvasRenderingContext2D;

    const surface: FakeSurface = {
      canvas: { height, width } as unknown as OffscreenCanvas,
      ctx,
      drawImageArgs,
      getImageDataArgs,
      height,
      putImageDataCalls,
      resize: () => {},
      resizePreserving: () => {},
      width,
    };
    created.push(surface);
    return surface;
  };

  return {
    createImageBitmap: () => Promise.resolve({ close: () => {}, height: 0, width: 0 } as unknown as ImageBitmap),
    createSurface,
    created,
    encodeSurface: (surface: RasterSurface, type = 'image/png') =>
      Promise.resolve(new Blob([`fake-${surface.width}x${surface.height}`], { type })),
  };
};

describe('renderControlTransparency', () => {
  it('allocates the output surface at the requested dimensions', () => {
    const backend = createFakeBackend(pixels([0, 0, 0, 255]));
    const cache = backend.createSurface(64, 48);

    const out = renderControlTransparency(backend, cache, 64, 48);

    expect(out.width).toBe(64);
    expect(out.height).toBe(48);
    expect(out).not.toBe(cache);
  });

  it('draws the cache canvas into the output surface', () => {
    const backend = createFakeBackend(pixels([0, 0, 0, 255]));
    const cache = backend.createSurface(10, 10);

    const out = renderControlTransparency(backend, cache, 10, 10) as FakeSurface;

    expect(out.drawImageArgs).toHaveLength(1);
    expect(out.drawImageArgs[0]![0]).toBe(cache.canvas);
    expect(out.drawImageArgs[0]!.slice(1)).toEqual([0, 0]);
  });

  it('applies the lightness->alpha transform: opaque black pixels become fully transparent', () => {
    const backend = createFakeBackend(pixels([0, 0, 0, 255], [0, 0, 0, 255]));
    const cache = backend.createSurface(2, 1);

    const out = renderControlTransparency(backend, cache, 2, 1) as FakeSurface;

    expect(out.getImageDataArgs).toHaveLength(1);
    expect(out.getImageDataArgs[0]).toEqual([0, 0, 2, 1]);
    expect(out.putImageDataCalls).toHaveLength(1);

    const result = out.putImageDataCalls[0]!.data;
    expect(result[3]).toBe(0);
    expect(result[7]).toBe(0);
    expect([result[0], result[1], result[2]]).toEqual([0, 0, 0]);
  });

  it('applies the lightness->alpha transform to a mixed bitmap', () => {
    const backend = createFakeBackend(
      pixels(
        [255, 255, 255, 255],
        [0, 0, 0, 255],
        [100, 50, 200, 255] // colored -> lightness 125 -> alpha 125
      )
    );
    const cache = backend.createSurface(3, 1);

    const out = renderControlTransparency(backend, cache, 3, 1) as FakeSurface;

    const result = out.putImageDataCalls[0]!.data;
    expect(result[3]).toBe(255);
    expect(result[7]).toBe(0);
    expect(result[11]).toBe(125);
    expect([result[8], result[9], result[10]]).toEqual([100, 50, 200]);
  });
});