diff --git a/otherarch/sdcpp/sdtype_adapter.cpp b/otherarch/sdcpp/sdtype_adapter.cpp index 4657d38e9..345dd7a27 100644 --- a/otherarch/sdcpp/sdtype_adapter.cpp +++ b/otherarch/sdcpp/sdtype_adapter.cpp @@ -604,22 +604,22 @@ sd_generation_outputs sdtype_generate(const sd_generation_inputs inputs) } if (desiredWidth < minsize || desiredHeight < minsize) { // Enforce minsize only if it won't exceed maxsize - if (aspect_ratio > 1.0f) { // wider than tall - // Try to scale width up to max of (minsize, maxsize) - desiredWidth = std::min(maxsize, std::max(minsize, desiredWidth)); - desiredHeight = static_cast(desiredWidth / aspect_ratio); - if (desiredHeight > maxsize) { // If height now exceeds maxsize, clamp based on height instead + float scale_w = static_cast(minsize) / desiredWidth; + float scale_h = static_cast(minsize) / desiredHeight; + float scale = std::max(scale_w, scale_h); + int newWidth = static_cast(desiredWidth * scale); + int newHeight = static_cast(desiredHeight * scale); + if (newWidth > maxsize || newHeight > maxsize) { + if (aspect_ratio > 1.0f) { + desiredWidth = maxsize; + desiredHeight = static_cast(maxsize / aspect_ratio); + } else { desiredHeight = maxsize; desiredWidth = static_cast(maxsize * aspect_ratio); } } else { - // Taller than wide or square - desiredHeight = std::min(maxsize, std::max(minsize, desiredHeight)); - desiredWidth = static_cast(desiredHeight * aspect_ratio); - if (desiredWidth > maxsize) { - desiredWidth = maxsize; - desiredHeight = static_cast(maxsize / aspect_ratio); - } + desiredWidth = newWidth; + desiredHeight = newHeight; } }