@@ -36,33 +36,51 @@ bool progress_callback(void* opaque_data, float portion_done)
3636 return (portion_done >= 1 .f );
3737}
3838
39- // int main(int argc, char* argv[]) {
40- int solidify_main (const std::string& mask_file, const std::string& inputFileName, const std::string& outputFileName) {
39+ std::pair<ImageBuf, ImageBuf> mask_load (const std::string& mask_file) {
40+ ImageBuf mask_buf (mask_file);
41+ std::cout << " Reading " << mask_file << std::endl;
42+ bool read_ok = mask_buf.read (0 , 0 , 0 , 1 , true , TypeDesc::FLOAT, nullptr , nullptr );
43+ if (!read_ok) {
44+ std::cerr << " Error: Could not read mask image\n " ;
45+ exit (-1 );
46+ }
47+
48+ ImageBuf rgb_alpha, temp_buff;
49+
50+ bool ok = ImageBufAlgo::channel_append (temp_buff, mask_buf, mask_buf);
51+ ok = ok && ImageBufAlgo::channel_append (rgb_alpha, temp_buff, mask_buf);
52+ if (!ok) {
53+ std::cerr << " Error: Could not append channels\n " ;
54+ exit (-1 );
55+ }
56+
57+ temp_buff.clear ();
58+
59+ return { mask_buf, rgb_alpha };
60+ }
61+
62+ bool solidify_main (const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair) {
4163 Timer g_timer;
4264
4365 // Create an ImageBuf object for the input file
4466 ImageBuf input_buf (inputFileName);
45- ImageBuf mask_buf (mask_file);
67+
68+ bool external_alpha = false ;
69+
70+ if (mask_pair.first .initialized () && mask_pair.second .initialized ()) {
71+ external_alpha = true ;
72+ }
4673
4774 // Read the image with a progress callback
4875
49- int last_channel = -1 ;
76+ int last_channel = (external_alpha) ? 3 : -1 ; // If we have an external alpha, read only 3 channels
5077
51- if (mask_file != " " ) {
52- last_channel = 3 ;
53- std::cout << " Reading " << mask_file << std::endl;
54- bool read_ok = mask_buf.read (0 , 0 , 0 , 1 , true , TypeDesc::FLOAT, nullptr , nullptr );
55- if (!read_ok) {
56- std::cerr << " Error: Could not read mask image\n " ;
57- return 1 ;
58- }
59- }
6078 std::cout << " Reading " << inputFileName << std::endl;
6179
6280 bool read_ok = input_buf.read (0 , 0 , 0 , last_channel, true , TypeUnknown, *progress_callback, nullptr );
6381 if (!read_ok) {
6482 std::cerr << " Error: Could not read input image\n " ;
65- return 1 ;
83+ return false ;
6684 }
6785 std::cout << std::endl;
6886
@@ -73,33 +91,31 @@ int solidify_main(const std::string& mask_file, const std::string& inputFileName
7391
7492 Timer pushpull_timer;
7593
76- if (mask_file != " " ) {
77- ImageBuf alpha_ch, alpha_c3;
78- bool ok = ImageBufAlgo::channel_append (alpha_ch, mask_buf, mask_buf);
79- ok = ok && ImageBufAlgo::channel_append (alpha_c3, alpha_ch, mask_buf);
80- ImageBufAlgo::mul (input_buf, input_buf, alpha_c3);
81- ok = ok && ImageBufAlgo::channel_append (rgba_buf, input_buf, mask_buf);
94+ if (external_alpha) {
95+ bool ok = ImageBufAlgo::mul (input_buf, input_buf, mask_pair.second );
96+ ok = ok && ImageBufAlgo::channel_append (rgba_buf, input_buf, mask_pair.first );
8297 if (!ok) {
8398 std::cerr << " Error: " << rgba_buf.geterror () << std::endl;
84- return 1 ;
99+ return false ;
85100 }
86101 }
87102
88- // Call fillholes_pushpull
103+ ImageBuf* input_buf_ptr = external_alpha ? &rgba_buf : &input_buf; // Use the multiplied RGBA buffer if have an external alpha
89104
90- bool ok = ImageBufAlgo::fillholes_pushpull (result_buf, (mask_file == " " ) ? input_buf : rgba_buf);
105+ // Call fillholes_pushpull
106+ bool ok = ImageBufAlgo::fillholes_pushpull (result_buf, *input_buf_ptr);
91107
92108 if (!ok) {
93109 std::cerr << " Error: " << result_buf.geterror () << std::endl;
94- return 1 ;
110+ return false ;
95111 }
96112
97113 std::cout << " Push-Pull time : " << pushpull_timer.nowText () << std::endl;
98114
99115 auto out = ImageOutput::create (outputFileName);
100116 if (!out) {
101117 std::cerr << " Could not create output file: " << outputFileName << std::endl;
102- return 1 ;
118+ return false ;
103119 }
104120
105121 ImageSpec spec = result_buf.spec ();
@@ -116,6 +132,8 @@ int solidify_main(const std::string& mask_file, const std::string& inputFileName
116132 out->close ();
117133
118134#if 0
135+
136+ // Write RGBA buffer for debug
119137 spec = rgba_buf.spec();
120138 spec.nchannels = 4;
121139
@@ -130,5 +148,5 @@ int solidify_main(const std::string& mask_file, const std::string& inputFileName
130148
131149 std::cout << std::endl << " Total processing time : " << g_timer.nowText () << std::endl;
132150
133- return 0 ;
151+ return true ;
134152}
0 commit comments