Line data Source code
1 : import type {
2 : Size2D,
3 : CompactRect as Rect,
4 : Point2D as Vec2
5 : } from '@comfyorg/shared-frontend-utils/geometry'
6 :
7 : export type SelectionOp = 'replace' | 'add' | 'subtract' | 'intersect'
8 :
9 : export interface GrayMask extends Size2D {
10 : data: Float32Array
11 : }
12 :
13 0 : export function emptyMask(width: number, height: number): GrayMask {
14 0 : return { data: new Float32Array(width * height), width, height }
15 : }
16 :
17 0 : export function maskFromCanvas(canvas: HTMLCanvasElement): GrayMask | null {
18 0 : const g = canvas.getContext('2d')
19 0 : if (!g) return null
20 0 : const img = g.getImageData(0, 0, canvas.width, canvas.height)
21 0 : const mask = emptyMask(canvas.width, canvas.height)
22 0 : for (let p = 0; p < mask.data.length; p++)
23 0 : mask.data[p] = img.data[p * 4] / 255
24 0 : return mask
25 : }
26 :
27 0 : export function maskToCanvas(mask: GrayMask): HTMLCanvasElement | null {
28 0 : const c = document.createElement('canvas')
29 0 : c.width = mask.width
30 0 : c.height = mask.height
31 0 : const g = c.getContext('2d')
32 0 : if (!g) return null
33 0 : const img = g.createImageData(mask.width, mask.height)
34 0 : for (let p = 0; p < mask.data.length; p++) {
35 0 : const v = Math.round(Math.max(0, Math.min(1, mask.data[p])) * 255)
36 0 : img.data[p * 4] = img.data[p * 4 + 1] = img.data[p * 4 + 2] = v
37 0 : img.data[p * 4 + 3] = 255
38 : }
39 0 : g.putImageData(img, 0, 0)
40 0 : return c
41 : }
42 :
43 0 : export function combineMasks(
44 : base: GrayMask,
45 : addOn: GrayMask,
46 : op: SelectionOp
47 : ): GrayMask {
48 0 : const out = emptyMask(base.width, base.height)
49 0 : const a = base.data
50 0 : const b = addOn.data
51 0 : const d = out.data
52 0 : switch (op) {
53 : case 'replace':
54 0 : d.set(b)
55 0 : break
56 : case 'add':
57 0 : for (let p = 0; p < d.length; p++) d[p] = Math.min(a[p] + b[p], 1)
58 0 : break
59 : case 'subtract':
60 0 : for (let p = 0; p < d.length; p++) d[p] = Math.max(a[p] - b[p], 0)
61 0 : break
62 : case 'intersect':
63 0 : for (let p = 0; p < d.length; p++) d[p] = Math.min(a[p], b[p])
64 : break
65 : }
66 0 : return out
67 : }
68 :
69 0 : export function maskBounds(mask: GrayMask): Rect | null {
70 0 : let minX = mask.width
71 0 : let minY = mask.height
72 0 : let maxX = -1
73 0 : let maxY = -1
74 0 : for (let y = 0; y < mask.height; y++) {
75 0 : const row = y * mask.width
76 0 : for (let x = 0; x < mask.width; x++) {
77 0 : if (mask.data[row + x] > 0) {
78 0 : if (x < minX) minX = x
79 0 : if (x > maxX) maxX = x
80 0 : if (y < minY) minY = y
81 0 : if (y > maxY) maxY = y
82 : }
83 : }
84 : }
85 0 : if (maxX < 0) return null
86 0 : return { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }
87 : }
88 :
89 : function vanHerk(
90 : line: Float32Array,
91 : out: Float32Array,
92 : n: number,
93 : radius: number,
94 : pick: (a: number, b: number) => number,
95 : pad: number
96 : ): void {
97 : const w = 2 * radius + 1
98 : const fwd = new Float32Array(n)
99 : const bwd = new Float32Array(n)
100 : for (let i = 0; i < n; i++) {
101 : fwd[i] = i % w === 0 ? line[i] : pick(fwd[i - 1], line[i])
102 : }
103 : for (let i = n - 1; i >= 0; i--) {
104 : bwd[i] =
105 : i % w === w - 1 || i === n - 1 ? line[i] : pick(bwd[i + 1], line[i])
106 : }
107 : for (let i = 0; i < n; i++) {
108 : const lo = i - radius
109 : const hi = i + radius
110 : const a = hi < n ? fwd[hi] : pad
111 : const b = lo >= 0 ? bwd[lo] : pad
112 : out[i] =
113 : hi < n && lo >= 0
114 : ? pick(a, b)
115 : : pick(pick(hi < n ? a : pad, lo >= 0 ? b : pad), pad)
116 : }
117 : }
118 :
119 : function verticalFilter(
120 : mask: GrayMask,
121 : radius: number,
122 : pick: (a: number, b: number) => number,
123 : pad: number
124 : ): GrayMask {
125 : const out = emptyMask(mask.width, mask.height)
126 : const col = new Float32Array(mask.height)
127 : const res = new Float32Array(mask.height)
128 : for (let x = 0; x < mask.width; x++) {
129 : for (let y = 0; y < mask.height; y++) col[y] = mask.data[y * mask.width + x]
130 : vanHerk(col, res, mask.height, radius, pick, pad)
131 : for (let y = 0; y < mask.height; y++) out.data[y * mask.width + x] = res[y]
132 : }
133 : return out
134 : }
135 :
136 : function ellipticalFilter(
137 : mask: GrayMask,
138 : radius: number,
139 : pick: (a: number, b: number) => number,
140 : pad: number
141 : ): GrayMask {
142 : const r = Math.max(1, Math.round(radius))
143 : const circ = new Int32Array(2 * r + 1)
144 : for (let i = -r; i <= r; i++) {
145 : const t = Math.max(0, Math.abs(i) - 0.5)
146 : circ[i + r] = Math.round(Math.sqrt(Math.max(0, r * r - t * t)))
147 : }
148 : const byHeight = new Map<number, GrayMask>()
149 : for (const h of circ) {
150 : if (!byHeight.has(h))
151 : byHeight.set(h, h === 0 ? mask : verticalFilter(mask, h, pick, pad))
152 : }
153 : const out = emptyMask(mask.width, mask.height)
154 : for (let y = 0; y < mask.height; y++) {
155 : const row = y * mask.width
156 : for (let x = 0; x < mask.width; x++) {
157 : let v: number | null = null
158 : for (let i = -r; i <= r; i++) {
159 : const xx = x + i
160 : const src =
161 : xx >= 0 && xx < mask.width
162 : ? byHeight.get(circ[i + r])!.data[row + xx]
163 : : pad
164 : v = v === null ? src : pick(v, src)
165 : }
166 : out.data[row + x] = v ?? pad
167 : }
168 : }
169 : return out
170 : }
171 :
172 : function cropMask(mask: GrayMask, r: Rect): GrayMask {
173 : const out = emptyMask(r.w, r.h)
174 : for (let y = 0; y < r.h; y++) {
175 : const src = (r.y + y) * mask.width + r.x
176 : out.data.set(mask.data.subarray(src, src + r.w), y * r.w)
177 : }
178 : return out
179 : }
180 :
181 : function pasteMask(
182 : sub: GrayMask,
183 : r: Rect,
184 : width: number,
185 : height: number
186 : ): GrayMask {
187 : const out = emptyMask(width, height)
188 : for (let y = 0; y < r.h; y++) {
189 : out.data.set(
190 : sub.data.subarray(y * r.w, y * r.w + r.w),
191 : (r.y + y) * width + r.x
192 : )
193 : }
194 : return out
195 : }
196 :
197 : function withBounds(
198 : mask: GrayMask,
199 : bounds: Rect | null | undefined,
200 : pad: number,
201 : run: (m: GrayMask) => GrayMask
202 : ): GrayMask {
203 : if (!bounds) return run(mask)
204 : const x0 = Math.max(0, Math.floor(bounds.x - pad))
205 : const y0 = Math.max(0, Math.floor(bounds.y - pad))
206 : const x1 = Math.min(mask.width, Math.ceil(bounds.x + bounds.w + pad))
207 : const y1 = Math.min(mask.height, Math.ceil(bounds.y + bounds.h + pad))
208 : if (x1 <= x0 || y1 <= y0) return emptyMask(mask.width, mask.height)
209 : const rect: Rect = { x: x0, y: y0, w: x1 - x0, h: y1 - y0 }
210 : if (
211 : rect.x === 0 &&
212 : rect.y === 0 &&
213 : rect.w === mask.width &&
214 : rect.h === mask.height
215 : )
216 : return run(mask)
217 : return pasteMask(run(cropMask(mask, rect)), rect, mask.width, mask.height)
218 : }
219 :
220 : export function growMask(
221 : mask: GrayMask,
222 : radius: number,
223 : bounds: Rect | null = null
224 : ): GrayMask {
225 : return withBounds(mask, bounds, Math.round(radius) + 1, (m) =>
226 : ellipticalFilter(m, radius, Math.max, 0)
227 : )
228 : }
229 :
230 : export function shrinkMask(
231 : mask: GrayMask,
232 : radius: number,
233 : bounds: Rect | null = null
234 : ): GrayMask {
235 : return withBounds(mask, bounds, Math.round(radius) + 1, (m) =>
236 : ellipticalFilter(m, radius, Math.min, 0)
237 : )
238 : }
239 :
240 : export function borderMask(
241 : mask: GrayMask,
242 : radius: number,
243 : bounds: Rect | null = null
244 : ): GrayMask {
245 : return withBounds(mask, bounds, Math.round(radius) + 1, (m) => {
246 : const grown = ellipticalFilter(m, radius, Math.max, 0)
247 : const shrunk = ellipticalFilter(m, radius, Math.min, 0)
248 : const out = emptyMask(m.width, m.height)
249 : for (let p = 0; p < out.data.length; p++) {
250 : out.data[p] = Math.max(grown.data[p] - shrunk.data[p], 0)
251 : }
252 : return out
253 : })
254 : }
255 :
256 : function boxBlurPass(
257 : src: Float32Array,
258 : dst: Float32Array,
259 : w: number,
260 : h: number,
261 : r: number
262 : ): void {
263 : const norm = 1 / (2 * r + 1)
264 : for (let y = 0; y < h; y++) {
265 : const row = y * w
266 : let acc = 0
267 : for (let i = -r; i <= r; i++)
268 : acc += src[row + Math.max(0, Math.min(w - 1, i))]
269 : for (let x = 0; x < w; x++) {
270 : dst[row + x] = acc * norm
271 : const outIdx = Math.max(0, Math.min(w - 1, x - r))
272 : const inIdx = Math.max(0, Math.min(w - 1, x + r + 1))
273 : acc += src[row + inIdx] - src[row + outIdx]
274 : }
275 : }
276 : }
277 :
278 : function transposeMask(mask: GrayMask): GrayMask {
279 : const out = {
280 : data: new Float32Array(mask.data.length),
281 : width: mask.height,
282 : height: mask.width
283 : }
284 : for (let y = 0; y < mask.height; y++) {
285 : for (let x = 0; x < mask.width; x++) {
286 : out.data[x * mask.height + y] = mask.data[y * mask.width + x]
287 : }
288 : }
289 : return out
290 : }
291 :
292 : function boxesForGauss(sigma: number, n: number): number[] {
293 : const wIdeal = Math.sqrt((12 * sigma * sigma) / n + 1)
294 : let wl = Math.floor(wIdeal)
295 : if (wl % 2 === 0) wl--
296 : const wu = wl + 2
297 : const mIdeal =
298 : (12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4 * wl - 4)
299 : const m = Math.round(mIdeal)
300 : const sizes: number[] = []
301 : for (let i = 0; i < n; i++) sizes.push(i < m ? wl : wu)
302 : return sizes
303 : }
304 :
305 : export function featherMask(
306 : mask: GrayMask,
307 : radius: number,
308 : bounds: Rect | null = null
309 : ): GrayMask {
310 : return withBounds(mask, bounds, Math.ceil(radius) + 2, (m) =>
311 : featherMaskFull(m, radius)
312 : )
313 : }
314 :
315 : function smallGaussian(mask: GrayMask, sigma: number): GrayMask {
316 : const r = 2
317 : const kernel = new Float32Array(2 * r + 1)
318 : let sum = 0
319 : for (let i = -r; i <= r; i++) {
320 : kernel[i + r] = Math.exp(-(i * i) / (2 * sigma * sigma))
321 : sum += kernel[i + r]
322 : }
323 : for (let i = 0; i < kernel.length; i++) kernel[i] /= sum
324 : const pass = (m: GrayMask): GrayMask => {
325 : const out = emptyMask(m.width, m.height)
326 : for (let y = 0; y < m.height; y++) {
327 : const row = y * m.width
328 : for (let x = 0; x < m.width; x++) {
329 : let acc = 0
330 : for (let i = -r; i <= r; i++) {
331 : const xx = Math.max(0, Math.min(m.width - 1, x + i))
332 : acc += m.data[row + xx] * kernel[i + r]
333 : }
334 : out.data[row + x] = acc
335 : }
336 : }
337 : return out
338 : }
339 : return transposeMask(pass(transposeMask(pass(mask))))
340 : }
341 :
342 : function featherMaskFull(mask: GrayMask, radius: number): GrayMask {
343 : const sigma = radius / 3.5
344 : if (sigma <= 0) return mask
345 : const boxes = boxesForGauss(sigma, 3)
346 : if (boxes.every((size) => Math.round((size - 1) / 2) < 1))
347 : return smallGaussian(mask, sigma)
348 : const sizes = boxes
349 : let cur: GrayMask = {
350 : data: Float32Array.from(mask.data),
351 : width: mask.width,
352 : height: mask.height
353 : }
354 : const blurAxis = (m: GrayMask): GrayMask => {
355 : const tmp = new Float32Array(m.data.length)
356 : let src = m.data
357 : for (const size of sizes) {
358 : const r = Math.max(0, (size - 1) / 2)
359 : if (r < 1) continue
360 : boxBlurPass(src, tmp, m.width, m.height, Math.round(r))
361 : src = Float32Array.from(tmp)
362 : }
363 : return { data: src, width: m.width, height: m.height }
364 : }
365 : cur = blurAxis(cur)
366 : cur = transposeMask(blurAxis(transposeMask(cur)))
367 : return cur
368 : }
369 :
370 : export interface FloodSource extends Size2D {
371 : data: Uint8ClampedArray
372 : }
373 :
374 : function pixelDifference(
375 : c1r: number,
376 : c1g: number,
377 : c1b: number,
378 : c1a: number,
379 : data: Uint8ClampedArray,
380 : idx: number,
381 : antialias: boolean,
382 : threshold: number
383 : ): number {
384 : const a2 = data[idx + 3] / 255
385 : if (c1a > 0 && a2 === 0) return 0
386 : let max: number
387 : if (c1a === 0) {
388 : max = Math.abs(c1a - a2)
389 : } else {
390 : max = Math.abs(c1r - data[idx] / 255)
391 : const dg = Math.abs(c1g - data[idx + 1] / 255)
392 : const db = Math.abs(c1b - data[idx + 2] / 255)
393 : if (dg > max) max = dg
394 : if (db > max) max = db
395 : }
396 : if (antialias && threshold > 0) {
397 : const aa = 1.5 - max / threshold
398 : if (aa <= 0) return 0
399 : if (aa < 0.5) return aa * 2
400 : return 1
401 : }
402 : return max > threshold ? 0 : 1
403 : }
404 :
405 : export function floodSelectMask(
406 : src: FloodSource,
407 : seed: Vec2,
408 : threshold: number,
409 : antialias: boolean,
410 : contiguous: boolean,
411 : diagonalNeighbors = false
412 : ): GrayMask | null {
413 : const w = src.width
414 : const h = src.height
415 : const sx = Math.floor(seed.x)
416 : const sy = Math.floor(seed.y)
417 : if (sx < 0 || sy < 0 || sx >= w || sy >= h) return null
418 : const si = (sy * w + sx) * 4
419 : const c1r = src.data[si] / 255
420 : const c1g = src.data[si + 1] / 255
421 : const c1b = src.data[si + 2] / 255
422 : const c1a = src.data[si + 3] / 255
423 : const mask = emptyMask(w, h)
424 :
425 : if (!contiguous) {
426 : for (let p = 0; p < w * h; p++) {
427 : mask.data[p] = pixelDifference(
428 : c1r,
429 : c1g,
430 : c1b,
431 : c1a,
432 : src.data,
433 : p * 4,
434 : antialias,
435 : threshold
436 : )
437 : }
438 : return mask
439 : }
440 :
441 : const diff = (x: number, y: number): number =>
442 : pixelDifference(
443 : c1r,
444 : c1g,
445 : c1b,
446 : c1a,
447 : src.data,
448 : (y * w + x) * 4,
449 : antialias,
450 : threshold
451 : )
452 :
453 : const queue: number[] = []
454 : const push = (
455 : y: number,
456 : oldY: number,
457 : start: number,
458 : end: number,
459 : newY: number,
460 : newStart: number,
461 : newEnd: number
462 : ): void => {
463 : if (newY !== oldY) {
464 : queue.push(newY, y, newStart, newEnd)
465 : } else {
466 : if (newStart < start) queue.push(newY, y, newStart, start + 1)
467 : if (newEnd > end) queue.push(newY, y, end - 1, newEnd)
468 : }
469 : }
470 :
471 : const findSegment = (x: number, y: number): [number, number] | null => {
472 : const d0 = diff(x, y)
473 : if (d0 === 0) return null
474 : const row = y * w
475 : mask.data[row + x] = d0
476 : let start = x - 1
477 : while (start >= 0) {
478 : const d = diff(start, y)
479 : if (d === 0) break
480 : mask.data[row + start] = d
481 : start--
482 : }
483 : let end = x + 1
484 : while (end < w) {
485 : const d = diff(end, y)
486 : if (d === 0) break
487 : mask.data[row + end] = d
488 : end++
489 : }
490 : return [start, end]
491 : }
492 :
493 : push(sy, -1, 0, 0, sy, sx - 1, sx + 1)
494 : while (queue.length) {
495 : const end = queue.pop()!
496 : const start = queue.pop()!
497 : const oldY = queue.pop()!
498 : const y = queue.pop()!
499 : for (let x = start + 1; x < end; x++) {
500 : if (mask.data[y * w + x] !== 0) {
501 : x++
502 : continue
503 : }
504 : const seg = findSegment(x, y)
505 : if (!seg) continue
506 : let [newStart, newEnd] = seg
507 : x = newEnd
508 : if (diagonalNeighbors) {
509 : if (newStart >= 0) newStart--
510 : if (newEnd < w) newEnd++
511 : }
512 : if (y + 1 < h) push(y, oldY, start, end, y + 1, newStart, newEnd)
513 : if (y - 1 >= 0) push(y, oldY, start, end, y - 1, newStart, newEnd)
514 : }
515 : }
516 : return mask
517 : }
518 :
519 0 : export function maskBoundary(mask: GrayMask, threshold = 0.5): Vec2[][] {
520 0 : const w = mask.width
521 0 : const h = mask.height
522 0 : const inside = (x: number, y: number): boolean =>
523 : x >= 0 && y >= 0 && x < w && y < h && mask.data[y * w + x] >= threshold
524 0 : const edges = new Map<number, Array<{ ex: number; ey: number }>>()
525 0 : const key = (x: number, y: number): number => y * (w + 1) + x
526 0 : const addEdge = (sx: number, sy: number, ex: number, ey: number): void => {
527 0 : const k = key(sx, sy)
528 0 : const list = edges.get(k)
529 0 : if (list) list.push({ ex, ey })
530 0 : else edges.set(k, [{ ex, ey }])
531 : }
532 0 : for (let y = 0; y < h; y++) {
533 0 : for (let x = 0; x < w; x++) {
534 0 : if (!inside(x, y)) continue
535 0 : if (!inside(x, y - 1)) addEdge(x, y, x + 1, y)
536 0 : if (!inside(x, y + 1)) addEdge(x + 1, y + 1, x, y + 1)
537 0 : if (!inside(x - 1, y)) addEdge(x, y + 1, x, y)
538 0 : if (!inside(x + 1, y)) addEdge(x + 1, y, x + 1, y + 1)
539 : }
540 : }
541 0 : const loops: Vec2[][] = []
542 0 : while (edges.size) {
543 0 : const firstKey = edges.keys().next().value as number
544 0 : const first = edges.get(firstKey)!
545 0 : const start = { x: firstKey % (w + 1), y: Math.floor(firstKey / (w + 1)) }
546 0 : let cur = first.shift()!
547 0 : if (!first.length) edges.delete(firstKey)
548 0 : const pts: Vec2[] = [start, { x: cur.ex, y: cur.ey }]
549 0 : while (cur.ex !== start.x || cur.ey !== start.y) {
550 0 : const k = key(cur.ex, cur.ey)
551 0 : const list = edges.get(k)
552 0 : if (!list || !list.length) break
553 0 : cur = list.shift()!
554 0 : if (!list.length) edges.delete(k)
555 0 : pts.push({ x: cur.ex, y: cur.ey })
556 : }
557 0 : const last = pts[pts.length - 1]
558 0 : if (pts.length > 1 && last.x === pts[0].x && last.y === pts[0].y) pts.pop()
559 0 : if (pts.length > 2) {
560 0 : const compact: Vec2[] = []
561 0 : for (let i = 0; i < pts.length; i++) {
562 0 : const prev = pts[(i - 1 + pts.length) % pts.length]
563 0 : const next = pts[(i + 1) % pts.length]
564 0 : const p = pts[i]
565 : const collinear =
566 : (prev.x === p.x && p.x === next.x) ||
567 : (prev.y === p.y && p.y === next.y)
568 0 : if (!collinear) compact.push(p)
569 : }
570 0 : if (compact.length >= 3) loops.push(compact)
571 : }
572 : }
573 0 : return loops
574 : }
575 :
576 : export function rectMask(width: number, height: number, rect: Rect): GrayMask {
577 : const out = emptyMask(width, height)
578 : const x0 = Math.max(0, Math.round(rect.x))
579 : const y0 = Math.max(0, Math.round(rect.y))
580 : const x1 = Math.min(width, Math.round(rect.x + rect.w))
581 : const y1 = Math.min(height, Math.round(rect.y + rect.h))
582 : for (let y = y0; y < y1; y++) out.data.fill(1, y * width + x0, y * width + x1)
583 : return out
584 : }
585 :
586 : export function ellipseMask(
587 : width: number,
588 : height: number,
589 : rect: Rect
590 : ): GrayMask {
591 : const out = emptyMask(width, height)
592 : const cx = rect.x + rect.w / 2
593 : const cy = rect.y + rect.h / 2
594 : const rx = rect.w / 2
595 : const ry = rect.h / 2
596 : if (rx <= 0 || ry <= 0) return out
597 : const y0 = Math.max(0, Math.floor(rect.y))
598 : const y1 = Math.min(height, Math.ceil(rect.y + rect.h))
599 : const x0 = Math.max(0, Math.floor(rect.x))
600 : const x1 = Math.min(width, Math.ceil(rect.x + rect.w))
601 : for (let y = y0; y < y1; y++) {
602 : for (let x = x0; x < x1; x++) {
603 : const dx = (x + 0.5 - cx) / rx
604 : const dy = (y + 0.5 - cy) / ry
605 : const d = Math.sqrt(dx * dx + dy * dy)
606 : const edge = (1 - d) * Math.min(rx, ry)
607 : out.data[y * width + x] = Math.max(0, Math.min(1, edge + 0.5))
608 : }
609 : }
610 : return out
611 : }
612 :
613 : export function polygonMask(
614 : width: number,
615 : height: number,
616 : points: Vec2[]
617 : ): GrayMask {
618 : const out = emptyMask(width, height)
619 : if (points.length < 3) return out
620 : const SS = 2
621 : for (let y = 0; y < height; y++) {
622 : for (let s = 0; s < SS; s++) {
623 : const py = y + (s + 0.5) / SS
624 : const xs: number[] = []
625 : for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
626 : const a = points[i]
627 : const b = points[j]
628 : if (a.y <= py === b.y <= py) continue
629 : xs.push(a.x + ((py - a.y) / (b.y - a.y)) * (b.x - a.x))
630 : }
631 : xs.sort((m, n) => m - n)
632 : for (let k = 0; k + 1 < xs.length; k += 2) {
633 : const x0 = xs[k]
634 : const x1 = xs[k + 1]
635 : const ix0 = Math.max(0, Math.floor(x0))
636 : const ix1 = Math.min(width - 1, Math.ceil(x1))
637 : for (let x = ix0; x <= ix1; x++) {
638 : const cov = Math.max(0, Math.min(x + 1, x1) - Math.max(x, x0))
639 : out.data[y * width + x] = Math.min(
640 : 1,
641 : out.data[y * width + x] + cov / SS
642 : )
643 : }
644 : }
645 : }
646 : }
647 : return out
648 : }
|