Upload Salia_Min_Alpha_Overlay.py
Browse files- Salia_Min_Alpha_Overlay.py +127 -40
Salia_Min_Alpha_Overlay.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
Salia Max Alpha Overlay -- a cropped RGBA overlay
|
| 3 |
|
| 4 |
Install this file in ComfyUI/custom_nodes/, replacing the previous version,
|
| 5 |
and restart ComfyUI. The registration key and output order are unchanged:
|
|
@@ -10,9 +10,11 @@ Image_A is the original image; Image_C is the desired composite. Both inputs
|
|
| 10 |
may be RGB or straight/unassociated RGBA and must have the same resolution.
|
| 11 |
RGB means fully opaque. A singleton batch is broadcast to the other batch.
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
Filters, in order:
|
| 18 |
1. Preserve the old solver's feasibility check. An ordinary source-over
|
|
@@ -26,20 +28,29 @@ Filters, in order:
|
|
| 26 |
and white backgrounds, plus its alpha change; take the largest channel
|
| 27 |
difference. Hidden RGB under zero alpha cannot create a false difference.
|
| 28 |
A tiny floating-point tolerance makes threshold equality inclusive.
|
| 29 |
-
4.
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
Image_B is cropped AFTER all filters. x/y are the zero-based top-left corner
|
| 34 |
on the original canvas; width/height are the crop's actual dimensions. Batches
|
| 35 |
share one union rectangle. An empty mask returns transparent 1x1 at (0,0).
|
| 36 |
Image_C_check is the actual FULL-SIZE result of placing the final overlay
|
| 37 |
-
over Image_A, including
|
| 38 |
|
| 39 |
This is deliberately an approximate reconstruction: rejected pixels leave A
|
| 40 |
-
unchanged
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
Composite at x/y, original size, normal source-over, opacity 100%; apply the
|
| 44 |
overlay alpha once. There is no resizing, smoothing, or color-space conversion.
|
| 45 |
Only PyTorch (already provided by ComfyUI) is required.
|
|
@@ -47,6 +58,7 @@ Only PyTorch (already provided by ComfyUI) is required.
|
|
| 47 |
from __future__ import annotations
|
| 48 |
|
| 49 |
import logging
|
|
|
|
| 50 |
from typing import Tuple
|
| 51 |
|
| 52 |
import torch
|
|
@@ -142,6 +154,67 @@ def _remove_isolated(mask: torch.Tensor) -> torch.Tensor:
|
|
| 142 |
return mask & has_neighbour
|
| 143 |
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
class Salia_Min_Alpha_Overlay:
|
| 146 |
@classmethod
|
| 147 |
def INPUT_TYPES(cls):
|
|
@@ -151,7 +224,7 @@ class Salia_Min_Alpha_Overlay:
|
|
| 151 |
"tooltip": "Original background. RGB or straight RGBA; same size as Image_C."
|
| 152 |
}),
|
| 153 |
"Image_C": ("IMAGE", {
|
| 154 |
-
"tooltip": "Target image.
|
| 155 |
}),
|
| 156 |
},
|
| 157 |
}
|
|
@@ -161,13 +234,15 @@ class Salia_Min_Alpha_Overlay:
|
|
| 161 |
FUNCTION = "extract_overlay"
|
| 162 |
CATEGORY = "Salia/Image/Alpha"
|
| 163 |
DESCRIPTION = (
|
| 164 |
-
"
|
| 165 |
-
"all others have alpha 0.
|
|
|
|
|
|
|
| 166 |
"replacement changes <=4%, impossible old source-over solutions, "
|
| 167 |
-
"and pixels without a north/south/east/west mask neighbour
|
|
|
|
| 168 |
"Crops after filtering; x/y are its top-left on the original canvas. "
|
| 169 |
-
"Image_C_check is the actual full-size
|
| 170 |
-
"Retained translucent target pixels become opaque."
|
| 171 |
)
|
| 172 |
|
| 173 |
@torch.no_grad()
|
|
@@ -176,7 +251,7 @@ class Salia_Min_Alpha_Overlay:
|
|
| 176 |
mode: str | None = None,
|
| 177 |
) -> Tuple[torch.Tensor, int, int, int, int, torch.Tensor]:
|
| 178 |
# Accept a legacy direct/API mode argument without changing behavior.
|
| 179 |
-
# The old mode widget is removed; output
|
| 180 |
_validate_shape(Image_A, "Image_A")
|
| 181 |
_validate_shape(Image_C, "Image_C")
|
| 182 |
if Image_A.shape[1:3] != Image_C.shape[1:3]:
|
|
@@ -209,7 +284,8 @@ class Salia_Min_Alpha_Overlay:
|
|
| 209 |
crop_x0, crop_y0 = width, height
|
| 210 |
crop_x1 = crop_y1 = 0
|
| 211 |
infeasible_count = old_alpha_count = similar_count = isolated_count = 0
|
| 212 |
-
|
|
|
|
| 213 |
max_rgb_error = max_alpha_error = 0.0
|
| 214 |
|
| 215 |
try:
|
|
@@ -225,6 +301,7 @@ class Salia_Min_Alpha_Overlay:
|
|
| 225 |
# Keep a whole per-image CPU mask so chunk boundaries do not
|
| 226 |
# behave like image edges during the neighbour aftercheck.
|
| 227 |
candidate = torch.empty((height, width), dtype=torch.bool, device="cpu")
|
|
|
|
| 228 |
for row0 in range(0, height, rows_per_chunk):
|
| 229 |
check_cancel()
|
| 230 |
row1 = min(height, row0 + rows_per_chunk)
|
|
@@ -239,19 +316,24 @@ class Salia_Min_Alpha_Overlay:
|
|
| 239 |
old_alpha, feasible = _old_minimum_alpha(a, c)
|
| 240 |
too_transparent = old_alpha <= _OLD_ALPHA_CUTOFF + threshold_tol
|
| 241 |
too_similar = _opaque_change(a, c[..., :3]) <= _SIMILARITY_CUTOFF + threshold_tol
|
| 242 |
-
|
|
|
|
|
|
|
| 243 |
candidate[row0:row1].copy_(keep)
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
output[batch, row0:row1].copy_(rgba)
|
| 247 |
-
# Counts are disjoint, in filter order.
|
| 248 |
infeasible_count += int((~feasible).sum().item())
|
| 249 |
old_alpha_count += int((feasible & too_transparent).sum().item())
|
| 250 |
similar_count += int((feasible & ~too_transparent & too_similar).sum().item())
|
|
|
|
| 251 |
|
| 252 |
check_cancel()
|
| 253 |
-
|
| 254 |
-
isolated_count += int((candidate & ~
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
kept_count += int(keep_mask.sum().item())
|
| 256 |
visible_rows = torch.where(keep_mask.any(dim=1))[0]
|
| 257 |
if visible_rows.numel():
|
|
@@ -261,7 +343,8 @@ class Salia_Min_Alpha_Overlay:
|
|
| 261 |
crop_y0 = min(crop_y0, int(visible_rows[0].item()))
|
| 262 |
crop_y1 = max(crop_y1, int(visible_rows[-1].item()) + 1)
|
| 263 |
|
| 264 |
-
#
|
|
|
|
| 265 |
for row0 in range(0, height, rows_per_chunk):
|
| 266 |
check_cancel()
|
| 267 |
row1 = min(height, row0 + rows_per_chunk)
|
|
@@ -271,21 +354,23 @@ class Salia_Min_Alpha_Overlay:
|
|
| 271 |
c = Image_C[0 if nc == 1 else batch, row0:row1].detach().to(
|
| 272 |
device="cpu", dtype=torch.float64
|
| 273 |
)
|
| 274 |
-
|
| 275 |
-
|
|
|
|
| 276 |
output[batch, row0:row1].copy_(b)
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
| 282 |
reconstructed = torch.cat((rgb, alpha), dim=-1)
|
| 283 |
check_image[batch, row0:row1].copy_(reconstructed.to(output_dtype))
|
| 284 |
visible = (alpha > 0.0) | (_alpha(c) > 0.0)
|
| 285 |
rgb_error = torch.where(visible, (rgb - c[..., :3]).abs(), 0.0)
|
| 286 |
max_rgb_error = max(max_rgb_error, float(rgb_error.max().item()))
|
| 287 |
max_alpha_error = max(max_alpha_error, float((alpha - _alpha(c)).abs().max().item()))
|
| 288 |
-
|
| 289 |
|
| 290 |
if crop_x1 == 0:
|
| 291 |
crop_x0 = crop_y0 = 0
|
|
@@ -301,15 +386,17 @@ class Salia_Min_Alpha_Overlay:
|
|
| 301 |
)
|
| 302 |
|
| 303 |
_LOG.info(
|
| 304 |
-
"[Salia Max Alpha Overlay] %dx%d, batch=%d; kept=%d;
|
| 305 |
-
"infeasible=%d, old-alpha<=1%%=%d, change<=4%%=%d,
|
|
|
|
| 306 |
"crop x=%d, y=%d, width=%d, height=%d; actual full-image "
|
| 307 |
"max RGB difference=%.9g, max alpha difference=%.9g; "
|
| 308 |
-
"kept
|
| 309 |
width, height, batches, kept_count,
|
| 310 |
-
infeasible_count, old_alpha_count, similar_count,
|
|
|
|
| 311 |
crop_x0, crop_y0, crop_width, crop_height,
|
| 312 |
-
max_rgb_error, max_alpha_error,
|
| 313 |
)
|
| 314 |
return (output, crop_x0, crop_y0, crop_width, crop_height, check_image)
|
| 315 |
|
|
|
|
| 1 |
"""
|
| 2 |
+
Salia Max Alpha Overlay -- a cropped RGBA overlay capped by Image_A alpha.
|
| 3 |
|
| 4 |
Install this file in ComfyUI/custom_nodes/, replacing the previous version,
|
| 5 |
and restart ComfyUI. The registration key and output order are unchanged:
|
|
|
|
| 10 |
may be RGB or straight/unassociated RGBA and must have the same resolution.
|
| 11 |
RGB means fully opaque. A singleton batch is broadcast to the other batch.
|
| 12 |
|
| 13 |
+
Retained and hole-filled pixels copy Image_C RGB directly. Their overlay
|
| 14 |
+
alpha equals Image_A alpha; rejected pixels are (0,0,0,0). Thus output alpha
|
| 15 |
+
never exceeds Image_A alpha, and a fully transparent Image_A pixel ALWAYS
|
| 16 |
+
has a fully transparent overlay pixel. RGB inputs mean Image_A alpha=1.
|
| 17 |
+
No inverse-solved RGB is used, and there is no minimum-alpha output mode.
|
| 18 |
|
| 19 |
Filters, in order:
|
| 20 |
1. Preserve the old solver's feasibility check. An ordinary source-over
|
|
|
|
| 28 |
and white backgrounds, plus its alpha change; take the largest channel
|
| 29 |
difference. Hidden RGB under zero alpha cannot create a false difference.
|
| 30 |
A tiny floating-point tolerance makes threshold equality inclusive.
|
| 31 |
+
4. Reject pixels where Image_A alpha is zero, then simultaneously remove
|
| 32 |
+
every remaining pixel with no north, south, west, OR east mask neighbour.
|
| 33 |
+
Any positive alpha counts as a mask neighbour. Diagonals do not count.
|
| 34 |
+
Pixels beyond the image boundary are transparent. This is one pass.
|
| 35 |
+
5. Fill enclosed transparent holes of any size with Image_C RGB, using
|
| 36 |
+
Image_A alpha. A hole is a background region with no 8-connected path to
|
| 37 |
+
the canvas boundary. Diagonal openings therefore stay open. This covers
|
| 38 |
+
single-pixel holes, two-pixel holes, and larger enclosed regions. Filling
|
| 39 |
+
may restore pixels removed by the initial filters, but NEVER restores
|
| 40 |
+
alpha where Image_A alpha is zero. A final four-neighbour cleanup removes
|
| 41 |
+
isolated pixels created by the alpha restriction. Each batch is independent.
|
| 42 |
|
| 43 |
Image_B is cropped AFTER all filters. x/y are the zero-based top-left corner
|
| 44 |
on the original canvas; width/height are the crop's actual dimensions. Batches
|
| 45 |
share one union rectangle. An empty mask returns transparent 1x1 at (0,0).
|
| 46 |
Image_C_check is the actual FULL-SIZE result of placing the final overlay
|
| 47 |
+
over Image_A, including thresholds, cleanup, hole filling, and the alpha cap.
|
| 48 |
|
| 49 |
This is deliberately an approximate reconstruction: rejected pixels leave A
|
| 50 |
+
unchanged. Retained overlay RGB copies C at output precision, but capped
|
| 51 |
+
partial alpha blends it with A. Compositing can still increase the RESULT's
|
| 52 |
+
alpha: the cap applies to the overlay, not to the source-over composite.
|
| 53 |
+
Saving an 8-bit PNG quantizes both RGB and partial alpha.
|
| 54 |
Composite at x/y, original size, normal source-over, opacity 100%; apply the
|
| 55 |
overlay alpha once. There is no resizing, smoothing, or color-space conversion.
|
| 56 |
Only PyTorch (already provided by ComfyUI) is required.
|
|
|
|
| 58 |
from __future__ import annotations
|
| 59 |
|
| 60 |
import logging
|
| 61 |
+
from collections import deque
|
| 62 |
from typing import Tuple
|
| 63 |
|
| 64 |
import torch
|
|
|
|
| 154 |
return mask & has_neighbour
|
| 155 |
|
| 156 |
|
| 157 |
+
def _fill_holes(
|
| 158 |
+
mask: torch.Tensor, allowed: torch.Tensor, check_cancel=None
|
| 159 |
+
) -> torch.Tensor:
|
| 160 |
+
"""Fill enclosed background with an 8-connected scanline flood fill.
|
| 161 |
+
|
| 162 |
+
Both tensors are CPU bool [height,width]. Mark exterior background from
|
| 163 |
+
all canvas edges, then fill only the unvisited background allowed by A.
|
| 164 |
+
Scanline spans avoid a queue entry for every pixel and do not require
|
| 165 |
+
scipy, OpenCV, NumPy, or repeated full-image dilation passes.
|
| 166 |
+
"""
|
| 167 |
+
if not bool(mask.any()) or bool(mask.all()):
|
| 168 |
+
return mask & allowed
|
| 169 |
+
height, width = mask.shape
|
| 170 |
+
# 0=unvisited background, 1=foreground, 2=exterior background.
|
| 171 |
+
data = bytearray(mask.reshape(-1).tolist())
|
| 172 |
+
spans = deque()
|
| 173 |
+
|
| 174 |
+
def scan_row(y: int, x0: int, x1: int) -> None:
|
| 175 |
+
row = y * width
|
| 176 |
+
x = x0
|
| 177 |
+
while x <= x1:
|
| 178 |
+
if data[row + x] != 0:
|
| 179 |
+
x += 1
|
| 180 |
+
continue
|
| 181 |
+
left = x
|
| 182 |
+
while left > 0 and data[row + left - 1] == 0:
|
| 183 |
+
left -= 1
|
| 184 |
+
right = x
|
| 185 |
+
while right + 1 < width and data[row + right + 1] == 0:
|
| 186 |
+
right += 1
|
| 187 |
+
data[row + left:row + right + 1] = b"\x02" * (right - left + 1)
|
| 188 |
+
spans.append((y, left, right))
|
| 189 |
+
x = right + 1
|
| 190 |
+
|
| 191 |
+
scan_row(0, 0, width - 1)
|
| 192 |
+
if height > 1:
|
| 193 |
+
scan_row(height - 1, 0, width - 1)
|
| 194 |
+
for y in range(1, height - 1):
|
| 195 |
+
if check_cancel is not None and y % 256 == 0:
|
| 196 |
+
check_cancel()
|
| 197 |
+
scan_row(y, 0, 0)
|
| 198 |
+
if width > 1:
|
| 199 |
+
scan_row(y, width - 1, width - 1)
|
| 200 |
+
|
| 201 |
+
processed = 0
|
| 202 |
+
while spans:
|
| 203 |
+
y, left, right = spans.popleft()
|
| 204 |
+
# The extra column on each side permits diagonal escape routes.
|
| 205 |
+
x0, x1 = max(0, left - 1), min(width - 1, right + 1)
|
| 206 |
+
if y > 0:
|
| 207 |
+
scan_row(y - 1, x0, x1)
|
| 208 |
+
if y + 1 < height:
|
| 209 |
+
scan_row(y + 1, x0, x1)
|
| 210 |
+
processed += 1
|
| 211 |
+
if check_cancel is not None and processed % 256 == 0:
|
| 212 |
+
check_cancel()
|
| 213 |
+
|
| 214 |
+
holes = torch.frombuffer(data, dtype=torch.uint8).reshape(height, width) == 0
|
| 215 |
+
return (mask | holes) & allowed
|
| 216 |
+
|
| 217 |
+
|
| 218 |
class Salia_Min_Alpha_Overlay:
|
| 219 |
@classmethod
|
| 220 |
def INPUT_TYPES(cls):
|
|
|
|
| 224 |
"tooltip": "Original background. RGB or straight RGBA; same size as Image_C."
|
| 225 |
}),
|
| 226 |
"Image_C": ("IMAGE", {
|
| 227 |
+
"tooltip": "Target image. Retained and hole-filled pixels copy its RGB, with alpha capped at Image_A alpha."
|
| 228 |
}),
|
| 229 |
},
|
| 230 |
}
|
|
|
|
| 234 |
FUNCTION = "extract_overlay"
|
| 235 |
CATEGORY = "Salia/Image/Alpha"
|
| 236 |
DESCRIPTION = (
|
| 237 |
+
"RGBA overlay: retained and hole-filled pixels copy Image_C RGB "
|
| 238 |
+
"with alpha equal to Image_A alpha; all others have alpha 0. "
|
| 239 |
+
"Never adds overlay alpha where A is transparent. Rejects old "
|
| 240 |
+
"minimum alpha <=1%, opaque "
|
| 241 |
"replacement changes <=4%, impossible old source-over solutions, "
|
| 242 |
+
"and pixels without a north/south/east/west mask neighbour, then "
|
| 243 |
+
"fills enclosed holes (8-connected background, any size). "
|
| 244 |
"Crops after filtering; x/y are its top-left on the original canvas. "
|
| 245 |
+
"Image_C_check is the actual full-size source-over composite."
|
|
|
|
| 246 |
)
|
| 247 |
|
| 248 |
@torch.no_grad()
|
|
|
|
| 251 |
mode: str | None = None,
|
| 252 |
) -> Tuple[torch.Tensor, int, int, int, int, torch.Tensor]:
|
| 253 |
# Accept a legacy direct/API mode argument without changing behavior.
|
| 254 |
+
# The old mode widget is removed; output always obeys the A-alpha cap.
|
| 255 |
_validate_shape(Image_A, "Image_A")
|
| 256 |
_validate_shape(Image_C, "Image_C")
|
| 257 |
if Image_A.shape[1:3] != Image_C.shape[1:3]:
|
|
|
|
| 284 |
crop_x0, crop_y0 = width, height
|
| 285 |
crop_x1 = crop_y1 = 0
|
| 286 |
infeasible_count = old_alpha_count = similar_count = isolated_count = 0
|
| 287 |
+
zero_alpha_count = holes_filled_count = 0
|
| 288 |
+
kept_count = partial_alpha_count = 0
|
| 289 |
max_rgb_error = max_alpha_error = 0.0
|
| 290 |
|
| 291 |
try:
|
|
|
|
| 301 |
# Keep a whole per-image CPU mask so chunk boundaries do not
|
| 302 |
# behave like image edges during the neighbour aftercheck.
|
| 303 |
candidate = torch.empty((height, width), dtype=torch.bool, device="cpu")
|
| 304 |
+
allowed = torch.empty_like(candidate)
|
| 305 |
for row0 in range(0, height, rows_per_chunk):
|
| 306 |
check_cancel()
|
| 307 |
row1 = min(height, row0 + rows_per_chunk)
|
|
|
|
| 316 |
old_alpha, feasible = _old_minimum_alpha(a, c)
|
| 317 |
too_transparent = old_alpha <= _OLD_ALPHA_CUTOFF + threshold_tol
|
| 318 |
too_similar = _opaque_change(a, c[..., :3]) <= _SIMILARITY_CUTOFF + threshold_tol
|
| 319 |
+
alpha_allowed = _alpha(a)[..., 0] > 0.0
|
| 320 |
+
before_cap = feasible & ~too_transparent & ~too_similar
|
| 321 |
+
keep = before_cap & alpha_allowed
|
| 322 |
candidate[row0:row1].copy_(keep)
|
| 323 |
+
allowed[row0:row1].copy_(alpha_allowed)
|
| 324 |
+
# Initial removal counts, before enclosed holes are restored.
|
|
|
|
|
|
|
| 325 |
infeasible_count += int((~feasible).sum().item())
|
| 326 |
old_alpha_count += int((feasible & too_transparent).sum().item())
|
| 327 |
similar_count += int((feasible & ~too_transparent & too_similar).sum().item())
|
| 328 |
+
zero_alpha_count += int((before_cap & ~alpha_allowed).sum().item())
|
| 329 |
|
| 330 |
check_cancel()
|
| 331 |
+
cleaned = _remove_isolated(candidate)
|
| 332 |
+
isolated_count += int((candidate & ~cleaned).sum().item())
|
| 333 |
+
filled = _fill_holes(cleaned, allowed, check_cancel)
|
| 334 |
+
keep_mask = _remove_isolated(filled)
|
| 335 |
+
isolated_count += int((filled & ~keep_mask).sum().item())
|
| 336 |
+
holes_filled_count += int((keep_mask & ~cleaned).sum().item())
|
| 337 |
kept_count += int(keep_mask.sum().item())
|
| 338 |
visible_rows = torch.where(keep_mask.any(dim=1))[0]
|
| 339 |
if visible_rows.numel():
|
|
|
|
| 343 |
crop_y0 = min(crop_y0, int(visible_rows[0].item()))
|
| 344 |
crop_y1 = max(crop_y1, int(visible_rows[-1].item()) + 1)
|
| 345 |
|
| 346 |
+
# Build all final pixels afresh from C, including filled holes;
|
| 347 |
+
# then reconstruct using general source-over for capped alpha.
|
| 348 |
for row0 in range(0, height, rows_per_chunk):
|
| 349 |
check_cancel()
|
| 350 |
row1 = min(height, row0 + rows_per_chunk)
|
|
|
|
| 354 |
c = Image_C[0 if nc == 1 else batch, row0:row1].detach().to(
|
| 355 |
device="cpu", dtype=torch.float64
|
| 356 |
)
|
| 357 |
+
aa = _alpha(a)
|
| 358 |
+
rgba = torch.cat((c[..., :3], aa), dim=-1)
|
| 359 |
+
b = torch.where(keep_mask[row0:row1, :, None], rgba, 0.0).to(output_dtype)
|
| 360 |
output[batch, row0:row1].copy_(b)
|
| 361 |
+
stored_b = b.to(torch.float64)
|
| 362 |
+
ba = stored_b[..., 3:4]
|
| 363 |
+
alpha = ba + aa * (1.0 - ba)
|
| 364 |
+
premult = stored_b[..., :3] * ba + a[..., :3] * aa * (1.0 - ba)
|
| 365 |
+
safe_alpha = torch.where(alpha > 0.0, alpha, 1.0)
|
| 366 |
+
rgb = torch.where(alpha > 0.0, premult / safe_alpha, a[..., :3])
|
| 367 |
reconstructed = torch.cat((rgb, alpha), dim=-1)
|
| 368 |
check_image[batch, row0:row1].copy_(reconstructed.to(output_dtype))
|
| 369 |
visible = (alpha > 0.0) | (_alpha(c) > 0.0)
|
| 370 |
rgb_error = torch.where(visible, (rgb - c[..., :3]).abs(), 0.0)
|
| 371 |
max_rgb_error = max(max_rgb_error, float(rgb_error.max().item()))
|
| 372 |
max_alpha_error = max(max_alpha_error, float((alpha - _alpha(c)).abs().max().item()))
|
| 373 |
+
partial_alpha_count += int(((ba > 0.0) & (ba < 1.0)).sum().item())
|
| 374 |
|
| 375 |
if crop_x1 == 0:
|
| 376 |
crop_x0 = crop_y0 = 0
|
|
|
|
| 386 |
)
|
| 387 |
|
| 388 |
_LOG.info(
|
| 389 |
+
"[Salia Max Alpha Overlay] %dx%d, batch=%d; kept=%d; initial removals: "
|
| 390 |
+
"infeasible=%d, old-alpha<=1%%=%d, change<=4%%=%d, zero-A-alpha=%d, "
|
| 391 |
+
"isolated=%d; enclosed pixels filled=%d; "
|
| 392 |
"crop x=%d, y=%d, width=%d, height=%d; actual full-image "
|
| 393 |
"max RGB difference=%.9g, max alpha difference=%.9g; "
|
| 394 |
+
"kept pixels with partial overlay alpha=%d.",
|
| 395 |
width, height, batches, kept_count,
|
| 396 |
+
infeasible_count, old_alpha_count, similar_count, zero_alpha_count,
|
| 397 |
+
isolated_count, holes_filled_count,
|
| 398 |
crop_x0, crop_y0, crop_width, crop_height,
|
| 399 |
+
max_rgb_error, max_alpha_error, partial_alpha_count,
|
| 400 |
)
|
| 401 |
return (output, crop_x0, crop_y0, crop_width, crop_height, check_image)
|
| 402 |
|