88
99using namespace rta ;
1010
11+ bool check_and_add (
12+ const std::filesystem::path &path, std::vector<std::string> &batch )
13+ {
14+ if ( std::filesystem::is_regular_file ( path ) ||
15+ std::filesystem::is_symlink ( path ) )
16+ {
17+ auto e = path.extension ();
18+
19+ if ( e == " .exr" || e == " .EXR" )
20+ return false ;
21+ if ( e == " .jpg" || e == " .JPG" )
22+ return false ;
23+ if ( e == " .jpeg" || e == " .JPEG" )
24+ return false ;
25+
26+ std::string str = path.string ();
27+ batch.push_back ( str );
28+ }
29+ else
30+ {
31+ std::cerr << " Not a regular file: " << path << std::endl;
32+ }
33+ return true ;
34+ }
35+
1136int main ( int argc, const char *argv[] )
1237{
1338 OIIO::ArgParse argParse;
@@ -26,9 +51,11 @@ int main( int argc, const char *argv[] )
2651 return 1 ;
2752 }
2853
29- auto files = argParse[" filename" ].as_vec <std::string>();
30- std::vector<std::string> files_to_convert;
54+ // Create a separate batch for each input directory.
55+ // Reserve the first batch for the individual input files.
56+ std::vector<std::vector<std::string>> batches ( 1 );
3157
58+ auto files = argParse[" filename" ].as_vec <std::string>();
3259 for ( auto filename: files )
3360 {
3461 if ( !std::filesystem::exists ( filename ) )
@@ -42,102 +69,85 @@ int main( int argc, const char *argv[] )
4269
4370 if ( std::filesystem::is_directory ( filename ) )
4471 {
72+ std::vector<std::string> &curr_batch = batches.emplace_back ();
4573 auto it = std::filesystem::directory_iterator ( filename );
4674
4775 for ( auto filename2: it )
4876 {
49- if ( std::filesystem::is_regular_file ( filename2 ) ||
50- std::filesystem::is_symlink ( filename2 ) )
51- {
52- auto e = filename2.path ().extension ();
53-
54- if ( e == " .exr" || e == " .EXR" )
55- continue ;
56- if ( e == " .jpg" || e == " .JPG" )
57- continue ;
58- if ( e == " .jpeg" || e == " .JPEG" )
59- continue ;
60-
61- files_to_convert.push_back ( filename2.path ().string () );
62- }
77+ if ( !check_and_add ( filename2, curr_batch ) )
78+ continue ;
6379 }
6480 }
65- else if (
66- std::filesystem::is_regular_file ( filename ) ||
67- std::filesystem::is_symlink ( filename ) )
68- {
69- auto e = std::filesystem::path ( filename ).extension ();
70-
71- if ( e == " .exr" || e == " .EXR" )
72- continue ;
73- if ( e == " .jpg" || e == " .JPG" )
74- continue ;
75- if ( e == " .jpeg" || e == " .JPEG" )
76- continue ;
77-
78- files_to_convert.push_back ( filename );
79- }
8081 else
8182 {
82- std::cerr << " Not a file or directory: " << filename << std::endl;
83- return 1 ;
83+ if ( ! check_and_add ( filename, batches[ 0 ] ) )
84+ continue ;
8485 }
8586 }
8687
8788 bool result = true ;
88- for ( auto const &input_filename: files_to_convert )
89+ for ( auto const &batch: batches )
8990 {
90- std::string output_filename = input_filename;
91- if ( !converter.make_output_path ( output_filename ) )
92- continue ;
91+ for ( auto const &input_filename: batch )
92+ {
93+ std::string output_filename = input_filename;
94+ if ( !converter.make_output_path ( output_filename ) )
95+ continue ;
9396
94- OIIO::ParamValueList options;
97+ OIIO::ParamValueList options;
9598
96- if ( !converter.configure ( input_filename, options ) )
97- {
98- std::cerr << " Failed to configure the reader for the file: "
99- << input_filename << std::endl;
100- result = false ;
101- continue ;
102- }
99+ if ( !converter.configure ( input_filename, options ) )
100+ {
101+ std::cerr << " Failed to configure the reader for the file: "
102+ << input_filename << std::endl;
103+ result = false ;
104+ continue ;
105+ }
103106
104- OIIO::ImageSpec imageSpec;
105- imageSpec.extra_attribs = options;
107+ OIIO::ImageSpec imageSpec;
108+ imageSpec.extra_attribs = options;
106109
107- OIIO::ImageBuf buffer = OIIO::ImageBuf (
108- input_filename, 0 , 0 , nullptr , &imageSpec, nullptr );
110+ OIIO::ImageBuf buffer = OIIO::ImageBuf (
111+ input_filename, 0 , 0 , nullptr , &imageSpec, nullptr );
109112
110- if ( !buffer.read (
111- 0 , 0 , 0 , buffer.nchannels (), true , OIIO::TypeDesc::FLOAT ) )
112- {
113- std::cerr << " Failed to read for the file: " << input_filename
114- << std::endl;
115- result = false ;
116- continue ;
117- }
113+ if ( !buffer.read (
114+ 0 ,
115+ 0 ,
116+ 0 ,
117+ buffer.nchannels (),
118+ true ,
119+ OIIO::TypeDesc::FLOAT ) )
120+ {
121+ std::cerr << " Failed to read for the file: " << input_filename
122+ << std::endl;
123+ result = false ;
124+ continue ;
125+ }
118126
119- if ( !converter.apply_matrix ( buffer, buffer ) )
120- {
121- std::cerr << " Failed to apply colour space conversion to the file: "
122- << input_filename << std::endl;
123- result = false ;
124- continue ;
125- }
127+ if ( !converter.apply_matrix ( buffer, buffer ) )
128+ {
129+ std::cerr
130+ << " Failed to apply colour space conversion to the file: "
131+ << input_filename << std::endl;
132+ result = false ;
133+ continue ;
134+ }
126135
127- if ( !converter.apply_scale ( buffer, buffer ) )
128- {
129- std::cerr << " Failed to apply scale to the file: " << input_filename
130- << std::endl;
131- result = false ;
132- continue ;
133- }
136+ if ( !converter.apply_scale ( buffer, buffer ) )
137+ {
138+ std::cerr << " Failed to apply scale to the file: "
139+ << input_filename << std::endl;
140+ result = false ;
141+ continue ;
142+ }
134143
135- if ( !converter.save ( output_filename, buffer ) )
136- {
137- std::cerr << " Failed to save the file: " << output_filename
138- << std::endl;
139- result = false ;
140- continue ;
144+ if ( !converter.save ( output_filename, buffer ) )
145+ {
146+ std::cerr << " Failed to save the file: " << output_filename
147+ << std::endl;
148+ result = false ;
149+ continue ;
150+ }
141151 }
142152 }
143153
0 commit comments