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 /// Texture paths are UTF-8 strings, resolvable by AR. Texture system dispatch
53 /// is driven by extension, with [A-Z] (and no other characters) case folded.
54 class HioImage
55 {
56 public:
57 
58  /// Specifies whether to treat the image origin as the upper-left corner
59  /// or the lower left
61  {
64  };
65 
66  /// Specifies the source color space in which the texture is encoded, with
67  /// "Auto" indicating the texture reader should determine color space based
68  /// on hints from the image (e.g. file type, number of channels, image
69  /// metadata)
71  {
72  Raw,
75  };
76 
77  /// \class StorageSpec
78  ///
79  /// Describes the memory layout and storage of a texture image
80  ///
82  {
83  public:
85  : width(0), height(0), depth(0)
87  , flipped(false)
88  , data(0) { }
89 
92  bool flipped;
93  void * data;
94  };
95 
96 public:
97  HioImage() = default;
98 
99  HIO_API
100  virtual ~HioImage();
101 
102  // Disallow copies
103  HioImage(const HioImage&) = delete;
104  HioImage& operator=(const HioImage&) = delete;
105 
106  /// Returns whether \a filename opened as a texture image.
107  HIO_API
108  static bool IsSupportedImageFile(std::string const & filename);
109 
110  /// \name Reading
111  /// {@
112 
113  /// Opens \a filename for reading from the given \a subimage at mip level
114  /// \a mip, using \a sourceColorSpace to help determine the color space
115  /// with which to interpret the texture
116  HIO_API
118  int subimage = 0,
119  int mip = 0,
120  SourceColorSpace sourceColorSpace =
121  SourceColorSpace::Auto,
122  bool suppressErrors = false);
123 
124  /// Reads the image file into \a storage.
125  virtual bool Read(StorageSpec const & storage) = 0;
126 
127  /// Reads the cropped sub-image into \a storage.
128  virtual bool ReadCropped(int const cropTop,
129  int const cropBottom,
130  int const cropLeft,
131  int const cropRight,
132  StorageSpec const & storage) = 0;
133 
134  /// }@
135 
136  /// \name Writing
137  /// {@
138 
139  /// Opens \a filename for writing from the given \a storage.
140  HIO_API
142 
143  /// Writes the image with \a metadata.
144  virtual bool Write(StorageSpec const & storage,
145  VtDictionary const & metadata = VtDictionary()) = 0;
146 
147  /// }@
148 
149  /// Returns the image filename.
150  virtual std::string const & GetFilename() const = 0;
151 
152  /// Returns the image width.
153  virtual int GetWidth() const = 0;
154 
155  /// Returns the image height.
156  virtual int GetHeight() const = 0;
157 
158  /// Returns the destination HioFormat.
159  virtual HioFormat GetFormat() const = 0;
160 
161  /// Returns the number of bytes per pixel.
162  virtual int GetBytesPerPixel() const = 0;
163 
164  /// Returns the number of mips available.
165  virtual int GetNumMipLevels() const = 0;
166 
167  /// Returns whether the image is in the sRGB color space.
168  virtual bool IsColorSpaceSRGB() const = 0;
169 
170  /// \name Metadata
171  /// {@
172  template <typename T>
173  bool GetMetadata(TfToken const & key, T * value) const;
174 
175  virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
176 
177  virtual bool GetSamplerMetadata(HioAddressDimension dim,
178  HioAddressMode * param) const = 0;
179 
180  /// }@
181 
182 protected:
183  virtual bool _OpenForReading(std::string const & filename,
184  int subimage,
185  int mip,
186  SourceColorSpace sourceColorSpace,
187  bool suppressErrors) = 0;
188 
189  virtual bool _OpenForWriting(std::string const & filename) = 0;
190 };
191 
192 template <typename T>
193 bool
194 HioImage::GetMetadata(TfToken const & key, T * value) const
195 {
196  VtValue any;
197  if (!GetMetadata(key, &any) || !any.IsHolding<T>()) {
198  return false;
199  }
200  *value = any.UncheckedGet<T>();
201  return true;
202 }
203 
205 public:
206  virtual HioImageSharedPtr New() const = 0;
207 };
208 
209 template <class T>
211 public:
212  virtual HioImageSharedPtr New() const
213  {
214  return HioImageSharedPtr(new T);
215  }
216 };
217 
218 
220 
221 #endif // PXR_IMAGING_HIO_IMAGE_H
ImageOriginLocation
Definition: image.h:60
bool GetMetadata(TfToken const &key, T *value) const
}@
Definition: image.h:194
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:1121
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:91
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:212
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:1432
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:1081
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SourceColorSpace
Definition: image.h:70
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:164
virtual HIO_API ~HioImage()
Definition: format.h:895
Definition: image.h:54