Fix image_alpha_fix only padding a single channel
When the destination had more channels than the source, image_alpha_fix padded the source by exactly one channel regardless of the actual difference, so a channel gap greater than one (e.g. a grayscale source against an RGBA destination) left the tensors mismatched and broke the downstream blend/composite. Pad by the full channel difference and fill the added channels with 1.0.
This commit is contained in:
parent
f3a36e7484
commit
5d70dbbdf7
|
|
@ -86,6 +86,7 @@ def image_alpha_fix(destination, source):
|
|||
if destination.shape[-1] < source.shape[-1]:
|
||||
source = source[...,:destination.shape[-1]]
|
||||
elif destination.shape[-1] > source.shape[-1]:
|
||||
source = torch.nn.functional.pad(source, (0, 1))
|
||||
source[..., -1] = 1.0
|
||||
pad = destination.shape[-1] - source.shape[-1]
|
||||
source = torch.nn.functional.pad(source, (0, pad))
|
||||
source[..., -pad:] = 1.0
|
||||
return destination, source
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
import torch
|
||||
|
||||
import node_helpers
|
||||
|
||||
|
||||
class TestImageAlphaFix:
|
||||
def test_pads_rgb_to_rgba(self):
|
||||
destination = torch.zeros(1, 2, 2, 4)
|
||||
source = torch.full((1, 2, 2, 3), 0.5)
|
||||
_, source = node_helpers.image_alpha_fix(destination, source)
|
||||
assert source.shape[-1] == 4
|
||||
assert torch.all(source[..., :3] == 0.5) # original channels preserved
|
||||
assert torch.all(source[..., 3] == 1.0) # added alpha
|
||||
|
||||
def test_pads_more_than_one_channel(self):
|
||||
# channel gap > 1 (grayscale source, RGBA destination) must still match up.
|
||||
destination = torch.zeros(1, 2, 2, 4)
|
||||
source = torch.zeros(1, 2, 2, 1)
|
||||
_, source = node_helpers.image_alpha_fix(destination, source)
|
||||
assert source.shape[-1] == 4
|
||||
assert torch.all(source[..., 1:] == 1.0)
|
||||
|
||||
def test_truncates_when_source_has_more_channels(self):
|
||||
destination = torch.zeros(1, 2, 2, 3)
|
||||
source = torch.ones(1, 2, 2, 4)
|
||||
_, source = node_helpers.image_alpha_fix(destination, source)
|
||||
assert source.shape[-1] == 3
|
||||
Loading…
Reference in New Issue