@@ -37,26 +37,32 @@ bool progress_callback(void* opaque_data, float portion_done)
3737}
3838
3939std::pair<ImageBuf, ImageBuf> mask_load (const std::string& mask_file) {
40- ImageBuf mask_buf (mask_file);
40+ ImageBuf alpha_buf (mask_file);
4141 std::cout << " Reading " << mask_file << std::endl;
42- bool read_ok = mask_buf .read (0 , 0 , 0 , 1 , true , TypeDesc::FLOAT, nullptr , nullptr );
42+ bool read_ok = alpha_buf .read (0 , 0 , 0 , 1 , true , TypeDesc::FLOAT, nullptr , nullptr );
4343 if (!read_ok) {
4444 std::cerr << " Error: Could not read mask image\n " ;
45+ system (" pause" );
4546 exit (-1 );
4647 }
47-
48+
49+ // rename channel to alpha and set it as an alpha channel
50+ alpha_buf.specmod ().channelnames [0 ] = " A" ;
51+ alpha_buf.specmod ().alpha_channel = 0 ;
52+
4853 ImageBuf rgb_alpha, temp_buff;
4954
50- bool ok = ImageBufAlgo::channel_append (temp_buff, mask_buf, mask_buf );
51- ok = ok && ImageBufAlgo::channel_append (rgb_alpha, temp_buff, mask_buf );
55+ bool ok = ImageBufAlgo::channel_append (temp_buff, alpha_buf, alpha_buf );
56+ ok = ok && ImageBufAlgo::channel_append (rgb_alpha, temp_buff, alpha_buf );
5257 if (!ok) {
5358 std::cerr << " Error: Could not append channels\n " ;
59+ system (" pause" );
5460 exit (-1 );
5561 }
5662
5763 temp_buff.clear ();
5864
59- return { mask_buf , rgb_alpha };
65+ return { alpha_buf , rgb_alpha };
6066}
6167
6268bool solidify_main (const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair) {
@@ -66,6 +72,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
6672 ImageBuf input_buf (inputFileName);
6773
6874 bool external_alpha = false ;
75+ bool grayscale = false ;
6976
7077 if (mask_pair.first .initialized () && mask_pair.second .initialized ()) {
7178 external_alpha = true ;
@@ -82,7 +89,55 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
8289 std::cerr << " Error: Could not read input image\n " ;
8390 return false ;
8491 }
92+
8593 std::cout << std::endl;
94+ std::cout << " channels: " << input_buf.nchannels () << std::endl;
95+
96+ int alpha_channel = input_buf.spec ().alpha_channel ;
97+ std::cout << " alpha channel: " << alpha_channel << std::endl;
98+
99+ bool isValid = true ;
100+ int inputCh = input_buf.nchannels ();
101+ //
102+ // Valid with and without external alpha:
103+ // RGBA - 4 channels
104+ // Grayscale with alpha - 2 channels
105+ // RGB with alpha - 4 channels
106+ //
107+ // Valid only with external alpha:
108+ // Grayscale - 1 channel
109+ // RGB - 3 channels
110+ //
111+ switch (inputCh)
112+ {
113+ case 1 :
114+ isValid = external_alpha ? true : false ;
115+ grayscale = true ;
116+ break ;
117+ case 2 :
118+ isValid = true ;
119+ input_buf.specmod ().channelnames [0 ] = " Y" ;
120+ input_buf.specmod ().channelnames [0 ] = " A" ;
121+ input_buf.specmod ().alpha_channel = 1 ;
122+ grayscale = true ;
123+ break ;
124+ case 3 :
125+ isValid = external_alpha ? true : false ;
126+ grayscale = false ;
127+ break ;
128+ case 4 :
129+ isValid = true ;
130+ input_buf.specmod ().channelnames [3 ] = " A" ;
131+ input_buf.specmod ().alpha_channel = 3 ;
132+ grayscale = false ;
133+ break ;
134+ default :
135+ isValid = false ;
136+ std::cerr << " Error: Only Grayscale, RGB and RGBA images are supported" << std::endl;
137+ std::cerr << " Grayscale and RGB images must have an external alpha channel or use external alpha file" << std::endl;
138+ return false ;
139+ break ;
140+ }
86141
87142 // Create an ImageBuf object to store the result
88143 ImageBuf result_buf, rgba_buf;
@@ -92,16 +147,28 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
92147 Timer pushpull_timer;
93148
94149 if (external_alpha) {
95- bool ok = ImageBufAlgo::mul (input_buf, input_buf, mask_pair.second );
150+ bool ok = ImageBufAlgo::mul (input_buf, input_buf, grayscale ? mask_pair. first : mask_pair.second );
96151 ok = ok && ImageBufAlgo::channel_append (rgba_buf, input_buf, mask_pair.first );
97152 if (!ok) {
98153 std::cerr << " Error: " << rgba_buf.geterror () << std::endl;
99154 return false ;
100155 }
156+ // rename last channel to alpha and set alpha channel
157+ rgba_buf.specmod ().channelnames [rgba_buf.nchannels () - 1 ] = " A" ;
158+ rgba_buf.specmod ().alpha_channel = rgba_buf.nchannels () - 1 ;
101159 }
102160
103161 ImageBuf* input_buf_ptr = external_alpha ? &rgba_buf : &input_buf; // Use the multiplied RGBA buffer if have an external alpha
104162
163+ #if 0
164+ std::cout << "channels: " << input_buf_ptr->nchannels() << std::endl;
165+ //std::vector<std::string> chnames = input_buf_ptr->spec().channelnames;
166+ //for (int i = 0; i < input_buf_ptr->nchannels(); ++i)
167+ // std::cout << "channel " << i << " : " << chnames[i] << std::endl;
168+
169+ std::cout << "alpha channel: " << input_buf_ptr->spec().alpha_channel << std::endl;
170+ #endif
171+
105172 // Call fillholes_pushpull
106173 bool ok = ImageBufAlgo::fillholes_pushpull (result_buf, *input_buf_ptr);
107174
@@ -119,7 +186,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
119186 }
120187
121188 ImageSpec spec = result_buf.spec ();
122- spec.nchannels = 3 ; // Only write RGB channels
189+ spec.nchannels = grayscale ? 1 : 3 ; // Only write RGB channels
123190 spec.alpha_channel = -1 ; // No alpha channel
124191
125192 out->open (outputFileName, spec, ImageOutput::Create);
@@ -131,21 +198,6 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
131198 *progress_callback, nullptr );
132199 out->close ();
133200
134- #if 0
135-
136- // Write RGBA buffer for debug
137- spec = rgba_buf.spec();
138- spec.nchannels = 4;
139-
140- out->open("debug.exr", spec, ImageOutput::Create);
141-
142- out->write_image(rgba_buf.spec().format, rgba_buf.localpixels(),
143- rgba_buf.pixel_stride(), rgba_buf.scanline_stride(), rgba_buf.z_stride(),
144- *progress_callback, nullptr);
145- out->close();
146-
147- #endif
148-
149201 std::cout << std::endl << " Total processing time : " << g_timer.nowText () << std::endl;
150202
151203 return true ;
0 commit comments