From 839e26abf50f1e2ad5dc219e966431857a603318 Mon Sep 17 00:00:00 2001 From: Wagner Bruna Date: Mon, 9 Feb 2026 21:06:22 -0300 Subject: [PATCH] fix: correct sdapi handling of reference images on txt2img --- examples/server/main.cpp | 63 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/examples/server/main.cpp b/examples/server/main.cpp index 2173b1fb8..de1c6e6ef 100644 --- a/examples/server/main.cpp +++ b/examples/server/main.cpp @@ -914,28 +914,29 @@ int main(int argc, const char** argv) { std::vector pmid_images; std::vector ref_images; - if (img2img) { - auto decode_image = [](sd_image_t& image, std::string encoded) -> bool { - // remove data URI prefix if present ("data:image/png;base64,") - auto comma_pos = encoded.find(','); - if (comma_pos != std::string::npos) { - encoded = encoded.substr(comma_pos + 1); - } - std::vector img_data = base64_decode(encoded); - if (!img_data.empty()) { - int img_w = image.width; - int img_h = image.height; - uint8_t* raw_data = load_image_from_memory( - (const char*)img_data.data(), (int)img_data.size(), - img_w, img_h, - image.width, image.height, image.channel); - if (raw_data) { - image = {(uint32_t)img_w, (uint32_t)img_h, image.channel, raw_data}; - return true; - } + auto decode_image = [](sd_image_t& image, std::string encoded) -> bool { + // remove data URI prefix if present ("data:image/png;base64,") + auto comma_pos = encoded.find(','); + if (comma_pos != std::string::npos) { + encoded = encoded.substr(comma_pos + 1); + } + std::vector img_data = base64_decode(encoded); + if (!img_data.empty()) { + int img_w = image.width; + int img_h = image.height; + uint8_t* raw_data = load_image_from_memory( + (const char*)img_data.data(), (int)img_data.size(), + img_w, img_h, + image.width, image.height, image.channel); + if (raw_data) { + image = {(uint32_t)img_w, (uint32_t)img_h, image.channel, raw_data}; + return true; } - return false; - }; + } + return false; + }; + + if (img2img) { if (j.contains("init_images") && j["init_images"].is_array() && !j["init_images"].empty()) { std::string encoded = j["init_images"][0].get(); @@ -959,16 +960,6 @@ int main(int argc, const char** argv) { mask_image.data = mask_data.data(); } - if (j.contains("extra_images") && j["extra_images"].is_array()) { - for (auto extra_image : j["extra_images"]) { - std::string encoded = extra_image.get(); - sd_image_t tmp_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr}; - if (decode_image(tmp_image, encoded)) { - ref_images.push_back(tmp_image); - } - } - } - float denoising_strength = j.value("denoising_strength", -1.f); if (denoising_strength >= 0.f) { denoising_strength = std::min(denoising_strength, 1.0f); @@ -976,6 +967,16 @@ int main(int argc, const char** argv) { } } + if (j.contains("extra_images") && j["extra_images"].is_array()) { + for (auto extra_image : j["extra_images"]) { + std::string encoded = extra_image.get(); + sd_image_t tmp_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr}; + if (decode_image(tmp_image, encoded)) { + ref_images.push_back(tmp_image); + } + } + } + sd_img_gen_params_t img_gen_params = { sd_loras.data(), static_cast(sd_loras.size()),