MAX_OUTPUT_DIM = 2048 MAX_OUTPUT_DIM_FAST = 1024 def max_dim_for_mode(mode): return MAX_OUTPUT_DIM_FAST if mode == "fast" else MAX_OUTPUT_DIM def compute_output_dimensions(w, h, max_dim=MAX_OUTPUT_DIM): # Pin the long side to max_dim and scale the short side proportionally. # We snap to the nearest multiple of 8 (not floor) to minimise aspect ratio # distortion: floor always undershoots, whereas rounding keeps the error # within ±4px, which halves the worst-case ratio deviation. if w > h: nw = max_dim nh = round(nw * h / w / 8) * 8 else: nh = max_dim nw = round(nh * w / h / 8) * 8 return nw, nh