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 GLF_IMAGE_H
25 #define 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 <boost/noncopyable.hpp>
39 #include <boost/shared_ptr.hpp>
40 
41 #include <string>
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
45 
46 typedef boost::shared_ptr<class GlfImage> GlfImageSharedPtr;
47 
54 class GlfImage : public boost::noncopyable {
55 
56 public:
57 
61  {
62  OriginUpperLeft,
63  OriginLowerLeft
64  };
65 
70  class StorageSpec {
71  public:
72  StorageSpec()
73  : width(0), height(0)
74  , format(GL_NONE)
75  , type(GL_NONE)
76  , flipped(false)
77  , data(0) { }
78 
79  int width, height, depth;
80  GLenum format, type;
81  bool flipped;
82  void * data;
83  };
84 
85 public:
86  GLF_API
87  virtual ~GlfImage();
88 
90  GLF_API
91  static bool IsSupportedImageFile(std::string const & filename);
92 
95 
97  GLF_API
98  static GlfImageSharedPtr OpenForReading(std::string const & filename,
99  int subimage = 0,
100  int mip = 0,
101  bool suppressErrors = false);
102 
104  virtual bool Read(StorageSpec const & storage) = 0;
105 
107  virtual bool ReadCropped(int const cropTop,
108  int const cropBottom,
109  int const cropLeft,
110  int const cropRight,
111  StorageSpec const & storage) = 0;
112 
114 
117 
119  GLF_API
120  static GlfImageSharedPtr OpenForWriting(std::string const & filename);
121 
123  virtual bool Write(StorageSpec const & storage,
124  VtDictionary const & metadata = VtDictionary()) = 0;
125 
127 
129  virtual std::string const & GetFilename() const = 0;
130 
132  virtual int GetWidth() const = 0;
133 
135  virtual int GetHeight() const = 0;
136 
138  virtual GLenum GetFormat() const = 0;
139 
141  virtual GLenum GetType() const = 0;
142 
144  virtual int GetBytesPerPixel() const = 0;
145 
147  virtual int GetNumMipLevels() const = 0;
148 
150  virtual bool IsColorSpaceSRGB() const = 0;
151 
154  template <typename T>
155  bool GetMetadata(TfToken const & key, T * value) const;
156 
157  virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
158 
159  template <typename T>
160  bool GetSamplerMetadata(GLenum pname, T * param) const;
161 
162  virtual bool GetSamplerMetadata(GLenum pname, VtValue * param) const = 0;
163 
165 
166 protected:
167  virtual bool _OpenForReading(std::string const & filename,
168  int subimage,
169  int mip,
170  bool suppressErrors) = 0;
171 
172  virtual bool _OpenForWriting(std::string const & filename) = 0;
173 };
174 
175 template <typename T>
176 bool
177 GlfImage::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 
187 template <typename T>
188 bool
189 GlfImage::GetSamplerMetadata(GLenum pname, T * param) const
190 {
191  VtValue any;
192  if (!GetSamplerMetadata(pname, &any) || !any.IsHolding<T>()) {
193  return false;
194  }
195  *param = any.UncheckedGet<T>();
196  return true;
197 }
198 
199 class GlfImageFactoryBase : public TfType::FactoryBase {
200 public:
201  virtual GlfImageSharedPtr New() const = 0;
202 };
203 
204 template <class T>
205 class GlfImageFactory : public GlfImageFactoryBase {
206 public:
207  virtual GlfImageSharedPtr New() const
208  {
209  return GlfImageSharedPtr(new T);
210  }
211 };
212 
213 
214 PXR_NAMESPACE_CLOSE_SCOPE
215 
216 #endif // 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:836
Describes the memory layout and storage of a texture image.
Definition: image.h:70
A map with string keys and VtValue values.
Definition: dictionary.h:61
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:808
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:79
virtual GLenum GetType() const =0
Returns the image type.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left...
Definition: image.h:60
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:182
A base class for reading and writing texture image data.
Definition: image.h:54