|
| 1 | +import * as cm from "charmingjs"; |
| 2 | +import * as d3 from "d3"; |
| 3 | +import ClipperLib from "clipper-lib"; |
| 4 | + |
| 5 | +export const CLIPPER_SCALE = 256; |
| 6 | +export const SCREEN_PATH_PADDING = 20; |
| 7 | +export const SCREEN_PATH_PADDING_TOP = 30; |
| 8 | +export const MASK_CONTOUR_MAX_DIM = 140; |
| 9 | +export const BODYPIX_NUM_PARTS = 24; |
| 10 | +export const VIDEO_MAX_LONG_EDGE = 480; |
| 11 | + |
| 12 | +export function videoBufferDimensions(layerW, layerH) { |
| 13 | + const lw = Math.max(2, Math.floor(layerW)); |
| 14 | + const lh = Math.max(2, Math.floor(layerH)); |
| 15 | + const ar = lw / lh; |
| 16 | + let vw; |
| 17 | + let vh; |
| 18 | + if (lw >= lh) { |
| 19 | + vw = Math.min(VIDEO_MAX_LONG_EDGE, lw); |
| 20 | + vh = Math.max(2, Math.round(vw / ar)); |
| 21 | + } else { |
| 22 | + vh = Math.min(VIDEO_MAX_LONG_EDGE, lh); |
| 23 | + vw = Math.max(2, Math.round(vh * ar)); |
| 24 | + } |
| 25 | + return {vw, vh}; |
| 26 | +} |
| 27 | + |
| 28 | +export function rectToRing(x, y, w, h) { |
| 29 | + return [ |
| 30 | + [x, y], |
| 31 | + [x + w, y], |
| 32 | + [x + w, y + h], |
| 33 | + [x, y + h], |
| 34 | + ]; |
| 35 | +} |
| 36 | + |
| 37 | +export function unionRingsToHolePathStrings(rings) { |
| 38 | + const valid = rings.filter((ring) => ring.length >= 3); |
| 39 | + if (valid.length === 0) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + const s = CLIPPER_SCALE; |
| 43 | + const paths = valid.map((ring) => |
| 44 | + ring.map(([x, y]) => ({ |
| 45 | + X: Math.round(x * s), |
| 46 | + Y: Math.round(y * s), |
| 47 | + })), |
| 48 | + ); |
| 49 | + const c = new ClipperLib.Clipper(); |
| 50 | + c.AddPaths(paths, ClipperLib.PolyType.ptSubject, true); |
| 51 | + const solution = new ClipperLib.Paths(); |
| 52 | + const ok = c.Execute( |
| 53 | + ClipperLib.ClipType.ctUnion, |
| 54 | + solution, |
| 55 | + ClipperLib.PolyFillType.pftNonZero, |
| 56 | + ClipperLib.PolyFillType.pftNonZero, |
| 57 | + ); |
| 58 | + const inv = 1 / s; |
| 59 | + if (!ok || !solution.length) { |
| 60 | + return [cm.pathPolygon(valid[0])]; |
| 61 | + } |
| 62 | + const out = solution |
| 63 | + .filter((path) => path.length >= 3) |
| 64 | + .map((path) => cm.pathPolygon(path.map(({X, Y}) => [X * inv, Y * inv]))); |
| 65 | + return out.length ? out : [cm.pathPolygon(valid[0])]; |
| 66 | +} |
| 67 | + |
| 68 | +export function getScreenInsetBounds(width, height) { |
| 69 | + const maxPad = Math.max(0, (Math.min(width, height) - 8) / 2); |
| 70 | + const p = Math.min(SCREEN_PATH_PADDING, maxPad); |
| 71 | + const pt = Math.min(SCREEN_PATH_PADDING_TOP, maxPad); |
| 72 | + const innerW = Math.max(1, width - 2 * p); |
| 73 | + const innerH = Math.max(1, height - pt - p); |
| 74 | + return { |
| 75 | + p, |
| 76 | + pt, |
| 77 | + innerW, |
| 78 | + innerH, |
| 79 | + left: p, |
| 80 | + top: pt, |
| 81 | + right: p + innerW, |
| 82 | + bottom: pt + innerH, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +function clipPolygonRingToInset(ring, left, top, right, bottom) { |
| 87 | + const s = CLIPPER_SCALE; |
| 88 | + if (ring.length < 3) { |
| 89 | + return []; |
| 90 | + } |
| 91 | + const subj = [ |
| 92 | + ring.map(([x, y]) => ({ |
| 93 | + X: Math.round(x * s), |
| 94 | + Y: Math.round(y * s), |
| 95 | + })), |
| 96 | + ]; |
| 97 | + const clip = [ |
| 98 | + [ |
| 99 | + {X: Math.round(left * s), Y: Math.round(top * s)}, |
| 100 | + {X: Math.round(right * s), Y: Math.round(top * s)}, |
| 101 | + {X: Math.round(right * s), Y: Math.round(bottom * s)}, |
| 102 | + {X: Math.round(left * s), Y: Math.round(bottom * s)}, |
| 103 | + ], |
| 104 | + ]; |
| 105 | + const c = new ClipperLib.Clipper(); |
| 106 | + c.AddPaths(subj, ClipperLib.PolyType.ptSubject, true); |
| 107 | + c.AddPaths(clip, ClipperLib.PolyType.ptClip, true); |
| 108 | + const solution = new ClipperLib.Paths(); |
| 109 | + const ok = c.Execute( |
| 110 | + ClipperLib.ClipType.ctIntersection, |
| 111 | + solution, |
| 112 | + ClipperLib.PolyFillType.pftNonZero, |
| 113 | + ClipperLib.PolyFillType.pftNonZero, |
| 114 | + ); |
| 115 | + if (!ok || !solution.length) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + const inv = 1 / s; |
| 119 | + const out = []; |
| 120 | + for (const path of solution) { |
| 121 | + if (path.length < 3) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + out.push(path.map(({X, Y}) => [X * inv, Y * inv])); |
| 125 | + } |
| 126 | + return out; |
| 127 | +} |
| 128 | + |
| 129 | +function samplePartIdGrid(imageData, mw, mh) { |
| 130 | + const iw = imageData.width; |
| 131 | + const ih = imageData.height; |
| 132 | + const ids = new Int32Array(mw * mh); |
| 133 | + const alpha = new Uint8Array(mw * mh); |
| 134 | + for (let j = 0; j < mh; j++) { |
| 135 | + for (let i = 0; i < mw; i++) { |
| 136 | + const sx = Math.min(iw - 1, Math.floor((i + 0.5) * (iw / mw))); |
| 137 | + const sy = Math.min(ih - 1, Math.floor((j + 0.5) * (ih / mh))); |
| 138 | + const o = (sy * iw + sx) * 4; |
| 139 | + const ix = j * mw + i; |
| 140 | + ids[ix] = imageData.data[o]; |
| 141 | + alpha[ix] = imageData.data[o + 3]; |
| 142 | + } |
| 143 | + } |
| 144 | + return {ids, alpha}; |
| 145 | +} |
| 146 | + |
| 147 | +function contourBinaryMaskToRings(values, mw, mh, layerW, layerH, minAreaGrid, inset) { |
| 148 | + let sum = 0; |
| 149 | + for (let i = 0; i < values.length; i++) { |
| 150 | + sum += values[i]; |
| 151 | + } |
| 152 | + if (sum < minAreaGrid) { |
| 153 | + return []; |
| 154 | + } |
| 155 | + |
| 156 | + const contourGen = d3.contours().size([mw, mh]).smooth(true).thresholds([0.5]); |
| 157 | + const layers = contourGen(values); |
| 158 | + if (!layers.length) { |
| 159 | + return []; |
| 160 | + } |
| 161 | + |
| 162 | + const multi = layers[0]; |
| 163 | + if (!multi.coordinates?.length) { |
| 164 | + return []; |
| 165 | + } |
| 166 | + |
| 167 | + const sx = layerW / mw; |
| 168 | + const sy = layerH / mh; |
| 169 | + const maxHoleArea = layerW * layerH * 0.45; |
| 170 | + const out = []; |
| 171 | + |
| 172 | + for (const poly of multi.coordinates) { |
| 173 | + if (!poly?.length) { |
| 174 | + continue; |
| 175 | + } |
| 176 | + const outer = poly[0]; |
| 177 | + const aGrid = Math.abs(d3.polygonArea(outer)); |
| 178 | + if (aGrid < minAreaGrid || outer.length < 4) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + const aLayer = aGrid * sx * sy; |
| 182 | + if (aLayer > maxHoleArea) { |
| 183 | + continue; |
| 184 | + } |
| 185 | + const scaled = outer.map(([x, y]) => [x * sx, y * sy]); |
| 186 | + const clipped = clipPolygonRingToInset(scaled, inset.left, inset.top, inset.right, inset.bottom); |
| 187 | + for (const ring of clipped) { |
| 188 | + out.push(ring); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return out; |
| 193 | +} |
| 194 | + |
| 195 | +export function maskImageDataToPartHoleRings(imageData, layerW, layerH) { |
| 196 | + if (!imageData?.data || imageData.width < 2 || imageData.height < 2) { |
| 197 | + return []; |
| 198 | + } |
| 199 | + |
| 200 | + const inset = getScreenInsetBounds(layerW, layerH); |
| 201 | + const iw = imageData.width; |
| 202 | + const ih = imageData.height; |
| 203 | + const scale = Math.min(MASK_CONTOUR_MAX_DIM / iw, MASK_CONTOUR_MAX_DIM / ih, 1); |
| 204 | + const mw = Math.max(8, Math.floor(iw * scale)); |
| 205 | + const mh = Math.max(8, Math.floor(ih * scale)); |
| 206 | + |
| 207 | + const {ids, alpha} = samplePartIdGrid(imageData, mw, mh); |
| 208 | + const seen = new Set(); |
| 209 | + for (let i = 0; i < ids.length; i++) { |
| 210 | + if (alpha[i] <= 40) { |
| 211 | + continue; |
| 212 | + } |
| 213 | + const pid = ids[i]; |
| 214 | + if (pid >= 0 && pid < BODYPIX_NUM_PARTS) { |
| 215 | + seen.add(pid); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + const sortedParts = Array.from(seen).sort((a, b) => a - b); |
| 220 | + const values = new Float64Array(mw * mh); |
| 221 | + const minAreaGrid = 6; |
| 222 | + const holeRings = []; |
| 223 | + |
| 224 | + for (const pid of sortedParts) { |
| 225 | + for (let i = 0; i < values.length; i++) { |
| 226 | + values[i] = ids[i] === pid && alpha[i] > 40 ? 1 : 0; |
| 227 | + } |
| 228 | + holeRings.push(...contourBinaryMaskToRings(values, mw, mh, layerW, layerH, minAreaGrid, inset)); |
| 229 | + } |
| 230 | + |
| 231 | + return holeRings; |
| 232 | +} |
| 233 | + |
| 234 | +export function buildTextPath(width, height, holes) { |
| 235 | + const {p, pt, innerW, innerH} = getScreenInsetBounds(width, height); |
| 236 | + const outer = cm.pathRect(p, pt, innerW, innerH); |
| 237 | + const list = Array.isArray(holes) ? holes.filter(Boolean) : holes ? [holes] : []; |
| 238 | + if (list.length === 0) { |
| 239 | + return outer; |
| 240 | + } |
| 241 | + return `${outer} ${list.join(" ")}`; |
| 242 | +} |
0 commit comments