11#include " stable-diffusion-abi.h"
22
33#include " stable-diffusion.h"
4- #include " base64.hpp"
54#include < string>
65#include < cstring>
76#include < map>
7+ #include < algorithm>
8+ #include < iterator>
89#include < vector>
10+
911/* ================================================= StableDiffusion ABI API =============================================*/
1012
1113const static std::map<std::string, enum SDLogLevel> SDLogLevelMap = {
@@ -188,7 +190,7 @@ bool load_from_file(void* sd, const char* file_path, const char* schedule) {
188190 return false ;
189191};
190192
191- const char * txt2img (void * sd, const sd_txt2img_options* opt) {
193+ const uint8_t * txt2img (void * sd, const sd_txt2img_options* opt) {
192194 const auto sm = std::string (opt->sample_method );
193195 const auto it = SampleMethodMap.find (sm);
194196 if (it != SampleMethodMap.end ()) {
@@ -204,16 +206,23 @@ const char* txt2img(void* sd, const sd_txt2img_options* opt) {
204206 opt->seed ,
205207 opt->batch_count
206208 );
207- const auto str = code::base64_encode<std::string, std::vector<uint8_t *>>(result, false );
208- const auto buffer = new char [str.size ()];
209- std::memcpy (buffer, str.c_str (), str.size ());
209+ const auto image_size = opt->width * opt->height * 3 ;
210+ std::vector<uint8_t > images;
211+ images.reserve (image_size * opt->batch_count );
212+ for (const auto img: result) {
213+ if (img != nullptr ) {
214+ std::copy_n (img, image_size, std::back_inserter (images));
215+ }
216+ };
217+ const auto buffer = new uint8_t [image_size * opt->batch_count ];
218+ std::memcpy (buffer, images.data (), images.size ());
210219 return buffer;
211220 }
212221 delete opt;
213222 return nullptr ;
214223};
215224
216- const char * img2img (void * sd, const sd_img2img_options* opt) {
225+ const uint8_t * img2img (void * sd, const sd_img2img_options* opt) {
217226 const auto sm = std::string (opt->sample_method );
218227 const auto it = SampleMethodMap.find (sm);
219228 if (it != SampleMethodMap.end ()) {
@@ -230,9 +239,16 @@ const char* img2img(void* sd, const sd_img2img_options* opt) {
230239 opt->strength ,
231240 opt->seed
232241 );
233- const auto str = code::base64_encode<std::string, std::vector<uint8_t *>>(result, false );
234- const auto buffer = new char [str.size ()];
235- std::memcpy (buffer, str.c_str (), str.size ());
242+ const auto image_size = opt->width * opt->height * 3 ;
243+ std::vector<uint8_t > images;
244+ images.reserve (image_size);
245+ for (const auto img: result) {
246+ if (img != nullptr ) {
247+ std::copy_n (img, image_size, std::back_inserter (images));
248+ }
249+ };
250+ const auto buffer = new uint8_t [image_size];
251+ std::memcpy (buffer, images.data (), images.size ());
236252 return buffer;
237253 }
238254 delete opt;
0 commit comments