saliacoel commited on
Commit
55c9620
·
verified ·
1 Parent(s): f29842e

Upload Salia_RGBA_Self_Underlay.py

Browse files
Files changed (1) hide show
  1. Salia_RGBA_Self_Underlay.py +332 -0
Salia_RGBA_Self_Underlay.py ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ComfyUI: strengthen RGBA alpha with an outline-aware, colored underlay.
2
+
3
+ Install this standalone file in ComfyUI/custom_nodes/ and restart ComfyUI.
4
+ Node: Salia RGBA Self Underlay (Salia/Image).
5
+ The original Black Underlay file is NOT required and can remain installed.
6
+
7
+ Processing order:
8
+ 1. Run an embedded copy of the supplied SaliaRGBABlackUnderlay algorithm,
9
+ with its fixed alpha curve and its outline protection ON.
10
+ 2. Read that result's alpha: >= 0.0001 (0.01%) is black/visible; below that
11
+ is white/empty. This radical mask is ONLY a guide, never the output alpha.
12
+ 3. Peel six nonoverlapping one-pixel layers inward using all eight neighbors.
13
+ Transparent holes count as outlines. Canvas edges replicate their nearest
14
+ pixel, matching the original node; outside-canvas space is not extra white.
15
+ 4. Build a new underlay from the ORIGINAL input's RGB at each coordinate:
16
+ white / black+1 / black+2 / black+3: black
17
+ black+4: 50% grayscale + 50% black
18
+ black+5: grayscale
19
+ black+6: 50% grayscale + 50% original RGB
20
+ remaining black (interior): original RGB
21
+ Grayscale uses Rec.709 weights: 0.2126 R + 0.7152 G + 0.0722 B.
22
+ 5. Composite the ORIGINAL input over that underlay, using the original
23
+ strengthened underlay alpha, then apply always-on outline protection.
24
+
25
+ The preliminary black result supplies only the mask. It is not composited
26
+ again, so the alpha boost and darkening are not compounded.
27
+
28
+ Final outline controls (the preliminary reference pass stays fixed):
29
+ outline_strength: blend toward filtered coverage; higher = stronger smoothing.
30
+ outline_side_weight: weight of each horizontal/vertical neighbor.
31
+ outline_diagonal_weight: weight of each diagonal neighbor.
32
+ The center weight is 4. All controls stay positive; there is no off switch.
33
+ Defaults 1 / 2 / 1 reproduce the original normalized binomial 3x3 filter.
34
+ Existing visible RGB is preserved by that filter; newly covered pixels borrow
35
+ premultiplied neighbor color. Final alpha remains smooth, not binary.
36
+
37
+ Input/output: ComfyUI IMAGE [B,H,W,4], straight RGBA. RGB also works with an
38
+ optional ComfyUI transparency MASK (1 = transparent). Batch, dimensions,
39
+ device and floating-point dtype are preserved. The input is never modified.
40
+ Only PyTorch is required; it is already provided by ComfyUI.
41
+ """
42
+
43
+ import math
44
+
45
+ import torch
46
+ import torch.nn.functional as F
47
+
48
+
49
+ ALPHA_START_PERCENT = 50
50
+ AMPLITUDE = 0.5
51
+ RAMP_START_PERCENT = 1
52
+ RADICAL_ALPHA_THRESHOLD = 0.0001 # 0.01%, not 1%.
53
+
54
+
55
+ class _BlackUnderlayReference:
56
+ """Embedded supplied black-underlay math; never registered as a node."""
57
+
58
+ @staticmethod
59
+ def _protect_outline(
60
+ rgb, alpha, strength=1.0, side_weight=2.0, diagonal_weight=1.0,
61
+ ):
62
+ premultiplied = torch.cat((rgb * alpha, alpha), dim=-1)
63
+ plane = premultiplied.permute(0, 3, 1, 2)
64
+ kernel = plane.new_tensor([
65
+ [diagonal_weight, side_weight, diagonal_weight],
66
+ [side_weight, 4.0, side_weight],
67
+ [diagonal_weight, side_weight, diagonal_weight],
68
+ ]).reshape(1, 1, 3, 3) / (4.0 + 4.0 * side_weight + 4.0 * diagonal_weight)
69
+ kernel = kernel.expand(4, 1, 3, 3).contiguous()
70
+ filtered = F.conv2d(
71
+ F.pad(plane, (1, 1, 1, 1), mode="replicate"),
72
+ kernel, groups=4,
73
+ ).permute(0, 2, 3, 1)
74
+
75
+ filtered_alpha = filtered[..., 3:4]
76
+ safe_alpha = torch.where(
77
+ filtered_alpha > 0.0, filtered_alpha, torch.ones_like(filtered_alpha)
78
+ )
79
+ borrowed_rgb = filtered[..., :3] / safe_alpha
80
+ output_rgb = torch.where(alpha > 0.0, rgb, borrowed_rgb)
81
+ # Keep the default path exactly equal to the supplied node. For an
82
+ # originally empty pixel, strength cancels from borrowed RGB because
83
+ # both its filtered premultiplied color and coverage scale together.
84
+ if strength == 1.0:
85
+ output_alpha = filtered_alpha.clamp(0.0, 1.0)
86
+ else:
87
+ output_alpha = (
88
+ alpha * (1.0 - strength) + filtered_alpha * strength
89
+ ).clamp(0.0, 1.0)
90
+ output_rgb = torch.where(
91
+ output_alpha > 0.0, output_rgb, torch.zeros_like(output_rgb)
92
+ )
93
+ return output_rgb, output_alpha
94
+
95
+ @staticmethod
96
+ def _ease(t, amplitude):
97
+ strength = 4.0 * abs(amplitude)
98
+ if strength < 1e-6:
99
+ return t + (2.0 * amplitude) * t * (1.0 - t)
100
+ if math.isinf(strength) or strength > torch.finfo(t.dtype).max:
101
+ if amplitude > 0.0:
102
+ return torch.where(t > 0.0, torch.ones_like(t), torch.zeros_like(t))
103
+ return torch.where(t >= 1.0, torch.ones_like(t), torch.zeros_like(t))
104
+ normalized = -torch.expm1(-strength * t) / (-math.expm1(-strength))
105
+ if amplitude < 0.0:
106
+ return torch.exp(-strength * (1.0 - t)) * normalized
107
+ return normalized
108
+
109
+ @classmethod
110
+ def _black_alpha(cls, alpha, ramp_start_percent, alpha_start_percent, amplitude):
111
+ start = ramp_start_percent / 100.0
112
+ end = alpha_start_percent / 100.0
113
+ width = (alpha_start_percent - ramp_start_percent) / 100.0
114
+ t = ((alpha - start) / width).clamp(0.0, 1.0)
115
+ ramp_alpha = start + (1.0 - start) * cls._ease(t, amplitude)
116
+ copy_alpha = torch.where(
117
+ alpha <= start,
118
+ alpha,
119
+ torch.where(alpha >= end, torch.ones_like(alpha), ramp_alpha),
120
+ )
121
+ return torch.where(
122
+ alpha > 0.0, copy_alpha.clamp(0.0, 1.0), torch.zeros_like(alpha)
123
+ )
124
+
125
+ @staticmethod
126
+ def _alpha_from_mask(mask, image):
127
+ if not isinstance(mask, torch.Tensor):
128
+ raise TypeError("mask must be a ComfyUI MASK tensor.")
129
+ if mask.ndim == 2:
130
+ mask = mask.unsqueeze(0)
131
+ elif mask.ndim == 4:
132
+ spatial_shape = tuple(image.shape[1:3])
133
+ if mask.shape[-1] == 1 and tuple(mask.shape[1:3]) == spatial_shape:
134
+ mask = mask[..., 0]
135
+ elif mask.shape[1] == 1 and tuple(mask.shape[2:]) == spatial_shape:
136
+ mask = mask[:, 0, :, :]
137
+ if mask.ndim != 3:
138
+ raise ValueError("mask must have shape [H,W] or [B,H,W].")
139
+ if tuple(mask.shape[1:]) != tuple(image.shape[1:3]):
140
+ raise ValueError("mask height and width must match the image exactly.")
141
+ if mask.shape[0] == 1:
142
+ mask = mask.expand(image.shape[0], -1, -1)
143
+ elif mask.shape[0] != image.shape[0]:
144
+ raise ValueError("mask batch size must be 1 or match the image batch.")
145
+ mask = mask.to(device=image.device, dtype=image.dtype).clamp(0.0, 1.0)
146
+ return (1.0 - mask).unsqueeze(-1)
147
+
148
+ @classmethod
149
+ def _prepare_source(cls, image, mask):
150
+ if not isinstance(image, torch.Tensor):
151
+ raise TypeError("image must be a ComfyUI IMAGE tensor.")
152
+ if image.ndim != 4 or image.shape[-1] not in (3, 4):
153
+ raise ValueError("image must have shape [B,H,W,4] (RGBA) or [B,H,W,3].")
154
+ if not image.is_floating_point():
155
+ raise TypeError("image must be a floating-point tensor with values in [0,1].")
156
+ if any(size == 0 for size in image.shape[:3]):
157
+ raise ValueError("image batch, height and width must be nonzero.")
158
+ work_dtype = torch.float64 if image.dtype == torch.float64 else torch.float32
159
+ source = image.to(dtype=work_dtype)
160
+ if image.shape[-1] == 3:
161
+ if mask is None:
162
+ raise ValueError(
163
+ "RGB input has no alpha channel. Supply an RGBA image, or "
164
+ "connect Load Image's MASK output to this node's mask input."
165
+ )
166
+ source = torch.cat((source, cls._alpha_from_mask(mask, source)), dim=-1)
167
+ return source
168
+
169
+ @classmethod
170
+ def _compose(cls, source, output_dtype, underlay_rgb=None):
171
+ alpha = source[..., 3:4].clamp(0.0, 1.0)
172
+ copy_alpha = cls._black_alpha(
173
+ alpha, RAMP_START_PERCENT, ALPHA_START_PERCENT, AMPLITUDE
174
+ )
175
+ composited_alpha = alpha + copy_alpha * (1.0 - alpha)
176
+ last_translucent = 1.0 - torch.finfo(output_dtype).eps / 2.0
177
+ before_end = (alpha < ALPHA_START_PERCENT / 100.0) & (alpha < 1.0)
178
+ output_alpha = torch.where(
179
+ before_end, composited_alpha.clamp(max=last_translucent), composited_alpha
180
+ )
181
+ safe_alpha = torch.where(
182
+ composited_alpha > 0.0, composited_alpha, torch.ones_like(composited_alpha)
183
+ )
184
+ numerator = source[..., :3] * alpha
185
+ if underlay_rgb is not None:
186
+ numerator = numerator + underlay_rgb * copy_alpha * (1.0 - alpha)
187
+ return numerator / safe_alpha, output_alpha
188
+
189
+ @staticmethod
190
+ def _pack_rgba(rgb, alpha, dtype):
191
+ output = torch.cat((rgb, alpha), dim=-1).to(dtype=dtype)
192
+ output[..., :3] = torch.where(
193
+ output[..., 3:4] > 0.0, output[..., :3], torch.zeros_like(output[..., :3])
194
+ )
195
+ return output
196
+
197
+ @classmethod
198
+ def _run_black_reference(cls, source, output_dtype):
199
+ rgb, alpha = cls._compose(source, output_dtype)
200
+ # Always the supplied node's default 1-2-1 / 2-4-2 / 1-2-1 filter.
201
+ rgb, alpha = cls._protect_outline(rgb, alpha)
202
+ return cls._pack_rgba(rgb, alpha, output_dtype)
203
+
204
+
205
+ class SaliaRGBASelfUnderlay(_BlackUnderlayReference):
206
+ @classmethod
207
+ def INPUT_TYPES(cls):
208
+ return {
209
+ "required": {
210
+ "image": ("IMAGE", {"tooltip": (
211
+ "Original straight RGBA. For RGB, also connect its "
212
+ "ComfyUI transparency MASK to the optional mask input."
213
+ )}),
214
+ "outline_strength": ("FLOAT", {
215
+ "default": 1.0, "min": 0.01, "max": 1.0, "step": 0.01,
216
+ "tooltip": (
217
+ "Final outline smoothing strength. 1.0 matches the "
218
+ "original node; smaller values preserve more original "
219
+ "alpha coverage. Always positive: protection stays on."
220
+ ),
221
+ }),
222
+ "outline_side_weight": ("FLOAT", {
223
+ "default": 2.0, "min": 0.01, "max": 16.0, "step": 0.01,
224
+ "tooltip": (
225
+ "Weight of each horizontal/vertical neighbor in final "
226
+ "3x3 protection. Center weight is fixed at 4. "
227
+ "Higher values spread more alpha to/from these neighbors."
228
+ ),
229
+ }),
230
+ "outline_diagonal_weight": ("FLOAT", {
231
+ "default": 1.0, "min": 0.01, "max": 16.0, "step": 0.01,
232
+ "tooltip": (
233
+ "Weight of each diagonal neighbor in final protection. "
234
+ "Higher values give diagonal neighbors more influence. "
235
+ "The kernel is normalized so opaque interiors stay opaque."
236
+ ),
237
+ }),
238
+ },
239
+ "optional": {
240
+ "mask": ("MASK", {"tooltip": (
241
+ "Used only for RGB input: 1 = transparent, 0 = opaque. "
242
+ "RGBA uses its own alpha. Size must match the image."
243
+ )}),
244
+ },
245
+ }
246
+
247
+ RETURN_TYPES = ("IMAGE",)
248
+ RETURN_NAMES = ("rgba",)
249
+ FUNCTION = "apply"
250
+ CATEGORY = "Salia/Image"
251
+ DESCRIPTION = (
252
+ "Runs the original protected black underlay to locate the outline, "
253
+ "then builds six inward 8-neighbor bands. Uses black near the outline, "
254
+ "grayscale transition bands, and original pixel color inside. "
255
+ "Original RGBA goes on top; adjustable outline protection is always on."
256
+ )
257
+
258
+ @staticmethod
259
+ def _inward_bands(visible):
260
+ """[B,H,W,1] bool -> labels 0=white, 1..6=rings, 7=interior.
261
+
262
+ Each erosion sees previously removed layers as white. Only pixels
263
+ still inside may join the next ring, so rings cannot overlap.
264
+ """
265
+ remaining = visible.permute(0, 3, 1, 2)
266
+ labels = remaining.to(dtype=torch.uint8) * 7
267
+ for depth in range(1, 7):
268
+ touches_outside = F.max_pool2d(
269
+ F.pad((~remaining).to(dtype=torch.float32),
270
+ (1, 1, 1, 1), mode="replicate"),
271
+ kernel_size=3, stride=1,
272
+ ) > 0.0
273
+ ring = remaining & touches_outside
274
+ labels = torch.where(ring, torch.full_like(labels, depth), labels)
275
+ remaining = remaining & ~ring
276
+ return labels.permute(0, 2, 3, 1)
277
+
278
+ @staticmethod
279
+ def _underlay_rgb(source_rgb, bands):
280
+ gray = (source_rgb * source_rgb.new_tensor(
281
+ [0.2126, 0.7152, 0.0722]
282
+ )).sum(dim=-1, keepdim=True)
283
+ underlay = torch.zeros_like(source_rgb)
284
+ underlay = torch.where(bands == 4, gray * 0.5, underlay)
285
+ underlay = torch.where(bands == 5, gray, underlay)
286
+ underlay = torch.where(bands == 6, gray * 0.5 + source_rgb * 0.5, underlay)
287
+ return torch.where(bands == 7, source_rgb, underlay)
288
+
289
+ @staticmethod
290
+ def _control(name, value, maximum):
291
+ try:
292
+ value = float(value)
293
+ except (TypeError, ValueError) as error:
294
+ raise ValueError(name + " must be a finite positive float.") from error
295
+ if not math.isfinite(value) or not 0.01 <= value <= maximum:
296
+ raise ValueError(
297
+ "{} must be between 0.01 and {}. Outline protection is always on."
298
+ .format(name, maximum)
299
+ )
300
+ return value
301
+
302
+ @torch.no_grad()
303
+ def apply(
304
+ self, image, outline_strength=1.0, outline_side_weight=2.0,
305
+ outline_diagonal_weight=1.0, mask=None,
306
+ ):
307
+ strength = self._control("outline_strength", outline_strength, 1.0)
308
+ side = self._control("outline_side_weight", outline_side_weight, 16.0)
309
+ diagonal = self._control("outline_diagonal_weight", outline_diagonal_weight, 16.0)
310
+ source = self._prepare_source(image, mask)
311
+
312
+ # First pass: exact supplied black-underlay output with protection ON.
313
+ # Threshold AFTER casting to the input dtype, just like that output.
314
+ reference = self._run_black_reference(source, image.dtype)
315
+ visible = reference[..., 3:4].to(dtype=source.dtype) >= RADICAL_ALPHA_THRESHOLD
316
+ bands = self._inward_bands(visible)
317
+ del reference, visible
318
+
319
+ # Second pass starts from the ORIGINAL image, with the repaired color.
320
+ underlay = self._underlay_rgb(source[..., :3], bands)
321
+ rgb, alpha = self._compose(source, image.dtype, underlay)
322
+ rgb, alpha = self._protect_outline(rgb, alpha, strength, side, diagonal)
323
+ return (self._pack_rgba(rgb, alpha, image.dtype),)
324
+
325
+
326
+ NODE_CLASS_MAPPINGS = {
327
+ "SaliaRGBASelfUnderlay": SaliaRGBASelfUnderlay,
328
+ }
329
+
330
+ NODE_DISPLAY_NAME_MAPPINGS = {
331
+ "SaliaRGBASelfUnderlay": "Salia RGBA Self Underlay",
332
+ }