|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +#include "openimageio_metadata.hpp" |
| 3 | + |
| 4 | +#include <filesystem> |
| 5 | +#include <unordered_set> |
| 6 | + |
| 7 | +#include <OpenImageIO/imageio.h> |
| 8 | +#include <OpenImageIO/typedesc.h> |
| 9 | + |
| 10 | +#include "xstudio/utility/helpers.hpp" |
| 11 | + |
| 12 | +namespace fs = std::filesystem; |
| 13 | + |
| 14 | +using namespace xstudio::media_metadata; |
| 15 | +using namespace xstudio::utility; |
| 16 | +using namespace xstudio; |
| 17 | + |
| 18 | +OpenImageIOMediaMetadata::OpenImageIOMediaMetadata() : MediaMetadata("OpenImageIO") {} |
| 19 | + |
| 20 | +MMCertainty OpenImageIOMediaMetadata::supported( |
| 21 | + const caf::uri &uri, const std::array<uint8_t, 16> &signature) { |
| 22 | + // Step 1: List of supported extensions by OIIO |
| 23 | + static const std::unordered_set<std::string> supported_extensions = { |
| 24 | + "JPG", |
| 25 | + "JPEG", |
| 26 | + "PNG", |
| 27 | + "TIF", |
| 28 | + "TIFF", |
| 29 | + "TGA", |
| 30 | + "BMP", |
| 31 | + "PSD", |
| 32 | + "HDR", |
| 33 | + "DPX", |
| 34 | + "ACES", |
| 35 | + "JP2", |
| 36 | + "J2K", |
| 37 | + "WEBP", |
| 38 | + "EXR", |
| 39 | + }; |
| 40 | + |
| 41 | + // Step 2: Convert the URI to a POSIX path string and fs::path |
| 42 | + std::string path = uri_to_posix_path(uri); |
| 43 | + fs::path p(path); |
| 44 | + |
| 45 | + // Step 3: Check if the file exists and is a regular file |
| 46 | + // Return not supported if the file does not exist or is not a regular file |
| 47 | + if (!fs::exists(p) || !fs::is_regular_file(p)) { |
| 48 | + return MMC_NO; |
| 49 | + } |
| 50 | + |
| 51 | + // Step 4: Get the upper-case extension (handling platform differences) |
| 52 | +#ifdef _WIN32 |
| 53 | + std::string ext = ltrim_char(to_upper_path(p.extension()), '.'); |
| 54 | +#else |
| 55 | + std::string ext = ltrim_char(to_upper(p.extension().string()), '.'); |
| 56 | +#endif |
| 57 | + |
| 58 | + // Step 5: Check if the extension is in the supported list |
| 59 | + // Return fully supported if the extension is in the supported list |
| 60 | + if (supported_extensions.count(ext)) { |
| 61 | + return MMC_FULLY; |
| 62 | + } |
| 63 | + |
| 64 | + // Step 6: Try to detect via OIIO if the extension is supported |
| 65 | + // Return maybe supported if the extension is supported by OIIO |
| 66 | + auto in = OIIO::ImageInput::open(path); |
| 67 | + if (in) { |
| 68 | + in->close(); |
| 69 | + return MMC_MAYBE; |
| 70 | + } |
| 71 | + |
| 72 | + // Step 7: Return not supported if all checks fail |
| 73 | + return MMC_NO; |
| 74 | +} |
| 75 | + |
| 76 | +nlohmann::json OpenImageIOMediaMetadata::read_metadata(const caf::uri &uri) { |
| 77 | + std::string path = uri_to_posix_path(uri); |
| 78 | + |
| 79 | + try { |
| 80 | + // Step 1: Open the image using OpenImageIO |
| 81 | + auto in = OIIO::ImageInput::open(path); |
| 82 | + if (!in) { |
| 83 | + throw std::runtime_error("Cannot open: " + OIIO::geterror()); |
| 84 | + } |
| 85 | + |
| 86 | + // Step 2: Get the image specification |
| 87 | + const OIIO::ImageSpec &spec = in->spec(); |
| 88 | + |
| 89 | + // Step 3: Initialize a JSON object for metadata |
| 90 | + nlohmann::json metadata; |
| 91 | + |
| 92 | + // Step 4: Populate basic image properties (width, height, resolution) |
| 93 | + metadata["width"] = spec.width; |
| 94 | + metadata["height"] = spec.height; |
| 95 | + metadata["resolution"] = fmt::format("{} x {}", spec.width, spec.height); |
| 96 | + |
| 97 | + // Step 5: Extract and format the file extension (format) |
| 98 | + fs::path p(path); |
| 99 | +#ifdef _WIN32 |
| 100 | + std::string ext = ltrim_char(to_upper_path(p.extension()), '.'); |
| 101 | +#else |
| 102 | + std::string ext = ltrim_char(to_upper(p.extension().string()), '.'); |
| 103 | +#endif |
| 104 | + metadata["format"] = ext; |
| 105 | + |
| 106 | + // Step 6: Determine bit depth and add to metadata |
| 107 | + if (spec.format == OIIO::TypeDesc::UINT8) { |
| 108 | + metadata["bit_depth"] = "8 bits"; |
| 109 | + } else if (spec.format == OIIO::TypeDesc::UINT16) { |
| 110 | + metadata["bit_depth"] = "16 bits"; |
| 111 | + } else if (spec.format == OIIO::TypeDesc::HALF) { |
| 112 | + metadata["bit_depth"] = "16 bits float"; |
| 113 | + } else if (spec.format == OIIO::TypeDesc::FLOAT) { |
| 114 | + metadata["bit_depth"] = "32 bits float"; |
| 115 | + } else { |
| 116 | + metadata["bit_depth"] = "unknown"; |
| 117 | + } |
| 118 | + |
| 119 | + // Step 7: Read pixel aspect ratio and aspect ratio |
| 120 | + metadata["pixel_aspect"] = spec.get_float_attribute("PixelAspectRatio", 1.0f); |
| 121 | + metadata["aspect_ratio"] = spec.get_float_attribute("XResolution", spec.width) |
| 122 | + / spec.get_float_attribute("YResolution", spec.height); |
| 123 | + |
| 124 | + // Step 8: Add extra attributes to metadata |
| 125 | + for (const auto ¶m : spec.extra_attribs) { |
| 126 | + metadata[param.name().string()] = param.get_string(); |
| 127 | + } |
| 128 | + |
| 129 | + // Step 9: Return metadata |
| 130 | + return metadata; |
| 131 | + } catch (const std::exception &e) { |
| 132 | + spdlog::error("Failed to read metadata from {}: {}", path, e.what()); |
| 133 | + return nlohmann::json::object(); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +std::optional<MediaMetadata::StandardFields> |
| 138 | +OpenImageIOMediaMetadata::fill_standard_fields(const nlohmann::json &metadata) { |
| 139 | + // Step 1: Initialize StandardFields and set default format |
| 140 | + StandardFields fields; |
| 141 | + fields.format_ = "OpenImageIO"; |
| 142 | + |
| 143 | + // Step 2: Fill in the resolution if present in metadata |
| 144 | + if (metadata.contains("resolution")) { |
| 145 | + fields.resolution_ = metadata["resolution"].get<std::string>(); |
| 146 | + } |
| 147 | + |
| 148 | + // Step 3: Fill in the image extension (format) if present in metadata |
| 149 | + if (metadata.contains("format")) { |
| 150 | + fields.format_ = metadata["format"].get<std::string>(); |
| 151 | + } |
| 152 | + |
| 153 | + // Step 4: Fill in the bit depth if present in metadata |
| 154 | + if (metadata.contains("bit_depth")) { |
| 155 | + fields.bit_depth_ = metadata["bit_depth"].get<std::string>(); |
| 156 | + } |
| 157 | + |
| 158 | + // Step 5: Fill in the pixel aspect ratio if present in metadata |
| 159 | + if (metadata.contains("pixel_aspect")) { |
| 160 | + fields.pixel_aspect_ = metadata["pixel_aspect"].get<float>(); |
| 161 | + } |
| 162 | + |
| 163 | + return std::make_optional(fields); |
| 164 | +} |
| 165 | + |
| 166 | +// Point d'entrée du plugin |
| 167 | +extern "C" { |
| 168 | +plugin_manager::PluginFactoryCollection *plugin_factory_collection_ptr() { |
| 169 | + return new plugin_manager::PluginFactoryCollection( |
| 170 | + std::vector<std::shared_ptr<plugin_manager::PluginFactory>>({std::make_shared< |
| 171 | + MediaMetadataPlugin<MediaMetadataActor<OpenImageIOMediaMetadata>>>( |
| 172 | + Uuid("8f3c4a7e-9b2d-4e1f-a5c8-3d6f7e8a9b1c"), |
| 173 | + "OpenImageIO", |
| 174 | + "xStudio", |
| 175 | + "OpenImageIO Media Metadata Reader", |
| 176 | + semver::version("1.0.0"))})); |
| 177 | +} |
| 178 | +} |
0 commit comments