File size: 5,475 Bytes
37d34c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import custom_nodes.Derfuu_Nodes.types as type
import custom_nodes.Derfuu_Nodes.fields as field
from custom_nodes.Derfuu_Nodes.tree import TREE_LATENTS, TREE_IMAGES

import math
import torch

import comfy.utils


class EmptyLatentImage:
    def __init__(self, device="cpu"):
        self.device = device

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "TUPLE": (type.TUPLE,),
                "batch_size": field.INT,
            }
        }

    RETURN_TYPES = (type.LATENT,)
    FUNCTION = "generate"
    CATEGORY = TREE_LATENTS

    def generate(self, TUPLE, batch_size=1):
        width = int(TUPLE[0])
        height = int(TUPLE[1])

        latent = torch.zeros([batch_size, 4, height // 8, width // 8])
        return ({"samples": latent},)


class ImageScale_Ratio:
    upscale_methods = ["nearest-exact", "bilinear", "area"]
    crop_methods = ["disabled", "center"]

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "IMAGE": (type.IMAGE,),
                "TUPLE": (type.TUPLE,),
                "modifier": field.FLOAT,
                "upscale_method": (cls.upscale_methods,),
                "crop": (cls.crop_methods,)}}

    RETURN_TYPES = (type.IMAGE, type.TUPLE,)
    FUNCTION = "upscale"

    CATEGORY = TREE_IMAGES

    def upscale(self, IMAGE, upscale_method, TUPLE, modifier, crop):
        samples = IMAGE.movedim(-1, 1)

        width_B = int(TUPLE[0])
        height_B = int(TUPLE[1])

        height = math.ceil(height_B * modifier)
        width = math.ceil(width_B * modifier)
        cls = comfy.utils.common_upscale(samples, width, height, upscale_method, crop)
        cls = cls.movedim(1, -1)
        return (cls, (width, height),)


class ImageScale_Side:
    upscale_methods = ["nearest-exact", "bilinear", "area"]
    crop_methods = ["disabled", "center"]

    def __init__(self) -> None:
        pass

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "IMAGE": (type.IMAGE,),
                "TUPLE": (type.TUPLE,),
                "side_length": field.INT,
                "side": (["Width", "Height"],),
                "upscale_method": (cls.upscale_methods,),
                "crop": (cls.crop_methods,)}}

    RETURN_TYPES = (type.IMAGE, type.TUPLE,)
    FUNCTION = "upscale"

    CATEGORY = TREE_IMAGES

    def upscale(self, IMAGE, upscale_method, TUPLE, side_length, side, crop):
        samples = IMAGE.movedim(-1, 1)

        width_B = int(TUPLE[0])
        height_B = int(TUPLE[1])

        width = width_B
        height = height_B

        if side == "Width":
            heigh_ratio = height_B / width_B
            width = side_length
            height = heigh_ratio * width
        elif side == "Height":
            width_ratio = width_B / height_B
            height = side_length
            width = width_ratio * height

        width = math.ceil(width)
        height = math.ceil(height)

        cls = comfy.utils.common_upscale(samples, width, height, upscale_method, crop)
        cls = cls.movedim(1, -1)
        return (cls, (width, height), )


class LatentScale_Ratio:
    scale_methods = (["nearest-exact", "bilinear", "area"],)
    crop_methods = (["disabled", "center"],)

    def __init__(self):
        pass

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "LATENT": (type.LATENT,),
                "TUPLE": (type.TUPLE,),
                "modifier": field.FLOAT,
                "scale_method": cls.scale_methods,
                "crop": cls.crop_methods,
            }
        }

    RETURN_TYPES = (type.LATENT, type.TUPLE,)
    FUNCTION = "scale"
    CATEGORY = TREE_LATENTS

    def scale(self, LATENT, scale_method, crop, modifier, TUPLE):

        width = int(TUPLE[0] * modifier)
        height = int(TUPLE[1] * modifier)

        cls = LATENT.copy()
        cls["samples"] = comfy.utils.common_upscale(LATENT["samples"], width // 8, height // 8, scale_method, crop)
        return (cls, (width, height),)


class LatentScale_Side:
    upscale_methods = ["nearest-exact", "bilinear", "area"]
    crop_methods = ["disabled", "center"]

    def __init__(self) -> None:
        pass

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "LATENT": (type.LATENT,),
                "TUPLE": (type.TUPLE,),
                "side_length": field.INT,
                "side": (["Width", "Height"],),
                "scale_method": (cls.upscale_methods,),
                "crop": (cls.crop_methods,)}}

    RETURN_TYPES = (type.LATENT, type.TUPLE,)
    FUNCTION = "upscale"

    CATEGORY = TREE_LATENTS

    def upscale(self, LATENT, scale_method, TUPLE, side_length, side, crop):
        width_B = int(TUPLE[0])
        height_B = int(TUPLE[1])

        width = width_B
        height = height_B

        if side == "Width":
            heigh_ratio = height_B / width_B
            width = side_length
            height = heigh_ratio * width
        elif side == "Height":
            width_ratio = width_B / height_B
            height = side_length
            width = width_ratio * height

        width = math.ceil(width)
        height = math.ceil(height)

        cls = LATENT.copy()
        cls["samples"] = comfy.utils.common_upscale(LATENT["samples"], width // 8, height // 8, scale_method, crop)
        return (cls, (width, height),)