// Pure helpers (no DOM, no model) so they can be unit-tested in Node. // Coordinates are always in ORIGINAL image pixels unless stated otherwise. export const LIMITS = { maxImageBytes: 15 * 1024 * 1024, maxImageMegapixels: 24, maxWorkSide: 1600, // images larger than this are downscaled before inference (SAM resizes to 1008 anyway) maxVideoBytes: 60 * 1024 * 1024, maxVideoSeconds: 20, maxVideoFrames: 60, maxPoints: 12, videoWorkSide: 960, }; export const IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/webp']; export const VIDEO_TYPES = ['video/mp4', 'video/webm', 'video/quicktime', 'video/ogg']; /** Validate an uploaded file. Returns {ok, error?} (error in Spanish). */ export function validateFile({ type, size }, kind) { const types = kind === 'video' ? VIDEO_TYPES : IMAGE_TYPES; const max = kind === 'video' ? LIMITS.maxVideoBytes : LIMITS.maxImageBytes; if (!types.includes(type)) return { ok: false, error: `Formato no admitido (${type || 'desconocido'}).` }; if (!(size > 0)) return { ok: false, error: 'El archivo está vacío.' }; if (size > max) return { ok: false, error: `El archivo supera ${Math.round(max / 1048576)} MB.` }; return { ok: true }; } /** Fit (w,h) inside maxSide keeping aspect ratio; never upscales. */ export function fitSize(w, h, maxSide) { if (!(w > 0 && h > 0)) throw new Error('Dimensiones inválidas'); const s = Math.min(1, maxSide / Math.max(w, h)); return { width: Math.max(1, Math.round(w * s)), height: Math.max(1, Math.round(h * s)), scale: s }; } export function checkMegapixels(w, h) { return w * h <= LIMITS.maxImageMegapixels * 1e6; } /** Map a pointer position on a displayed element to image pixel coords (clamped). */ export function displayToImage(px, py, rect, imgW, imgH) { const x = ((px - rect.left) / rect.width) * imgW; const y = ((py - rect.top) / rect.height) * imgH; return [clamp(x, 0, imgW - 1), clamp(y, 0, imgH - 1)]; } export function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); } /** Normalise a box drawn in any direction to [x0,y0,x1,y1] clamped to the image. Returns null if too small. */ export function normBox(a, b, imgW, imgH, minSide = 4) { const x0 = clamp(Math.min(a[0], b[0]), 0, imgW - 1), x1 = clamp(Math.max(a[0], b[0]), 0, imgW - 1); const y0 = clamp(Math.min(a[1], b[1]), 0, imgH - 1), y1 = clamp(Math.max(a[1], b[1]), 0, imgH - 1); if (x1 - x0 < minSide || y1 - y0 < minSide) return null; return [x0, y0, x1, y1]; } /** * Build SAM processor prompt arguments for ONE object. * points: [{x,y,label:1|0}], box: [x0,y0,x1,y1] | null * Shapes follow transformers.js Sam2/Sam3 processor: points [[[[x,y],...]]], labels [[[l,...]]], boxes [[[x0,y0,x1,y1]]]. */ export function buildPrompt(points, box) { const pts = (points || []).slice(0, LIMITS.maxPoints); if (!pts.length && !box) throw new Error('Añade al menos un punto o una caja.'); const out = {}; if (pts.length) { out.input_points = [[pts.map((p) => [round2(p.x), round2(p.y)])]]; out.input_labels = [[pts.map((p) => (p.label === 0 ? 0 : 1))]]; } if (box) out.input_boxes = [[box.map(round2)]]; return out; } const round2 = (v) => Math.round(v * 100) / 100; /** Index of the best candidate mask given iou_scores data (Float32Array/array). */ export function bestMaskIndex(scores) { let best = 0; for (let i = 1; i < scores.length; i++) if (scores[i] > scores[best]) best = i; return best; } /** Extract mask k from a bool/uint8 tensor data laid out [..., K, H, W]. Returns Uint8Array(H*W) of 0/1. */ export function sliceMask(data, k, H, W) { const n = H * W, out = new Uint8Array(n), off = k * n; for (let i = 0; i < n; i++) out[i] = data[off + i] ? 1 : 0; return out; } export function maskArea(mask) { let a = 0; for (let i = 0; i < mask.length; i++) a += mask[i]; return a; } /** Tight bbox [x0,y0,x1,y1] (inclusive pixel coords) of a mask, or null if empty. */ export function maskBBox(mask, W, H) { let x0 = W, y0 = H, x1 = -1, y1 = -1; for (let y = 0; y < H; y++) { const row = y * W; for (let x = 0; x < W; x++) if (mask[row + x]) { if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y; } } return x1 < 0 ? null : [x0, y0, x1, y1]; } export function maskCentroid(mask, W, H) { let sx = 0, sy = 0, n = 0; for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) if (mask[y * W + x]) { sx += x; sy += y; n++; } return n ? [sx / n, sy / n] : null; } /** A point guaranteed to lie on the mask: centroid if inside, else nearest mask pixel (sampled search). */ export function interiorPoint(mask, W, H) { const c = maskCentroid(mask, W, H); if (!c) return null; const cx = Math.round(c[0]), cy = Math.round(c[1]); if (mask[cy * W + cx]) return [cx, cy]; // Prefer the pixel with the largest distance to the mask border among a coarse grid: approximated by // choosing the mask pixel nearest to the centroid (cheap and deterministic). let best = null, bd = Infinity; for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) if (mask[y * W + x]) { const d = (x - cx) ** 2 + (y - cy) ** 2; if (d < bd) { bd = d; best = [x, y]; } } return best; } export function boxIoU(a, b) { const ix = Math.max(0, Math.min(a[2], b[2]) - Math.max(a[0], b[0])); const iy = Math.max(0, Math.min(a[3], b[3]) - Math.max(a[1], b[1])); const inter = ix * iy; const ua = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter; return ua > 0 ? inter / ua : 0; } export function maskIoU(a, b) { if (a.length !== b.length) throw new Error('Tamaños de máscara distintos'); let inter = 0, uni = 0; for (let i = 0; i < a.length; i++) { const p = a[i] ? 1 : 0, q = b[i] ? 1 : 0; inter += p & q; uni += p | q; } return uni ? inter / uni : 0; } /** Fraction of mask pixels that fall inside box (inclusive). */ export function fractionInside(mask, W, H, box) { let inside = 0, n = 0; for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) if (mask[y * W + x]) { n++; if (x >= box[0] && x <= box[2] && y >= box[1] && y <= box[3]) inside++; } return n ? inside / n : 0; } export function expandBox(box, frac, W, H) { const w = box[2] - box[0], h = box[3] - box[1]; return [clamp(box[0] - w * frac, 0, W - 1), clamp(box[1] - h * frac, 0, H - 1), clamp(box[2] + w * frac, 0, W - 1), clamp(box[3] + h * frac, 0, H - 1)]; } /** Nearest-neighbour resize of a binary mask. */ export function resizeMask(mask, W, H, W2, H2) { const out = new Uint8Array(W2 * H2); for (let y = 0; y < H2; y++) { const sy = Math.min(H - 1, Math.floor(((y + 0.5) * H) / H2)); for (let x = 0; x < W2; x++) { const sx = Math.min(W - 1, Math.floor(((x + 0.5) * W) / W2)); out[y * W2 + x] = mask[sy * W + sx]; } } return out; } /** * Prompt for the next video frame from the previous frame's mask ("re-prompting" propagation, a heuristic of * this app — NOT SAM's memory-based video tracker). Box = previous bbox expanded by `margin`; point = interior point. * Masks/coords in frame pixels (all frames share the same size). */ export function nextFramePrompt(prevMask, W, H, margin = 0.1) { const bb = maskBBox(prevMask, W, H); if (!bb) return null; const p = interiorPoint(prevMask, W, H); return { points: [{ x: p[0], y: p[1], label: 1 }], box: expandBox(bb, margin, W, H) }; } /** Decide whether the object was lost on a frame. */ export function isLost({ objectScoreLogit, area, prevArea }) { if (objectScoreLogit !== undefined && objectScoreLogit < 0) return true; // model says "object absent" if (!area) return true; if (prevArea && (area > prevArea * 4 || area < prevArea / 4)) return true; // implausible jump return false; } /** Timestamps (s) to sample from a video of `duration` at `fps`, capped to maxFrames and maxSeconds. */ export function sampleTimes(duration, fps, maxFrames = LIMITS.maxVideoFrames, maxSeconds = LIMITS.maxVideoSeconds) { if (!(duration > 0) || !(fps > 0)) return []; const end = Math.min(duration, maxSeconds); const out = []; for (let i = 0; ; i++) { const t = i / fps; if (t >= end - 1e-6 || out.length >= maxFrames) break; out.push(Math.round(t * 1000) / 1000); } return out; } /** Composite a coloured, semi-transparent mask + 1px contour over RGBA pixels (returns new Uint8ClampedArray). */ export function overlayMask(rgba, mask, W, H, color = [0, 200, 255], alpha = 0.45) { const out = new Uint8ClampedArray(rgba); for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { const i = y * W + x; if (!mask[i]) continue; const edge = x === 0 || y === 0 || x === W - 1 || y === H - 1 || !mask[i - 1] || !mask[i + 1] || !mask[i - W] || !mask[i + W]; const a = edge ? 1 : alpha, j = i * 4; for (let c = 0; c < 3; c++) out[j + c] = Math.round(out[j + c] * (1 - a) + color[c] * a); } return out; } /** RGBA cut-out: original pixels where mask=1, transparent elsewhere. */ export function cutout(rgba, mask) { const out = new Uint8ClampedArray(rgba.length); for (let i = 0; i < mask.length; i++) if (mask[i]) { const j = i * 4; out[j] = rgba[j]; out[j + 1] = rgba[j + 1]; out[j + 2] = rgba[j + 2]; out[j + 3] = rgba[j + 3]; } return out; } /** Human summary of a mask in Spanish. */ export function describeMask(mask, W, H) { const area = maskArea(mask); const bb = maskBBox(mask, W, H); return { area, areaPct: Math.round((1000 * area) / (W * H)) / 10, bbox: bb }; } export function formatBytes(b) { if (b >= 1e6) return (b / 1e6).toFixed(1).replace('.', ',') + ' MB'; if (b >= 1e3) return Math.round(b / 1e3) + ' kB'; return b + ' B'; }