|
| 1 | +// Copyright Contributors to the rawtoaces project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// https://github.com/AcademySoftwareFoundation/rawtoaces |
| 4 | + |
| 5 | +#include "exiftool.h" |
| 6 | + |
| 7 | +#include <iomanip> |
| 8 | +#include <iostream> |
| 9 | +#include <filesystem> |
| 10 | + |
| 11 | +#include <OpenImageIO/imagebuf.h> |
| 12 | + |
| 13 | +namespace rta |
| 14 | +{ |
| 15 | +namespace util |
| 16 | +{ |
| 17 | +namespace exiftool |
| 18 | +{ |
| 19 | + |
| 20 | +std::string find_exiftool() |
| 21 | +{ |
| 22 | + const char *env = getenv( "RAWTOACES_EXIFTOOL_PATH" ); |
| 23 | + if ( env != nullptr ) |
| 24 | + { |
| 25 | + return env; |
| 26 | + } |
| 27 | + |
| 28 | +#if defined( WIN32 ) || defined( WIN64 ) |
| 29 | + const std::string delimiter = ";"; |
| 30 | + const std::string name = "exiftool.exe"; |
| 31 | +#else |
| 32 | + const std::string delimiter = ":"; |
| 33 | + const std::string name = "exiftool"; |
| 34 | +#endif |
| 35 | + |
| 36 | + env = getenv( "PATH" ); |
| 37 | + if ( env ) |
| 38 | + { |
| 39 | + std::string temp = env; |
| 40 | + |
| 41 | + std::vector<std::string> paths; |
| 42 | + size_t pos = 0; |
| 43 | + std::string token; |
| 44 | + while ( ( pos = temp.find( delimiter ) ) != std::string::npos ) |
| 45 | + { |
| 46 | + token = temp.substr( 0, pos ); |
| 47 | + paths.push_back( token ); |
| 48 | + temp.erase( 0, pos + delimiter.length() ); |
| 49 | + } |
| 50 | + paths.push_back( temp ); |
| 51 | + |
| 52 | + for ( const auto &path: paths ) |
| 53 | + { |
| 54 | + std::filesystem::path p( path ); |
| 55 | + p /= name; |
| 56 | + |
| 57 | + if ( std::filesystem::exists( p ) ) |
| 58 | + return p.string(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return ""; |
| 63 | +} |
| 64 | + |
| 65 | +bool execute( const std::string &command, std::stringstream &stream ) |
| 66 | +{ |
| 67 | + constexpr size_t buf_size = 255; |
| 68 | + char buffer1[buf_size]; |
| 69 | + // std::stringstream stream; |
| 70 | + |
| 71 | + errno = 0; |
| 72 | + |
| 73 | +#if defined( WIN32 ) || defined( WIN64 ) |
| 74 | + FILE *file = _popen( command.c_str(), "r" ); |
| 75 | +#else |
| 76 | + // clang-format off |
| 77 | + FILE *file = popen( command.c_str(), "r" ); |
| 78 | + // clang-format on |
| 79 | +#endif |
| 80 | + |
| 81 | + bool success = ( errno == 0 ); |
| 82 | + |
| 83 | + // Struggling to detect errors consistently across all platforms. In some |
| 84 | + // cases getting errno != 0 even when the command has been executed |
| 85 | + // successfully. As a workaround, I'm treating the empty output as error. |
| 86 | + bool empty = true; |
| 87 | + if ( success ) |
| 88 | + { |
| 89 | + while ( fgets( buffer1, buf_size, file ) != NULL ) |
| 90 | + { |
| 91 | + stream << buffer1; |
| 92 | + empty = false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +#if defined( WIN32 ) || defined( WIN64 ) |
| 97 | + _pclose( file ); |
| 98 | +#else |
| 99 | + pclose( file ); |
| 100 | +#endif |
| 101 | + |
| 102 | + stream.flush(); |
| 103 | + return success && !empty; |
| 104 | +} |
| 105 | + |
| 106 | +bool fetch_metadata( |
| 107 | + OIIO::ImageSpec &spec, |
| 108 | + const std::string &path, |
| 109 | + const std::vector<std::string> &keys ) |
| 110 | +{ |
| 111 | + |
| 112 | + std::string exiftool_path = find_exiftool(); |
| 113 | + |
| 114 | + if ( exiftool_path.empty() ) |
| 115 | + { |
| 116 | + std::cerr |
| 117 | + << "Exiftool not found, please provide the path to " |
| 118 | + << "exiftool binary via the RAWTOACES_EXIFTOOL_PATH environment " |
| 119 | + << "variable." << std::endl; |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + // Mapping from OIIO attribute names to ExifTool attribute names. |
| 124 | + const std::map<std::string, std::string> oiio_to_exiftool = { |
| 125 | + { "cameraMake", "Make" }, |
| 126 | + { "cameraModel", "Model" }, |
| 127 | + { "lensModel", "LensID" }, |
| 128 | + { "aperture", "FNumber" }, |
| 129 | + { "focalLength", "FocalLength" } |
| 130 | + }; |
| 131 | + |
| 132 | + // Mapping from ExifTool attribute names to OIIO attribute names, |
| 133 | + // the bool flag specifies whether the value must be converted |
| 134 | + // from string to float. |
| 135 | + const std::map<std::string, std::pair<std::string, bool>> |
| 136 | + exiftool_to_oiio = { { "Make", { "cameraMake", false } }, |
| 137 | + { "Model", { "cameraModel", false } }, |
| 138 | + { "LensID", { "lensModel", false } }, |
| 139 | + { "LensModel", { "lensModel", false } }, |
| 140 | + { "FNumber", { "aperture", true } }, |
| 141 | + { "FocalLength", { "focalLength", true } } }; |
| 142 | + |
| 143 | + std::string command = exiftool_path + " -S "; |
| 144 | + for ( auto key: keys ) |
| 145 | + { |
| 146 | + if ( oiio_to_exiftool.count( key ) ) |
| 147 | + { |
| 148 | + command += " -" + oiio_to_exiftool.at( key ); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + std::cerr << "Exiftool: unknown key " << key << std::endl; |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + command += " " + path; |
| 158 | + |
| 159 | + std::stringstream stream2; |
| 160 | + if ( !execute( command, stream2 ) ) |
| 161 | + { |
| 162 | + std::cerr |
| 163 | + << "Failed to execute exiftool. Please provide the path to " |
| 164 | + << "exiftool binary via the RAWTOACES_EXIFTOOL_PATH environment " |
| 165 | + << "variable." << std::endl; |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + std::map<std::string, std::string> result; |
| 170 | + std::vector<std::string> lines; |
| 171 | + std::string line; |
| 172 | + while ( std::getline( stream2, line ) ) |
| 173 | + { |
| 174 | + auto pos = line.find( ": " ); |
| 175 | + if ( pos != line.npos ) |
| 176 | + { |
| 177 | + std::string exiftool_key = line.substr( 0, pos ); |
| 178 | + std::string value = line.substr( pos + 2 ); |
| 179 | + |
| 180 | + auto &map = exiftool_to_oiio.at( exiftool_key ); |
| 181 | + const std::string &oiio_key = std::get<0>( map ); |
| 182 | + bool to_float = std::get<1>( map ); |
| 183 | + |
| 184 | + if ( to_float ) |
| 185 | + { |
| 186 | + spec[oiio_key] = std::stof( value ); |
| 187 | + } |
| 188 | + else |
| 189 | + { |
| 190 | + spec[oiio_key] = value; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return true; |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace exiftool |
| 199 | +} // namespace util |
| 200 | +} // namespace rta |
0 commit comments