@@ -604,22 +604,22 @@ sd_generation_outputs sdtype_generate(const sd_generation_inputs inputs)
604604 }
605605
606606 if (desiredWidth < minsize || desiredHeight < minsize) { // Enforce minsize only if it won't exceed maxsize
607- if (aspect_ratio > 1 .0f ) { // wider than tall
608- // Try to scale width up to max of (minsize, maxsize)
609- desiredWidth = std::min (maxsize, std::max (minsize, desiredWidth));
610- desiredHeight = static_cast <int >(desiredWidth / aspect_ratio);
611- if (desiredHeight > maxsize) { // If height now exceeds maxsize, clamp based on height instead
607+ float scale_w = static_cast <float >(minsize) / desiredWidth;
608+ float scale_h = static_cast <float >(minsize) / desiredHeight;
609+ float scale = std::max (scale_w, scale_h);
610+ int newWidth = static_cast <int >(desiredWidth * scale);
611+ int newHeight = static_cast <int >(desiredHeight * scale);
612+ if (newWidth > maxsize || newHeight > maxsize) {
613+ if (aspect_ratio > 1 .0f ) {
614+ desiredWidth = maxsize;
615+ desiredHeight = static_cast <int >(maxsize / aspect_ratio);
616+ } else {
612617 desiredHeight = maxsize;
613618 desiredWidth = static_cast <int >(maxsize * aspect_ratio);
614619 }
615620 } else {
616- // Taller than wide or square
617- desiredHeight = std::min (maxsize, std::max (minsize, desiredHeight));
618- desiredWidth = static_cast <int >(desiredHeight * aspect_ratio);
619- if (desiredWidth > maxsize) {
620- desiredWidth = maxsize;
621- desiredHeight = static_cast <int >(maxsize / aspect_ratio);
622- }
621+ desiredWidth = newWidth;
622+ desiredHeight = newHeight;
623623 }
624624 }
625625
0 commit comments