HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
colorSpaceAPI.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef USD_GENERATED_COLORSPACEAPI_H
8 #define USD_GENERATED_COLORSPACEAPI_H
9 
10 /// \file usd/colorSpaceAPI.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usd/api.h"
15 #include "pxr/usd/usd/prim.h"
16 #include "pxr/usd/usd/stage.h"
17 #include "pxr/usd/usd/tokens.h"
18 
19 #include "pxr/base/gf/colorSpace.h"
20 #include "pxr/base/tf/bigRWMutex.h"
21 
22 
23 #include "pxr/base/vt/value.h"
24 
25 #include "pxr/base/gf/vec3d.h"
26 #include "pxr/base/gf/vec3f.h"
27 #include "pxr/base/gf/matrix4d.h"
28 
29 #include "pxr/base/tf/token.h"
30 #include "pxr/base/tf/type.h"
31 
33 
34 class SdfAssetPath;
35 
36 // -------------------------------------------------------------------------- //
37 // COLORSPACEAPI //
38 // -------------------------------------------------------------------------- //
39 
40 /// \class UsdColorSpaceAPI
41 ///
42 /// UsdColorSpaceAPI is an API schema that introduces a `colorSpace`
43 /// property for authoring scene referred color space opinions. It also provides
44 /// a mechanism to determine the applicable color space within a scope through
45 /// inheritance. Accordingly, this schema may be applied to any prim to
46 /// introduce a color space at any point in a compositional hierarchy.
47 ///
48 /// Color space resolution involves determining the color space authored on an
49 /// attribute by first examining the attribute itself for a color space which
50 /// may have been authored via `UsdAttribute::SetColorSpace()`. If none is found,
51 /// the attribute's prim is checked for the existence of the `UsdColorSpaceAPI`,
52 /// and any color space authored there. If none is found on the attribute's
53 /// prim, the prim's ancestors are examined up the hierarchy until an authored
54 /// color space is found. If no color space is found, an empty `TfToken` is
55 /// returned. When no color space is found, the default color space is linear,
56 /// with Rec709 primaries and D65 white point, corresponding to the GfColorSpace
57 /// token `LinearRec709`.
58 ///
59 /// For a list of built in color space token values, see `GfColorSpaceNames`.
60 ///
61 /// Use a pattern like this when determining an attribute's resolved color space:
62 ///
63 /// ```
64 /// TfToken attrCs = attr.GetColorSpace();
65 /// if (!attrCs.IsEmpty()) {
66 /// return attrCs;
67 /// }
68 /// auto csAPI = UsdColorSpaceAPI(attr.GetPrim());
69 /// return UsdColorSpaceAPI::ComputeColorSpaceName(attr);
70 /// ```
71 ///
72 /// `GfColorSpace` and its associated utilities can be used to perform color
73 /// transformations; some examples:
74 ///
75 /// ```
76 /// srcSpace = GfColorSpace(ComputeColorSpaceName(attr))
77 /// targetSpace = GfColorSpace(targetSpaceName)
78 /// targetColor = srcSpace.Convert(targetSpace, srcColor)
79 /// srcSpace.ConvertRGBSpan(targetSpace, colorSpan)
80 /// ```
81 ///
82 /// It is recommended that in situations where performance is a concern, an
83 /// application should perform conversions infrequently and cache results
84 /// wherever possible.
85 ///
86 ///
87 ///
88 /// For any described attribute \em Fallback \em Value or \em Allowed \em Values below
89 /// that are text/tokens, the actual token is published and defined in \ref UsdTokens.
90 /// So to set an attribute to the value "rightHanded", use UsdTokens->rightHanded
91 /// as the value.
92 ///
94 {
95 public:
96  /// Compile time constant representing what kind of schema this class is.
97  ///
98  /// \sa UsdSchemaKind
100 
101  /// Construct a UsdColorSpaceAPI on UsdPrim \p prim .
102  /// Equivalent to UsdColorSpaceAPI::Get(prim.GetStage(), prim.GetPath())
103  /// for a \em valid \p prim, but will not immediately throw an error for
104  /// an invalid \p prim
105  explicit UsdColorSpaceAPI(const UsdPrim& prim=UsdPrim())
106  : UsdAPISchemaBase(prim)
107  {
108  }
109 
110  /// Construct a UsdColorSpaceAPI on the prim held by \p schemaObj .
111  /// Should be preferred over UsdColorSpaceAPI(schemaObj.GetPrim()),
112  /// as it preserves SchemaBase state.
113  explicit UsdColorSpaceAPI(const UsdSchemaBase& schemaObj)
114  : UsdAPISchemaBase(schemaObj)
115  {
116  }
117 
118  /// Destructor.
119  USD_API
120  virtual ~UsdColorSpaceAPI();
121 
122  /// Return a vector of names of all pre-declared attributes for this schema
123  /// class and all its ancestor classes. Does not include attributes that
124  /// may be authored by custom/extended methods of the schemas involved.
125  USD_API
126  static const TfTokenVector &
127  GetSchemaAttributeNames(bool includeInherited=true);
128 
129  /// Return a UsdColorSpaceAPI holding the prim adhering to this
130  /// schema at \p path on \p stage. If no prim exists at \p path on
131  /// \p stage, or if the prim at that path does not adhere to this schema,
132  /// return an invalid schema object. This is shorthand for the following:
133  ///
134  /// \code
135  /// UsdColorSpaceAPI(stage->GetPrimAtPath(path));
136  /// \endcode
137  ///
138  USD_API
139  static UsdColorSpaceAPI
140  Get(const UsdStagePtr &stage, const SdfPath &path);
141 
142 
143  /// Returns true if this <b>single-apply</b> API schema can be applied to
144  /// the given \p prim. If this schema can not be a applied to the prim,
145  /// this returns false and, if provided, populates \p whyNot with the
146  /// reason it can not be applied.
147  ///
148  /// Note that if CanApply returns false, that does not necessarily imply
149  /// that calling Apply will fail. Callers are expected to call CanApply
150  /// before calling Apply if they want to ensure that it is valid to
151  /// apply a schema.
152  ///
153  /// \sa UsdPrim::GetAppliedSchemas()
154  /// \sa UsdPrim::HasAPI()
155  /// \sa UsdPrim::CanApplyAPI()
156  /// \sa UsdPrim::ApplyAPI()
157  /// \sa UsdPrim::RemoveAPI()
158  ///
159  USD_API
160  static bool
161  CanApply(const UsdPrim &prim, std::string *whyNot=nullptr);
162 
163  /// Applies this <b>single-apply</b> API schema to the given \p prim.
164  /// This information is stored by adding "ColorSpaceAPI" to the
165  /// token-valued, listOp metadata \em apiSchemas on the prim.
166  ///
167  /// \return A valid UsdColorSpaceAPI object is returned upon success.
168  /// An invalid (or empty) UsdColorSpaceAPI object is returned upon
169  /// failure. See \ref UsdPrim::ApplyAPI() for conditions
170  /// resulting in failure.
171  ///
172  /// \sa UsdPrim::GetAppliedSchemas()
173  /// \sa UsdPrim::HasAPI()
174  /// \sa UsdPrim::CanApplyAPI()
175  /// \sa UsdPrim::ApplyAPI()
176  /// \sa UsdPrim::RemoveAPI()
177  ///
178  USD_API
179  static UsdColorSpaceAPI
180  Apply(const UsdPrim &prim);
181 
182 protected:
183  /// Returns the kind of schema this class belongs to.
184  ///
185  /// \sa UsdSchemaKind
186  USD_API
187  UsdSchemaKind _GetSchemaKind() const override;
188 
189 private:
190  // needs to invoke _GetStaticTfType.
191  friend class UsdSchemaRegistry;
192  USD_API
193  static const TfType &_GetStaticTfType();
194 
195  static bool _IsTypedSchema();
196 
197  // override SchemaBase virtuals.
198  USD_API
199  const TfType &_GetTfType() const override;
200 
201 public:
202  // --------------------------------------------------------------------- //
203  // COLORSPACENAME
204  // --------------------------------------------------------------------- //
205  /// The color space that applies to attributes with
206  /// unauthored color spaces on this prim and its descendents.
207  ///
208  ///
209  /// | ||
210  /// | -- | -- |
211  /// | Declaration | `uniform token colorSpace:name` |
212  /// | C++ Type | TfToken |
213  /// | \ref Usd_Datatypes "Usd Type" | SdfValueTypeNames->Token |
214  /// | \ref SdfVariability "Variability" | SdfVariabilityUniform |
215  USD_API
217 
218  /// See GetColorSpaceNameAttr(), and also
219  /// \ref Usd_Create_Or_Get_Property for when to use Get vs Create.
220  /// If specified, author \p defaultValue as the attribute's default,
221  /// sparsely (when it makes sense to do so) if \p writeSparsely is \c true -
222  /// the default for \p writeSparsely is \c false.
223  USD_API
224  UsdAttribute CreateColorSpaceNameAttr(VtValue const &defaultValue = VtValue(), bool writeSparsely=false) const;
225 
226 public:
227  // ===================================================================== //
228  // Feel free to add custom code below this line, it will be preserved by
229  // the code generator.
230  //
231  // Just remember to:
232  // - Close the class declaration with };
233  // - Close the namespace with PXR_NAMESPACE_CLOSE_SCOPE
234  // - Close the include guard with #endif
235  // ===================================================================== //
236  // --(BEGIN CUSTOM CODE)--
237 
238  /// A minimalistic cache for color space lookups.
239  /// An application may provide its own cache implementation to avoid
240  /// redundant color space lookups. The cache should be cleared or updated
241  /// when the color space properties have changed. A typical use pattern
242  /// would be to create a temporary cache at the beginning of a traversal,
243  /// and pass it to the color space API methods.
245  public:
246  ColorSpaceCache() = default;
247  virtual ~ColorSpaceCache() = default;
248 
249  virtual TfToken Find(const SdfPath& prim) = 0;
250  virtual void Insert(const SdfPath& prim, const TfToken& colorSpace) = 0;
251  };
252 
253  /// This is a simple example of a color space cache implementation.
254  /// An application can use this cache to avoid redundant color space lookups,
255  /// but may substitute its own cache implementation if desired. This
256  /// implementation does not track changes to the color space properties, so
257  /// it is the application's responsibility to clear or update the cache when
258  /// necessary.
259  ///
260  /// A more sophisticated cache implementation might also be intelligent
261  /// about caching locally defined color spaces and their computed
262  /// GfColorSpaces. It might also be aware of changes to the scene hierarchy
263  /// and update the cache accordingly.
264  ///
265  /// As it is, this simple cache will be efficient in a typical scene using
266  /// built in color spaces. It uses `TfBigRWMutex` to enable many-reader
267  /// single-writer thread safety; however this example does not provide a
268  /// guarantee of thread-efficiency for any given cache access pattern.
270  protected:
273  public:
274  ColorSpaceHashCache() = default;
275  ~ColorSpaceHashCache() = default;
276 
277  TfToken Find(const SdfPath& prim) override {
278  TfBigRWMutex::ScopedLock lock(_mutex, /*write=*/false);
279  auto it = _cache.find(prim);
280  if (it != _cache.end()) {
281  return it->second;
282  }
283  return TfToken();
284  }
285 
286  void Insert(const SdfPath& prim, const TfToken& colorSpace) override {
288  _cache[prim] = colorSpace;
289  }
290  };
291 
292  /// Computes the color space name for the given attribute.
293  /// The attribute is first checked for an authored color space; if one
294  /// exists, it's returned. Otherwise, the attribute's prim is consulted,
295  /// following the inheritance rules for color space determination on a prim.
296  /// If one is found, it's returned. Otherwise, the value on the attribute's
297  /// prim definition is returned if there is one. Otherwise, an empty TfToken
298  /// is returned.
299  ///
300  /// This function may be considered a reference implementation for
301  /// determining the color space of an attribute. Since the algorithm is
302  /// implemented as an exhaustive search performed through the prim
303  /// hierarchy, applications may want to implement a caching mechanism to
304  /// avoid redundant searches.
305  ///
306  /// \sa UsdColorSpaceAPI::Apply
307  /// \sa UsdColorSpaceAPI::GetColorSpaceAttr
308  /// \param attribute The attribute to compute the color space for.
309  /// \param cache An optional cache for accelerating color space lookups.
310  USD_API
312  ColorSpaceCache* cache = nullptr);
313 
314  /// Computes the color space for the given attribute on this prim, using
315  /// the same algorithm as `ComputeColorSpaceName`. The same performance
316  /// caveat applies.
317  /// \param attribute The attribute to compute the color space for.
318  /// \param cache A cache object for accelerating color space lookups.
319  USD_API
321  ColorSpaceCache* cache = nullptr);
322 
323  /// Computes the color space name for the given prim.
324  /// The color space is determined by checking this prim for a
325  /// `colorSpace` property. If no `colorSpace `property is authored,
326  /// the search continues up the prim's hierarchy until a `colorSpace`
327  /// property is found or the root prim is reached. If no `colorSpace`
328  /// property is found, an empty `TfToken` is returned.
329  ///
330  /// If a `colorSpace` name is found, but does not match one of the standard
331  /// color spaces or a user defined color space, an empty `TfToken` is
332  /// returned.
333  ///
334  /// This function should be considered as a reference implementation, and
335  /// applications may want to implement a caching mechanism for performance.
336  ///
337  /// \sa UsdColorSpaceAPI::Apply
338  /// \sa UsdColorSpaceAPI::GetColorSpaceAttr
339  /// \param prim The prim to compute the color space for.
340  /// \param cache A cache object for accelerating color space lookups.
341  USD_API
343  ColorSpaceCache* cache = nullptr);
344 
345  /// Creates a color space object for the named color space if it built in,
346  /// defined on the prim or on an ancestor.
347  /// \param prim The prim from which a search for a defined color space begins.
348  /// \param colorSpace The name of the color space.
349  /// \param cache A cache object for accelerating color space lookups.
350  USD_API
352  const TfToken& colorSpace,
353  ColorSpaceCache* cache = nullptr);
354 
355  /// Computes the color space for this prim, using the same algorithm as
356  /// `ComputeColorSpaceName()`. The same performance caveat applies.
357  /// \param prim The prim to check for the color space.
358  /// \param cache A cache object for accelerating color space lookups.
359  USD_API
361  ColorSpaceCache* cache = nullptr);
362 
363  /// Returns true if the named color space is built in, defined on the
364  /// supplied prim, or on one of the prim's ancestors.
365  /// \param prim The prim from which a search for a defined color space begins.
366  /// \param colorSpace The name of the color space to verify.
367  /// \param cache A cache object for accelerating color space lookups.
368  USD_API
369  static bool IsValidColorSpaceName(UsdPrim prim,
370  const TfToken& colorSpace,
371  ColorSpaceCache* cache = nullptr);
372 };
373 
375 
376 #endif
static const UsdSchemaKind schemaKind
Definition: colorSpaceAPI.h:99
static USD_API const TfTokenVector & GetSchemaAttributeNames(bool includeInherited=true)
#define USD_API
Definition: api.h:23
static USD_API GfColorSpace ComputeColorSpace(const UsdAttribute &attribute, ColorSpaceCache *cache=nullptr)
Single Apply API schema.
iterator end()
Definition: hashmap.h:301
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USD_API UsdAttribute GetColorSpaceNameAttr() const
virtual ~ColorSpaceCache()=default
virtual USD_API ~UsdColorSpaceAPI()
Destructor.
Definition: token.h:70
static USD_API bool CanApply(const UsdPrim &prim, std::string *whyNot=nullptr)
UsdColorSpaceAPI(const UsdPrim &prim=UsdPrim())
USD_API UsdSchemaKind _GetSchemaKind() const override
TfHashMap< SdfPath, TfToken, SdfPath::Hash > _cache
static USD_API UsdColorSpaceAPI Apply(const UsdPrim &prim)
Definition: prim.h:116
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
Definition: path.h:273
UsdSchemaKind
Definition: common.h:112
void Insert(const SdfPath &prim, const TfToken &colorSpace) override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static USD_API bool IsValidColorSpaceName(UsdPrim prim, const TfToken &colorSpace, ColorSpaceCache *cache=nullptr)
virtual TfToken Find(const SdfPath &prim)=0
static USD_API UsdColorSpaceAPI Get(const UsdStagePtr &stage, const SdfPath &path)
static USD_API TfToken ComputeColorSpaceName(const UsdAttribute &attribute, ColorSpaceCache *cache=nullptr)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
Definition: type.h:47
USD_API UsdAttribute CreateColorSpaceNameAttr(VtValue const &defaultValue=VtValue(), bool writeSparsely=false) const
virtual void Insert(const SdfPath &prim, const TfToken &colorSpace)=0
UsdColorSpaceAPI(const UsdSchemaBase &schemaObj)
TfToken Find(const SdfPath &prim) override
Definition: value.h:146