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_HIO_IMAGE_H
25 #define PXR_IMAGING_HIO_IMAGE_H
26 
27 /// \file hio/image.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/hio/api.h"
31 #include "pxr/imaging/hio/types.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 using HioImageSharedPtr = std::shared_ptr<class HioImage>;
45 
46 /// \class HioImage
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 HioImage
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  /// Specifies the source color space in which the texture is encoded, with
65  /// "Auto" indicating the texture reader should determine color space based
66  /// on hints from the image (e.g. file type, number of channels, image
67  /// metadata)
69  {
70  Raw,
73  };
74 
75  /// \class StorageSpec
76  ///
77  /// Describes the memory layout and storage of a texture image
78  ///
80  {
81  public:
83  : width(0), height(0), depth(0)
85  , flipped(false)
86  , data(0) { }
87 
90  bool flipped;
91  void * data;
92  };
93 
94 public:
95  HioImage() = default;
96 
97  HIO_API
98  virtual ~HioImage();
99 
100  // Disallow copies
101  HioImage(const HioImage&) = delete;
102  HioImage& operator=(const HioImage&) = delete;
103 
104  /// Returns whether \a filename opened as a texture image.
105  HIO_API
106  static bool IsSupportedImageFile(std::string const & filename);
107 
108  /// \name Reading
109  /// {@
110 
111  /// Opens \a filename for reading from the given \a subimage at mip level
112  /// \a mip, using \a sourceColorSpace to help determine the color space
113  /// with which to interpret the texture
114  HIO_API
116  int subimage = 0,
117  int mip = 0,
118  SourceColorSpace sourceColorSpace =
119  SourceColorSpace::Auto,
120  bool suppressErrors = false);
121 
122  /// Reads the image file into \a storage.
123  virtual bool Read(StorageSpec const & storage) = 0;
124 
125  /// Reads the cropped sub-image into \a storage.
126  virtual bool ReadCropped(int const cropTop,
127  int const cropBottom,
128  int const cropLeft,
129  int const cropRight,
130  StorageSpec const & storage) = 0;
131 
132  /// }@
133 
134  /// \name Writing
135  /// {@
136 
137  /// Opens \a filename for writing from the given \a storage.
138  HIO_API
140 
141  /// Writes the image with \a metadata.
142  virtual bool Write(StorageSpec const & storage,
143  VtDictionary const & metadata = VtDictionary()) = 0;
144 
145  /// }@
146 
147  /// Returns the image filename.
148  virtual std::string const & GetFilename() const = 0;
149 
150  /// Returns the image width.
151  virtual int GetWidth() const = 0;
152 
153  /// Returns the image height.
154  virtual int GetHeight() const = 0;
155 
156  /// Returns the destination HioFormat.
157  virtual HioFormat GetFormat() const = 0;
158 
159  /// Returns the number of bytes per pixel.
160  virtual int GetBytesPerPixel() const = 0;
161 
162  /// Returns the number of mips available.
163  virtual int GetNumMipLevels() const = 0;
164 
165  /// Returns whether the image is in the sRGB color space.
166  virtual bool IsColorSpaceSRGB() const = 0;
167 
168  /// \name Metadata
169  /// {@
170  template <typename T>
171  bool GetMetadata(TfToken const & key, T * value) const;
172 
173  virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
174 
175  virtual bool GetSamplerMetadata(HioAddressDimension dim,
176  HioAddressMode * param) const = 0;
177 
178  /// }@
179 
180 protected:
181  virtual bool _OpenForReading(std::string const & filename,
182  int subimage,
183  int mip,
184  SourceColorSpace sourceColorSpace,
185  bool suppressErrors) = 0;
186 
187  virtual bool _OpenForWriting(std::string const & filename) = 0;
188 };
189 
190 template <typename T>
191 bool
192 HioImage::GetMetadata(TfToken const & key, T * value) const
193 {
194  VtValue any;
195  if (!GetMetadata(key, &any) || !any.IsHolding<T>()) {
196  return false;
197  }
198  *value = any.UncheckedGet<T>();
199  return true;
200 }
201 
203 public:
204  virtual HioImageSharedPtr New() const = 0;
205 };
206 
207 template <class T>
209 public:
210  virtual HioImageSharedPtr New() const
211  {
212  return HioImageSharedPtr(new T);
213  }
214 };
215 
216 
218 
219 #endif // PXR_IMAGING_HIO_IMAGE_H
ImageOriginLocation
Definition: image.h:58
bool GetMetadata(TfToken const &key, T *value) const
}@
Definition: image.h:192
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:148
HioImage()=default
T const & UncheckedGet() const &
Definition: value.h:1094
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 GLchar *const * string
Definition: glcorearb.h:814
static HIO_API HioImageSharedPtr OpenForWriting(std::string const &filename)
Opens filename for writing from the given storage.
HioAddressDimension
Definition: types.h:137
#define HIO_API
Definition: api.h:40
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:73
HioFormat format
Definition: image.h:89
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
virtual int GetBytesPerPixel() const =0
Returns the number of bytes per pixel.
Definition: token.h:87
virtual HioImageSharedPtr New() const
Definition: image.h:210
bool any(const vbool4 &v)
Definition: simd.h:3468
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:42
GLenum GLfloat param
Definition: glcorearb.h:104
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
std::shared_ptr< class HioImage > HioImageSharedPtr
Definition: image.h:44
virtual bool Write(StorageSpec const &storage, VtDictionary const &metadata=VtDictionary())=0
Writes the image with metadata.
bool IsHolding() const
Definition: value.h:1054
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SourceColorSpace
Definition: image.h:68
GLint GLsizei width
Definition: glcorearb.h:103
Definition: core.h:1131
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:167
virtual HIO_API ~HioImage()
Definition: format.h:895
Definition: image.h:52