saliacoel commited on
Commit
f0a4e6c
·
verified ·
1 Parent(s): 94c88fd

Upload Salia_Min_Alpha_Overlay.py

Browse files
Files changed (1) hide show
  1. Salia_Min_Alpha_Overlay.py +71 -19
Salia_Min_Alpha_Overlay.py CHANGED
@@ -1,10 +1,11 @@
1
  """
2
- Salia_Min_Alpha_Overlay -- recover and crop a minimum-alpha RGBA overlay.
3
 
4
  Install this single file in ComfyUI/custom_nodes/ and restart ComfyUI.
5
  Node: Salia Min Alpha Overlay Category: Salia/Image/Alpha
6
- Inputs: Image_A, Image_C (IMAGE; RGB or straight/unassociated RGBA).
7
- Outputs: Image_B (IMAGE; cropped straight/unassociated RGBA), x, y, width, height.
 
8
  Coordinates are zero-based relative to the original full-resolution canvas;
9
  x/y identify the rectangle's top-left corner. Place Image_B there to restore
10
  its original position. Width/height are the actual cropped image dimensions.
@@ -13,6 +14,23 @@ there is no alpha threshold. Batches use one shared union rectangle so all
13
  images remain the same size. An entirely invisible overlay returns a fully
14
  transparent 1x1 image at x=0, y=0, width=1, height=1.
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  Solves normal Porter-Duff source-over, B over A = C, per pixel:
17
  C_alpha = B_alpha + A_alpha * (1 - B_alpha)
18
  C_rgb * C_alpha = B_rgb * B_alpha
@@ -23,7 +41,7 @@ No resizing, smoothing, difference threshold, AI, or color-space conversion.
23
  Both inputs must already be in the color space used by your compositor.
24
  A singleton batch is broadcast to match the other input's batch.
25
 
26
- For A_alpha == 1 (which also requires C_alpha == 1):
27
  q_i = (C_i-A_i)/(1-A_i) if C_i>A_i
28
  (A_i-C_i)/A_i if C_i<A_i
29
  0 otherwise
@@ -36,7 +54,7 @@ Precision contract:
36
  * The equations give an exact real-number solution WHEN one exists.
37
  * Computation/validation uses float64 in bounded CPU chunks. Output is float32
38
  (float64 if either input is float64). Float16/bfloat16 inputs are promoted.
39
- * For opaque backgrounds only, alpha is rounded UP if needed to the next
40
  representable output value, never below the calculated mathematical bound.
41
  RGB is then solved again at the stored alpha.
42
  * For translucent backgrounds, the uniquely forced alpha is rounded to the
@@ -71,6 +89,8 @@ import torch
71
  _LOG = logging.getLogger(__name__)
72
  _CHUNK_PIXELS = 131_072
73
  _MATH_TOL = 32.0 * torch.finfo(torch.float64).eps
 
 
74
 
75
 
76
  def _validate_shape(image: torch.Tensor, name: str) -> None:
@@ -140,7 +160,8 @@ def _solve_chunk(
140
  output_dtype: torch.dtype,
141
  batch: int,
142
  row0: int,
143
- ) -> Tuple[torch.Tensor, float, float, int, int]:
 
144
  """Input chunks are [rows, width, channels], CPU float64."""
145
  _validate_values(a, "Image_A", batch, row0)
146
  _validate_values(c, "Image_C", batch, row0)
@@ -165,6 +186,13 @@ def _solve_chunk(
165
  forced_for_translucent = (ca - aa) / safe_transparency
166
  beta = torch.where(opaque_a, minimum_for_opaque, forced_for_translucent)
167
 
 
 
 
 
 
 
 
168
  # Equal translucent alpha forces beta=0, so visible RGB cannot change.
169
  skipped = skipped | (
170
  (beta == 0.0) & (ca > 0.0) & (cr != ar).any(dim=-1, keepdim=True)
@@ -209,6 +237,11 @@ def _solve_chunk(
209
  premult = b[..., :3] * ba + ar * aa * (1.0 - ba)
210
  safe_alpha = torch.where(reconstructed_alpha > 0.0, reconstructed_alpha, 1.0)
211
  reconstructed_rgb = premult / safe_alpha
 
 
 
 
 
212
 
213
  rgb_error = torch.where(
214
  (ca > 0.0) & ~skipped, (reconstructed_rgb - cr).abs(), 0.0
@@ -230,6 +263,7 @@ def _solve_chunk(
230
  )
231
  return (
232
  overlay,
 
233
  float(rgb_error.max().item()),
234
  float(alpha_error.max().item()),
235
  hidden_rgb_pixels,
@@ -248,27 +282,43 @@ class Salia_Min_Alpha_Overlay:
248
  "Image_C": ("IMAGE", {
249
  "tooltip": "Desired composite. RGB or straight RGBA. Its alpha is respected."
250
  }),
251
- }
 
 
 
 
 
 
 
 
 
 
252
  }
253
 
254
- RETURN_TYPES = ("IMAGE", "INT", "INT", "INT", "INT")
255
- RETURN_NAMES = ("Image_B", "x", "y", "width", "height")
256
  FUNCTION = "extract_overlay"
257
  CATEGORY = "Salia/Image/Alpha"
258
  DESCRIPTION = (
259
- "Calculates the minimum-alpha straight RGBA overlay Image_B. "
 
 
260
  "Impossible pixels get zero overlay alpha and leave A unchanged. "
261
  "Crops to all alpha > 0 and returns x/y (top-left on the original "
262
  "canvas) and width/height. Batches share one union crop; an empty "
263
  "overlay returns transparent 1x1 at (0,0). RGB means opaque. "
264
- "RGBA alpha is respected. Float precision applies; saving as 8-bit "
265
- "PNG can introduce error. Hidden RGB at alpha=0 is ignored."
 
266
  )
267
 
268
  @torch.no_grad()
269
  def extract_overlay(
270
- self, Image_A: torch.Tensor, Image_C: torch.Tensor
271
- ) -> Tuple[torch.Tensor, int, int, int, int]:
 
 
 
272
  _validate_shape(Image_A, "Image_A")
273
  _validate_shape(Image_C, "Image_C")
274
  if Image_A.shape[1:3] != Image_C.shape[1:3]:
@@ -295,6 +345,7 @@ class Salia_Min_Alpha_Overlay:
295
  (batches, height, width, 4),
296
  dtype=output_dtype, device=Image_A.device,
297
  )
 
298
  rows_per_chunk = max(1, _CHUNK_PIXELS // width)
299
  max_rgb_error = max_alpha_error = 0.0
300
  hidden_rgb_pixels = skipped_pixels = 0
@@ -321,10 +372,11 @@ class Salia_Min_Alpha_Overlay:
321
  c = Image_C[0 if nc == 1 else batch, row0:row1].detach().to(
322
  device="cpu", dtype=torch.float64
323
  )
324
- result, ergb, ea, hidden, skipped = _solve_chunk(
325
- a, c, output_dtype, batch, row0
326
  )
327
  output[batch, row0:row1].copy_(result)
 
328
  max_rgb_error = max(max_rgb_error, ergb)
329
  max_alpha_error = max(max_alpha_error, ea)
330
  hidden_rgb_pixels += hidden
@@ -356,10 +408,10 @@ class Salia_Min_Alpha_Overlay:
356
  )
357
 
358
  _LOG.info(
359
- "[Salia Min Alpha Overlay] %dx%d, batch=%d, dtype=%s; "
360
  "verified solved-pixel max visible RGB error=%.9g, "
361
  "max alpha error=%.9g; crop x=%d, y=%d, width=%d, height=%d.",
362
- width, height, batches, output_dtype, max_rgb_error, max_alpha_error,
363
  crop_x0, crop_y0, crop_width, crop_height,
364
  )
365
  if skipped_pixels:
@@ -376,7 +428,7 @@ class Salia_Min_Alpha_Overlay:
376
  "these hidden colors; only visible RGB and alpha are verified.",
377
  hidden_rgb_pixels,
378
  )
379
- return (output, crop_x0, crop_y0, crop_width, crop_height)
380
 
381
 
382
  NODE_CLASS_MAPPINGS = {
 
1
  """
