saliacoel commited on
Commit
22e83a6
·
verified ·
1 Parent(s): abba1af

Upload 2 files

Browse files
Salia_GraphCut_Blend.py ADDED
@@ -0,0 +1,439 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Salia Graph-Cut Blend -- a SEPARATE ComfyUI custom node, v1.0
3
+ =========================================================
4
+ Install this file in ComfyUI/custom_nodes/ and restart ComfyUI.
5
+ Menu: Salia/Image -> Salia Graph-Cut Blend (RGBA).
6
+ Requires NumPy, SciPy, PyTorch and OpenCV with stitching/detail support.
7
+ If cv2 is missing, install opencv-python-headless using ComfyUI's Python.
8
+ No models, downloads, sibling modules, or changes to the alignment node.
9
+
10
+ Wiring from the EXISTING Salia Auto Align Overlay node:
11
+ original background -> background
12
+ overlay_canvas_rgba -> overlay_canvas_rgba
13
+ x, y, width, height -> matching FLOAT sockets
14
+ Use overlay_canvas_rgba, not composite_rgba and not the original small crop.
15
+ There are two IMAGE inputs plus coordinate sockets and blending controls.
16
+ The overlay is already rendered at background size: it is never rescaled,
17
+ moved, aligned again, or inferred from coordinates.
18
+
19
+ Graph-cut selects a low-disagreement seam within search_band_px of the
20
+ rectangle's original border. The center is protected. Optional protect_mask
21
+ uses WHITE=keep overlay, BLACK=automatic seam selection. Search-band width
22
+ is capped at 45% of the rectangle's shortest side. Very small patches may
23
+ have little or no seam-search space. feather_px softens the selected seam.
24
+ blend_mode optionally adds a Laplacian-pyramid color blend in the border.
25
+ seam_max_side limits graph resolution only; final output stays full size.
26
+ Large downsampling may skip fine seam detail; status reports the graph size.
27
+
28
+ RGBA is straight-alpha. This node REPLACES an edited region: it interpolates
29
+ premultiplied RGB and alpha instead of stacking alpha with source-over.
30
+ Matching half-transparent edges therefore stay half-transparent. Transparent
31
+ overlay pixels in the rectangle can intentionally remove background pixels.
32
+ RGB-only inputs are opaque unless a matching Load Image MASK is supplied
33
+ (MASK=1-alpha). RGBA always takes its embedded alpha instead of that MASK.
34
+ One image per input, matching the current alignment node. FLOAT coordinates
35
+ are pixel edges, not pixel centers. Negative/off-canvas placement is clipped.
36
+ EWA's fractional edge coverage is removed from source alpha and applied once
37
+ as replacement coverage, preserving the untouched fraction of edge pixels.
38
+ Width or height 0 (alignment-failure sentinel) returns background unchanged.
39
+
40
+ Outputs:
41
+ composite_rgba : [1,H,W,4], straight RGBA, float32
42
+ overlay_weight : [1,H,W], WHITE=overlay / BLACK=background (not alpha)
43
+ output_mask : [1,H,W], 1-output alpha, for ComfyUI mask workflows
44
+ status : graph size, clipping, controls and any fallback notices
45
+ The overlay_weight is a replacement weight, not a source-over opacity mask.
46
+
47
+ Existing implementation used: OpenCV GraphCutSeamFinder, COST_COLOR_GRAD.
48
+ Python API and usage:
49
+ https://docs.opencv.org/4.x/db/dda/classcv_1_1detail_1_1GraphCutSeamFinder.html
50
+ https://github.com/opencv/opencv/blob/4.x/samples/python/stitching_detailed.py
51
+ Optional pyramid blend follows the OpenCV image-pyramids construction:
52
+ https://docs.opencv.org/4.x/dc/dff/tutorial_py_pyramids.html
53
+ OpenCV is used as a dependency; its source is not vendored here.
54
+ """
55
+ from __future__ import annotations
56
+
57
+ import math
58
+ import time
59
+
60
+ import numpy as np
61
+ from scipy.ndimage import distance_transform_edt
62
+
63
+ _EPS = 1.0e-8
64
+ _MODES = ("Graph cut + feather", "Graph cut + multiband")
65
+
66
+
67
+ def _opencv():
68
+ # Lazy import keeps this node discoverable if the dependency is missing.
69
+ try:
70
+ import cv2
71
+ except ImportError as exc:
72
+ raise RuntimeError(
73
+ "Salia Graph-Cut Blend needs OpenCV. Install opencv-python-headless "
74
+ "using the Python environment that runs ComfyUI, then restart it."
75
+ ) from exc
76
+ constructor = getattr(cv2, "detail_GraphCutSeamFinder", None)
77
+ if constructor is None:
78
+ constructor = getattr(getattr(cv2, "detail", None), "GraphCutSeamFinder", None)
79
+ if constructor is None:
80
+ raise RuntimeError("This cv2 build has no GraphCutSeamFinder; use a build with stitching support.")
81
+ return cv2, constructor
82
+
83
+
84
+ def _numpy(value):
85
+ if hasattr(value, "detach"):
86
+ return value.detach().cpu().float().numpy()
87
+ return np.asarray(value)
88
+
89
+
90
+ def _mask(value, shape, name, allow_loader_dummy=False):
91
+ array = _numpy(value)
92
+ if array.ndim == 3 and array.shape[0] == 1:
93
+ array = array[0]
94
+ if array.ndim != 2 or not np.isfinite(array).all():
95
+ raise ValueError(name + " must be a finite [1,H,W] or [H,W] mask.")
96
+ if array.shape != shape:
97
+ # Load Image produces this placeholder for an image without alpha.
98
+ if allow_loader_dummy and array.shape == (64, 64) and not np.any(array):
99
+ return np.zeros(shape, dtype=np.float32)
100
+ raise ValueError(name + " must match the image dimensions exactly.")
101
+ return np.clip(array, 0.0, 1.0).astype(np.float32)
102
+
103
+
104
+ def _rgba(value, name, load_mask=None):
105
+ array = _numpy(value)
106
+ if array.ndim == 4 and array.shape[0] == 1:
107
+ array = array[0]
108
+ if (array.ndim != 3 or array.shape[-1] not in (3, 4)
109
+ or min(array.shape[:2]) < 1):
110
+ raise ValueError(name + " must contain one RGB/RGBA image; split batches upstream.")
111
+ if not np.issubdtype(array.dtype, np.floating) or not np.isfinite(array).all():
112
+ raise ValueError(name + " must contain finite floating point pixels in [0,1].")
113
+ array = np.clip(array, 0.0, 1.0).astype(np.float32, copy=True)
114
+ if array.shape[-1] == 3:
115
+ alpha = np.ones(array.shape[:2], dtype=np.float32)
116
+ if load_mask is not None:
117
+ alpha -= _mask(load_mask, alpha.shape, name + "_mask", allow_loader_dummy=True)
118
+ array = np.concatenate((array, alpha[..., None]), axis=-1)
119
+ return np.ascontiguousarray(array)
120
+
121
+
122
+ def _linear(rgb):
123
+ return np.where(rgb <= 0.04045, rgb / 12.92,
124
+ ((rgb + 0.055) / 1.055) ** 2.4).astype(np.float32)
125
+
126
+
127
+ def _srgb(rgb):
128
+ rgb = np.clip(rgb, 0.0, 1.0)
129
+ return np.where(rgb <= 0.0031308, rgb * 12.92,
130
+ 1.055 * rgb ** (1.0 / 2.4) - 0.055).astype(np.float32)
131
+
132
+
133
+ def _smoothstep(value):
134
+ value = np.clip(value, 0.0, 1.0)
135
+ return value * value * (3.0 - 2.0 * value)
136
+
137
+
138
+ def _geometry(shape, x0, y0, x, y, width, height):
139
+ # The original, unclipped floating rectangle controls all border distances.
140
+ # Coverage only defines pixel support; EWA already put coverage into alpha.
141
+ h, w = shape
142
+ xx = x0 + np.arange(w, dtype=np.float64)
143
+ yy = y0 + np.arange(h, dtype=np.float64)
144
+ cx = np.maximum(0.0, np.minimum(xx + 1.0, x + width) - np.maximum(xx, x))
145
+ cy = np.maximum(0.0, np.minimum(yy + 1.0, y + height) - np.maximum(yy, y))
146
+ support = (cy[:, None] > 0.0) & (cx[None, :] > 0.0)
147
+ dx = np.minimum(xx + 0.5 - x, x + width - xx - 0.5)
148
+ dy = np.minimum(yy + 0.5 - y, y + height - yy - 0.5)
149
+ coverage = (cy[:, None] * cx[None, :]).astype(np.float32)
150
+ return support, np.minimum(dy[:, None], dx[None, :]), coverage
151
+
152
+
153
+ def _graph_cut(background, overlay, support, core, max_side, cv, constructor, interrupt):
154
+ h, w = support.shape
155
+ scale = min(1.0, max_side / max(h, w))
156
+ gw, gh = max(1, int(round(w * scale))), max(1, int(round(h * scale)))
157
+ a = background[..., 3:4]
158
+ b = overlay[..., 3:4]
159
+ # Neutral-matte proxies ignore hidden RGB. OpenCV expects 3-channel float32
160
+ # colors at the usual 0..255 scale, not raw four-channel RGBA.
161
+ bg_proxy = 255.0 * (background[..., :3] * a + 0.5 * (1.0 - a))
162
+ ov_proxy = 255.0 * (overlay[..., :3] * b + 0.5 * (1.0 - b))
163
+ # Padded pixels of the overlay canvas are not another photograph. Supply
164
+ # background proxy outside support to avoid a synthetic black border cost.
165
+ ov_proxy[~support] = bg_proxy[~support]
166
+ if (gh, gw) != (h, w):
167
+ bg_proxy = cv.resize(bg_proxy, (gw, gh), interpolation=cv.INTER_AREA)
168
+ ov_proxy = cv.resize(ov_proxy, (gw, gh), interpolation=cv.INTER_AREA)
169
+ valid = cv.resize(support.astype(np.uint8), (gw, gh), interpolation=cv.INTER_NEAREST) > 0
170
+ fixed = cv.resize(core.astype(np.uint8), (gw, gh), interpolation=cv.INTER_NEAREST) > 0
171
+ else:
172
+ valid, fixed = support.copy(), core.copy()
173
+ fixed &= valid
174
+ if not np.any(fixed) and np.any(valid):
175
+ # Downsampling must not eliminate the only mandatory overlay seed.
176
+ candidates = np.argwhere(core)
177
+ if candidates.size:
178
+ yy, xx = candidates[len(candidates) // 2]
179
+ gy, gx = min(gh - 1, int(yy * gh / h)), min(gw - 1, int(xx * gw / w))
180
+ fixed[gy, gx] = valid[gy, gx] = True
181
+ if not np.any(valid):
182
+ return core.copy(), (gw, gh), "Graph support vanished at reduced resolution; protected pixels retained."
183
+ if np.all(fixed) or not np.any(~valid):
184
+ # A fully covering/off-canvas rectangle can have no visible exterior.
185
+ # Never invent a seam against the cropped canvas border.
186
+ return support.copy(), (gw, gh), "No visible background-only graph seed; full visible overlay retained."
187
+ bg_mask = np.where(fixed, 0, 255).astype(np.uint8)
188
+ ov_mask = np.where(valid, 255, 0).astype(np.uint8)
189
+ masks = [cv.UMat(bg_mask), cv.UMat(ov_mask)]
190
+ interrupt()
191
+ finder = constructor("COST_COLOR_GRAD")
192
+ try:
193
+ result = finder.find(
194
+ [np.ascontiguousarray(bg_proxy, dtype=np.float32),
195
+ np.ascontiguousarray(ov_proxy, dtype=np.float32)],
196
+ [(0, 0), (0, 0)], masks)
197
+ except cv.error as exc:
198
+ raise RuntimeError("OpenCV graph-cut seam finding failed: " + str(exc)) from exc
199
+ interrupt()
200
+ # New bindings return updated masks; older versions mutate UMat in place.
201
+ selected = result[1] if result is not None else masks[1]
202
+ selected = selected.get() if hasattr(selected, "get") else np.asarray(selected)
203
+ if selected is None or selected.shape != (gh, gw):
204
+ raise RuntimeError("OpenCV returned an invalid graph-cut seam mask.")
205
+ hard = selected > 0
206
+ if (gh, gw) != (h, w):
207
+ hard = cv.resize(hard.astype(np.uint8), (w, h), interpolation=cv.INTER_NEAREST) > 0
208
+ hard &= support
209
+ hard[core] = True
210
+ return hard, (gw, gh), ""
211
+
212
+
213
+ def _feather(hard, core, support, distance, radius):
214
+ if radius <= 0.0:
215
+ return hard.astype(np.float32)
216
+ if np.any(hard) and np.any(~hard):
217
+ signed = distance_transform_edt(hard) - distance_transform_edt(~hard)
218
+ weight = _smoothstep(0.5 + signed / (2.0 * radius))
219
+ else:
220
+ weight = hard.astype(np.float32)
221
+ # Envelopes meet the fixed center and outer border continuously, even if
222
+ # graph-cut runs directly beside the protected center or the outer edge.
223
+ if np.any(core) and np.any(~core):
224
+ near_core = _smoothstep(1.0 - distance_transform_edt(~core) / radius)
225
+ weight = np.maximum(weight, near_core)
226
+ outer_limit = _smoothstep((distance + 0.5) / radius)
227
+ outer_limit[core] = 1.0
228
+ weight = np.minimum(weight, outer_limit)
229
+ weight[~support] = 0.0
230
+ weight[core] = 1.0
231
+ return weight.astype(np.float32)
232
+
233
+
234
+ def _multiband(bg_premul, ov_premul, weight, levels, cv, interrupt):
235
+ bg, ov, wm = [bg_premul], [ov_premul], [weight[..., None]]
236
+ for _ in range(levels):
237
+ interrupt()
238
+ if min(bg[-1].shape[:2]) <= 2:
239
+ break
240
+ bg.append(cv.pyrDown(bg[-1]))
241
+ ov.append(cv.pyrDown(ov[-1]))
242
+ small = cv.pyrDown(wm[-1])
243
+ wm.append(small[..., None] if small.ndim == 2 else small)
244
+ mixed = (1.0 - wm[-1]) * bg[-1] + wm[-1] * ov[-1]
245
+ for i in range(len(bg) - 2, -1, -1):
246
+ interrupt()
247
+ size = (bg[i].shape[1], bg[i].shape[0])
248
+ lb = bg[i] - cv.pyrUp(bg[i + 1], dstsize=size)
249
+ lo = ov[i] - cv.pyrUp(ov[i + 1], dstsize=size)
250
+ mixed = cv.pyrUp(mixed, dstsize=size) + (1.0 - wm[i]) * lb + wm[i] * lo
251
+ return mixed
252
+
253
+
254
+ def blend_arrays(background, overlay_canvas_rgba, x, y, width, height,
255
+ search_band_px=24.0, feather_px=3.0, blend_mode=_MODES[0],
256
+ pyramid_levels=3, seam_max_side=1024, protect_mask=None,
257
+ background_mask=None, overlay_mask=None,
258
+ interrupt=lambda: None, progress=lambda value: None):
259
+ """Numpy entry point, also useful for testing without a ComfyUI server."""
260
+ started = time.monotonic()
261
+ bg = _rgba(background, "background", background_mask)
262
+ ov = _rgba(overlay_canvas_rgba, "overlay_canvas_rgba", overlay_mask)
263
+ if bg.shape != ov.shape:
264
+ raise ValueError("overlay_canvas_rgba must have background dimensions. Connect the alignment node's overlay_canvas_rgba output, not the small overlay or composite.")
265
+ values = np.asarray((x, y, width, height, search_band_px, feather_px), dtype=np.float64)
266
+ if not np.isfinite(values).all():
267
+ raise ValueError("Coordinates and blending distances must be finite.")
268
+ x, y, width, height, search_band_px, feather_px = map(float, values)
269
+ if min(width, height, search_band_px, feather_px) < 0.0:
270
+ raise ValueError("Width, height and blending distances must be nonnegative.")
271
+ if blend_mode not in _MODES:
272
+ raise ValueError("Unknown blend_mode: " + str(blend_mode))
273
+ if isinstance(pyramid_levels, bool) or int(pyramid_levels) != pyramid_levels or not 1 <= pyramid_levels <= 8:
274
+ raise ValueError("pyramid_levels must be an integer in [1,8].")
275
+ if isinstance(seam_max_side, bool) or int(seam_max_side) != seam_max_side or not 64 <= seam_max_side <= 2048:
276
+ raise ValueError("seam_max_side must be an integer in [64,2048].")
277
+ h, w = bg.shape[:2]
278
+ full_weight = np.zeros((h, w), dtype=np.float32)
279
+ def unchanged(reason):
280
+ progress(100)
281
+ return {"composite": bg.copy(), "weight": full_weight,
282
+ "mask": 1.0 - bg[..., 3], "status": reason}
283
+ if width == 0.0 or height == 0.0:
284
+ return unchanged("NO BLEND: zero-sized coordinates (including alignment-failure sentinel); background retained.")
285
+ if x >= w or y >= h or x + width <= 0.0 or y + height <= 0.0:
286
+ return unchanged("NO BLEND: rectangle is fully outside the background.")
287
+ interrupt()
288
+ cv, constructor = _opencv()
289
+ band = min(search_band_px, 0.45 * min(width, height))
290
+ # Keep room for both feather envelopes without cutting into the center.
291
+ feather = min(feather_px, band / 3.0) if band > 0.0 else 0.0
292
+ padding = max(2, int(math.ceil(feather)) + 2)
293
+ if blend_mode == _MODES[1]:
294
+ padding = max(padding, 2 ** (int(pyramid_levels) + 1))
295
+ x0 = max(0, int(math.floor(x)) - padding)
296
+ y0 = max(0, int(math.floor(y)) - padding)
297
+ x1 = min(w, int(math.ceil(x + width)) + padding)
298
+ y1 = min(h, int(math.ceil(y + height)) + padding)
299
+ bg_roi, ov_roi = bg[y0:y1, x0:x1], ov[y0:y1, x0:x1].copy()
300
+ support, distance, coverage = _geometry(bg_roi.shape[:2], x0, y0, x, y, width, height)
301
+ # The existing EWA renderer multiplies alpha by subpixel coverage. Recover
302
+ # intrinsic source alpha, then apply that coverage exactly ONCE to the
303
+ # replacement weight, retaining the background fraction outside the rect.
304
+ intrinsic_alpha = np.zeros_like(coverage)
305
+ np.divide(ov_roi[..., 3], coverage, out=intrinsic_alpha, where=coverage > 0.0)
306
+ ov_roi[..., 3] = np.clip(intrinsic_alpha, 0.0, 1.0)
307
+ core = support & (distance >= band)
308
+ if protect_mask is not None:
309
+ protected = _mask(protect_mask, (h, w), "protect_mask")
310
+ core |= support & (protected[y0:y1, x0:x1] > 0.0)
311
+ if not np.any(core) and np.any(support):
312
+ index = np.argmax(np.where(support, distance, -np.inf))
313
+ core.flat[index] = True
314
+ progress(10)
315
+ if band == 0.0 or not np.any(support & ~core):
316
+ hard, graph_size, note = support.copy(), (0, 0), "No border-search space; full visible overlay retained."
317
+ else:
318
+ hard, graph_size, note = _graph_cut(bg_roi, ov_roi, support, core,
319
+ int(seam_max_side), cv, constructor, interrupt)
320
+ progress(65)
321
+ weight = _feather(hard, core, support, distance, feather) * coverage
322
+ interrupt()
323
+ t = weight[..., None]
324
+ ab, ao = bg_roi[..., 3:4], ov_roi[..., 3:4]
325
+ bp, op = _linear(bg_roi[..., :3]) * ab, _linear(ov_roi[..., :3]) * ao
326
+ alpha = (1.0 - t) * ab + t * ao
327
+ premul = (1.0 - t) * bp + t * op
328
+ if blend_mode == _MODES[1]:
329
+ # Transparent canvas padding is not edited image content. Extend the
330
+ # source with background there, or the pyramid creates a false halo
331
+ # even when the two visible regions are identical.
332
+ background_fields = np.concatenate((bp, ab), axis=-1)
333
+ pyramid_source = np.concatenate((op, ao), axis=-1)
334
+ pyramid_source[~support] = background_fields[~support]
335
+ pyramid = _multiband(background_fields, pyramid_source, weight,
336
+ int(pyramid_levels), cv, interrupt)
337
+ # Filter premultiplied RGB AND alpha with the same pyramid operator.
338
+ # Normalize color by that filtered alpha before adopting the desired
339
+ # direct-interpolation alpha. Filtering RGB alone changes color even
340
+ # when both sources have identical straight RGB but different alpha.
341
+ pyramid_color = np.zeros_like(premul)
342
+ stable = pyramid[..., 3:4] > _EPS
343
+ np.divide(pyramid[..., :3], pyramid[..., 3:4],
344
+ out=pyramid_color, where=stable)
345
+ pyramid_premul = np.where(stable, pyramid_color * alpha, premul)
346
+ # Restrict the pyramid result to the actual transition. Restore both
347
+ # exact endpoints so center edits and background are never color-shifted.
348
+ transition = (weight > 0.0) & (weight < 1.0)
349
+ premul[transition] = pyramid_premul[transition]
350
+ premul = np.minimum(np.maximum(premul, 0.0), alpha)
351
+ straight = np.zeros_like(premul)
352
+ np.divide(premul, alpha, out=straight, where=alpha > _EPS)
353
+ mixed = np.concatenate((_srgb(straight), alpha), axis=-1).astype(np.float32)
354
+ mixed[weight <= 0.0] = bg_roi[weight <= 0.0]
355
+ mixed[weight >= 1.0] = ov_roi[weight >= 1.0]
356
+ composite = bg.copy()
357
+ composite[y0:y1, x0:x1] = mixed
358
+ full_weight[y0:y1, x0:x1] = weight
359
+ progress(100)
360
+ notes = [note] if note else []
361
+ if band != search_band_px:
362
+ notes.append("Search band capped for rectangle size.")
363
+ if feather != feather_px:
364
+ notes.append("Feather capped to one-third of the effective search band.")
365
+ if x < 0.0 or y < 0.0 or x + width > w or y + height > h:
366
+ notes.append("Off-canvas rectangle clipped; border distances use original geometry.")
367
+ status = "\n".join([
368
+ "BLENDED: " + blend_mode + "; premultiplied, linear-light RGBA replacement.",
369
+ "Rect x=%.8f y=%.8f width=%.8f height=%.8f; input canvas=%dx%d."
370
+ % (x, y, width, height, w, h),
371
+ "Effective search_band_px=%.3f feather_px=%.3f; graph=%dx%d; protected=%d pixels."
372
+ % (band, feather, graph_size[0], graph_size[1], np.count_nonzero(core)),
373
+ "ROI=(%d,%d)-(%d,%d); seconds=%.3f."
374
+ % (x0, y0, x1, y1, time.monotonic() - started), *notes])
375
+ return {"composite": composite, "weight": full_weight,
376
+ "mask": 1.0 - composite[..., 3], "status": status}
377
+
378
+
379
+ class Salia_GraphCut_Blend:
380
+ @classmethod
381
+ def INPUT_TYPES(cls):
382
+ coord = {"default": 0.0, "min": -1000000.0, "max": 1000000.0, "step": 0.01, "forceInput": True}
383
+ size = {"default": 0.0, "min": 0.0, "max": 1000000.0, "step": 0.01, "forceInput": True}
384
+ return {"required": {
385
+ "background": ("IMAGE", {"tooltip": "Original full-size background, RGB or RGBA."}),
386
+ "overlay_canvas_rgba": ("IMAGE", {"tooltip": "Aligned, background-sized overlay canvas from Salia Auto Align Overlay. Do not connect composite_rgba or the original small crop."}),
387
+ "x": ("FLOAT", dict(coord)), "y": ("FLOAT", dict(coord)),
388
+ "width": ("FLOAT", dict(size)), "height": ("FLOAT", dict(size)),
389
+ "search_band_px": ("FLOAT", {"default": 24.0, "min": 0.0, "max": 512.0, "step": 1.0,
390
+ "tooltip": "Search for a seam within this distance of the rect border. Center stays intact; 0 keeps entire overlay."}),
391
+ "feather_px": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 64.0, "step": 0.5,
392
+ "tooltip": "Soften the selected seam; capped to one-third of effective search-band width."}),
393
+ "blend_mode": (list(_MODES), {"default": _MODES[0]}),
394
+ "pyramid_levels": ("INT", {"default": 3, "min": 1, "max": 8,
395
+ "tooltip": "Used only for multiband mode; more levels spread color blending further."}),
396
+ "seam_max_side": ("INT", {"default": 1024, "min": 64, "max": 2048, "step": 64,
397
+ "tooltip": "Maximum graph dimension. Output resolution stays unchanged."}),
398
+ }, "optional": {
399
+ "background_mask": ("MASK", {"tooltip": "For RGB background only: Load Image MASK (1-alpha). Embedded RGBA alpha takes precedence."}),
400
+ "overlay_mask": ("MASK", {"tooltip": "For RGB overlay canvas only: Load Image MASK (1-alpha). Embedded RGBA alpha takes precedence."}),
401
+ "protect_mask": ("MASK", {"tooltip": "White pixels inside the rectangle must keep overlay content; black allows automatic seam selection. Full canvas size."}),
402
+ }}
403
+
404
+ RETURN_TYPES = ("IMAGE", "MASK", "MASK", "STRING")
405
+ RETURN_NAMES = ("composite_rgba", "overlay_weight", "output_mask", "status")
406
+ FUNCTION = "blend"
407
+ CATEGORY = "Salia/Image"
408
+ DESCRIPTION = ("Separate graph-cut border blender for an already aligned overlay. "
409
+ "Two image inputs plus fractional coordinates. Protects the center, "
410
+ "preserves RGBA with premultiplied replacement, and optionally blends color with pyramids.")
411
+
412
+ def blend(self, background, overlay_canvas_rgba, x, y, width, height,
413
+ search_band_px=24.0, feather_px=3.0, blend_mode=_MODES[0],
414
+ pyramid_levels=3, seam_max_side=1024, background_mask=None,
415
+ overlay_mask=None, protect_mask=None):
416
+ import torch
417
+ try:
418
+ from comfy.model_management import throw_exception_if_processing_interrupted as interrupt
419
+ except ImportError:
420
+ interrupt = lambda: None
421
+ try:
422
+ from comfy.utils import ProgressBar
423
+ bar = ProgressBar(100)
424
+ progress = lambda value: bar.update_absolute(int(value), 100)
425
+ except ImportError:
426
+ progress = lambda value: None
427
+ with torch.inference_mode():
428
+ result = blend_arrays(
429
+ background, overlay_canvas_rgba, x, y, width, height,
430
+ search_band_px, feather_px, blend_mode, pyramid_levels, seam_max_side,
431
+ protect_mask, background_mask, overlay_mask, interrupt, progress)
432
+ image = torch.from_numpy(np.ascontiguousarray(result["composite"])).unsqueeze(0)
433
+ weight = torch.from_numpy(np.ascontiguousarray(result["weight"])).unsqueeze(0)
434
+ mask = torch.from_numpy(np.ascontiguousarray(result["mask"])).unsqueeze(0)
435
+ return image, weight, mask, result["status"]
436
+
437
+
438
+ NODE_CLASS_MAPPINGS = {"Salia_GraphCut_Blend": Salia_GraphCut_Blend}
439
+ NODE_DISPLAY_NAME_MAPPINGS = {"Salia_GraphCut_Blend": "Salia Graph-Cut Blend (RGBA)"}
Salia_Seamless_Overlay_Blend.py ADDED
@@ -0,0 +1,978 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Salia Seamless Overlay Blend -- separate, standalone ComfyUI custom node
3
+ =====================================================================
4
+ Install this file in ComfyUI/custom_nodes/ and restart ComfyUI.
5
+ Menu: Salia/Image -> Salia Seamless Overlay Blend (OpenCV).
6
+ This is a NEW node. It neither imports nor changes Salia_Auto_Align_Overlay.
7
+
8
+ Requires NumPy, SciPy, PyTorch and OpenCV with its stitching module.
9
+ If OpenCV is missing, install in the SAME Python environment as ComfyUI:
10
+ python -m pip install opencv-python-headless
11
+ Do not install an extra OpenCV package when a working cv2 already exists.
12
+ No models, network calls, external processes or sibling files are used.
13
+
14
+ Inputs / wiring
15
+ ---------------
16
+ background: ORIGINAL background image, not the alignment node's composite.
17
+ overlay: original edited crop, or the alignment node's overlay_canvas_rgba
18
+ when overlay_mode is set to Aligned canvas.
19
+ x, y, width, height: FLOAT coordinates from the alignment node, in pixels.
20
+ x/y are the final TOP-LEFT, not center coordinates. They may be fractional.
21
+ The four numeric sockets connect directly to the four corresponding outputs.
22
+ Original crop mode: original full-resolution overlay is placed/scaled once
23
+ from its original pixels using embedded EWA LanczosSharp. Exact fractional
24
+ position/dimensions are retained. No registration/search is done here.
25
+ Aligned canvas mode: overlay must already match background canvas size;
26
+ it is not rescaled or moved again. Use the SAME placement coordinates that
27
+ created that canvas; its alpha is expected to include rectangle coverage.
28
+
29
+ Controls
30
+ --------
31
+ seam_band (48): width in output pixels of the adjustable border region.
32
+ The inner rectangle is protected and must use the edited version. The outer
33
+ pixel frame must use background. OpenCV chooses a low-cost seam in between.
34
+ Large values allow the seam to move farther inward. Small rectangles clamp
35
+ this band to retain a protected core.
36
+ feather_pixels (4): narrow smooth transition across the selected seam;
37
+ zero gives a hard seam. It does not blur the entire overlay image.
38
+ blend_mode: Graph cut + feather (default) or Graph cut + multiband.
39
+ multiband_bands (4): OpenCV Laplacian pyramid depth for the optional color
40
+ correction. Color correction is restricted to the soft transition; alpha,
41
+ fully selected source and unselected background pixels remain exact.
42
+ gradient_weight (.35): weight of outline/gradient disagreement in seam cost.
43
+
44
+ Optional masks
45
+ --------------
46
+ Embedded RGBA alpha ALWAYS takes priority. background_mask and overlay_mask
47
+ are used only for RGB inputs and mean standard Load Image MASK = 1-alpha.
48
+ protect_mask is different: white means MUST keep the edited version. It
49
+ OVERRIDES the automatic inner-rectangle core. Its resolution may match the
50
+ background or original overlay; background-sized masks are treated as already
51
+ aligned. It is independent of object alpha, so it can protect transparent
52
+ edited pixels that intentionally erase old content. A nonempty protected
53
+ region must fall inside the placed rectangle.
54
+
55
+ Existing implementations actually used
56
+ -------------------------------------
57
+ - OpenCV GraphCutSeamFinder: real max-flow/min-cut solver, COST_COLOR.
58
+ We encode RGBA disagreement as 3-channel cost proxies because the OpenCV
59
+ API accepts RGB, not RGBA. Cost compares appearance over BOTH black and
60
+ white and adds gradient disagreement. Hidden RGB has no seam influence.
61
+ - OpenCV MultiBandBlender: optional color correction using real Laplacian
62
+ pyramid blending, two passes for premultiplied RGB and alpha calibration.
63
+ Its internal color values are 16-bit; final output remains float32.
64
+ Graph size is capped at 1536 on the longest side for memory. Larger image
65
+ costs are downsampled for seam selection, then the mask is restored to full
66
+ resolution and hard constraints are reapplied. Final image stays full-size.
67
+
68
+ RGBA composition
69
+ ----------------
70
+ This is edited-image REPLACEMENT, not source-over layering. Blend weight,
71
+ object alpha and geometric pixel coverage are kept separate. In linear RGB,
72
+ interpolate premultiplied color AND alpha between original and edited images.
73
+ An identical 50%-alpha edge remains 50% alpha instead of accumulating to 75%.
74
+ Transparent source pixels inside the valid rectangle can erase original
75
+ pixels. Pixels outside the rectangle or with zero blend weight are unchanged.
76
+ The EWA resampler uses linear-light premultiplied alpha, copied from the
77
+ provided Salia rescaler and extended to fractional placement. The final
78
+ renderer samples the original only once in Original crop mode.
79
+
80
+ Outputs
81
+ -------
82
+ composite_rgba: [1,background_H,background_W,4], CPU float32.
83
+ blend_mask: [1,background_H,background_W]; white selects the edited version.
84
+ This is a replacement weight, not a source-over alpha mask.
85
+ seam_preview: RGBA debug composite; green marks seam, cyan protected core.
86
+ status: actual placement, seam settings, constraints, method and notes.
87
+ No original-image crop location, scale search, Poisson recoloring, or
88
+ semantic reconstruction is performed. One image per input; split batches.
89
+
90
+ References (OpenCV is called as a dependency, not vendored C++ source):
91
+ https://docs.opencv.org/4.x/db/dda/classcv_1_1detail_1_1GraphCutSeamFinder.html
92
+ https://docs.opencv.org/4.x/d5/d4b/classcv_1_1detail_1_1MultiBandBlender.html
93
+ https://grail.cs.washington.edu/projects/photomontage/
94
+ """
95
+ from __future__ import annotations
96
+ import math
97
+ from functools import lru_cache
98
+ from typing import Callable, Tuple
99
+ import numpy as np
100
+ from scipy import sparse
101
+ from scipy.special import j1, jn_zeros
102
+ _MODES = ("EWA LanczosSharp", "EWA LanczosRadius / Jinc-Jinc")
103
+ _PAIR_BUDGET = 262144
104
+ _LUT_INTERVALS = 65536
105
+ _ALPHA_EPSILON = 1.0e-8
106
+
107
+ def _to_linear(rgb: np.ndarray) -> np.ndarray:
108
+ return np.where(
109
+ rgb <= 0.04045,
110
+ rgb / 12.92,
111
+ np.power((rgb + 0.055) / 1.055, 2.4),
112
+ ).astype(np.float32, copy=False)
113
+
114
+ def _to_srgb(rgb: np.ndarray) -> np.ndarray:
115
+ rgb = np.clip(rgb, 0.0, 1.0)
116
+ return np.where(
117
+ rgb <= 0.0031308,
118
+ rgb * 12.92,
119
+ 1.055 * np.power(rgb, 1.0 / 2.4) - 0.055,
120
+ ).astype(np.float32, copy=False)
121
+
122
+ def _prepare_image(source: np.ndarray) -> np.ndarray:
123
+ if not np.isfinite(source).all():
124
+ raise ValueError("Salia_Rescale_Image: image contains NaN or infinity.")
125
+ image = np.clip(source, 0.0, 1.0).astype(np.float32, copy=True)
126
+ image[..., :3] = _to_linear(image[..., :3])
127
+ if image.shape[-1] == 4:
128
+ image[..., :3] *= image[..., 3:4]
129
+ return image
130
+
131
+ def _finish_image(filtered: np.ndarray, channels: int) -> np.ndarray:
132
+ filtered = np.asarray(filtered, dtype=np.float32)
133
+ rgb = filtered[..., :3]
134
+ if channels == 4:
135
+ raw_alpha = filtered[..., 3:4]
136
+ visible = raw_alpha > _ALPHA_EPSILON
137
+ # Divide by the ORIGINAL filtered alpha, before clipping alpha.
138
+ # Negative-lobe filters can overshoot 1; clipping before division
139
+ # would alter the color of those pixels.
140
+ straight_rgb = np.zeros_like(rgb)
141
+ np.divide(rgb, raw_alpha, out=straight_rgb, where=visible)
142
+ rgb = _to_srgb(straight_rgb)
143
+ alpha = np.where(visible, np.clip(raw_alpha, 0.0, 1.0), 0.0)
144
+ result = np.concatenate((rgb, alpha), axis=-1)
145
+ else:
146
+ result = _to_srgb(rgb)
147
+ return np.ascontiguousarray(np.clip(result, 0.0, 1.0), dtype=np.float32)
148
+
149
+ def _jinc(x: np.ndarray) -> np.ndarray:
150
+ # Normalized Jinc = 2 * J1(pi*x) / (pi*x), with value 1 at zero.
151
+ z = math.pi * np.asarray(x, dtype=np.float64)
152
+ result = np.ones_like(z)
153
+ np.divide(2.0 * j1(z), z, out=result, where=np.abs(z) > 1.0e-12)
154
+ return result
155
+
156
+ @lru_cache(maxsize=2)
157
+ def _ewa_definition(mode: str) -> Tuple[float, np.ndarray, float]:
158
+ first_zero, _, third_zero = jn_zeros(1, 3) / math.pi
159
+ if mode == _MODES[0]:
160
+ blur = 0.9812505644269356
161
+ elif mode == _MODES[1]:
162
+ blur = 3.0 / third_zero
163
+ else:
164
+ raise ValueError("Unknown EWA mode: " + str(mode))
165
+ radius = float(third_zero * blur)
166
+ squared_radius = radius * radius
167
+ # A uniform table in squared distance avoids per-weight square roots.
168
+ squared_distances = np.linspace(0.0, squared_radius, _LUT_INTERVALS + 1)
169
+ r = np.sqrt(squared_distances) / blur
170
+ table = _jinc(r) * _jinc(r * first_zero / third_zero)
171
+ table[-1] = 0.0
172
+ # Duplicate endpoint makes interpolation at/outside support safe.
173
+ table = np.concatenate((table, np.zeros(1, dtype=np.float64)))
174
+ table.setflags(write=False)
175
+ return radius, table, _LUT_INTERVALS / squared_radius
176
+
177
+ def _lookup_ewa(squared_distance: np.ndarray, table: np.ndarray,
178
+ table_scale: float) -> np.ndarray:
179
+ position = np.minimum(squared_distance * table_scale, float(_LUT_INTERVALS))
180
+ index = position.astype(np.intp)
181
+ fraction = position - index
182
+ return table[index] + fraction * (table[index + 1] - table[index])
183
+
184
+ def _ewa_axis(source_size: int, target_size: int,
185
+ radius: float) -> Tuple[np.ndarray, np.ndarray]:
186
+ ratio = source_size / target_size
187
+ footprint_scale = max(1.0, ratio)
188
+ support = radius * footprint_scale
189
+ centers = (np.arange(target_size, dtype=np.float64) + 0.5) * ratio - 0.5
190
+ # All source indices with nonzero support fit in this fixed-width table.
191
+ count = min(source_size, int(math.ceil(2.0 * support)) + 1)
192
+ starts = np.maximum(0, np.ceil(centers - support).astype(np.int64))
193
+ raw_indices = starts[:, None] + np.arange(count, dtype=np.int64)[None, :]
194
+ squared_distance = ((raw_indices - centers[:, None]) / footprint_scale) ** 2
195
+ valid = (raw_indices < source_size) & (squared_distance < radius * radius)
196
+ # Infinity is deliberately mapped to the zero endpoint of the lookup table.
197
+ squared_distance[~valid] = np.inf
198
+ indices = np.minimum(raw_indices, source_size - 1).astype(np.int32)
199
+ return indices, squared_distance
200
+
201
+ def _resize_ewa(image: np.ndarray, out_width: int, out_height: int,
202
+ definition: Tuple[float, np.ndarray, float],
203
+ x_axis: Tuple[np.ndarray, np.ndarray],
204
+ y_axis: Tuple[np.ndarray, np.ndarray],
205
+ interrupt: Callable[[], None],
206
+ progress: Callable[[float], None]) -> np.ndarray:
207
+ _, table, table_scale = definition
208
+ in_height, in_width, channels = image.shape
209
+ x_indices, x_squared = x_axis
210
+ y_indices, y_squared = y_axis
211
+ nx, ny = x_indices.shape[1], y_indices.shape[1]
212
+ total_pixels = out_width * out_height
213
+ output = np.empty((total_pixels, channels), dtype=np.float32)
214
+ # Match the float64 weights once; otherwise SciPy may repeatedly upcast
215
+ # the complete source image for every output tile.
216
+ source_flat = np.ascontiguousarray(image, dtype=np.float64).reshape(-1, channels)
217
+ pixel_block = max(1, min(2048, _PAIR_BUDGET // max(1, nx * ny)))
218
+ index_dtype = np.int32 if in_width * in_height < 2**31 else np.int64
219
+
220
+ for begin in range(0, total_pixels, pixel_block):
221
+ interrupt()
222
+ end = min(begin + pixel_block, total_pixels)
223
+ output_pixels = np.arange(begin, end, dtype=np.int64)
224
+ oy, ox = np.divmod(output_pixels, out_width)
225
+ count = end - begin
226
+ weighted_sum = np.zeros((count, channels), dtype=np.float64)
227
+ weight_sum = np.zeros(count, dtype=np.float64)
228
+ # Also tile source taps, so even a huge reduction to 1x1 doesn't
229
+ # allocate one enormous output_pixels x source_pixels weight array.
230
+ x_chunk = min(nx, max(1, _PAIR_BUDGET // count))
231
+ y_chunk = min(ny, max(1, _PAIR_BUDGET // (count * x_chunk)))
232
+
233
+ for ys in range(0, ny, y_chunk):
234
+ ye = min(ys + y_chunk, ny)
235
+ yi = y_indices[oy, ys:ye]
236
+ dy = y_squared[oy, ys:ye]
237
+ for xs in range(0, nx, x_chunk):
238
+ interrupt()
239
+ xe = min(xs + x_chunk, nx)
240
+ xi = x_indices[ox, xs:xe]
241
+ dx = x_squared[ox, xs:xe]
242
+ distance = dy[:, :, None] + dx[:, None, :]
243
+ weights = _lookup_ewa(distance, table, table_scale)
244
+ taps_per_pixel = (ye - ys) * (xe - xs)
245
+ flat_weights = weights.reshape(count, taps_per_pixel)
246
+ weight_sum += flat_weights.sum(axis=1)
247
+ source_indices = (
248
+ yi.astype(np.int64)[:, :, None] * in_width
249
+ + xi[:, None, :]
250
+ ).astype(index_dtype, copy=False)
251
+ row_pointers = np.arange(count + 1, dtype=np.int64) * taps_per_pixel
252
+ # Sparse multiply avoids materializing a large [pixel,tap,RGBA]
253
+ # gather. Each row is one target pixel's full 2-D footprint.
254
+ operator = sparse.csr_matrix(
255
+ (weights.ravel(), source_indices.ravel(), row_pointers),
256
+ shape=(count, in_width * in_height),
257
+ )
258
+ weighted_sum += operator @ source_flat
259
+ if not np.isfinite(weight_sum).all() or np.any(np.abs(weight_sum) < 1.0e-12):
260
+ raise RuntimeError("Salia_Rescale_Image: degenerate EWA normalization.")
261
+ output[begin:end] = weighted_sum / weight_sum[:, None]
262
+ progress(end / total_pixels)
263
+ return output.reshape(out_height, out_width, channels)
264
+
265
+ class EwaLanczosSharpResizer:
266
+ """Resample each requested size from the original prepared source.
267
+
268
+ Color preparation happens once. No candidate is resized from a previous
269
+ candidate. Axis tables are cheap and computed for each requested size.
270
+ The caller can cache finished candidate images when useful.
271
+ """
272
+ def __init__(self, source: np.ndarray, interrupt: Callable[[], None] = lambda: None):
273
+ source = np.asarray(source)
274
+ if source.ndim != 3 or source.shape[2] not in (3, 4) or min(source.shape[:2]) < 1:
275
+ raise ValueError("EWA source must be a nonempty HWC RGB/RGBA array.")
276
+ if not np.isfinite(source).all():
277
+ raise ValueError("EWA source contains NaN or infinity.")
278
+ self.source = np.ascontiguousarray(source, dtype=np.float32)
279
+ self.prepared = _prepare_image(self.source)
280
+ self.definition = _ewa_definition(_MODES[0])
281
+ self.interrupt = interrupt
282
+
283
+ def resize(self, width: int, height: int) -> np.ndarray:
284
+ if width < 1 or height < 1:
285
+ raise ValueError("EWA dimensions must be positive.")
286
+ self.interrupt()
287
+ if (int(width), int(height)) == (self.source.shape[1], self.source.shape[0]):
288
+ return self.source.copy()
289
+ radius = self.definition[0]
290
+ x_axis = _ewa_axis(self.source.shape[1], int(width), radius)
291
+ y_axis = _ewa_axis(self.source.shape[0], int(height), radius)
292
+ filtered = _resize_ewa(
293
+ self.prepared, int(width), int(height), self.definition,
294
+ x_axis, y_axis, self.interrupt, lambda value: None,
295
+ )
296
+ self.interrupt()
297
+ return _finish_image(filtered, self.source.shape[2])
298
+
299
+
300
+ def resize_rgba_ewa(source: np.ndarray, width: int, height: int,
301
+ interrupt: Callable[[], None] = lambda: None) -> np.ndarray:
302
+ """Standalone original-source EWA LanczosSharp resize (RGB or RGBA)."""
303
+ return EwaLanczosSharpResizer(source, interrupt).resize(width, height)
304
+
305
+
306
+ def _rgba_array(image, name="image"):
307
+ """Private normalized float32 copy; hidden RGB is retained but never scored."""
308
+ result = np.asarray(image)
309
+ if result.ndim != 3 or result.shape[-1] not in (3, 4):
310
+ raise ValueError("%s must be HWC RGB or RGBA." % name)
311
+ if min(result.shape[:2]) < 1:
312
+ raise ValueError("%s is empty." % name)
313
+ if not np.issubdtype(result.dtype, np.floating):
314
+ raise ValueError("%s must contain floating-point values in [0, 1]." % name)
315
+ if not np.isfinite(result).all():
316
+ raise ValueError("%s contains NaN or infinity." % name)
317
+ result = np.clip(result, 0.0, 1.0).astype(np.float32, copy=True)
318
+ if result.shape[-1] == 3:
319
+ result = np.concatenate((result, np.ones((*result.shape[:2], 1), np.float32)), axis=-1)
320
+ return np.ascontiguousarray(result)
321
+
322
+ def _tensor_image(image, name, mask=None):
323
+ import torch
324
+ if not isinstance(image, torch.Tensor) or image.ndim != 4 or image.shape[0] != 1:
325
+ raise ValueError("%s must be one IMAGE shaped [1,H,W,3 or 4]; split batches first." % name)
326
+ if not image.is_floating_point():
327
+ raise ValueError("%s must be a floating-point IMAGE." % name)
328
+ result = _rgba_array(image[0].detach().to(device="cpu", dtype=torch.float32).numpy(), name)
329
+ # Embedded alpha is authoritative for RGBA. Do not replace, multiply or
330
+ # invert it through the external MASK, even if that mask is connected.
331
+ if int(image.shape[-1]) == 4:
332
+ return result
333
+ if mask is not None:
334
+ if not isinstance(mask, torch.Tensor):
335
+ raise ValueError("%s_mask must be a ComfyUI MASK tensor." % name)
336
+ value = mask.detach().to(device="cpu", dtype=torch.float32).numpy()
337
+ if value.ndim == 3 and value.shape[0] == 1:
338
+ value = value[0]
339
+ if value.shape != result.shape[:2]:
340
+ # Standard Load Image's no-alpha placeholder is a 64x64 all-zero
341
+ # MASK even when its RGB image has another resolution.
342
+ if value.shape == (64, 64) and not np.any(value):
343
+ value = np.zeros(result.shape[:2], dtype=np.float32)
344
+ else:
345
+ raise ValueError("%s_mask resolution must match %s. Masks are not resized." % (name, name))
346
+ if not np.isfinite(value).all():
347
+ raise ValueError("%s_mask contains NaN or infinity." % name)
348
+ # RGB has no alpha: reconstruct it from the Comfy Load Image MASK.
349
+ result[..., 3] = 1.0 - np.clip(value, 0.0, 1.0)
350
+ # Preserve the fact that RGB had NO alpha. Promoting it here used to erase
351
+ # that information before crop-location diagnostics and recovery.
352
+ if mask is None and int(image.shape[-1]) == 3:
353
+ return np.ascontiguousarray(result[..., :3])
354
+ return result
355
+
356
+ def _ewa_axis_at_centers(source_size, centers, source_per_output_pixel, radius):
357
+ """EWA source taps for arbitrary source-coordinate pixel centers.
358
+
359
+ ``centers`` are source pixel-center coordinates (first center is zero).
360
+ Footprint widening uses the continuous source/output scale, independent
361
+ of how many destination pixels happen to survive canvas clipping.
362
+ """
363
+ centers = np.asarray(centers, dtype=np.float64)
364
+ footprint = max(1.0, float(source_per_output_pixel))
365
+ support = radius * footprint
366
+ count = min(int(source_size), int(math.ceil(2.0 * support)) + 1)
367
+ starts = np.maximum(0, np.ceil(centers - support).astype(np.int64))
368
+ raw_indices = starts[:, None] + np.arange(count, dtype=np.int64)[None, :]
369
+ squared_distance = ((raw_indices - centers[:, None]) / footprint) ** 2
370
+ valid = (raw_indices < source_size) & (squared_distance < radius * radius)
371
+ squared_distance[~valid] = np.inf
372
+ indices = np.minimum(raw_indices, source_size - 1).astype(np.int32)
373
+ return indices, squared_distance
374
+
375
+ def render_rgba_ewa(source, background_width, background_height,
376
+ center_x, center_y, width, height, interrupt=lambda: None):
377
+ """Render a continuous RGBA rectangle from its original source, once.
378
+
379
+ Return ``(patch_rgba, x0, y0)``. ``x0/y0`` are integer pixel-edge canvas
380
+ coordinates; ``patch_rgba`` contains only the clipped destination bbox.
381
+ The requested rectangle has floating pixel-edge bounds
382
+ ``[center_x-width/2, center_x+width/2]`` and corresponding Y bounds.
383
+ Destination pixel centers are inverse-mapped directly to the original
384
+ source. RGB filtering is linear-light and alpha-premultiplied. Source
385
+ edge taps are truncated and normalized just as in the original rescaler.
386
+
387
+ The fraction of a destination pixel covered by the rectangle multiplies
388
+ filtered alpha (not RGB). This antialiases fractional outer bounds without
389
+ darkening their straight RGB. Filtering and placement stay continuous;
390
+ width, height and position are never rounded to an integer transform.
391
+
392
+ For integer-aligned geometry the output is the original resizer's output
393
+ cropped to the background. Same-size integer-aligned requests are exact
394
+ no-ops on pixel values, including hidden RGB, matching that resizer.
395
+ Empty overlap returns float32 array ``(0,0,4)`` and a clipped origin.
396
+ """
397
+ interrupt()
398
+ source = np.asarray(source)
399
+ if (source.ndim != 3 or source.shape[-1] != 4
400
+ or min(source.shape[:2]) < 1
401
+ or not np.issubdtype(source.dtype, np.floating)):
402
+ raise ValueError("render_rgba_ewa expects a nonempty floating HWC RGBA image.")
403
+ geometry = np.asarray((center_x, center_y, width, height), dtype=np.float64)
404
+ if not np.isfinite(geometry).all() or width < 1.0 or height < 1.0:
405
+ raise ValueError("EWA rectangle geometry must be finite with width/height >= 1.")
406
+ if (isinstance(background_width, bool) or isinstance(background_height, bool)
407
+ or int(background_width) != background_width
408
+ or int(background_height) != background_height
409
+ or background_width < 1 or background_height < 1):
410
+ raise ValueError("EWA background dimensions must be positive integers.")
411
+ if not np.isfinite(source).all():
412
+ raise ValueError("EWA source contains NaN or infinity.")
413
+ source = np.ascontiguousarray(np.clip(source, 0.0, 1.0), dtype=np.float32)
414
+ bw, bh = int(background_width), int(background_height)
415
+ left, top = float(center_x - width / 2.0), float(center_y - height / 2.0)
416
+ right, bottom = left + float(width), top + float(height)
417
+ x0 = max(0, min(bw, int(math.floor(left))))
418
+ y0 = max(0, min(bh, int(math.floor(top))))
419
+ x1 = max(0, min(bw, int(math.ceil(right))))
420
+ y1 = max(0, min(bh, int(math.ceil(bottom))))
421
+ if x1 <= x0 or y1 <= y0:
422
+ return np.empty((0, 0, 4), dtype=np.float32), x0, y0
423
+ source_height, source_width = source.shape[:2]
424
+ if (float(width) == source_width and float(height) == source_height
425
+ and left == math.floor(left) and top == math.floor(top)):
426
+ return (source[y0 - int(top):y1 - int(top),
427
+ x0 - int(left):x1 - int(left)].copy(), x0, y0)
428
+
429
+ definition = _ewa_definition(_MODES[0])
430
+ ratio_x, ratio_y = source_width / float(width), source_height / float(height)
431
+ output_x = np.arange(x0, x1, dtype=np.float64)
432
+ output_y = np.arange(y0, y1, dtype=np.float64)
433
+ source_x = (output_x + 0.5 - left) * ratio_x - 0.5
434
+ source_y = (output_y + 0.5 - top) * ratio_y - 0.5
435
+ x_axis = _ewa_axis_at_centers(source_width, source_x, ratio_x, definition[0])
436
+ y_axis = _ewa_axis_at_centers(source_height, source_y, ratio_y, definition[0])
437
+ filtered = _resize_ewa(_prepare_image(source), x1 - x0, y1 - y0,
438
+ definition, x_axis, y_axis, interrupt, lambda value: None)
439
+ result = _finish_image(filtered, 4)
440
+ coverage_x = np.maximum(0.0, np.minimum(output_x + 1.0, right)
441
+ - np.maximum(output_x, left))
442
+ coverage_y = np.maximum(0.0, np.minimum(output_y + 1.0, bottom)
443
+ - np.maximum(output_y, top))
444
+ result[..., 3] *= (coverage_y[:, None] * coverage_x[None, :]).astype(np.float32)
445
+ result[..., :3][result[..., 3] <= 0.0] = 0.0
446
+ interrupt()
447
+ return np.ascontiguousarray(result, dtype=np.float32), x0, y0
448
+
449
+ """OpenCV-backed constrained graph-cut seam helpers (no node registration)."""
450
+
451
+ import numpy as np
452
+
453
+
454
+ def rgba_disagreement(background, overlay):
455
+ """Visible disagreement in [0,1], independent of RGB beneath alpha zero.
456
+
457
+ Inputs: equal HWC straight RGBA float arrays in the same color space.
458
+ Compute the RMS difference of their RGB composites over both black and
459
+ white. This is a genuine alpha-aware cost; a single grey matte would miss
460
+ alpha differences whenever a foreground color equals that grey.
461
+ """
462
+ a = np.asarray(background, dtype=np.float64)
463
+ b = np.asarray(overlay, dtype=np.float64)
464
+ if a.shape != b.shape or a.ndim != 3 or a.shape[-1] != 4:
465
+ raise ValueError("RGBA disagreement requires equal HWC RGBA arrays.")
466
+ if not np.isfinite(a).all() or not np.isfinite(b).all():
467
+ raise ValueError("RGBA disagreement inputs must be finite.")
468
+ a, b = np.clip(a, 0.0, 1.0), np.clip(b, 0.0, 1.0)
469
+ delta_premultiplied = b[..., :3] * b[..., 3:4] - a[..., :3] * a[..., 3:4]
470
+ delta_alpha = b[..., 3:4] - a[..., 3:4]
471
+ squared = 0.5 * (np.mean(delta_premultiplied ** 2, axis=-1)
472
+ + np.mean((delta_premultiplied - delta_alpha) ** 2, axis=-1))
473
+ return np.ascontiguousarray(np.sqrt(np.clip(squared, 0.0, 1.0)), dtype=np.float32)
474
+
475
+
476
+ def graphcut_mask(error, core, frame, interrupt=lambda: None):
477
+ """Return bool mask selecting the overlay, using OpenCV's actual solver.
478
+
479
+ ``error`` is a finite HxW scalar disagreement in [0,1]. ``core`` forces
480
+ edited/overlay pixels and ``frame`` forces original/background pixels.
481
+ Availability masks are geometry constraints, NOT image alpha: a source
482
+ pixel with alpha zero may intentionally erase an old opaque pixel.
483
+
484
+ OpenCV's GraphCutSeamFinder expects three-channel float cost images. Its
485
+ COST_COLOR edge term is norm(image1[p]-image2[p]) plus the same at q plus
486
+ one. Feed zero versus repeated(error*255/sqrt(3)) to make its color norm
487
+ exactly the caller's RGBA-aware error. COST_COLOR_GRAD is deliberately
488
+ not used: gradients of an error proxy are not gradients of the artwork.
489
+ This calls the existing OpenCV graph-cut implementation; it does not
490
+ reimplement max-flow/min-cut in Python.
491
+ """
492
+ error = np.asarray(error, dtype=np.float32)
493
+ core, frame = np.asarray(core, dtype=bool), np.asarray(frame, dtype=bool)
494
+ if error.ndim != 2 or min(error.shape) < 1 or core.shape != error.shape or frame.shape != error.shape:
495
+ raise ValueError("Graph-cut error/core/frame must have matching nonempty HxW shapes.")
496
+ if not np.isfinite(error).all():
497
+ raise ValueError("Graph-cut error contains NaN or infinity.")
498
+ if np.any(core & frame):
499
+ raise ValueError("Protected overlay core and background frame must not overlap.")
500
+ if not np.any(core):
501
+ raise ValueError("Graph-cut requires at least one protected overlay core pixel.")
502
+ try:
503
+ import cv2
504
+ except ImportError as exc:
505
+ raise RuntimeError("OpenCV is required. Install opencv-python-headless in ComfyUI's Python environment.") from exc
506
+ constructor = getattr(getattr(cv2, "detail", None), "GraphCutSeamFinder", None)
507
+ if constructor is None:
508
+ constructor = getattr(cv2, "detail_GraphCutSeamFinder", None)
509
+ if constructor is None:
510
+ raise RuntimeError("This OpenCV build does not include GraphCutSeamFinder (stitching module).")
511
+
512
+ interrupt()
513
+ h, w = error.shape
514
+ first = np.zeros((h, w, 3), dtype=np.float32)
515
+ scalar = np.clip(error, 0.0, 1.0) * np.float32(255.0 / np.sqrt(3.0))
516
+ second = np.ascontiguousarray(np.repeat(scalar[..., None], 3, axis=-1))
517
+ background_available = np.full((h, w), 255, dtype=np.uint8)
518
+ overlay_available = np.full((h, w), 255, dtype=np.uint8)
519
+ background_available[core] = 0
520
+ overlay_available[frame] = 0
521
+ # UMat makes the in/out mutation behavior reliable across Python builds.
522
+ masks = [cv2.UMat(background_available), cv2.UMat(overlay_available)]
523
+ finder = constructor("COST_COLOR", 1.0e7, 0.0)
524
+ changed = finder.find([cv2.UMat(first), cv2.UMat(second)], [(0, 0), (0, 0)], masks)
525
+ if changed is not None:
526
+ masks = list(changed)
527
+ selected = masks[1].get() if hasattr(masks[1], "get") else np.asarray(masks[1])
528
+ selected = np.asarray(selected) > 0
529
+ # Explicit final constraints also guard against changed OpenCV penalties
530
+ # or precision behavior in alternative builds.
531
+ selected[core] = True
532
+ selected[frame] = False
533
+ interrupt()
534
+ return np.ascontiguousarray(selected)
535
+
536
+
537
+ """OpenCV MultiBandBlender adapter for premultiplied linear RGBA fields.
538
+
539
+ This is a fragment for the separate blending node, not a node registration.
540
+ The helper imports cv2 lazily. NumPy must already be installed.
541
+ """
542
+
543
+ import numpy as np
544
+
545
+
546
+ def opencv_multiband_fields(background_fields, source_fields, source_weight,
547
+ bands=4, interrupt=lambda: None):
548
+ """Blend ``[linear-premultiplied R,G,B,alpha]`` arrays with OpenCV.
549
+
550
+ Inputs are equally sized HWC float arrays; source_weight is HW in [0,1]
551
+ and is independent of either image's alpha. The source fields must be
552
+ UNCOVERED fields (placement coverage is already part of source_weight).
553
+
554
+ Uses the actual cv2.detail.MultiBandBlender implementation in two passes:
555
+ premultiplied RGB, then alpha in two channels plus a unit calibration
556
+ channel. OpenCV requires
557
+ signed 16-bit 3-channel images and 8-bit masks. Values are quantized at
558
+ scale 32767 internally; output remains float32 premultiplied RGBA fields.
559
+ The caller should constrain any pyramid overshoot, unpremultiply, and
560
+ restamp protected/source or unchanged/background regions as appropriate.
561
+
562
+ This helper does not impose locality: multiband filters can affect pixels
563
+ outside a simple mask's transition. The caller owns the permitted region.
564
+ """
565
+ interrupt()
566
+ background = np.asarray(background_fields)
567
+ source = np.asarray(source_fields)
568
+ weight = np.asarray(source_weight)
569
+ if (background.ndim != 3 or background.shape[-1] != 4
570
+ or source.shape != background.shape or min(background.shape[:2]) < 1):
571
+ raise ValueError("Multiband fields must be equal, nonempty HWC four-channel arrays.")
572
+ if weight.shape != background.shape[:2]:
573
+ raise ValueError("Multiband source_weight must match the image H/W.")
574
+ if (not np.issubdtype(background.dtype, np.floating)
575
+ or not np.issubdtype(source.dtype, np.floating)
576
+ or not np.issubdtype(weight.dtype, np.floating)):
577
+ raise ValueError("Multiband fields and source_weight must be floating point.")
578
+ if (not np.isfinite(background).all() or not np.isfinite(source).all()
579
+ or not np.isfinite(weight).all()):
580
+ raise ValueError("Multiband fields contain NaN or infinity.")
581
+ if isinstance(bands, bool) or not isinstance(bands, (int, np.integer)) or not 0 <= bands <= 10:
582
+ raise ValueError("Multiband bands must be an integer from 0 to 10.")
583
+ background = np.ascontiguousarray(np.clip(background, 0.0, 1.0), dtype=np.float32)
584
+ source = np.ascontiguousarray(np.clip(source, 0.0, 1.0), dtype=np.float32)
585
+ weight = np.clip(weight, 0.0, 1.0)
586
+ if not np.any(weight > 0.0):
587
+ return background.copy()
588
+ if not np.any(weight < 1.0):
589
+ return source.copy()
590
+ if np.array_equal(background, source):
591
+ return background.copy()
592
+
593
+ import cv2
594
+ factory = getattr(cv2, "detail_MultiBandBlender", None)
595
+ if factory is None:
596
+ factory = getattr(getattr(cv2, "detail", None), "MultiBandBlender", None)
597
+ if factory is None:
598
+ raise RuntimeError("This OpenCV build does not provide MultiBandBlender.")
599
+
600
+ height, width = background.shape[:2]
601
+ # Quantize once and construct the complement from the same integer mask.
602
+ # This guarantees masks sum to 255 instead of independent rounding gaps.
603
+ source_mask = np.ascontiguousarray(np.rint(weight * 255.0), dtype=np.uint8)
604
+ background_mask = np.ascontiguousarray(255 - source_mask, dtype=np.uint8)
605
+ if not np.any(source_mask):
606
+ return background.copy()
607
+ if np.all(source_mask == 255):
608
+ return source.copy()
609
+ value_scale = 32767.0
610
+
611
+ def blend_three(background_three, source_three):
612
+ interrupt()
613
+ blender = factory()
614
+ blender.setNumBands(int(bands))
615
+ blender.prepare((0, 0, int(width), int(height)))
616
+ for values, mask in ((background_three, background_mask), (source_three, source_mask)):
617
+ interrupt()
618
+ # MultiBandBlender accepts CV_16SC3 rather than float color input.
619
+ quantized = np.ascontiguousarray(np.rint(values * value_scale), dtype=np.int16)
620
+ blender.feed(quantized, mask, (0, 0))
621
+ interrupt()
622
+ output, _output_mask = blender.blend(None, None)
623
+ interrupt()
624
+ output = np.asarray(output)
625
+ if output.shape != (height, width, 3):
626
+ raise RuntimeError("OpenCV returned an unexpected multiband image shape.")
627
+ return output.astype(np.float32) / value_scale
628
+
629
+ premultiplied_rgb = blend_three(background[..., :3], source[..., :3])
630
+ # The 16-bit implementation can produce a small mask-dependent gain
631
+ # bias even for constant unit input. Carry a unit field alongside alpha
632
+ # to measure/correct it using the very same masks and pyramid levels.
633
+ ones = np.ones((height, width, 1), dtype=np.float32)
634
+ background_alpha = np.concatenate((background[..., 3:4], background[..., 3:4], ones), axis=-1)
635
+ source_alpha = np.concatenate((source[..., 3:4], source[..., 3:4], ones), axis=-1)
636
+ alpha_three = blend_three(background_alpha, source_alpha)
637
+ gain = alpha_three[..., 2:3]
638
+ if not np.isfinite(gain).all() or np.any(gain <= 0.0):
639
+ raise RuntimeError("OpenCV multiband normalization failed.")
640
+ premultiplied_rgb = premultiplied_rgb / gain
641
+ alpha = alpha_three[..., :1] / gain
642
+ if np.array_equal(background[..., 3], source[..., 3]):
643
+ alpha = background[..., 3:4].copy()
644
+ return np.ascontiguousarray(np.concatenate((premultiplied_rgb, alpha), axis=-1), dtype=np.float32)
645
+
646
+
647
+ # ---------------------------------------------------------------------------
648
+ # Placement, constraints, feathering and RGBA replacement composition.
649
+ # ---------------------------------------------------------------------------
650
+
651
+ import logging
652
+ import time
653
+ from scipy.ndimage import distance_transform_edt, binary_erosion
654
+
655
+ _LOG = logging.getLogger(__name__)
656
+ _MAX_GRAPH_SIDE = 1536
657
+ _BLEND_MODES = ("Graph cut + feather", "Graph cut + multiband")
658
+ _OVERLAY_MODES = ("Original crop", "Aligned canvas")
659
+
660
+
661
+ def _coverage(x, y, width, height, x0, y0, out_width, out_height):
662
+ px = np.arange(x0, x0 + out_width, dtype=np.float64)
663
+ py = np.arange(y0, y0 + out_height, dtype=np.float64)
664
+ kx = np.maximum(0.0, np.minimum(px + 1.0, x + width) - np.maximum(px, x))
665
+ ky = np.maximum(0.0, np.minimum(py + 1.0, y + height) - np.maximum(py, y))
666
+ return np.ascontiguousarray(ky[:, None] * kx[None, :], dtype=np.float32)
667
+
668
+
669
+ def _placed_overlay(background, overlay, x, y, width, height, overlay_mode, interrupt):
670
+ bh, bw = background.shape[:2]
671
+ if overlay_mode == _OVERLAY_MODES[0]:
672
+ patch, x0, y0 = render_rgba_ewa(overlay, bw, bh, x + width / 2.0,
673
+ y + height / 2.0, width, height, interrupt)
674
+ else:
675
+ if overlay.shape[:2] != background.shape[:2]:
676
+ raise ValueError("Aligned canvas mode requires overlay resolution to equal background resolution.")
677
+ x0, y0 = max(0, min(bw, int(math.floor(x)))), max(0, min(bh, int(math.floor(y))))
678
+ x1, y1 = max(0, min(bw, int(math.ceil(x + width)))), max(0, min(bh, int(math.ceil(y + height))))
679
+ patch = overlay[y0:y1, x0:x1].copy()
680
+ if not patch.size:
681
+ return patch, np.empty((0, 0), np.float32), x0, y0
682
+ k = _coverage(x, y, width, height, x0, y0, patch.shape[1], patch.shape[0])
683
+ # The alignment node's EWA renderer includes geometric pixel coverage in
684
+ # output alpha. Undo ONLY that coverage for replacement interpolation.
685
+ # Keep coverage k separately: alpha zero inside the rectangle may erase.
686
+ uncovered = patch.copy()
687
+ np.divide(patch[..., 3], k, out=uncovered[..., 3], where=k > 0.0)
688
+ uncovered[..., 3] = np.clip(uncovered[..., 3], 0.0, 1.0)
689
+ return uncovered, k, x0, y0
690
+
691
+
692
+ def _protect_on_patch(mask, background_shape, overlay_shape, x, y, width, height,
693
+ x0, y0, k, interrupt):
694
+ value = np.asarray(mask)
695
+ if value.ndim == 3 and value.shape[0] == 1:
696
+ value = value[0]
697
+ if value.ndim != 2 or not np.isfinite(value).all():
698
+ raise ValueError("protect_mask must be one finite HxW MASK; white means keep edited pixels.")
699
+ value = np.clip(value, 0.0, 1.0).astype(np.float32)
700
+ if value.shape == background_shape[:2]:
701
+ return value[y0:y0 + k.shape[0], x0:x0 + k.shape[1]] > 0.5
702
+ if value.shape == overlay_shape[:2]:
703
+ rgba = np.zeros((*value.shape, 4), np.float32)
704
+ rgba[..., 3] = value
705
+ rendered, mx, my = render_rgba_ewa(rgba, background_shape[1], background_shape[0],
706
+ x + width / 2.0, y + height / 2.0,
707
+ width, height, interrupt)
708
+ if (mx, my) != (x0, y0) or rendered.shape[:2] != k.shape:
709
+ raise RuntimeError("Protection mask placement disagrees with overlay placement.")
710
+ alpha = np.zeros_like(k)
711
+ np.divide(rendered[..., 3], k, out=alpha, where=k > 0.0)
712
+ return alpha > 0.5
713
+ raise ValueError("protect_mask resolution must match background or the original overlay.")
714
+
715
+
716
+ def _linear_fields(rgba):
717
+ alpha = rgba[..., 3:4]
718
+ return np.concatenate((_to_linear(rgba[..., :3]) * alpha, alpha), axis=-1)
719
+
720
+
721
+ def _seam_cost(background, overlay, gradient_weight):
722
+ # Compare appearance over BOTH black and white, so neither hidden RGB
723
+ # nor a lucky match to a single matte can hide an alpha disagreement.
724
+ a, b = background.copy(), overlay.copy()
725
+ a[..., :3], b[..., :3] = _to_linear(a[..., :3]), _to_linear(b[..., :3])
726
+ error = rgba_disagreement(a, b)
727
+ if gradient_weight > 0.0 and min(error.shape) >= 2:
728
+ af, bf = _linear_fields(background), _linear_fields(overlay)
729
+ ady, adx = np.gradient(af, axis=(0, 1))
730
+ bdy, bdx = np.gradient(bf, axis=(0, 1))
731
+ gradient_error = np.sqrt(np.mean((ady - bdy) ** 2 + (adx - bdx) ** 2, axis=-1))
732
+ error = (error + float(gradient_weight) * gradient_error) / (1.0 + float(gradient_weight))
733
+ return np.ascontiguousarray(np.clip(error, 0.0, 1.0), dtype=np.float32)
734
+
735
+
736
+ def _bounded_graphcut(error, core, frame, interrupt):
737
+ h, w = error.shape
738
+ if max(h, w) <= _MAX_GRAPH_SIDE:
739
+ return graphcut_mask(error, core, frame, interrupt), (w, h)
740
+ import cv2
741
+ ratio = _MAX_GRAPH_SIDE / max(h, w)
742
+ gw, gh = max(3, int(round(w * ratio))), max(3, int(round(h * ratio)))
743
+ small_error = cv2.resize(error, (gw, gh), interpolation=cv2.INTER_AREA)
744
+ small_core = cv2.resize(core.astype(np.float32), (gw, gh), interpolation=cv2.INTER_AREA) > 0.0
745
+ small_frame = cv2.resize(frame.astype(np.float32), (gw, gh), interpolation=cv2.INTER_AREA) > 0.0
746
+ small_frame &= ~small_core
747
+ small = graphcut_mask(small_error, small_core, small_frame, interrupt)
748
+ result = cv2.resize(small.astype(np.uint8), (w, h), interpolation=cv2.INTER_NEAREST) > 0
749
+ result[core], result[frame] = True, False
750
+ return result, (gw, gh)
751
+
752
+
753
+ def _feather_seam(selected, core, frame, radius):
754
+ if radius <= 0 or np.all(selected) or not np.any(selected):
755
+ result = selected.astype(np.float32)
756
+ else:
757
+ signed = distance_transform_edt(selected) - distance_transform_edt(~selected)
758
+ # The seam lies halfway between the two adjacent pixel centers.
759
+ signed = np.sign(signed) * np.maximum(0.0, np.abs(signed) - 0.5)
760
+ t = np.clip(0.5 + signed / (2.0 * float(radius)), 0.0, 1.0)
761
+ result = (t * t * (3.0 - 2.0 * t)).astype(np.float32)
762
+ result[core], result[frame] = 1.0, 0.0
763
+ return result
764
+
765
+
766
+ def _replace_rgba(background, source, weight, coverage, blend_mode, bands, interrupt):
767
+ # k is rectangle coverage, a is object alpha, w is seam preference.
768
+ # Keep them distinct. Source-over would thicken an unchanged alpha edge.
769
+ effective = np.clip(weight * coverage, 0.0, 1.0)
770
+ bg_fields, src_fields = _linear_fields(background), _linear_fields(source)
771
+ base = bg_fields * (1.0 - effective[..., None]) + src_fields * effective[..., None]
772
+ fields = base.copy()
773
+ if blend_mode == _BLEND_MODES[1] and np.any((effective > 0.0) & (effective < 1.0)):
774
+ mb = opencv_multiband_fields(bg_fields, src_fields, effective, bands, interrupt)
775
+ # Keep exact replacement alpha so Laplacian bands do not spread the
776
+ # transparent silhouette. Multiband adjusts color in the transition.
777
+ mb_rgb = np.zeros_like(mb[..., :3])
778
+ np.divide(mb[..., :3], mb[..., 3:4], out=mb_rgb,
779
+ where=mb[..., 3:4] > _ALPHA_EPSILON)
780
+ mb_premultiplied = np.clip(mb_rgb, 0.0, 1.0) * base[..., 3:4]
781
+ gate = 4.0 * effective * (1.0 - effective)
782
+ fields[..., :3] += gate[..., None] * (mb_premultiplied - base[..., :3])
783
+ fields[..., 3] = np.clip(base[..., 3], 0.0, 1.0)
784
+ fields[..., :3] = np.clip(fields[..., :3], 0.0, fields[..., 3:4])
785
+ result = _finish_image(fields, 4)
786
+ # Keep unselected original pixels byte-for-byte. Full-source pixels stay
787
+ # byte-for-byte equal to the placed source; identical pairs stay identical.
788
+ result[effective <= 0.0] = background[effective <= 0.0]
789
+ result[effective >= 1.0] = source[effective >= 1.0]
790
+ # Original-source EWA can leave ~1 ULP in a constant field. Do not let
791
+ # OpenCV's internal 16-bit color path amplify that into a visible change.
792
+ # Fully selected source/core pixels keep their exact placed-source values.
793
+ identical = (np.all(np.abs(source - background) <= 2.0e-7, axis=-1)
794
+ & (effective < 1.0))
795
+ result[identical] = background[identical]
796
+ return result, effective
797
+
798
+
799
+ def blend_arrays(background, overlay, x, y, width, height, seam_band=48,
800
+ feather_pixels=4, blend_mode="Graph cut + feather", multiband_bands=4,
801
+ gradient_weight=0.35, overlay_mode="Original crop", protect_mask=None,
802
+ interrupt=lambda: None, progress=lambda value: None):
803
+ """Pure NumPy/OpenCV core; background/overlay are HWC RGB or straight RGBA."""
804
+ started = time.monotonic()
805
+ geometry = np.asarray((x, y, width, height), dtype=np.float64)
806
+ if (not np.isfinite(geometry).all() or width < 1.0 or height < 1.0
807
+ or width > 32768 or height > 32768):
808
+ raise ValueError("x/y must be finite and width/height must be in [1,32768]. Connect valid alignment coordinates.")
809
+ x, y, width, height = map(float, geometry)
810
+ for name, value, lo, hi in (("seam_band", seam_band, 1, 4096),
811
+ ("feather_pixels", feather_pixels, 0, 256),
812
+ ("multiband_bands", multiband_bands, 1, 8)):
813
+ if isinstance(value, bool) or not isinstance(value, (int, np.integer)) or not lo <= value <= hi:
814
+ raise ValueError("%s must be an integer in [%d,%d]." % (name, lo, hi))
815
+ if not math.isfinite(float(gradient_weight)) or not 0.0 <= gradient_weight <= 4.0:
816
+ raise ValueError("gradient_weight must be finite and in [0,4].")
817
+ if blend_mode not in _BLEND_MODES or overlay_mode not in _OVERLAY_MODES:
818
+ raise ValueError("Unknown blend_mode or overlay_mode.")
819
+ background, overlay = _rgba_array(background, "background"), _rgba_array(overlay, "overlay")
820
+ interrupt()
821
+ source, k, x0, y0 = _placed_overlay(background, overlay, x, y, width, height, overlay_mode, interrupt)
822
+ output = background.copy()
823
+ full_mask = np.zeros(background.shape[:2], dtype=np.float32)
824
+ preview = background.copy()
825
+ notes = []
826
+ if not source.size:
827
+ progress(100)
828
+ return {"composite": output, "blend_mask": full_mask, "seam_preview": preview,
829
+ "status": "NO OVERLAP: rectangle lies outside background; background unchanged."}
830
+ progress(35)
831
+ h, w = k.shape
832
+ roi = background[y0:y0 + h, x0:x0 + w]
833
+ valid = k > 0.0
834
+ inward = distance_transform_edt(np.pad(valid, 1, constant_values=False))[1:-1, 1:-1]
835
+ thin = min(h, w) < 3
836
+ # Keep a substantial automatic edit core even for a small crop whose
837
+ # requested border width exceeds the available space.
838
+ actual_band = min(int(seam_band), max(1, int(math.floor(inward.max() / 2.0))))
839
+ if protect_mask is None:
840
+ core = (inward > actual_band) & valid
841
+ else:
842
+ core = _protect_on_patch(protect_mask, background.shape, overlay.shape,
843
+ x, y, width, height, x0, y0, k, interrupt) & valid
844
+ if not np.any(core):
845
+ raise ValueError("protect_mask has no white pixels inside the placed rectangle.")
846
+ notes.append("protect_mask overrides the automatic rectangular edit core; white pixels force the edited version.")
847
+ if thin:
848
+ core = valid.copy()
849
+ frame = np.zeros_like(valid)
850
+ selected = valid.copy()
851
+ graph_shape = (0, 0)
852
+ notes.append("Rectangle is too thin for an interior seam; using direct RGBA replacement with pixel coverage.")
853
+ else:
854
+ frame = ((inward <= 1.0) | ~valid) & ~core
855
+ if not np.any(core):
856
+ core.flat[int(np.argmax(inward))] = True
857
+ frame[core] = False
858
+ if not np.any(frame):
859
+ selected = valid.copy()
860
+ graph_shape = (0, 0)
861
+ notes.append("Protected core covers the full visible rectangle; seam optimization not needed.")
862
+ else:
863
+ error = _seam_cost(roi, source, gradient_weight)
864
+ selected, graph_shape = _bounded_graphcut(error, core, frame, interrupt)
865
+ selected &= valid
866
+ interrupt()
867
+ progress(65)
868
+ weight = _feather_seam(selected, core, frame, feather_pixels)
869
+ weight[~valid] = 0.0
870
+ blended, effective = _replace_rgba(roi, source, weight, k, blend_mode, multiband_bands, interrupt)
871
+ output[y0:y0 + h, x0:x0 + w] = blended
872
+ full_mask[y0:y0 + h, x0:x0 + w] = effective
873
+ preview[:] = output
874
+ seam_edge = selected ^ binary_erosion(selected, border_value=0)
875
+ core_edge = core ^ binary_erosion(core, border_value=0)
876
+ marks = preview[y0:y0 + h, x0:x0 + w]
877
+ marks[seam_edge] = (0.0, 1.0, 0.0, 1.0)
878
+ marks[core_edge] = (0.0, 0.7, 1.0, 1.0)
879
+ if actual_band != seam_band:
880
+ notes.append("seam_band reduced to %d pixels to retain a protected center in this rectangle." % actual_band)
881
+ if x < 0 or y < 0 or x + width > background.shape[1] or y + height > background.shape[0]:
882
+ notes.append("Placement is clipped to background dimensions.")
883
+ if blend_mode == _BLEND_MODES[1]:
884
+ notes.append("OpenCV MultiBandBlender adjusts linear color only in the soft transition; replacement alpha and protected pixels remain exact.")
885
+ status = "\n".join([
886
+ "BLENDED: %s; RGBA replacement with independent placement coverage." % blend_mode,
887
+ "Overlay mode=%s; x=%.8f; y=%.8f; width=%.8f; height=%.8f" % (overlay_mode, x, y, width, height),
888
+ "Raster bbox=(%d,%d,%d,%d); protected pixels=%d; graph size=%dx%d; seam_band=%d; feather_pixels=%d"
889
+ % (x0, y0, w, h, int(core.sum()), *graph_shape, actual_band, feather_pixels),
890
+ "Seam weight is separate from object alpha. Transparent edited pixels can replace/erase old pixels; unchanged translucent pixels do not accumulate opacity.",
891
+ "Seam preview: green=selected seam; cyan=protected core boundary.",
892
+ *notes, "seconds=%.3f" % (time.monotonic() - started),
893
+ ])
894
+ progress(100)
895
+ return {"composite": output, "blend_mask": full_mask, "seam_preview": preview,
896
+ "status": status, "core": core, "frame": frame, "selected": selected,
897
+ "weight": weight, "coverage": k, "placed_source": source, "bbox": (x0, y0, w, h)}
898
+
899
+
900
+ class Salia_Seamless_Overlay_Blend:
901
+ @classmethod
902
+ def INPUT_TYPES(cls):
903
+ return {"required": {
904
+ "background": ("IMAGE", {"tooltip": "Original background, not the alignment node's composite."}),
905
+ "overlay": ("IMAGE", {"tooltip": "Original edited crop, or aligned overlay_canvas_rgba with Aligned canvas mode."}),
906
+ "x": ("FLOAT", {"default": 0.0, "min": -32768.0, "max": 32768.0, "forceInput": True,
907
+ "tooltip": "Connect alignment output x: fractional top-left X, not center."}),
908
+ "y": ("FLOAT", {"default": 0.0, "min": -32768.0, "max": 32768.0, "forceInput": True,
909
+ "tooltip": "Connect alignment output y: fractional top-left Y, not center."}),
910
+ "width": ("FLOAT", {"default": 550.0, "min": 1.0, "max": 32768.0, "forceInput": True,
911
+ "tooltip": "Connect alignment output width, retaining fractions."}),
912
+ "height": ("FLOAT", {"default": 550.0, "min": 1.0, "max": 32768.0, "forceInput": True,
913
+ "tooltip": "Connect alignment output height, retaining fractions."}),
914
+ "overlay_mode": (list(_OVERLAY_MODES), {"default": _OVERLAY_MODES[0],
915
+ "tooltip": "Original crop renders the original source at coordinates. Aligned canvas uses the already placed overlay without rescaling again."}),
916
+ "seam_band": ("INT", {"default": 48, "min": 1, "max": 4096, "step": 1,
917
+ "tooltip": "Adjustable border width in output pixels; center is kept from overlay, outer frame from background."}),
918
+ "feather_pixels": ("INT", {"default": 4, "min": 0, "max": 256, "step": 1,
919
+ "tooltip": "Narrow smooth transition across the chosen content-aware seam. 0 = hard seam."}),
920
+ "blend_mode": (list(_BLEND_MODES), {"default": _BLEND_MODES[0],
921
+ "tooltip": "Uses OpenCV GraphCutSeamFinder. Optional MultiBandBlender corrects transition colors while preserving replacement alpha."}),
922
+ "multiband_bands": ("INT", {"default": 4, "min": 1, "max": 8, "step": 1,
923
+ "tooltip": "Laplacian pyramid bands, used only in Graph cut + multiband mode."}),
924
+ "gradient_weight": ("FLOAT", {"default": 0.35, "min": 0.0, "max": 4.0, "step": 0.05,
925
+ "tooltip": "Penalty for disagreement in outlines/gradients when choosing the seam."}),
926
+ }, "optional": {
927
+ "background_mask": ("MASK", {"tooltip": "RGB only: standard Load Image MASK (1-alpha). RGBA uses its own alpha."}),
928
+ "overlay_mask": ("MASK", {"tooltip": "RGB only: standard Load Image MASK (1-alpha). RGBA uses its own alpha."}),
929
+ "protect_mask": ("MASK", {"tooltip": "White MUST use edited pixels, including transparent erasures. Overrides automatic core; match background or original overlay size."}),
930
+ }}
931
+
932
+ RETURN_TYPES = ("IMAGE", "MASK", "IMAGE", "STRING")
933
+ RETURN_NAMES = ("composite_rgba", "blend_mask", "seam_preview", "status")
934
+ FUNCTION = "blend"
935
+ CATEGORY = "Salia/Image"
936
+ DESCRIPTION = (
937
+ "Separate coordinate-driven overlay blender using OpenCV GraphCutSeamFinder and optional "
938
+ "MultiBandBlender. Protects the edited core, blends a content-aware seam, and preserves RGBA "
939
+ "transparency through premultiplied replacement. Connect x/y/width/height from alignment."
940
+ )
941
+
942
+ def blend(self, background, overlay, x, y, width, height, overlay_mode="Original crop",
943
+ seam_band=48, feather_pixels=4, blend_mode="Graph cut + feather",
944
+ multiband_bands=4, gradient_weight=0.35, background_mask=None,
945
+ overlay_mask=None, protect_mask=None):
946
+ import torch
947
+ try:
948
+ from comfy.model_management import throw_exception_if_processing_interrupted as interrupt
949
+ except ImportError:
950
+ interrupt = lambda: None
951
+ try:
952
+ from comfy.utils import ProgressBar
953
+ bar = ProgressBar(100)
954
+ progress = lambda value: bar.update_absolute(int(value), 100)
955
+ except ImportError:
956
+ progress = lambda value: None
957
+ protection = None
958
+ if protect_mask is not None:
959
+ if not isinstance(protect_mask, torch.Tensor):
960
+ raise ValueError("protect_mask must be a ComfyUI MASK tensor.")
961
+ protection = protect_mask.detach().to(device="cpu", dtype=torch.float32).numpy()
962
+ with torch.inference_mode():
963
+ result = blend_arrays(
964
+ _tensor_image(background, "background", background_mask),
965
+ _tensor_image(overlay, "overlay", overlay_mask),
966
+ x, y, width, height, seam_band=seam_band, feather_pixels=feather_pixels,
967
+ blend_mode=blend_mode, multiband_bands=multiband_bands,
968
+ gradient_weight=gradient_weight, overlay_mode=overlay_mode,
969
+ protect_mask=protection, interrupt=interrupt, progress=progress,
970
+ )
971
+ composite = torch.from_numpy(np.ascontiguousarray(result["composite"])).unsqueeze(0)
972
+ mask = torch.from_numpy(np.ascontiguousarray(result["blend_mask"])).unsqueeze(0)
973
+ preview = torch.from_numpy(np.ascontiguousarray(result["seam_preview"])).unsqueeze(0)
974
+ return composite, mask, preview, result["status"]
975
+
976
+
977
+ NODE_CLASS_MAPPINGS = {"Salia_Seamless_Overlay_Blend": Salia_Seamless_Overlay_Blend}
978
+ NODE_DISPLAY_NAME_MAPPINGS = {"Salia_Seamless_Overlay_Blend": "Salia Seamless Overlay Blend (OpenCV)"}