All files / src/shared/lib/sprites spriteData.ts

79.25% Statements 84/106
88.57% Branches 31/35
66.67% Functions 8/12
80% Lines 68/85

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 1833x                         3x 3x 3x 3x 3x 3x 3x     3x 2176x   2176x 1253x 923x 164x   759x       3x           18x 18x 18x   18x 272x 2176x 2176x 2176x 2176x 2176x             160x 2016x   368x         18x     3x           18x 18x 18x 18x 272x 2176x 2176x 2176x 2176x         18x     3x       20x             20x 1017x         17x     3x     3x 18x 276x       16x     2x     3x       3x 3x 384x 64x     3x     3x     24x 24x 1536x 208x     24x           3x                                                 3x   3x 3x  
import {
  cloneIndexedImage,
  ImageIndexFunction,
  IndexedImage,
  toIndex,
} from "shared/lib/tiles/indexedImage";
 
export type OptimisedTile = {
  tile: number;
  flipX: boolean;
  flipY: boolean;
};
 
enum Color {
  Transparent = 0,
  Light = 1,
  Mid = 2,
  Dark = 3,
  Divider = 254,
  Unknown = 255,
}
 
export const spriteDataIndexFn: ImageIndexFunction = (r, g, b, _a) => {
  Iif ((g > 249 && r < 180 && b < 20) || (b >= 200 && g < 20) || _a < 200) {
    return Color.Transparent;
  } else if (g >= 205) {
    return Color.Light;
  } else if (g >= 130) {
    return Color.Mid;
  } else {
    return Color.Dark;
  }
};
 
export const removeIndexedImageMask = (
  inData: IndexedImage,
  maskData: IndexedImage,
  offsetX: number,
  offsetY: number,
): IndexedImage => {
  const output = cloneIndexedImage(inData);
  const inWidth = inData.width;
  const inHeight = inData.height;
 
  for (let y = 0; y < inHeight; y++) {
    for (let x = 0; x < inWidth; x++) {
      const drawIndex = toIndex(x, y, inData);
      const maskIndex = toIndex(x + offsetX, y + offsetY, maskData);
      const maskX = x + offsetX;
      const maskY = y + offsetY;
      if (
        maskX < 0 ||
        maskY < 0 ||
        maskX >= maskData.width ||
        maskY >= maskData.height
      ) {
        // Mark tile data outside of canvas bounds as unknown
        output.data[drawIndex] = Color.Unknown;
      } else if (maskData.data[maskIndex] !== Color.Transparent) {
        // Mark occluded tile data as unknown
        output.data[drawIndex] = Color.Unknown;
      }
    }
  }
 
  return output;
};
 
export const blitIndexedImageData = (
  canvasData: IndexedImage,
  inData: IndexedImage,
  offsetX: number,
  offsetY: number,
): IndexedImage => {
  const output = cloneIndexedImage(canvasData);
  const drawWidth = inData.width;
  const drawHeight = inData.height;
  for (let y = 0; y < drawHeight; y++) {
    for (let x = 0; x < drawWidth; x++) {
      const drawIndex = toIndex(x, y, inData);
      if (inData.data[drawIndex] !== Color.Transparent) {
        const canvasIndex = toIndex(x + offsetX, y + offsetY, canvasData);
        output.data[canvasIndex] = inData.data[drawIndex];
      }
    }
  }
 
  return output;
};
 
export const isIndexedImageEqual = (
  dataA: IndexedImage,
  dataB: IndexedImage,
): boolean => {
  Iif (
    dataA.width !== dataB.width ||
    dataA.height !== dataB.height ||
    dataA.data.length !== dataB.data.length
  ) {
    return false;
  }
  for (let i = 0; i < dataA.data.length; i++) {
    if (
      dataA.data[i] !== dataB.data[i] &&
      dataA.data[i] !== Color.Unknown &&
      dataB.data[i] !== Color.Unknown
    ) {
      return false;
    }
  }
  return true;
};
 
export const isBlankIndexedImage = (image: IndexedImage): boolean => {
  for (let i = 0; i < image.data.length; i++) {
    if (
      image.data[i] !== Color.Transparent &&
      image.data[i] !== Color.Unknown
    ) {
      return false;
    }
  }
  return true;
};
 
export const mergeIndexedImages = (
  dataA: IndexedImage,
  dataB: IndexedImage,
): IndexedImage => {
  const output = cloneIndexedImage(dataA);
  for (let i = 0; i < output.data.length; i++) {
    if (output.data[i] === Color.Unknown) {
      output.data[i] = dataB.data[i];
    }
  }
  return output;
};
 
export const indexedUnknownToTransparent = (
  inData: IndexedImage,
): IndexedImage => {
  const output = cloneIndexedImage(inData);
  for (let i = 0; i < output.data.length; i++) {
    if (output.data[i] === Color.Unknown) {
      output.data[i] = Color.Transparent;
    }
  }
  return output;
};
 
// ------------
// To 2bpp
 
export const indexedImageTo2bppSpriteData = (
  image: IndexedImage,
): Uint8Array => {
  const output = new Uint8Array(roundUp8((image.width * image.height) / 4));
  let i = 0;
  for (let y = 0; y < 8; y++) {
    let row1 = "";
    let row2 = "";
    for (let x = 0; x < 8; x++) {
      const index = toIndex(x, y, image);
      const binary = bin2(image.data[index]);
      row1 += binary[1];
      row2 += binary[0];
    }
    output[i] = binDec(row1);
    i++;
    output[i] = binDec(row2);
    i++;
  }
 
  return output;
};
 
// ------------
 
const roundUp8 = (x: number): number => Math.ceil(x / 8) * 8;
 
const bin2 = (value: Color) => value.toString(2).padStart(2, "0");
const binDec = (binary: string) => parseInt(binary, 2);