Skip to content

Commit

Permalink
add the --overwrite, --create-dirs, --output-dir parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Dukhovnikov <antond@wetafx.co.nz>
  • Loading branch information
antond-weta committed Feb 20, 2025
1 parent 72466a0 commit 7665b22
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 14 deletions.
16 changes: 15 additions & 1 deletion include/rawtoaces/rawtoaces_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ class ImageConverter
int cropbox[4] = { 0, 0, 0, 0 };
int verbosity = 0;

bool overwrite = false;
bool create_dirs = false;
std::string output_dir;

/// Initialise the parser object with all the command line parameters
/// used by this tool. The method also sets the help and usage strings.
/// The parser object can be amended by the calling code afterwards if
/// needed. This method is optional, all of the settings above can be
/// modified directly.
/// @param argParse
/// The command line parser object to be updated.
void init_parser( OIIO::ArgParse &argParse ) const;
void init_parser( OIIO::ArgParse &argParse );

/// Initialise the converter settings from the command line parser object.
/// Prior to calling this, first initialise the object via
Expand Down Expand Up @@ -158,6 +162,16 @@ class ImageConverter
bool apply_scale(
OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi = {} );

/// Make output file path and check if it is writable.
/// @param path
/// A reference to a variable containing the input file path. The output file path gets generated
/// in-place.
/// @result
/// `true` if the file can be written, e.g. the output directory exists, or creating directories
/// is allowed; the file does not exist or overwriting is allowed.
bool
make_output_path( std::string &path, const std::string &suffix = "_oiio" );

/// Saves the image into ACES Container.
/// @param output_filename
/// Full path to the file to be saved.
Expand Down
26 changes: 20 additions & 6 deletions src/rawtoaces2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ int main( int argc, const char *argv[] )
if ( std::filesystem::is_regular_file( filename2 ) ||
std::filesystem::is_symlink( filename2 ) )
{
auto e = filename2.path().extension();

if ( e == ".exr" || e == ".EXR" )
continue;
if ( e == ".jpg" || e == ".JPG" )
continue;
if ( e == ".jpeg" || e == ".JPEG" )
continue;

files_to_convert.push_back( filename2.path().string() );
}
}
Expand All @@ -57,6 +66,15 @@ int main( int argc, const char *argv[] )
std::filesystem::is_regular_file( filename ) ||
std::filesystem::is_symlink( filename ) )
{
auto e = std::filesystem::path( filename ).extension();

if ( e == ".exr" || e == ".EXR" )
continue;
if ( e == ".jpg" || e == ".JPG" )
continue;
if ( e == ".jpeg" || e == ".JPEG" )
continue;

files_to_convert.push_back( filename );
}
else
Expand All @@ -70,12 +88,8 @@ int main( int argc, const char *argv[] )
for ( auto const &input_filename: files_to_convert )
{
std::string output_filename = input_filename;
size_t pos = input_filename.rfind( '.' );
if ( pos != std::string::npos )
{
output_filename = input_filename.substr( 0, pos );
}
output_filename += "_oiio.exr";
if ( !converter.make_output_path( output_filename ) )
continue;

OIIO::ParamValueList options;

Expand Down
91 changes: 84 additions & 7 deletions src/rawtoaces_util2/rawtoaces_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ bool check_param(
}
}

void ImageConverter::init_parser( OIIO::ArgParse &argParse ) const
void ImageConverter::init_parser( OIIO::ArgParse &argParse )
{
argParse.intro( HelpString );
argParse.usage( UsageString );
Expand Down Expand Up @@ -192,6 +192,26 @@ void ImageConverter::init_parser( OIIO::ArgParse &argParse ) const
.defaultval( 6.0f )
.action( OIIO::ArgParse::store<float>() );

argParse.separator( "General options:" );

argParse.arg( "--overwrite" )
.help(
"Allows overwriting existing files. If not set, trying to write "
"to an existing file will generate an error." )
.action( OIIO::ArgParse::store_true() );

argParse.arg( "--output-dir" )
.help(
"The directory to write the output files to. "
"This gets applied to every input directory, so it is better to "
"be used with a single input directory." )
.metavar( "STR" )
.action( OIIO::ArgParse::store() );

argParse.arg( "--create-dirs" )
.help( "Create output directories if they don't exist." )
.action( OIIO::ArgParse::store_true() );

argParse.separator( "Raw conversion options:" );

argParse.arg( "--no-auto-bright" )
Expand Down Expand Up @@ -273,10 +293,13 @@ void ImageConverter::init_parser( OIIO::ArgParse &argParse ) const
.action( OIIO::ArgParse::store_true() );

argParse.arg( "--verbose" )
.help( "Verbosity level. 0 = off, 1 = info, 2 = debug." )
.metavar( "VAL" )
.defaultval( 0 )
.action( OIIO::ArgParse::store<int>() );
.help(
"(-v) Print progress messages. "
"Repeated -v will increase verbosity." )
.action( [&]( OIIO::cspan<const char *> argv ) { verbosity++; } );

argParse.arg( "-v" ).hidden().action(
[&]( OIIO::cspan<const char *> argv ) { verbosity++; } );
}

bool ImageConverter::parse_params( const OIIO::ArgParse &argParse )
Expand Down Expand Up @@ -335,8 +358,6 @@ bool ImageConverter::parse_params( const OIIO::ArgParse &argParse )
exit( 0 );
}

verbosity = argParse["verbose"].get<int>();

std::string wb_method = argParse["wb-method"].get();

if ( wb_method == "metadata" )
Expand Down Expand Up @@ -488,6 +509,10 @@ bool ImageConverter::parse_params( const OIIO::ArgParse &argParse )
highlight_mode = argParse["highlight-mode"].get<int>();
flip = argParse["flip"].get<int>();

overwrite = argParse["overwrite"].get<int>();
create_dirs = argParse["create-dirs"].get<int>();
output_dir = argParse["output-dir"].get();

return true;
}

Expand Down Expand Up @@ -767,6 +792,58 @@ bool ImageConverter::apply_scale(
return OIIO::ImageBufAlgo::mul( dst, src, headroom );
}

bool ImageConverter::make_output_path(
std::string &path, const std::string &suffix )
{
std::filesystem::path temp_path( path );

temp_path.replace_extension();
temp_path += suffix + ".exr";

if ( !output_dir.empty() )
{
auto new_directory = std::filesystem::path( output_dir );

auto filename = temp_path.filename();
auto old_directory = temp_path.remove_filename();

new_directory = old_directory / new_directory;

if ( !std::filesystem::exists( new_directory ) )
{
if ( create_dirs )
{
if ( !std::filesystem::create_directory( new_directory ) )
{
std::cerr << "ERROR: Failed to create directory "
<< new_directory << "." << std::endl;
return false;
}
}
else
{
std::cerr << "ERROR: The output directory " << new_directory
<< " does not exist." << std::endl;
return false;
}
}

temp_path = std::filesystem::absolute( new_directory / filename );
}

if ( !overwrite && std::filesystem::exists( temp_path ) )
{
std::cerr
<< "ERROR: file " << temp_path << " already exists. Use "
<< "--overwrite to allow overwriting existing files. Skipping "
<< "this file." << std::endl;
return false;
}

path = temp_path.string();
return true;
}

bool ImageConverter::save(
const std::string &output_filename, const OIIO::ImageBuf &buf )
{
Expand Down

0 comments on commit 7665b22

Please sign in to comment.