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

Upload Salia_RGBA_Self_Underlay.py

Browse files
Files changed (1) hide show
  1. Salia_RGBA_Self_Underlay.py +58 -19
Salia_RGBA_Self_Underlay.py CHANGED
@@ -30,9 +30,13 @@ Final outline controls (the preliminary reference pass stays fixed):
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,
@@ -56,9 +60,8 @@ 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([
@@ -67,26 +70,60 @@ class _BlackUnderlayReference:
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
  )
@@ -198,7 +235,7 @@ class _BlackUnderlayReference:
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
 
@@ -214,9 +251,10 @@ class SaliaRGBASelfUnderlay(_BlackUnderlayReference):
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", {
@@ -252,7 +290,8 @@ class SaliaRGBASelfUnderlay(_BlackUnderlayReference):
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
 
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 coverage.
34
+ At alpha boundaries, premultiplied RGB and alpha are filtered and blended
35
+ together, then divided to recover straight RGB. Increasing coverage therefore
36
+ cannot promote a nearly invisible pale pixel while retaining its pale RGB.
37
+ Where all nine alpha values are equal, existing RGB detail is preserved.
38
+ Final alpha remains smooth, not binary. The initial reference pass retains
39
+ the supplied node's original math; only the final outline pass is repaired.
40
 
41
  Input/output: ComfyUI IMAGE [B,H,W,4], straight RGBA. RGB also works with an
42
  optional ComfyUI transparency MASK (1 = transparent). Batch, dimensions,
 
60
  """Embedded supplied black-underlay math; never registered as a node."""
61
 
62
  @staticmethod
63
+ def _filtered_rgba(rgb, alpha, side_weight=2.0, diagonal_weight=1.0):
64
+ """Filter premultiplied RGB and alpha with identical weights."""
 
65
  premultiplied = torch.cat((rgb * alpha, alpha), dim=-1)
66
  plane = premultiplied.permute(0, 3, 1, 2)
67
  kernel = plane.new_tensor([
 
70
  [diagonal_weight, side_weight, diagonal_weight],
71
  ]).reshape(1, 1, 3, 3) / (4.0 + 4.0 * side_weight + 4.0 * diagonal_weight)
72
  kernel = kernel.expand(4, 1, 3, 3).contiguous()
73
+ return F.conv2d(
74
  F.pad(plane, (1, 1, 1, 1), mode="replicate"),
75
  kernel, groups=4,
76
  ).permute(0, 2, 3, 1)
77
 
78
+ @classmethod
79
+ def _protect_outline_reference(cls, rgb, alpha):
80
+ """Exact supplied protection, used ONLY to build the radical mask."""
81
+ filtered = cls._filtered_rgba(rgb, alpha)
82
  filtered_alpha = filtered[..., 3:4]
83
  safe_alpha = torch.where(
84
  filtered_alpha > 0.0, filtered_alpha, torch.ones_like(filtered_alpha)
85
  )
86
  borrowed_rgb = filtered[..., :3] / safe_alpha
87
  output_rgb = torch.where(alpha > 0.0, rgb, borrowed_rgb)
88
+ output_alpha = filtered_alpha.clamp(0.0, 1.0)
89
+ output_rgb = torch.where(
90
+ output_alpha > 0.0, output_rgb, torch.zeros_like(output_rgb)
91
+ )
92
+ return output_rgb, output_alpha
93
+
94
+ @classmethod
95
+ def _protect_outline(
96
+ cls, rgb, alpha, strength=1.0, side_weight=2.0, diagonal_weight=1.0,
97
+ ):
98
+ """Repair coverage without amplifying faint straight-RGB residue.
99
+
100
+ Mixing alpha while keeping a faint pixel's old straight RGB is not
101
+ valid coverage filtering: a nearly invisible white pixel can become
102
+ a visible white outline. Color must carry the SAME alpha weights.
103
+ """
104
+ filtered = cls._filtered_rgba(rgb, alpha, side_weight, diagonal_weight)
105
  if strength == 1.0:
106
+ premultiplied_rgb = filtered[..., :3]
107
+ mixed_alpha = filtered[..., 3:4]
108
  else:
109
+ premultiplied_rgb = (
110
+ rgb * alpha * (1.0 - strength) + filtered[..., :3] * strength
111
+ )
112
+ mixed_alpha = alpha * (1.0 - strength) + filtered[..., 3:4] * strength
113
+
114
+ safe_alpha = torch.where(
115
+ mixed_alpha > 0.0, mixed_alpha, torch.ones_like(mixed_alpha)
116
+ )
117
+ output_rgb = premultiplied_rgb / safe_alpha
118
+
119
+ # This is outline protection, so do not blur RGB texture where
120
+ # coverage is locally constant (including opaque interiors).
121
+ plane = F.pad(alpha.permute(0, 3, 1, 2), (1, 1, 1, 1), mode="replicate")
122
+ local_max = F.max_pool2d(plane, kernel_size=3, stride=1)
123
+ local_min = -F.max_pool2d(-plane, kernel_size=3, stride=1)
124
+ constant_coverage = (local_max == local_min).permute(0, 2, 3, 1)
125
+ output_rgb = torch.where(constant_coverage, rgb, output_rgb)
126
+ output_alpha = mixed_alpha.clamp(0.0, 1.0)
127
  output_rgb = torch.where(
128
  output_alpha > 0.0, output_rgb, torch.zeros_like(output_rgb)
129
  )
 
235
  def _run_black_reference(cls, source, output_dtype):
236
  rgb, alpha = cls._compose(source, output_dtype)
237
  # Always the supplied node's default 1-2-1 / 2-4-2 / 1-2-1 filter.
238
+ rgb, alpha = cls._protect_outline_reference(rgb, alpha)
239
  return cls._pack_rgba(rgb, alpha, output_dtype)
240
 
241
 
 
251
  "outline_strength": ("FLOAT", {
252
  "default": 1.0, "min": 0.01, "max": 1.0, "step": 0.01,
253
  "tooltip": (
254
+ "Final outline smoothing strength. 1.0 uses the "
255
+ "full filter; smaller values preserve more pre-filter "
256
+ "coverage. Color and alpha blend together to prevent "
257
+ "pale fringes. Always positive: protection stays on."
258
  ),
259
  }),
260
  "outline_side_weight": ("FLOAT", {
 
290
  "Runs the original protected black underlay to locate the outline, "
291
  "then builds six inward 8-neighbor bands. Uses black near the outline, "
292
  "grayscale transition bands, and original pixel color inside. "
293
+ "Original RGBA goes on top; always-on outline protection filters "
294
+ "premultiplied color and alpha together to prevent pale fringes."
295
  )
296
 
297
  @staticmethod