function prepareMask(mask) {
    let x;
    let y;
    const w = mask.width;
    const data = mask.data;
    const minX = mask.bounds.minX;
    const maxX = mask.bounds.maxX;
    const minY = mask.bounds.minY;
    const maxY = mask.bounds.maxY;
    const rw = maxX - minX + 3; // bounds size +1 px on each side (a "white" border)
    const rh = maxY - minY + 3;
    const result = new Uint8Array(rw * rh); // reduced mask (bounds size)

    // walk through inner values and copy only "black" points to the result mask
    for (y = minY; y < maxY + 1; y++)
      for (x = minX; x < maxX + 1; x++) {
        if (data[y * w + x] === 1) result[(y - minY + 1) * rw + (x - minX + 1)] = 1;
      }

    return {
      data: result,
      width: rw,
      height: rh,
      offset: { x: minX - 1, y: minY - 1 },
    };
  }
Create a compressed mask with a "white" border (1px border with zero values) for the contour tracing
@param {Object} mask: {Uint8Array} data, {int} width, {int} height, {Object} bounds
@return {Object} border mask: {Uint8Array} data, {int} width, {int} height, {Object} offset