All files / src/renderer/lib/sprites detectSprite.worker.ts

0% Statements 0/17
100% Branches 0/0
0% Functions 0/3
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
import {
  indexedImageToSprites,
  spriteAlignmentOffsets,
  autoHint2,
  spritesToTiles2,
  clusterSprites,
  spriteDataWithDividerIndexFn,
  SliceDef,
  SpriteTileLocation,
  Position,
  SpriteCluster,
} from "shared/lib/sprites/spriteData";
import { imageToIndexedImage } from "renderer/lib/tiles/readCanvasToTiles";
 
declare const self: Worker;
const workerCtx: Worker = self;
 
export interface DetectSpriteResult {
  tileDefs: SliceDef[];
  spriteTileLocations: SpriteTileLocation[][];
  spriteDefs: SliceDef[];
  alignmentOffsets: Position[];
  spriteClusters: SpriteCluster[];
}
 
workerCtx.onmessage = async (evt) => {
  const src = evt.data as string;
  const imgblob = await fetch(src).then((r) => r.blob());
  const img = await createImageBitmap(imgblob);
  const indexedImage = imageToIndexedImage(img, spriteDataWithDividerIndexFn);
  const spriteDefs = indexedImageToSprites(indexedImage);
  const alignmentOffsets = spriteAlignmentOffsets(spriteDefs);
  const hintTileDefs = autoHint2(indexedImage);
  const { tileDefs, spriteTileLocations } = spritesToTiles2(
    spriteDefs,
    hintTileDefs
  );
  const spriteClusters = clusterSprites(spriteDefs);
 
  workerCtx.postMessage({
    tileDefs,
    spriteTileLocations,
    spriteDefs,
    alignmentOffsets,
    spriteClusters,
  });
};
 
// -----------------------------------------------------------------
 
export default class W extends Worker {
  constructor() {
    super("");
  }
}