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 
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 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
44 using HioImageSharedPtr = std::shared_ptr<class HioImage>;
45 
52 class HioImage
53 {
54 public:
55 
59  {
60  OriginUpperLeft,
61  OriginLowerLeft
62  };
63 
69  {
70  Raw,
71  SRGB,
72  Auto
73  };
74 
80  {
81  public:
82  StorageSpec()
83  : width(0), height(0), depth(0)
84  , format(HioFormatInvalid)
85  , flipped(false)
86  , data(0) { }
87 
88  int width, height, depth;
89  HioFormat format;
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 
105  HIO_API
106  static bool IsSupportedImageFile(std::string const & filename);
107 
110 
114  HIO_API
115  static HioImageSharedPtr OpenForReading(std::string const & filename,
116  int subimage = 0,
117  int mip = 0,
118  SourceColorSpace sourceColorSpace =
119  SourceColorSpace::Auto,
120  bool suppressErrors = false);
121 
123  virtual bool Read(StorageSpec const & storage) = 0;
124 
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 
133 
136 
138  HIO_API
139  static HioImageSharedPtr OpenForWriting(std::string const & filename);
140 
142  virtual bool Write(StorageSpec const & storage,
143  VtDictionary const & metadata = VtDictionary()) = 0;
144 
146 
148  virtual std::string const & GetFilename() const = 0;
149 
151  virtual int GetWidth() const = 0;
152 
154  virtual int GetHeight() const = 0;
155 
157  virtual HioFormat GetFormat() const = 0;
158 
160  virtual int GetBytesPerPixel() const = 0;
161 
163  virtual int GetNumMipLevels() const = 0;
164 
166  virtual bool IsColorSpaceSRGB() const = 0;
167 
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 
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 
202 class HIO_API HioImageFactoryBase : public TfType::FactoryBase {
203 public:
204  virtual HioImageSharedPtr New() const = 0;
205 };
206 
207 template <class T>
208 class HioImageFactory : public HioImageFactoryBase {
209 public:
210  virtual HioImageSharedPtr New() const
211  {
212  return HioImageSharedPtr(new T);
213  }
214 };
215 
216 
217 PXR_NAMESPACE_CLOSE_SCOPE
218 
219 #endif // PXR_IMAGING_HIO_IMAGE_H
static HIO_API bool IsSupportedImageFile(std::string const &filename)
Returns whether filename opened as a texture image.
virtual HioFormat GetFormat() const =0
Returns the destination HioFormat.
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1090
static HIO_API HioImageSharedPtr OpenForWriting(std::string const &filename)
Opens filename for writing from the given storage.
A map with string keys and VtValue values.
Definition: dictionary.h:63
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1062
virtual int GetHeight() const =0
Returns the image height.
virtual bool _OpenForReading(std::string const &filename, int subimage, int mip, SourceColorSpace sourceColorSpace, bool suppressErrors)=0
}@
virtual std::string const & GetFilename() const =0
}@
Base class of all factory types.
Definition: type.h:73
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left...
Definition: image.h:58
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.
SourceColorSpace
Specifies the source color space in which the texture is encoded, with &quot;Auto&quot; indicating the texture ...
Definition: image.h:68
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Describes the memory layout and storage of a texture image.
Definition: image.h:79
static HIO_API HioImageSharedPtr OpenForReading(std::string const &filename, int subimage=0, int mip=0, SourceColorSpace sourceColorSpace=SourceColorSpace::Auto, bool suppressErrors=false)
Opens filename for reading from the given subimage at mip level mip, using sourceColorSpace to help d...
virtual int GetNumMipLevels() const =0
Returns the number of mips available.
virtual bool IsColorSpaceSRGB() const =0
Returns whether the image is in the sRGB color space.
virtual bool Read(StorageSpec const &storage)=0
Reads the image file into storage.
virtual int GetBytesPerPixel() const =0
Returns the number of bytes per pixel.
virtual int GetWidth() const =0
Returns the image width.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:168
A base class for reading and writing texture image data.
Definition: image.h:52
virtual bool Write(StorageSpec const &storage, VtDictionary const &metadata=VtDictionary())=0
Writes the image with metadata.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...