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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_GLF_IMAGE_H
25 #define PXR_IMAGING_GLF_IMAGE_H
26 
27 /// \file glf/image.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
31 #include "pxr/imaging/garch/gl.h"
32 
33 #include "pxr/base/tf/token.h"
34 #include "pxr/base/tf/type.h"
35 #include "pxr/base/vt/dictionary.h"
36 #include "pxr/base/vt/value.h"
37 
38 #include <memory>
39 #include <string>
40 
42 
43 
44 typedef std::shared_ptr<class GlfImage> GlfImageSharedPtr;
45 
46 /// \class GlfImage
47 ///
48 /// A base class for reading and writing texture image data.
49 ///
50 /// The class allows basic access to texture image file data.
51 ///
52 class GlfImage
53 {
54 public:
55 
56  /// Specifies whether to treat the image origin as the upper-left corner
57  /// or the lower left
59  {
62  };
63 
64  /// \class StorageSpec
65  ///
66  /// Describes the memory layout and storage of a texture image
67  ///
68  class StorageSpec {
69  public:
71  : width(0), height(0)
72  , format(GL_NONE)
73  , type(GL_NONE)
74  , flipped(false)
75  , data(0) { }
76 
79  bool flipped;
80  void * data;
81  };
82 
83 public:
84  GlfImage() = default;
85 
86  GLF_API
87  virtual ~GlfImage();
88 
89  // Disallow copies
90  GlfImage(const GlfImage&) = delete;
91  GlfImage& operator=(const GlfImage&) = delete;
92 
93  /// Returns whether \a filename opened as a texture image.
94  GLF_API
95  static bool IsSupportedImageFile(std::string const & filename);
96 
97  /// \name Reading
98  /// {@
99 
100  /// Opens \a filename for reading from the given \a subimage.
101  GLF_API
103  int subimage = 0,
104  int mip = 0,
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  GLF_API
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 image format.
142  virtual GLenum GetFormat() const = 0;
143 
144  /// Returns the image type.
145  virtual GLenum GetType() const = 0;
146 
147  /// Returns the number of bytes per pixel.
148  virtual int GetBytesPerPixel() const = 0;
149 
150  /// Returns the number of mips available.
151  virtual int GetNumMipLevels() const = 0;
152 
153  /// Returns whether the iamge is in the sRGB color space.
154  virtual bool IsColorSpaceSRGB() const = 0;
155 
156  /// \name Metadata
157  /// {@
158  template <typename T>
159  bool GetMetadata(TfToken const & key, T * value) const;
160 
161  virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
162 
163  template <typename T>
164  bool GetSamplerMetadata(GLenum pname, T * param) const;
165 
166  virtual bool GetSamplerMetadata(GLenum pname, VtValue * param) const = 0;
167 
168  /// }@
169 
170 protected:
171  virtual bool _OpenForReading(std::string const & filename,
172  int subimage,
173  int mip,
174  bool suppressErrors) = 0;
175 
176  virtual bool _OpenForWriting(std::string const & filename) = 0;
177 };
178 
179 template <typename T>
180 bool
181 GlfImage::GetMetadata(TfToken const & key, T * value) const
182 {
183  VtValue any;
184  if (!GetMetadata(key, &any) || !any.IsHolding<T>()) {
185  return false;
186  }
187  *value = any.UncheckedGet<T>();
188  return true;
189 }
190 
191 template <typename T>
192 bool
194 {
195  VtValue any;
196  if (!GetSamplerMetadata(pname, &any) || !any.IsHolding<T>()) {
197  return false;
198  }
199  *param = any.UncheckedGet<T>();
200  return true;
201 }
202 
204 public:
205  virtual GlfImageSharedPtr New() const = 0;
206 };
207 
208 template <class T>
210 public:
211  virtual GlfImageSharedPtr New() const
212  {
213  return GlfImageSharedPtr(new T);
214  }
215 };
216 
217 
219 
220 #endif // PXR_IMAGING_GLF_IMAGE_H
T const & UncheckedGet() const
Definition: value.h:1101
virtual int GetHeight() const =0
Returns the image height.
GT_API const UT_StringHolder filename
#define GLF_API
Definition: api.h:40
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
ImageOriginLocation
Definition: image.h:58
virtual int GetWidth() const =0
Returns the image width.
virtual GLenum GetFormat() const =0
Returns the image format.
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1252
static GLF_API bool IsSupportedImageFile(std::string const &filename)
Returns whether filename opened as a texture image.
virtual bool Read(StorageSpec const &storage)=0
Reads the image file into storage.
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
static GLF_API GlfImageSharedPtr OpenForReading(std::string const &filename, int subimage=0, int mip=0, bool suppressErrors=false)
Opens filename for reading from the given subimage.
unsigned int GLenum
Definition: cl.hpp:166
GLenum pname
Definition: glew.h:1688
GLfloat param
Definition: glew.h:1530
bool GetSamplerMetadata(GLenum pname, T *param) const
}@
Definition: image.h:193
Base class of all factory types.
Definition: type.h:73
Definition: token.h:87
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.
virtual int GetBytesPerPixel() const =0
Returns the number of bytes per pixel.
bool any(const vbool4 &v)
Definition: simd.h:3372
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
bool GetMetadata(TfToken const &key, T *value) const
}@
Definition: image.h:181
virtual int GetNumMipLevels() const =0
Returns the number of mips available.
virtual bool IsColorSpaceSRGB() const =0
Returns whether the iamge is in the sRGB color space.
virtual bool _OpenForWriting(std::string const &filename)=0
}@
virtual GLF_API ~GlfImage()
PXR_NAMESPACE_OPEN_SCOPE typedef std::shared_ptr< class GlfImage > GlfImageSharedPtr
Definition: image.h:44
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
virtual GLenum GetType() const =0
Returns the image type.
GLsizei const GLchar *const * string
Definition: glew.h:1844
virtual bool Write(StorageSpec const &storage, VtDictionary const &metadata=VtDictionary())=0
Writes the image with metadata.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
GlfImage & operator=(const GlfImage &)=delete
virtual GlfImageSharedPtr New() const
Definition: image.h:211
bool IsHolding() const
Definition: value.h:1073
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
static GLF_API GlfImageSharedPtr OpenForWriting(std::string const &filename)
Opens filename for writing from the given storage.
getOption("OpenEXR.storage") storage
Definition: HDK_Image.dox:276
#define GL_NONE
Definition: glew.h:313
GlfImage()=default
virtual GlfImageSharedPtr New() const =0
GLsizei const GLfloat * value
Definition: glew.h:1849
Definition: value.h:174
Definition: image.h:52
virtual bool _OpenForReading(std::string const &filename, int subimage, int mip, bool suppressErrors)=0
}@
virtual std::string const & GetFilename() const =0
}@