2
+ Salia_Min_Alpha_Overlay -- recover, verify, and crop an RGBA overlay.
3
 
4
  Install this single file in ComfyUI/custom_nodes/ and restart ComfyUI.
5
  Node: Salia Min Alpha Overlay Category: Salia/Image/Alpha
6
+ Inputs: Image_A, Image_C (IMAGE; RGB or straight/unassociated RGBA), mode.
7
+ Outputs: Image_B (IMAGE; cropped straight/unassociated RGBA), x, y, width,
8
+ height, Image_C_check (IMAGE; full-size A + the actual computed overlay).
9
  Coordinates are zero-based relative to the original full-resolution canvas;
10
  x/y identify the rectangle's top-left corner. Place Image_B there to restore
11
  its original position. Width/height are the actual cropped image dimensions.
 
14
  images remain the same size. An entirely invisible overlay returns a fully
15
  transparent 1x1 image at x=0, y=0, width=1, height=1.
16
 
17
+ Modes:
18
+ * Exact opaque pixels (default): changed pixels whose target alpha is 1 use
19
+ overlay alpha 1 and exactly the target RGB. This preserves opaque target
20
+ pixels through ordinary PNG saving/loading and normal compositing, without
21
+ relying on a delicate low-alpha/color combination. Unchanged opaque pixels
22
+ remain transparent in the overlay. This mode prioritizes reconstruction
23
+ over minimizing alpha. Translucent target pixels use the solver below.
24
+ * Minimum alpha (float): the original mathematical minimum-alpha solver.
25
+ Its guarantee is for floating-point normal source-over. Saving its RGB and
26
+ alpha independently to 8-bit PNG generally changes the reconstructed color.
27
+
28
+ Image_C_check is reconstructed from the actual overlay values before cropping;
29
+ it is not a copy of Image_C. Compare it to Image_C to check the extraction.
30
+ For external recomposition, position the cropped Image_B at x/y at its original
31
+ size, use normal straight-alpha source-over with opacity 100%, and apply the
32
+ overlay's alpha exactly once. Do not multiply its alpha by a duplicate mask.
33
+
34
  Solves normal Porter-Duff source-over, B over A = C, per pixel:
