"""DUMAS resolution helpers inspired by Pixaroma's Resolution node.""" from __future__ import annotations import json DEFAULT_STATE = { "mode": "preset", "ratio": "1:1", "w": 1024, "h": 1024, "custom_w": 1024, "custom_h": 1024, "custom_ratio_w": 4, "custom_ratio_h": 3, "snap": 16, "batch": 1, } def _clamp(value, low=64, high=16384): return max(low, min(high, int(value))) def resolution_wh(state_text): try: state = json.loads(state_text or "{}") width = _clamp(state.get("w", DEFAULT_STATE["w"])) height = _clamp(state.get("h", DEFAULT_STATE["h"])) except (TypeError, ValueError, json.JSONDecodeError): width, height = DEFAULT_STATE["w"], DEFAULT_STATE["h"] return width, height def _empty_latent(state_text, width, height): import torch try: batch = int(json.loads(state_text or "{}").get("batch", 1)) except (TypeError, ValueError, json.JSONDecodeError): batch = 1 batch = max(1, min(64, batch)) return {"samples": torch.zeros((batch, 4, max(1, height // 8), max(1, width // 8)))} class DumasResolutionNode: DESCRIPTION = ( "DUMAS Resolution picker based on Pixaroma. Choose an AI-friendly aspect " "ratio and size, or enter a custom ratio/resolution with snapping. Outputs " "width, height, and an Empty-Latent-compatible image latent." ) @classmethod def INPUT_TYPES(cls): return { "required": {}, "hidden": { "ResolutionState": ( "STRING", {"default": json.dumps(DEFAULT_STATE)}, ), }, } RETURN_TYPES = ("INT", "INT", "LATENT") RETURN_NAMES = ("width", "height", "latent") FUNCTION = "get_resolution" CATEGORY = "Dumas/Values" def get_resolution(self, ResolutionState=""): width, height = resolution_wh(ResolutionState) return width, height, _empty_latent(ResolutionState, width, height) NODE_CLASS_MAPPINGS = {"DumasResolution": DumasResolutionNode} NODE_DISPLAY_NAME_MAPPINGS = {"DumasResolution": "DUMAS Resolution"}