@@ -656,13 +656,16 @@ int main(int argc, const char* argv[]) {
656
656
return 1 ;
657
657
}
658
658
659
- bool vae_decode_only = true ;
660
- uint8_t * input_image_buffer = NULL ;
659
+ bool vae_decode_only = true ;
660
+ uint8_t * input_image_buffer = NULL ;
661
+ uint8_t * control_image_buffer = NULL ;
661
662
if (params.mode == IMG2IMG || params.mode == IMG2VID) {
662
663
vae_decode_only = false ;
663
664
664
665
int c = 0 ;
665
- input_image_buffer = stbi_load (params.input_path .c_str (), ¶ms.width , ¶ms.height , &c, 3 );
666
+ int width = 0 ;
667
+ int height = 0 ;
668
+ input_image_buffer = stbi_load (params.input_path .c_str (), &width, &height, &c, 3 );
666
669
if (input_image_buffer == NULL ) {
667
670
fprintf (stderr, " load image from '%s' failed\n " , params.input_path .c_str ());
668
671
return 1 ;
@@ -672,29 +675,30 @@ int main(int argc, const char* argv[]) {
672
675
free (input_image_buffer);
673
676
return 1 ;
674
677
}
675
- if (params. width <= 0 ) {
678
+ if (width <= 0 ) {
676
679
fprintf (stderr, " error: the width of image must be greater than 0\n " );
677
680
free (input_image_buffer);
678
681
return 1 ;
679
682
}
680
- if (params. height <= 0 ) {
683
+ if (height <= 0 ) {
681
684
fprintf (stderr, " error: the height of image must be greater than 0\n " );
682
685
free (input_image_buffer);
683
686
return 1 ;
684
687
}
685
688
686
689
// Resize input image ...
687
- if (params.height % 64 != 0 || params.width % 64 != 0 ) {
688
- int resized_height = params.height + (64 - params.height % 64 );
689
- int resized_width = params.width + (64 - params.width % 64 );
690
+ if (params.height != height || params.width != width) {
691
+ printf (" resize input image from %dx%d to %dx%d\n " , width, height, params.width , params.height );
692
+ int resized_height = params.height ;
693
+ int resized_width = params.width ;
690
694
691
695
uint8_t * resized_image_buffer = (uint8_t *)malloc (resized_height * resized_width * 3 );
692
696
if (resized_image_buffer == NULL ) {
693
697
fprintf (stderr, " error: allocate memory for resize input image\n " );
694
698
free (input_image_buffer);
695
699
return 1 ;
696
700
}
697
- stbir_resize (input_image_buffer, params. width , params. height , 0 ,
701
+ stbir_resize (input_image_buffer, width, height, 0 ,
698
702
resized_image_buffer, resized_width, resized_height, 0 , STBIR_TYPE_UINT8,
699
703
3 /* RGB channel*/ , STBIR_ALPHA_CHANNEL_NONE, 0 ,
700
704
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
@@ -704,8 +708,6 @@ int main(int argc, const char* argv[]) {
704
708
// Save resized result
705
709
free (input_image_buffer);
706
710
input_image_buffer = resized_image_buffer;
707
- params.height = resized_height;
708
- params.width = resized_width;
709
711
}
710
712
}
711
713
@@ -732,31 +734,32 @@ int main(int argc, const char* argv[]) {
732
734
return 1 ;
733
735
}
734
736
737
+ sd_image_t * control_image = NULL ;
738
+ if (params.controlnet_path .size () > 0 && params.control_image_path .size () > 0 ) {
739
+ int c = 0 ;
740
+ control_image_buffer = stbi_load (params.control_image_path .c_str (), ¶ms.width , ¶ms.height , &c, 3 );
741
+ if (control_image_buffer == NULL ) {
742
+ fprintf (stderr, " load image from '%s' failed\n " , params.control_image_path .c_str ());
743
+ return 1 ;
744
+ }
745
+ control_image = new sd_image_t {(uint32_t )params.width ,
746
+ (uint32_t )params.height ,
747
+ 3 ,
748
+ control_image_buffer};
749
+ if (params.canny_preprocess ) { // apply preprocessor
750
+ control_image->data = preprocess_canny (control_image->data ,
751
+ control_image->width ,
752
+ control_image->height ,
753
+ 0 .08f ,
754
+ 0 .08f ,
755
+ 0 .8f ,
756
+ 1 .0f ,
757
+ false );
758
+ }
759
+ }
760
+
735
761
sd_image_t * results;
736
762
if (params.mode == TXT2IMG) {
737
- sd_image_t * control_image = NULL ;
738
- if (params.controlnet_path .size () > 0 && params.control_image_path .size () > 0 ) {
739
- int c = 0 ;
740
- input_image_buffer = stbi_load (params.control_image_path .c_str (), ¶ms.width , ¶ms.height , &c, 3 );
741
- if (input_image_buffer == NULL ) {
742
- fprintf (stderr, " load image from '%s' failed\n " , params.control_image_path .c_str ());
743
- return 1 ;
744
- }
745
- control_image = new sd_image_t {(uint32_t )params.width ,
746
- (uint32_t )params.height ,
747
- 3 ,
748
- input_image_buffer};
749
- if (params.canny_preprocess ) { // apply preprocessor
750
- control_image->data = preprocess_canny (control_image->data ,
751
- control_image->width ,
752
- control_image->height ,
753
- 0 .08f ,
754
- 0 .08f ,
755
- 0 .8f ,
756
- 1 .0f ,
757
- false );
758
- }
759
- }
760
763
results = txt2img (sd_ctx,
761
764
params.prompt .c_str (),
762
765
params.negative_prompt .c_str (),
@@ -828,7 +831,12 @@ int main(int argc, const char* argv[]) {
828
831
params.sample_steps ,
829
832
params.strength ,
830
833
params.seed ,
831
- params.batch_count );
834
+ params.batch_count ,
835
+ control_image,
836
+ params.control_strength ,
837
+ params.style_ratio ,
838
+ params.normalize_input ,
839
+ params.input_id_images_path .c_str ());
832
840
}
833
841
}
834
842
@@ -881,6 +889,8 @@ int main(int argc, const char* argv[]) {
881
889
}
882
890
free (results);
883
891
free_sd_ctx (sd_ctx);
892
+ free (control_image_buffer);
893
+ free (input_image_buffer);
884
894
885
895
return 0 ;
886
896
}
0 commit comments