HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
image.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 PXR_IMAGING_HIO_IMAGE_H
8 #define PXR_IMAGING_HIO_IMAGE_H
9 
10 /// \file hio/image.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/imaging/hio/api.h"
14 #include "pxr/imaging/hio/types.h"
15 
16 #include "pxr/base/tf/token.h"
17 #include "pxr/base/tf/type.h"
18 #include "pxr/base/vt/dictionary.h"
19 #include "pxr/base/vt/value.h"
20 
21 #include <memory>
22 #include <string>
23 
25 
26 
27 using HioImageSharedPtr = std::shared_ptr<class HioImage>;
28 
29 /// \class HioImage
30 ///
31 /// A base class for reading and writing texture image data.
32 ///
33 /// The class allows basic access to texture image file data.
34 ///
35 /// Texture paths are UTF-8 strings, resolvable by AR. Texture system dispatch
36 /// is driven by extension, with [A-Z] (and no other characters) case folded.
37 class HioImage
38 {
39 public:
40 
41  /// Specifies whether to treat the image origin as the upper-left corner
42  /// or the lower left
44  {
47  };
48 
49  /// Specifies the source color space in which the texture is encoded, with
50  /// "Auto" indicating the texture reader should determine color space based
51  /// on hints from the image (e.g. file type, number of channels, image
52  /// metadata)
54  {
55  Raw,
58  };
59 
60  /// \class StorageSpec
61  ///
62  /// Describes the memory layout and storage of a texture image
63  ///
65  {
66  public:
68  : width(0), height(0), depth(0)
70  , flipped(false)
71  , data(0) { }
72 
75  bool flipped;
76  void * data;
77  };
78 
79 public:
80  HioImage() = default;
81 
82  HIO_API
83  virtual ~HioImage();
84 
85  // Disallow copies
86  HioImage(const HioImage&) = delete;
87  HioImage& operator=(const HioImage&) = delete;
88 
89  /// Returns whether \a filename opened as a texture image.
90  HIO_API
91  static bool IsSupportedImageFile(std::string const & filename);
92 
93  /// \name Reading
94  /// {@
95 
96  /// Opens \a filename for reading from the given \a subimage at mip level
97  /// \a mip, using \a sourceColorSpace to help determine the color space
98  /// with which to interpret the texture
99  HIO_API
100  static HioImageSharedPtr OpenForReading(std::string const & filename,
101  int subimage = 0,
102  int mip = 0,
103  SourceColorSpace sourceColorSpace =
104  SourceColorSpace::Auto,
105  bool suppressErrors = false);
106 
107  /// Reads the image file into \a storage.
108  virtual bool Read(StorageSpec const & storage) = 0;
109 
110  /// Reads the cropped sub-image into \a storage.
111  virtual bool ReadCropped(int const cropTop,
112  int const cropBottom,
113  int const cropLeft,
114  int const cropRight,
115  StorageSpec const & storage) = 0;
116 
117  /// }@
118 
119  /// \name Writing
120  /// {@
121 
122  /// Opens \a filename for writing from the given \a storage.
123  HIO_API
124  static HioImageSharedPtr OpenForWriting(std::string const & filename);
125 
126  /// Writes the image with \a metadata.
127  virtual bool Write(StorageSpec const & storage,
128  VtDictionary const & metadata = VtDictionary()) = 0;
129 
130  /// }@
131 
132  /// Returns the image filename.
133  virtual std::string const & GetFilename() const = 0;
134 
135  /// Returns the image width.
136  virtual int GetWidth() const = 0;
137 
138  /// Returns the image height.
139  virtual int GetHeight() const = 0;
140 
141  /// Returns the destination HioFormat.
142  virtual HioFormat GetFormat() const = 0;
143 
144  /// Returns the number of bytes per pixel.
145  virtual int GetBytesPerPixel() const = 0;
146 
147  /// Returns the number of mips available.
148  virtual int GetNumMipLevels() const = 0;
149 
150  /// Returns whether the image is in the sRGB color space.
151  virtual bool IsColorSpaceSRGB() const = 0;
152 
153  /// \name Metadata
154  /// {@
155  template <typename T>
156  bool GetMetadata(TfToken const & key, T * value) const;
157 
158  virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
159 
160  virtual bool GetSamplerMetadata(HioAddressDimension dim,
161  HioAddressMode * param) const = 0;
162 
163  /// }@
164 
165 protected:
166  virtual bool _OpenForReading(std::string const & filename,
167  int subimage,
168  int mip,
169  SourceColorSpace sourceColorSpace,
170  bool suppressErrors) = 0;
171 
172  virtual bool _OpenForWriting(std::string const & filename) = 0;
173 };
174 
175 template <typename T>
176 bool
177 HioImage::GetMetadata(TfToken const & key, T * value) const
178 {
179  VtValue any;
180  if (!GetMetadata(key, &any) || !any.IsHolding<T>()) {
181  return false;
182  }
183  *value = any.UncheckedGet<T>();
184  return true;
185 }
186 
188 public:
189  virtual HioImageSharedPtr New() const = 0;
190 };
191 
192 template <class T>
194 public:
195  virtual HioImageSharedPtr New() const
196  {
197  return HioImageSharedPtr(new T);
198  }
199 };
200 
201 
203 
204 #endif // PXR_IMAGING_HIO_IMAGE_H
ImageOriginLocation
Definition: image.h:43
bool GetMetadata(TfToken const &key, T *value) const
}@
Definition: image.h:177
virtual bool IsColorSpaceSRGB() const =0
Returns whether the image is in the sRGB color space.
virtual HioFormat GetFormat() const =0
Returns the destination HioFormat.
GT_API const UT_StringHolder filename
virtual bool _OpenForWriting(std::string const &filename)=0
}@
HioAddressMode
Definition: types.h:131
HioImage()=default
T const & UncheckedGet() const &
Definition: value.h:1104
HioImage & operator=(const HioImage &)=delete
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
virtual bool _OpenForReading(std::string const &filename, int subimage, int mip, SourceColorSpace sourceColorSpace, bool suppressErrors)=0
}@
virtual bool ReadCropped(int const cropTop, int const cropBottom, int const cropLeft, int const cropRight, StorageSpec const &storage)=0
Reads the cropped sub-image into storage.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static HIO_API HioImageSharedPtr OpenForWriting(std::string const &filename)
Opens filename for writing from the given storage.
HioAddressDimension
Definition: types.h:120
#define HIO_API
Definition: api.h:23
static HIO_API HioImageSharedPtr OpenForReading(std::string const &filename, int subimage=0, int mip=0, SourceColorSpace sourceColorSpace=SourceColorSpace::Auto, bool suppressErrors=false)
Base class of all factory types.
Definition: type.h:56
HioFormat format
Definition: image.h:74
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
virtual int GetBytesPerPixel() const =0
Returns the number of bytes per pixel.
Definition: token.h:70
virtual HioImageSharedPtr New() const
Definition: image.h:195
bool any(const vbool4 &v)
Definition: simd.h:3600
virtual int GetWidth() const =0
Returns the image width.
virtual bool Read(StorageSpec const &storage)=0
Reads the image file into storage.
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
virtual bool GetSamplerMetadata(HioAddressDimension dim, HioAddressMode *param) const =0
}@
virtual std::string const & GetFilename() const =0
}@
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
HioFormat
Definition: types.h:25
GLenum GLfloat param
Definition: glcorearb.h:104
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::shared_ptr< class HioImage > HioImageSharedPtr
Definition: image.h:27
virtual bool Write(StorageSpec const &storage, VtDictionary const &metadata=VtDictionary())=0
Writes the image with metadata.
bool IsHolding() const
Definition: value.h:1064
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SourceColorSpace
Definition: image.h:53
GLint GLsizei width
Definition: glcorearb.h:103
virtual int GetNumMipLevels() const =0
Returns the number of mips available.
virtual int GetHeight() const =0
Returns the image height.
static HIO_API bool IsSupportedImageFile(std::string const &filename)
Returns whether filename opened as a texture image.
Definition: value.h:146
virtual HIO_API ~HioImage()
Definition: format.h:1821
Definition: image.h:37