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 
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 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
44 typedef std::shared_ptr<class GlfImage> GlfImageSharedPtr;
45 
52 class GlfImage
53 {
54 public:
55 
59  {
60  OriginUpperLeft,
61  OriginLowerLeft
62  };
63 
68  class StorageSpec {
69  public:
70  StorageSpec()
71  : width(0), height(0)
72  , format(GL_NONE)
73  , type(GL_NONE)
74  , flipped(false)
75  , data(0) { }
76 
77  int width, height, depth;
78  GLenum format, type;
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 
94  GLF_API
95  static bool IsSupportedImageFile(std::string const & filename);
96 
99 
101  GLF_API
102  static GlfImageSharedPtr OpenForReading(std::string const & filename,
103  int subimage = 0,
104  int mip = 0,
105  bool suppressErrors = false);
106 
108  virtual bool Read(StorageSpec const & storage) = 0;
109 
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 
118 
121 
123  GLF_API
124  static GlfImageSharedPtr OpenForWriting(std::string const & filename);
125 
127  virtual bool Write(StorageSpec const & storage,
128  VtDictionary const & metadata = VtDictionary()) = 0;
129 
131 
133  virtual std::string const & GetFilename() const = 0;
134 
136  virtual int GetWidth() const = 0;
137 
139  virtual int GetHeight() const = 0;
140 
142  virtual GLenum GetFormat() const = 0;
143 
145  virtual GLenum GetType() const = 0;
146 
148  virtual int GetBytesPerPixel() const = 0;
149 
151  virtual int GetNumMipLevels() const = 0;
152 
154  virtual bool IsColorSpaceSRGB() const = 0;
155 
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 
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
193 GlfImage::GetSamplerMetadata(GLenum pname, T * param) const
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 
203 class GlfImageFactoryBase : public TfType::FactoryBase {
204 public:
205  virtual GlfImageSharedPtr New() const = 0;
206 };
207 
208 template <class T>
209 class GlfImageFactory : public GlfImageFactoryBase {
210 public:
211  virtual GlfImageSharedPtr New() const
212  {
213  return GlfImageSharedPtr(new T);
214  }
215 };
216 
217 
218 PXR_NAMESPACE_CLOSE_SCOPE
219 
220 #endif // PXR_IMAGING_GLF_IMAGE_H
virtual bool Read(StorageSpec const &storage)=0
Reads the image file into storage.
virtual GLenum GetFormat() const =0
Returns the image format.
static GLF_API bool IsSupportedImageFile(std::string const &filename)
Returns whether filename opened as a texture image.
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.
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1101
Describes the memory layout and storage of a texture image.
Definition: image.h:68
A map with string keys and VtValue values.
Definition: dictionary.h:63
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.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1073
virtual int GetNumMipLevels() const =0
Returns the number of mips available.
virtual bool _OpenForReading(std::string const &filename, int subimage, int mip, bool suppressErrors)=0
}@
Base class of all factory types.
Definition: type.h:73
virtual GLenum GetType() const =0
Returns the image type.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left...
Definition: image.h:58
virtual std::string const & GetFilename() const =0
}@
virtual bool IsColorSpaceSRGB() const =0
Returns whether the iamge is in the sRGB color space.
virtual bool Write(StorageSpec const &storage, VtDictionary const &metadata=VtDictionary())=0
Writes the image with metadata.
virtual int GetWidth() const =0
Returns the image width.
virtual int GetBytesPerPixel() const =0
Returns the number of bytes per pixel.
static GLF_API GlfImageSharedPtr OpenForWriting(std::string const &filename)
Opens filename for writing from the given storage.
virtual int GetHeight() const =0
Returns the image height.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:174
A base class for reading and writing texture image data.
Definition: image.h:52