35
  C_alpha = B_alpha + A_alpha * (1 - B_alpha)
36
  C_rgb * C_alpha = B_rgb * B_alpha
 
41
  Both inputs must already be in the color space used by your compositor.
42
  A singleton batch is broadcast to match the other input's batch.
43
 
44
+ In Minimum alpha (float) mode, for A_alpha == 1 (and C_alpha == 1):
45
  q_i = (C_i-A_i)/(1-A_i) if C_i>A_i
46
  (A_i-C_i)/A_i if C_i<A_i
47
  0 otherwise
 
54
  * The equations give an exact real-number solution WHEN one exists.
55
  * Computation/validation uses float64 in bounded CPU chunks. Output is float32
56
  (float64 if either input is float64). Float16/bfloat16 inputs are promoted.
57
+ * In minimum-alpha mode on opaque backgrounds, alpha is rounded UP to the next
58
  representable output value, never below the calculated mathematical bound.
59
  RGB is then solved again at the stored alpha.
60
  * For translucent backgrounds, the uniquely forced alpha is rounded to the
 
89
  _LOG = logging.getLogger(__name__)
90
  _CHUNK_PIXELS = 131_072
91
  _MATH_TOL = 32.0 * torch.finfo(torch.float64).eps
92
+ _MODE_EXACT = "Exact opaque pixels"
93
+ _MODE_MINIMUM = "Minimum alpha (float)"
94
 
95
 
96
  def _validate_shape(image: torch.Tensor, name: str) -> None:
 
160
  output_dtype: torch.dtype,
161
  batch: int,
162
  row0: int,
163
+ minimum_alpha: bool,
164
+ ) -> Tuple[torch.Tensor, torch.Tensor, float, float, int, int]:
165
  """Input chunks are [rows, width, channels], CPU float64."""
166
  _validate_values(a, "Image_A", batch, row0)
167
  _validate_values(c, "Image_C", batch, row0)
 
186
  forced_for_translucent = (ca - aa) / safe_transparency
187
  beta = torch.where(opaque_a, minimum_for_opaque, forced_for_translucent)
188
 
189
+ if not minimum_alpha:
190
+ # Exact replacement on changed opaque target pixels. In particular,
191
+ # do not store a very small alpha and extreme RGB that only cancel
192
+ # correctly before 8-bit quantization or if alpha is applied once.
193
+ changed = (ca != aa) | (cr != ar).any(dim=-1, keepdim=True)
194
+ beta = torch.where((ca == 1.0) & changed, 1.0, beta)
195
+
196
  # Equal translucent alpha forces beta=0, so visible RGB cannot change.
