|
| 1 | +// Copyright Contributors to the rawtoaces project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// https://github.com/AcademySoftwareFoundation/rawtoaces |
| 4 | + |
| 5 | +#ifndef _RAWTOACES_UTIL_H_ |
| 6 | +#define _RAWTOACES_UTIL_H_ |
| 7 | + |
| 8 | +#include <OpenImageIO/argparse.h> |
| 9 | +#include <OpenImageIO/imageio.h> |
| 10 | +#include <OpenImageIO/imagebuf.h> |
| 11 | + |
| 12 | +namespace rta |
| 13 | +{ |
| 14 | + |
| 15 | +/// An image converter converts an image read from a camera raw image file |
| 16 | +/// into an ACESContainer compatible image. |
| 17 | +class ImageConverter |
| 18 | +{ |
| 19 | +public: |
| 20 | + ImageConverter(); |
| 21 | + |
| 22 | + /// The white balancing method to use for conversion can be specified |
| 23 | + /// |
| 24 | + enum class WBMethod |
| 25 | + { |
| 26 | + /// Use the metadata provided in the image file. This mode is mostly |
| 27 | + /// usable with DNG files, as the information needed for conversion |
| 28 | + /// is mandatory in the DNG format. |
| 29 | + Metadata, |
| 30 | + /// White balance to a specified illuminant. See the `illuminant` |
| 31 | + /// property for more information on the supported illuminants. This |
| 32 | + /// mode can only be used if spectral sensitivities are available for |
| 33 | + /// the camera. |
| 34 | + Illuminant, |
| 35 | + /// Calculate white balance by averaging over a specified region of |
| 36 | + /// the image. See `wbBox`. In this mode if an empty box if provided, |
| 37 | + /// white balancing is done by averaging over the whole image. |
| 38 | + Box, |
| 39 | + /// Use custom white balancing multipliers. This mode is useful if |
| 40 | + /// the white balancing coefficients are calculated by an external |
| 41 | + /// tool. |
| 42 | + Custom |
| 43 | + } wbMethod = WBMethod::Metadata; |
| 44 | + |
| 45 | + enum class MatrixMethod |
| 46 | + { |
| 47 | + /// Use the camera spectral sensitivity curves to solve for the colour |
| 48 | + /// conversion matrix. In this mode the illuminant is either provided |
| 49 | + /// directly in `illuminant` if `wbMethod` == |
| 50 | + /// `WBMethod::Illuminant`, or the best illuminant is derived from the |
| 51 | + /// white balancing multipliers. |
| 52 | + Spectral, |
| 53 | + /// Use the metadata provided in the image file. This mode is mostly |
| 54 | + /// usable with DNG files, as the information needed for conversion |
| 55 | + /// is mandatory in the DNG format. |
| 56 | + Metadata, |
| 57 | + /// Use the Adobe colour matrix for the camera supplied in LibRaw. |
| 58 | + Adobe, |
| 59 | + /// Specify a custom matrix in `colourMatrix`. This mode is useful if |
| 60 | + /// the matrix is calculated by an external tool. |
| 61 | + Custom |
| 62 | + } matrixMethod = MatrixMethod::Spectral; |
| 63 | + |
| 64 | + /// An illuminant to use for white balancing and/or colour matrix |
| 65 | + /// calculation. Only used when `wbMethod` == |
| 66 | + /// `WBMethod::Illuminant` or `matrixMethod` == `MatrixMethod::Spectral`. |
| 67 | + /// An illuminant can be provided as a black body correlated colour |
| 68 | + /// temperature, like `3200K`; or a D-series illuminant, like `D56`; |
| 69 | + /// or any other illuminant, in such case it must be present in the data |
| 70 | + /// folder. |
| 71 | + std::string illuminant; |
| 72 | + |
| 73 | + float headroom = 6.0; |
| 74 | + int wbBox[4] = { 0 }; |
| 75 | + float customWB[4] = { 1.0, 1.0, 1.0, 1.0 }; |
| 76 | + float customMatrix[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; |
| 77 | + |
| 78 | + bool no_auto_bright = false; |
| 79 | + float adjust_maximum_threshold = 0.75; |
| 80 | + int black_level = -1; |
| 81 | + int saturation_level = -1; |
| 82 | + bool half_size = false; |
| 83 | + int highlight_mode = 0; |
| 84 | + int flip = 0; |
| 85 | + 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 |
| 111 | + /// @result |
| 112 | + /// `true` if parsed successfully |
| 113 | + bool parse( int argc, const char *argv[] ); |
| 114 | + |
| 115 | + /// Configures the converter using the requested white balance and colour |
| 116 | + /// matrix method, and the metadata of the file provided in `input_file`. |
| 117 | + /// @param input_filename |
| 118 | + /// A file name of the raw image file to read the metadata from. |
| 119 | + /// @result |
| 120 | + /// `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. |
| 129 | + /// @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. |
| 134 | + /// @result |
| 135 | + /// `true` if converted successfully. |
| 136 | + bool process(); |
| 137 | + |
| 138 | + /// Saves the image into ACES Container. |
| 139 | + /// @result |
| 140 | + /// `true` if saved successfully. |
| 141 | + bool save( const std::string &output_filename ); |
| 142 | + |
| 143 | +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; |
| 159 | + |
| 160 | + std::vector<double> _WB_mults; |
| 161 | + std::vector<std::vector<double>> _IDT_matrix; |
| 162 | + std::vector<std::vector<double>> _CAT_matrix; |
| 163 | +}; |
| 164 | + |
| 165 | +} // namespace rta |
| 166 | + |
| 167 | +#endif // _RAWTOACES_UTIL_H_ |
0 commit comments