Marching-squares contour extraction from a binary raster mask.
mask is a row-major Uint8Array-style buffer of length
width * height, where each cell is 0 (outside) or 1
(inside). Output is one closed loop per connected 1-region,
walked with the 1-region on the LEFT (so under image-y-down
coordinates outer rings have positive signed area and holes
have negative).
Cells extend one position beyond the mask on every side, so
1 pixels at the buffer edge close their contour against the
implicit zero-padded border instead of leaking open.
Saddles (cases 5 and 10) use the disconnected convention —
each diagonal 1 produces its own contour piece. Avoids
spurious merges across sub-pixel saddle points.
Marching-squares contour extraction from a binary raster mask.
maskis a row-majorUint8Array-style buffer of lengthwidth * height, where each cell is0(outside) or1(inside). Output is one closed loop per connected1-region, walked with the 1-region on the LEFT (so under image-y-down coordinates outer rings have positive signed area and holes have negative).Cells extend one position beyond the mask on every side, so
1pixels at the buffer edge close their contour against the implicit zero-padded border instead of leaking open.Saddles (cases 5 and 10) use the disconnected convention — each diagonal
1produces its own contour piece. Avoids spurious merges across sub-pixel saddle points.