|
| 1 | +import torch |
| 2 | + |
| 3 | +from comfy.cli_args import args as cli_args |
| 4 | + |
| 5 | +if not torch.cuda.is_available(): |
| 6 | + cli_args.cpu = True |
| 7 | + |
| 8 | +import nodes # noqa: E402 |
| 9 | + |
| 10 | + |
| 11 | +def test_invert_rgb_inverts_every_channel(): |
| 12 | + image = torch.rand(1, 4, 4, 3) |
| 13 | + |
| 14 | + s, = nodes.ImageInvert().invert(image) |
| 15 | + |
| 16 | + assert s.shape == image.shape |
| 17 | + assert torch.allclose(s, 1.0 - image) |
| 18 | + |
| 19 | + |
| 20 | +def test_invert_rgba_leaves_alpha_untouched(): |
| 21 | + image = torch.rand(2, 4, 4, 4) |
| 22 | + |
| 23 | + s, = nodes.ImageInvert().invert(image) |
| 24 | + |
| 25 | + assert s.shape == image.shape |
| 26 | + assert torch.allclose(s[..., :3], 1.0 - image[..., :3]) |
| 27 | + assert torch.equal(s[..., 3], image[..., 3]) |
| 28 | + |
| 29 | + |
| 30 | +def test_invert_rgba_keeps_transparent_pixels_transparent(): |
| 31 | + image = torch.zeros(1, 1, 2, 4) |
| 32 | + image[..., 3] = torch.tensor([1.0, 0.0]) |
| 33 | + |
| 34 | + s, = nodes.ImageInvert().invert(image) |
| 35 | + |
| 36 | + assert s[0, 0, 0, 3] == 1.0 |
| 37 | + assert s[0, 0, 1, 3] == 0.0 |
| 38 | + |
| 39 | + |
| 40 | +def test_invert_does_not_mutate_input(): |
| 41 | + image = torch.rand(1, 4, 4, 4) |
| 42 | + original = image.clone() |
| 43 | + |
| 44 | + nodes.ImageInvert().invert(image) |
| 45 | + |
| 46 | + assert torch.equal(image, original) |
0 commit comments