1515// #include "preprocessing.hpp"
1616#include " stable-diffusion.h"
1717
18- #define STB_IMAGE_IMPLEMENTATION
19- #define STB_IMAGE_STATIC
20- #include " stb_image.h"
21-
22- #define STB_IMAGE_WRITE_IMPLEMENTATION
23- #define STB_IMAGE_WRITE_STATIC
24- #include " stb_image_write.h"
25-
26- #define STB_IMAGE_RESIZE_IMPLEMENTATION
27- #define STB_IMAGE_RESIZE_STATIC
28- #include " stb_image_resize.h"
18+ #include " common/common.hpp"
2919
3020#include " avi_writer.h"
3121
32- #include " common/common.hpp"
33-
3422const char * previews_str[] = {
3523 " none" ,
3624 " proj" ,
@@ -335,94 +323,6 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
335323 fflush (out_stream);
336324}
337325
338- uint8_t * load_image (const char * image_path, int & width, int & height, int expected_width = 0 , int expected_height = 0 , int expected_channel = 3 ) {
339- int c = 0 ;
340- uint8_t * image_buffer = (uint8_t *)stbi_load (image_path, &width, &height, &c, expected_channel);
341- if (image_buffer == nullptr ) {
342- fprintf (stderr, " load image from '%s' failed\n " , image_path);
343- return nullptr ;
344- }
345- if (c < expected_channel) {
346- fprintf (stderr,
347- " the number of channels for the input image must be >= %d,"
348- " but got %d channels, image_path = %s\n " ,
349- expected_channel,
350- c,
351- image_path);
352- free (image_buffer);
353- return nullptr ;
354- }
355- if (width <= 0 ) {
356- fprintf (stderr, " error: the width of image must be greater than 0, image_path = %s\n " , image_path);
357- free (image_buffer);
358- return nullptr ;
359- }
360- if (height <= 0 ) {
361- fprintf (stderr, " error: the height of image must be greater than 0, image_path = %s\n " , image_path);
362- free (image_buffer);
363- return nullptr ;
364- }
365-
366- // Resize input image ...
367- if ((expected_width > 0 && expected_height > 0 ) && (height != expected_height || width != expected_width)) {
368- float dst_aspect = (float )expected_width / (float )expected_height;
369- float src_aspect = (float )width / (float )height;
370-
371- int crop_x = 0 , crop_y = 0 ;
372- int crop_w = width, crop_h = height;
373-
374- if (src_aspect > dst_aspect) {
375- crop_w = (int )(height * dst_aspect);
376- crop_x = (width - crop_w) / 2 ;
377- } else if (src_aspect < dst_aspect) {
378- crop_h = (int )(width / dst_aspect);
379- crop_y = (height - crop_h) / 2 ;
380- }
381-
382- if (crop_x != 0 || crop_y != 0 ) {
383- printf (" crop input image from %dx%d to %dx%d, image_path = %s\n " , width, height, crop_w, crop_h, image_path);
384- uint8_t * cropped_image_buffer = (uint8_t *)malloc (crop_w * crop_h * expected_channel);
385- if (cropped_image_buffer == nullptr ) {
386- fprintf (stderr, " error: allocate memory for crop\n " );
387- free (image_buffer);
388- return nullptr ;
389- }
390- for (int row = 0 ; row < crop_h; row++) {
391- uint8_t * src = image_buffer + ((crop_y + row) * width + crop_x) * expected_channel;
392- uint8_t * dst = cropped_image_buffer + (row * crop_w) * expected_channel;
393- memcpy (dst, src, crop_w * expected_channel);
394- }
395-
396- width = crop_w;
397- height = crop_h;
398- free (image_buffer);
399- image_buffer = cropped_image_buffer;
400- }
401-
402- printf (" resize input image from %dx%d to %dx%d\n " , width, height, expected_width, expected_height);
403- int resized_height = expected_height;
404- int resized_width = expected_width;
405-
406- uint8_t * resized_image_buffer = (uint8_t *)malloc (resized_height * resized_width * expected_channel);
407- if (resized_image_buffer == nullptr ) {
408- fprintf (stderr, " error: allocate memory for resize input image\n " );
409- free (image_buffer);
410- return nullptr ;
411- }
412- stbir_resize (image_buffer, width, height, 0 ,
413- resized_image_buffer, resized_width, resized_height, 0 , STBIR_TYPE_UINT8,
414- expected_channel, STBIR_ALPHA_CHANNEL_NONE, 0 ,
415- STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
416- STBIR_FILTER_BOX, STBIR_FILTER_BOX,
417- STBIR_COLORSPACE_SRGB, nullptr );
418- width = resized_width;
419- height = resized_height;
420- free (image_buffer);
421- image_buffer = resized_image_buffer;
422- }
423- return image_buffer;
424- }
425-
426326bool load_images_from_dir (const std::string dir,
427327 std::vector<sd_image_t >& images,
428328 int expected_width = 0 ,
@@ -457,7 +357,7 @@ bool load_images_from_dir(const std::string dir,
457357 }
458358 int width = 0 ;
459359 int height = 0 ;
460- uint8_t * image_buffer = load_image (path.c_str (), width, height, expected_width, expected_height);
360+ uint8_t * image_buffer = load_image_from_file (path.c_str (), width, height, expected_width, expected_height);
461361 if (image_buffer == nullptr ) {
462362 fprintf (stderr, " load image from '%s' failed\n " , path.c_str ());
463363 return false ;
@@ -593,7 +493,7 @@ int main(int argc, const char* argv[]) {
593493
594494 int width = 0 ;
595495 int height = 0 ;
596- init_image.data = load_image (gen_params.init_image_path .c_str (), width, height, gen_params.width , gen_params.height );
496+ init_image.data = load_image_from_file (gen_params.init_image_path .c_str (), width, height, gen_params.width , gen_params.height );
597497 if (init_image.data == nullptr ) {
598498 fprintf (stderr, " load image from '%s' failed\n " , gen_params.init_image_path .c_str ());
599499 release_all_resources ();
@@ -606,7 +506,7 @@ int main(int argc, const char* argv[]) {
606506
607507 int width = 0 ;
608508 int height = 0 ;
609- end_image.data = load_image (gen_params.end_image_path .c_str (), width, height, gen_params.width , gen_params.height );
509+ end_image.data = load_image_from_file (gen_params.end_image_path .c_str (), width, height, gen_params.width , gen_params.height );
610510 if (end_image.data == nullptr ) {
611511 fprintf (stderr, " load image from '%s' failed\n " , gen_params.end_image_path .c_str ());
612512 release_all_resources ();
@@ -618,7 +518,7 @@ int main(int argc, const char* argv[]) {
618518 int c = 0 ;
619519 int width = 0 ;
620520 int height = 0 ;
621- mask_image.data = load_image (gen_params.mask_image_path .c_str (), width, height, gen_params.width , gen_params.height , 1 );
521+ mask_image.data = load_image_from_file (gen_params.mask_image_path .c_str (), width, height, gen_params.width , gen_params.height , 1 );
622522 if (mask_image.data == nullptr ) {
623523 fprintf (stderr, " load image from '%s' failed\n " , gen_params.mask_image_path .c_str ());
624524 release_all_resources ();
@@ -637,7 +537,7 @@ int main(int argc, const char* argv[]) {
637537 if (gen_params.control_image_path .size () > 0 ) {
638538 int width = 0 ;
639539 int height = 0 ;
640- control_image.data = load_image (gen_params.control_image_path .c_str (), width, height, gen_params.width , gen_params.height );
540+ control_image.data = load_image_from_file (gen_params.control_image_path .c_str (), width, height, gen_params.width , gen_params.height );
641541 if (control_image.data == nullptr ) {
642542 fprintf (stderr, " load image from '%s' failed\n " , gen_params.control_image_path .c_str ());
643543 release_all_resources ();
@@ -658,7 +558,7 @@ int main(int argc, const char* argv[]) {
658558 for (auto & path : gen_params.ref_image_paths ) {
659559 int width = 0 ;
660560 int height = 0 ;
661- uint8_t * image_buffer = load_image (path.c_str (), width, height);
561+ uint8_t * image_buffer = load_image_from_file (path.c_str (), width, height);
662562 if (image_buffer == nullptr ) {
663563 fprintf (stderr, " load image from '%s' failed\n " , path.c_str ());
664564 release_all_resources ();
0 commit comments