Skip to content

Commit 6e18fc7

Browse files
committed
change the public interfaces to make it easier to use the tool as a library
Signed-off-by: Anton Dukhovnikov <[email protected]>
1 parent 02d534d commit 6e18fc7

File tree

4 files changed

+377
-294
lines changed

4 files changed

+377
-294
lines changed

include/rawtoaces/rawtoaces_util.h

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace rta
1717
class ImageConverter
1818
{
1919
public:
20-
ImageConverter();
21-
2220
/// The white balancing method to use for conversion can be specified
2321
///
2422
enum class WBMethod
@@ -83,79 +81,92 @@ class ImageConverter
8381
int highlight_mode = 0;
8482
int flip = 0;
8583
int cropbox[4] = { 0, 0, 0, 0 };
86-
87-
int verbosity;
88-
89-
/// Returns a class which will be used for parsing the command line
90-
/// parameters. Can be used to add additional parameters before calling
91-
/// `parse()`. The additional parameters will be parsed by ignored by this
92-
/// class.
93-
/// @result A non-const reference to an
94-
/// initialised OIIO::ArgParse class.
95-
OIIO::ArgParse &argParse();
96-
97-
/// Returns an image buffer, containing the image in the current state
98-
/// after the previous step of processing. The image can be modified before
99-
/// executing the next step if needed.
100-
/// @result A non-const reference to the image buffer.
101-
OIIO::ImageBuf &imageBuffer();
102-
103-
/// Parse the command line parameters. This method can be used to
104-
/// configure the converter instead of modifying each conversion parameter
105-
/// individually. Additional command line parameters can be added by
106-
/// modifying the structure returned by `argParse()`.
107-
/// @param argc
108-
/// number of parameters
109-
/// @param argv
110-
/// list of parameters
84+
int verbosity = 0;
85+
86+
/// Initialise the parser object with all the command line parameters
87+
/// used by this tool. The method also sets the help and usage strings.
88+
/// The parser object can be amended by the calling code afterwards if
89+
/// needed. This method is optional, all of the settings above can be
90+
/// modified directly.
91+
/// @param argParse
92+
/// The command line parser object to be updated.
93+
void init_parser( OIIO::ArgParse &argParse ) const;
94+
95+
/// Initialise the converter settings from the command line parser object.
96+
/// Prior to calling this, first initialise the object via
97+
/// `ImageConverted::init_parser()`, and call
98+
/// `OIIO::ArgParse::parse_args()`.
99+
/// This method is optional, all of the settings above can be modified
100+
/// directly.
101+
/// @param argParse
102+
/// the command line parser object
111103
/// @result
112104
/// `true` if parsed successfully
113-
bool parse( int argc, const char *argv[] );
105+
bool parse_params( const OIIO::ArgParse &argParse );
114106

115107
/// Configures the converter using the requested white balance and colour
116108
/// matrix method, and the metadata of the file provided in `input_file`.
117109
/// @param input_filename
118110
/// A file name of the raw image file to read the metadata from.
111+
/// @param options
112+
/// Conversion hints to be passed to OIIO when reading an image file.
113+
/// The list can be pre- or post- updated with other hints, unrelated to
114+
/// the rawtoaces conversion.
119115
/// @result
120116
/// `true` if configured successfully.
121-
bool configure( const std::string &input_filename );
122-
123-
/// Loads an image to convert. Note that the image file name in
124-
/// `input_filename` can be differnt from the one used in `configure()`.
125-
/// This is useful for configuring the converter using one image, and
126-
/// applying the conversion to a different one, or multiple images.
127-
/// @param input_filename
128-
/// A file name of the raw image file to read the pixels from.
117+
bool configure(
118+
const std::string &input_filename, OIIO::ParamValueList &options );
119+
120+
/// Apply the colour space conversion matrix (or matrices) to convert the
121+
/// image buffer from the raw camera colour space to ACES.
122+
/// @param dst
123+
/// Destination image buffer.
124+
/// @param src
125+
/// Source image buffer, can be the same as `dst` for in-place
126+
/// conversion.
129127
/// @result
130-
/// `true` if loaded successfully.
131-
bool load( const std::string &input_filename );
132-
133-
/// Converts the image from raw camera colour space to ACES.
128+
/// `true` if converted successfully.
129+
bool apply_matrix(
130+
OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi = {} );
131+
132+
/// Apply the headroom scale to image buffer.
133+
/// @param dst
134+
/// Destination image buffer.
135+
/// @param src
136+
/// Source image buffer, can be the same as `dst` for in-place
137+
/// conversion.
134138
/// @result
135139
/// `true` if converted successfully.
136-
bool process();
140+
bool apply_scale(
141+
OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi = {} );
137142

138143
/// Saves the image into ACES Container.
144+
/// @param output_filename
145+
/// Full path to the file to be saved.
146+
/// @param buf
147+
/// Image buffer to be saved.
139148
/// @result
140149
/// `true` if saved successfully.
141-
bool save( const std::string &output_filename );
150+
bool save( const std::string &output_filename, const OIIO::ImageBuf &buf );
142151

143152
private:
144-
void initArgParse();
145-
void prepareIDT_DNG();
146-
void prepareIDT_nonDNG();
147-
void prepareIDT_spectral( bool calc_white_balance, bool calc_matrix );
148-
void applyMatrix( const std::vector<std::vector<double>> &matrix );
149-
150-
bool _is_DNG;
151-
std::string _configFilename;
152-
std::string _cameraMake;
153-
std::string _cameraModel;
154-
155-
OIIO::ArgParse _argParse;
156-
OIIO::ImageSpec _inputHint;
157-
OIIO::ImageSpec _inputFull;
158-
OIIO::ImageBuf _imageBuffer;
153+
void prepareIDT_DNG( const OIIO::ImageSpec &imageSpec );
154+
void prepareIDT_nonDNG( const OIIO::ImageSpec &imageSpec );
155+
void prepareIDT_spectral(
156+
const OIIO::ImageSpec &imageSpec,
157+
bool calc_white_balance,
158+
bool calc_matrix );
159+
bool applyMatrix(
160+
const std::vector<std::vector<double>> &matrix,
161+
OIIO::ImageBuf &dst,
162+
const OIIO::ImageBuf &src,
163+
OIIO::ROI roi );
164+
165+
bool _is_DNG;
166+
167+
/// Make libraw read the raw photosites data without any processing.
168+
/// This is set if the requested demosaicing method is set `None`.
169+
bool _read_raw = false;
159170

160171
std::vector<double> _WB_mults;
161172
std::vector<std::vector<double>> _IDT_matrix;

src/rawtoaces2/main.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ using namespace rta;
1010

1111
int main( int argc, const char *argv[] )
1212
{
13+
14+
OIIO::ArgParse argParse;
15+
1316
ImageConverter converter;
17+
converter.init_parser( argParse );
18+
19+
if ( argParse.parse_args( argc, argv ) < 0 )
20+
{
21+
return 1;
22+
}
1423

15-
if ( !converter.parse( argc, argv ) )
24+
if ( !converter.parse_params( argParse ) )
1625
{
1726
return 1;
1827
}
1928

20-
auto &argParse = converter.argParse();
2129
auto files = argParse["filename"].as_vec<std::string>();
2230
std::vector<std::string> files_to_convert;
2331

@@ -69,31 +77,48 @@ int main( int argc, const char *argv[] )
6977
}
7078
output_filename += "_oiio.exr";
7179

72-
if ( !converter.configure( input_filename ) )
80+
OIIO::ParamValueList options;
81+
82+
if ( !converter.configure( input_filename, options ) )
7383
{
7484
std::cerr << "Failed to configure the reader for the file: "
7585
<< input_filename << std::endl;
7686
result = false;
7787
continue;
7888
}
7989

80-
if ( !converter.load( input_filename ) )
90+
OIIO::ImageSpec imageSpec;
91+
imageSpec.extra_attribs = options;
92+
93+
OIIO::ImageBuf buffer = OIIO::ImageBuf(
94+
input_filename, 0, 0, nullptr, &imageSpec, nullptr );
95+
96+
if ( !buffer.read(
97+
0, 0, 0, buffer.nchannels(), true, OIIO::TypeDesc::FLOAT ) )
8198
{
8299
std::cerr << "Failed to read for the file: " << input_filename
83100
<< std::endl;
84101
result = false;
85102
continue;
86103
}
87104

88-
if ( !converter.process() )
105+
if ( !converter.apply_matrix( buffer, buffer ) )
106+
{
107+
std::cerr << "Failed to apply colour space conversion to the file: "
108+
<< input_filename << std::endl;
109+
result = false;
110+
continue;
111+
}
112+
113+
if ( !converter.apply_scale( buffer, buffer ) )
89114
{
90-
std::cerr << "Failed to convert the file: " << input_filename
115+
std::cerr << "Failed to apply scale to the file: " << input_filename
91116
<< std::endl;
92117
result = false;
93118
continue;
94119
}
95120

96-
if ( !converter.save( output_filename ) )
121+
if ( !converter.save( output_filename, buffer ) )
97122
{
98123
std::cerr << "Failed to save the file: " << output_filename
99124
<< std::endl;

src/rawtoaces_idt/rta.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ vector<double> Illum::cctToxy( const double &cctd ) const
233233
if ( cctd >= 4002.15 && cctd <= 7003.77 )
234234
xy[0] =
235235
( 0.244063 + 99.11 / cctd +
236-
2.9678 * 1000000 / ( std::pow( cctd, 2 ) ) -
237-
4.6070 * 1000000000 / ( std::pow( cctd, 3 ) ) );
236+
2.9678 * 1000000 / (std::pow( cctd, 2 ))-4.6070 * 1000000000 /
237+
( std::pow( cctd, 3 ) ) );
238238
else
239239
xy[0] =
240240
( 0.237040 + 247.48 / cctd +
241-
1.9018 * 1000000 / ( std::pow( cctd, 2 ) ) -
242-
2.0064 * 1000000000 / ( std::pow( cctd, 3 ) ) );
241+
1.9018 * 1000000 / (std::pow( cctd, 2 ))-2.0064 * 1000000000 /
242+
( std::pow( cctd, 3 ) ) );
243243

244244
xy[1] = -3.0 * ( std::pow( xy[0], 2 ) ) + 2.87 * xy[0] - 0.275;
245245

@@ -1028,13 +1028,15 @@ void Idt::loadCMF( const string &path )
10281028
BOOST_FOREACH (
10291029
ptree::value_type &row, pt.get_child( "spectral_data.data.main" ) )
10301030
{
1031-
_cmf[i]._wl = atoi( ( row.first ).c_str() );
1031+
int wl = atoi( ( row.first ).c_str() );
10321032

1033-
if ( _cmf[i]._wl < 380 || _cmf[i]._wl % 5 )
1033+
if ( wl < 380 || wl % 5 )
10341034
continue;
1035-
else if ( _cmf[i]._wl > 780 )
1035+
else if ( wl > 780 )
10361036
break;
10371037

1038+
_cmf[i]._wl = wl;
1039+
10381040
vector<double> data;
10391041
BOOST_FOREACH ( ptree::value_type &cell, row.second )
10401042
data.push_back( cell.second.get_value<double>() );

0 commit comments

Comments
 (0)