Skip to content

Commit 2594be7

Browse files
committed
fixed scaling behavior again
1 parent 2635e4b commit 2594be7

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

otherarch/sdcpp/sdtype_adapter.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ static inline int roundup_64(int n) {
332332
return ((n + 63) / 64) * 64;
333333
}
334334

335+
static inline int roundnearest(int multiple, int n) {
336+
return ((n + (multiple/2)) / multiple) * multiple;
337+
}
338+
335339
//scale dimensions to ensure width and height stay within limits
336340
//img_hard_limit = sdclamped, hard size limit per side, no side can exceed this
337341
//square limit = total NxN resolution based limit to also apply
@@ -589,54 +593,25 @@ sd_generation_outputs sdtype_generate(const sd_generation_inputs inputs)
589593
extraimage_buffers.push_back(kcpp_base64_decode(extra_image_data[i]));
590594
input_extraimage_buffers.push_back(stbi_load_from_memory(extraimage_buffers[i].data(), extraimage_buffers[i].size(), &nx2, &ny2, &nc2, desiredchannels));
591595
// Resize the image
596+
float aspect_ratio = static_cast<float>(nx2) / ny2;
592597
int desiredWidth = nx2;
593598
int desiredHeight = ny2;
594-
float aspect_ratio = static_cast<float>(nx2) / ny2;
595-
int maxsize = 1024; // no image can exceed this
596-
int minsize = 256;
597-
598-
if (desiredWidth > maxsize || desiredHeight > maxsize) { // Enforce maxsize first
599-
if (aspect_ratio > 1.0f) { // wider than tall
600-
desiredWidth = maxsize;
601-
desiredHeight = static_cast<int>(maxsize / aspect_ratio);
602-
} else { // taller than wide or square
603-
desiredHeight = maxsize;
604-
desiredWidth = static_cast<int>(maxsize * aspect_ratio);
605-
}
606-
}
607-
608-
if (desiredWidth < minsize || desiredHeight < minsize) { // Enforce minsize only if it won't exceed maxsize
609-
float scale_w = static_cast<float>(minsize) / desiredWidth;
610-
float scale_h = static_cast<float>(minsize) / desiredHeight;
611-
float scale = std::max(scale_w, scale_h);
612-
int newWidth = static_cast<int>(desiredWidth * scale);
613-
int newHeight = static_cast<int>(desiredHeight * scale);
614-
if (newWidth > maxsize || newHeight > maxsize) {
615-
if (aspect_ratio > 1.0f) {
616-
desiredWidth = maxsize;
617-
desiredHeight = static_cast<int>(maxsize / aspect_ratio);
618-
} else {
619-
desiredHeight = maxsize;
620-
desiredWidth = static_cast<int>(maxsize * aspect_ratio);
621-
}
622-
} else {
623-
desiredWidth = newWidth;
624-
desiredHeight = newHeight;
625-
}
626-
}
627-
628-
//round dims down to 64
629-
desiredWidth = rounddown_64(desiredWidth);
630-
desiredHeight = rounddown_64(desiredHeight);
631-
if(desiredWidth<64)
599+
int smallestsrcdim = std::min(img2imgW,img2imgH);
600+
if(desiredWidth > desiredHeight)
632601
{
633-
desiredWidth = 64;
634-
}
635-
if(desiredHeight<64)
636-
{
637-
desiredHeight = 64;
602+
desiredWidth = smallestsrcdim;
603+
desiredHeight = smallestsrcdim / aspect_ratio;
604+
} else {
605+
desiredHeight = smallestsrcdim;
606+
desiredWidth = smallestsrcdim * aspect_ratio;
638607
}
639608

609+
//round dims to 64
610+
desiredWidth = roundnearest(16,desiredWidth);
611+
desiredHeight = roundnearest(16,desiredHeight);
612+
desiredWidth = std::clamp(desiredWidth,64,1024);
613+
desiredHeight = std::clamp(desiredHeight,64,1024);
614+
640615
if(!sd_is_quiet && sddebugmode==1)
641616
{
642617
printf("Resize Extraimg: %dx%d to %dx%d\n",nx2,ny2,desiredWidth,desiredHeight);

0 commit comments

Comments
 (0)