|
| 1 | +// Copyright OpenFX and contributors to the OpenFX project. |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <ofxCore.h> |
| 7 | +#include <ofxImageEffect.h> |
| 8 | + |
| 9 | +#include <memory> // For std::unique_ptr |
| 10 | + |
| 11 | +#include "openfx/ofxExceptions.h" |
| 12 | +#include "openfx/ofxImage.h" |
| 13 | +#include "openfx/ofxPropsAccess.h" |
| 14 | + |
| 15 | +namespace openfx { |
| 16 | + |
| 17 | +class Clip { |
| 18 | + private: |
| 19 | + const OfxImageEffectSuiteV1* mEffectSuite; |
| 20 | + const OfxPropertySuiteV1* mPropertySuite; |
| 21 | + OfxImageClipHandle mClip{}; |
| 22 | + OfxPropertySetHandle mClipPropSet{}; // The clip property set handle |
| 23 | + std::unique_ptr<PropertyAccessor> mClipProps; // Accessor: use a pointer to defer construction |
| 24 | + |
| 25 | + public: |
| 26 | + // Construct a clip given the raw clip handle. |
| 27 | + // Gets the property set and sets up accessor for it. |
| 28 | + Clip(const OfxImageEffectSuiteV1* effect_suite, const OfxPropertySuiteV1* prop_suite, |
| 29 | + OfxImageClipHandle clip) |
| 30 | + : mClip(clip), mEffectSuite(effect_suite), mPropertySuite(prop_suite), mClipProps(nullptr) { |
| 31 | + if (clip != nullptr) { |
| 32 | + OfxStatus status = mEffectSuite->clipGetPropertySet(clip, &mClipPropSet); |
| 33 | + if (status != kOfxStatOK) |
| 34 | + throw ClipNotFoundException(status); |
| 35 | + if (mClipPropSet) { |
| 36 | + mClipProps = std::make_unique<PropertyAccessor>(mClipPropSet, prop_suite); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Construct a clip given effect and clip name. |
| 42 | + // Gets the clip with its property set, and sets up accessor for it. |
| 43 | + Clip(const OfxImageEffectSuiteV1* effect_suite, const OfxPropertySuiteV1* prop_suite, |
| 44 | + OfxImageEffectHandle effect, std::string_view clip_name) |
| 45 | + : mEffectSuite(effect_suite), mPropertySuite(prop_suite), mClipProps(nullptr) { |
| 46 | + OfxStatus status = effect_suite->clipGetHandle(effect, clip_name.data(), &mClip, &mClipPropSet); |
| 47 | + if (status != kOfxStatOK || !mClip || !mClipPropSet) |
| 48 | + throw ClipNotFoundException(status); |
| 49 | + mClipProps = std::make_unique<PropertyAccessor>(mClipPropSet, prop_suite); |
| 50 | + } |
| 51 | + |
| 52 | + // Default constructor: empty clip |
| 53 | + Clip() : mEffectSuite(nullptr), mClipProps(nullptr) {} |
| 54 | + |
| 55 | + // Destructor releases the resource |
| 56 | + ~Clip() { |
| 57 | + if (mClipPropSet) { |
| 58 | + mClipPropSet = nullptr; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Disable copying |
| 63 | + Clip(const Clip&) = delete; |
| 64 | + Clip& operator=(const Clip&) = delete; |
| 65 | + |
| 66 | + // Enable moving |
| 67 | + Clip(Clip&& other) noexcept |
| 68 | + : mEffectSuite(other.mEffectSuite), |
| 69 | + mPropertySuite(other.mPropertySuite), |
| 70 | + mClip(other.mClip), |
| 71 | + mClipPropSet(other.mClipPropSet), |
| 72 | + mClipProps(std::move(other.mClipProps)) { |
| 73 | + other.mClipPropSet = nullptr; |
| 74 | + } |
| 75 | + |
| 76 | + Clip& operator=(Clip&& other) noexcept { |
| 77 | + if (this != &other) { |
| 78 | + // Release any existing resource |
| 79 | + |
| 80 | + // Acquire the other's resource |
| 81 | + mEffectSuite = other.mEffectSuite; |
| 82 | + mPropertySuite = other.mPropertySuite; |
| 83 | + mClip = other.mClip; |
| 84 | + mClipPropSet = other.mClipPropSet; |
| 85 | + mClipProps = std::move(other.mClipProps); |
| 86 | + other.mClipPropSet = nullptr; |
| 87 | + } |
| 88 | + return *this; |
| 89 | + } |
| 90 | + |
| 91 | + // Get an image from the clip at this time |
| 92 | + openfx::Image get_image(OfxTime time, const OfxRectD* rect = nullptr) { |
| 93 | + return openfx::Image(mEffectSuite, mPropertySuite, mClip, time, rect); |
| 94 | + } |
| 95 | + |
| 96 | + // get the clip handle |
| 97 | + OfxImageClipHandle clip() const { return mClip; } |
| 98 | + |
| 99 | + // get the clip's prop set |
| 100 | + OfxPropertySetHandle get_propset() const { return mClipPropSet; } |
| 101 | + |
| 102 | + // Accessor for PropertyAccessor |
| 103 | + PropertyAccessor* props() { return mClipProps.get(); } |
| 104 | + const PropertyAccessor* props() const { return mClipProps.get(); } |
| 105 | + |
| 106 | + // Implicit conversions to the handle types |
| 107 | + explicit operator OfxPropertySetHandle() const { return mClipPropSet; } |
| 108 | + explicit operator OfxImageClipHandle() const { return mClip; } |
| 109 | + |
| 110 | + bool empty() const { return mClip == nullptr; } |
| 111 | +}; |
| 112 | + |
| 113 | +} // namespace openfx |
0 commit comments