197
  skipped = skipped | (
198
  (beta == 0.0) & (ca > 0.0) & (cr != ar).any(dim=-1, keepdim=True)
 
237
  premult = b[..., :3] * ba + ar * aa * (1.0 - ba)
238
  safe_alpha = torch.where(reconstructed_alpha > 0.0, reconstructed_alpha, 1.0)
239
  reconstructed_rgb = premult / safe_alpha
240
+ # Preserve A's unobservable hidden RGB when both layers are transparent.
241
+ reconstructed_rgb = torch.where(reconstructed_alpha > 0.0, reconstructed_rgb, ar)
242
+ reconstructed = torch.cat(
243
+ (reconstructed_rgb, reconstructed_alpha), dim=-1
244
+ ).to(dtype=output_dtype)
245
 
246
  rgb_error = torch.where(
247
  (ca > 0.0) & ~skipped, (reconstructed_rgb - cr).abs(), 0.0
 
263
  )
264
  return (
265
  overlay,
266
+ reconstructed,
267
  float(rgb_error.max().item()),
268
  float(alpha_error.max().item()),
269
  hidden_rgb_pixels,
 
282
  "Image_C": ("IMAGE", {
283
  "tooltip": "Desired composite. RGB or straight RGBA. Its alpha is respected."
284
  }),
285
+ },
286
+ "optional": {
287
+ "mode": ([_MODE_EXACT, _MODE_MINIMUM], {
288
+ "default": _MODE_EXACT,
289
+ "tooltip": (
290
+ "Exact opaque pixels: copy changed opaque target RGB at alpha 1. "
291
+ "Minimum alpha (float): smallest alpha before PNG quantization. "
292
+ "Both modes keep impossible pixels transparent."
293
+ ),
294
+ }),
295
+ },
296
  }
297
 
298
+ RETURN_TYPES = ("IMAGE", "INT", "INT", "INT", "INT", "IMAGE")
299
+ RETURN_NAMES = ("Image_B", "x", "y", "width", "height", "Image_C_check")
300
  FUNCTION = "extract_overlay"
301
  CATEGORY = "Salia/Image/Alpha"
302
  DESCRIPTION = (
303
+ "Extracts a straight RGBA overlay Image_B. Default Exact opaque pixels "
304
+ "copies changed opaque target pixels at alpha 1 for reliable matching. "
305
+ "Minimum alpha (float) is optional and is sensitive to PNG quantization. "
306
  "Impossible pixels get zero overlay alpha and leave A unchanged. "
307
  "Crops to all alpha > 0 and returns x/y (top-left on the original "
308
  "canvas) and width/height. Batches share one union crop; an empty "
309
  "overlay returns transparent 1x1 at (0,0). RGB means opaque. "
310
+ "Image_C_check is the actual full-size reconstructed composite. "
311
+ "RGBA alpha is respected. Translucent pixels remain subject to "
312
+ "float/PNG precision. Hidden RGB at alpha=0 is ignored."
313
  )
314
 
315
  @torch.no_grad()
316
  def extract_overlay(
317
+ self, Image_A: torch.Tensor, Image_C: torch.Tensor,
318
+ mode: str = _MODE_EXACT,
319
+ ) -> Tuple[torch.Tensor, int, int, int, int, torch.Tensor]:
320
+ if mode not in (_MODE_EXACT, _MODE_MINIMUM):
321
+ raise ValueError(f"Unknown overlay mode: {mode!r}.")
322
  _validate_shape(Image_A, "Image_A")
323
  _validate_shape(Image_C, "Image_C")
324
  if Image_A.shape[1:3] != Image_C.shape[1:3]:
 
345
  (batches, height, width, 4),
346
  dtype=output_dtype, device=Image_A.device,
347
  )
348
+ check_image = torch.empty_like(output)
349
  rows_per_chunk = max(1, _CHUNK_PIXELS // width)
350
  max_rgb_error = max_alpha_error = 0.0
351
  hidden_rgb_pixels = skipped_pixels = 0
 
372
  c = Image_C[0 if nc == 1 else batch, row0:row1].detach().to(
373
  device="cpu", dtype=torch.float64
374
  )
375
+ result, reconstructed, ergb, ea, hidden, skipped = _solve_chunk(
376
+ a, c, output_dtype, batch, row0, mode == _MODE_MINIMUM
377
  )
378
  output[batch, row0:row1].copy_(result)
379
+ check_image[batch, row0:row1].copy_(reconstructed)
380
  max_rgb_error = max(max_rgb_error, ergb)
381
  max_alpha_error = max(max_alpha_error, ea)
382
  hidden_rgb_pixels += hidden
 
408
  )
409
 
410
  _LOG.info(
411
+ "[Salia Min Alpha Overlay] mode=%s, %dx%d, batch=%d, dtype=%s; "
412
  "verified solved-pixel max visible RGB error=%.9g, "
413
  "max alpha error=%.9g; crop x=%d, y=%d, width=%d, height=%d.",
414
+ mode, width, height, batches, output_dtype, max_rgb_error, max_alpha_error,
415
  crop_x0, crop_y0, crop_width, crop_height,
416
  )
417
  if skipped_pixels:
 
428
  "these hidden colors; only visible RGB and alpha are verified.",
429
  hidden_rgb_pixels,
430
  )
431
+ return (output, crop_x0, crop_y0, crop_width, crop_height, check_image)
432
 
433
 
434
  NODE_CLASS_MAPPINGS = {