From 499e5a0078e975b171dfd235b2672bceeb48be6a Mon Sep 17 00:00:00 2001 From: EnragedAntelope Date: Thu, 16 Jul 2026 17:25:38 -0400 Subject: [PATCH] Enforce min_res guarantee after nearest-multiple rounding Nearest-multiple rounding could drop a dimension below min_res in min-res mode (e.g. min_res=1100, multiple_of=256 -> 1024); bump such dimensions to the next multiple instead. Co-Authored-By: Claude Fable 5 --- nodes.py | 9 +++++++++ tests/test_nodes.py | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/nodes.py b/nodes.py index c53bcdf..d9dd8e1 100644 --- a/nodes.py +++ b/nodes.py @@ -245,6 +245,15 @@ class ConstrainResolution(io.ComfyNode): final_width = ConstrainResolution.round_to_multiple(int(new_width), multiple_of) final_height = ConstrainResolution.round_to_multiple(int(new_height), multiple_of) + # Nearest-multiple rounding can drop a dimension back below min_res + # (e.g. min_res=1100, multiple_of=256 -> 1024). In min-res mode, bump + # such a dimension up to the next multiple so the guarantee holds. + if constraint_mode == ConstraintMode.MIN_RES.value: + if final_width < min_res: + final_width = math.ceil(min_res / multiple_of) * multiple_of + if final_height < min_res: + final_height = math.ceil(min_res / multiple_of) * multiple_of + # 4. In strict mode, rounding can exceed max_res (e.g. round_to_multiple(2160, 32) = 2176). # Clamp to the largest valid multiple of multiple_of that is <= max_res. if constraint_mode == ConstraintMode.MAX_RES_STRICT.value: diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 734a420..6ae40bd 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -84,6 +84,17 @@ class TestMinRes: # The short dimension should be at least min_res assert min(w, h) >= 704 + def test_rounding_never_drops_below_min_res(self): + """Nearest-multiple rounding must not violate the min_res guarantee. + + min_res=1100 with multiple_of=256: 1100 would round to 1024 (< min_res); + it must be bumped up to 1280 instead. + """ + w, h = calc(1000, 1000, 1100, 4096, 256, MIN) + assert min(w, h) >= 1100 + assert w % 256 == 0 + assert h % 256 == 0 + def test_min_res_normal_case(self): """In MIN_RES mode with no extreme ratio, both dims should stay within normal range.""" w, h = calc(500, 333, 704, 2048, 32, MIN)