HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageHandler.h
Go to the documentation of this file.
1 //
2 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
4 //
5 
6 #ifndef MATERIALX_IMAGEHANDLER_H
7 #define MATERIALX_IMAGEHANDLER_H
8 
9 /// @file
10 /// Image handler interfaces
11 
12 #include <MaterialXRender/Export.h>
13 #include <MaterialXRender/Image.h>
14 
15 #include <MaterialXFormat/File.h>
16 
17 #include <MaterialXCore/Document.h>
18 
20 
21 extern MX_RENDER_API const string IMAGE_PROPERTY_SEPARATOR;
22 extern MX_RENDER_API const string UADDRESS_MODE_SUFFIX;
23 extern MX_RENDER_API const string VADDRESS_MODE_SUFFIX;
24 extern MX_RENDER_API const string FILTER_TYPE_SUFFIX;
25 extern MX_RENDER_API const string DEFAULT_COLOR_SUFFIX;
26 
27 class ImageHandler;
28 class ImageLoader;
29 class VariableBlock;
30 
31 /// Shared pointer to an ImageHandler
32 using ImageHandlerPtr = std::shared_ptr<ImageHandler>;
33 
34 /// Shared pointer to an ImageLoader
35 using ImageLoaderPtr = std::shared_ptr<ImageLoader>;
36 
37 /// Map from strings to vectors of image loaders
38 using ImageLoaderMap = std::unordered_map< string, std::vector<ImageLoaderPtr> >;
39 
40 /// @class ImageSamplingProperties
41 /// Interface to describe sampling properties for images.
43 {
44  public:
45  /// Set the properties based on data in a uniform block.
46  /// @param fileNameUniform Name of the file name uniform. Used to find
47  /// corresponding sampler data in the uniform block
48  /// @param uniformBlock Block containing sampler uniforms
49  void setProperties(const string& fileNameUniform,
50  const VariableBlock& uniformBlock);
51 
52  /// Address mode options. Matches enumerations allowed for image address
53  /// modes, except UNSPECIFIED which indicates no explicit mode was defined.
54  enum class AddressMode : int
55  {
56  UNSPECIFIED = -1,
57  CONSTANT = 0,
58  CLAMP = 1,
59  PERIODIC = 2,
60  MIRROR = 3
61  };
62 
63  /// Address mode in U
64  AddressMode uaddressMode = AddressMode::UNSPECIFIED;
65  /// Address mode in V
66  AddressMode vaddressMode = AddressMode::UNSPECIFIED;
67 
68  /// Filter type options. Matches enumerations allowed for image filter
69  /// types, except UNSPECIFIED which indicates no explicit type was defined.
70  enum class FilterType : int
71  {
72  UNSPECIFIED = -1,
73  CLOSEST = 0,
74  LINEAR = 1,
75  CUBIC = 2
76  };
77 
78  /// Filter type
79  FilterType filterType = FilterType::UNSPECIFIED;
80 
81  /// Enable mipmaps
82  bool enableMipmaps = true;
83 
84  /// Default color. Corresponds to the "default" value on the image
85  /// node definition.
86  Color4 defaultColor = { 0.0f, 0.0f, 0.0f, 1.0f };
87 };
88 
89 /// @class ImageLoader
90 /// Abstract base class for file-system image loaders
92 {
93  public:
95  {
96  }
97  virtual ~ImageLoader() { }
98 
99  /// Standard image file extensions
100  static const string BMP_EXTENSION;
101  static const string EXR_EXTENSION;
102  static const string GIF_EXTENSION;
103  static const string HDR_EXTENSION;
104  static const string JPG_EXTENSION;
105  static const string JPEG_EXTENSION;
106  static const string PIC_EXTENSION;
107  static const string PNG_EXTENSION;
108  static const string PSD_EXTENSION;
109  static const string TGA_EXTENSION;
110  static const string TIF_EXTENSION;
111  static const string TIFF_EXTENSION;
112  static const string TXT_EXTENSION;
113  static const string TX_EXTENSION;
114  static const string TXR_EXTENSION;
115 
116  /// Returns a list of supported extensions
117  /// @return List of support extensions
119  {
120  return _extensions;
121  }
122 
123  /// Save an image to the file system. This method must be implemented by derived classes.
124  /// @param filePath File path to be written
125  /// @param image The image to be saved
126  /// @param verticalFlip Whether the image should be flipped in Y during save
127  /// @return if save succeeded
128  virtual bool saveImage(const FilePath& filePath,
130  bool verticalFlip = false);
131 
132  /// Load an image from the file system. This method must be implemented by derived classes.
133  /// @param filePath The requested image file path.
134  /// @return On success, a shared pointer to the loaded image; otherwise an empty shared pointer.
135  virtual ImagePtr loadImage(const FilePath& filePath);
136 
137  protected:
138  // List of supported string extensions
140 };
141 
142 /// @class ImageHandler
143 /// Base image handler class. Keeps track of images which are loaded from
144 /// disk via supplied ImageLoader. Derived classes are responsible for
145 /// determinining how to perform the logic for "binding" of these resources
146 /// for a given target (such as a given shading language).
148 {
149  public:
151  {
152  return ImageHandlerPtr(new ImageHandler(imageLoader));
153  }
154  virtual ~ImageHandler() { }
155 
156  /// Add another image loader to the handler, which will be invoked if
157  /// existing loaders cannot load a given image.
158  void addLoader(ImageLoaderPtr loader);
159 
160  /// Get a list of extensions supported by the handler.
161  StringSet supportedExtensions();
162 
163  /// Save image to disk. This method must be implemented by derived classes.
164  /// The first image loader which supports the file name extension will be used.
165  /// @param filePath File path to be written
166  /// @param image The image to be saved
167  /// @param verticalFlip Whether the image should be flipped in Y during save
168  /// @return if save succeeded
169  bool saveImage(const FilePath& filePath, ConstImagePtr image, bool verticalFlip = false);
170 
171  /// Acquire an image from the cache or file system. If the image is not
172  /// found in the cache, then each image loader will be applied in turn.
173  /// @param filePath File path of the image.
174  /// @return On success, a shared pointer to the acquired image.
175  ImagePtr acquireImage(const FilePath& filePath);
176 
177  /// Bind an image for rendering.
178  /// @param image The image to bind.
179  /// @param samplingProperties Sampling properties for the image.
180  virtual bool bindImage(ImagePtr image, const ImageSamplingProperties& samplingProperties);
181 
182  /// Unbind an image, making it no longer active for rendering.
183  /// @param image The image to unbind.
184  virtual bool unbindImage(ImagePtr image);
185 
186  /// Unbind all images that are currently stored in the cache.
187  void unbindImages();
188 
189  /// Set the search path to be used for finding images on the file system.
191  {
192  _searchPath = path;
193  }
194 
195  /// Return the image search path.
197  {
198  return _searchPath;
199  }
200 
201  /// Set the filename resolver for images.
203  {
204  _resolver = resolver;
205  }
206 
207  /// Return the filename resolver for images.
209  {
210  return _resolver;
211  }
212 
213  /// Create rendering resources for the given image.
214  virtual bool createRenderResources(ImagePtr image, bool generateMipMaps);
215 
216  /// Release rendering resources for the given image, or for all cached images
217  /// if no image pointer is specified.
218  virtual void releaseRenderResources(ImagePtr image = nullptr);
219 
220  /// Clear the contents of the image cache, first releasing any render
221  /// resources associated with cached images.
223  {
224  releaseRenderResources();
225  _imageCache.clear();
226  }
227 
228  /// Return a fallback image with zeroes in all channels.
230  {
231  return _zeroImage;
232  }
233 
234  /// Return the sentinel invalid image, representing images that cannot be loaded
235  /// and should be replaced with their declared default value.
237  {
238  return _invalidImage;
239  }
240 
241  /// Acquire all images referenced by the given document, and return the
242  /// images in a vector.
243  ImageVec getReferencedImages(DocumentPtr doc);
244 
245  protected:
246  // Protected constructor.
247  ImageHandler(ImageLoaderPtr imageLoader);
248 
249  // Load an image from the file system.
250  ImagePtr loadImage(const FilePath& filePath);
251 
252  // Add an image to the cache.
253  void cacheImage(const string& filePath, ImagePtr image);
254 
255  // Return the cached image, if found; otherwise return an empty
256  // shared pointer.
257  ImagePtr getCachedImage(const FilePath& filePath);
258 
259  protected:
266 };
267 
269 
270 #endif
static const string TXT_EXTENSION
Definition: ImageHandler.h:112
ImagePtr getZeroImage() const
Return a fallback image with zeroes in all channels.
Definition: ImageHandler.h:229
static const string GIF_EXTENSION
Definition: ImageHandler.h:102
static const string EXR_EXTENSION
Definition: ImageHandler.h:101
Definition: File.h:26
std::unordered_map< string, ImagePtr > ImageMap
A map from strings to images.
Definition: Image.h:29
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
StringResolverPtr _resolver
Definition: ImageHandler.h:263
StringResolverPtr getFilenameResolver() const
Return the filename resolver for images.
Definition: ImageHandler.h:208
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
std::vector< ImagePtr > ImageVec
A vetor of images.
Definition: Image.h:32
ImagePtr _invalidImage
Definition: ImageHandler.h:265
GLenum GLsizei GLenum GLenum const void * image
Definition: glew.h:4973
static ImageHandlerPtr create(ImageLoaderPtr imageLoader)
Definition: ImageHandler.h:150
Selectively extract and filter point data using a custom filter operator.
const FileSearchPath & getSearchPath() const
Return the image search path.
Definition: ImageHandler.h:196
static const string TIFF_EXTENSION
Definition: ImageHandler.h:111
MX_RENDER_API const string FILTER_TYPE_SUFFIX
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER IMFUTIL_EXPORT void saveImage(const std::string &fileName, const Header &hdr, const Image &img, DataWindowSource dws=USE_IMAGE_DATA_WINDOW)
virtual ~ImageHandler()
Definition: ImageHandler.h:154
const StringSet & supportedExtensions() const
Definition: ImageHandler.h:118
static const string PIC_EXTENSION
Definition: ImageHandler.h:106
static const string PNG_EXTENSION
Definition: ImageHandler.h:107
static const string TGA_EXTENSION
Definition: ImageHandler.h:109
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
MATERIALX_NAMESPACE_BEGIN MX_RENDER_API const string IMAGE_PROPERTY_SEPARATOR
static const string JPEG_EXTENSION
Definition: ImageHandler.h:105
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
std::shared_ptr< ImageLoader > ImageLoaderPtr
Shared pointer to an ImageLoader.
Definition: ImageHandler.h:35
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void setSearchPath(const FileSearchPath &path)
Set the search path to be used for finding images on the file system.
Definition: ImageHandler.h:190
MX_RENDER_API const string VADDRESS_MODE_SUFFIX
static const string TX_EXTENSION
Definition: ImageHandler.h:113
ImagePtr _zeroImage
Definition: ImageHandler.h:264
shared_ptr< StringResolver > StringResolverPtr
A shared pointer to a StringResolver.
Definition: Element.h:60
MX_RENDER_API const string UADDRESS_MODE_SUFFIX
#define MX_RENDER_API
Definition: Export.h:18
ImageMap _imageCache
Definition: ImageHandler.h:261
ImagePtr getInvalidImage() const
Definition: ImageHandler.h:236
static const string TIF_EXTENSION
Definition: ImageHandler.h:110
static const string TXR_EXTENSION
Definition: ImageHandler.h:114
FileSearchPath _searchPath
Definition: ImageHandler.h:262
ImageLoaderMap _imageLoaders
Definition: ImageHandler.h:260
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
void clearImageCache()
Definition: ImageHandler.h:222
static const string JPG_EXTENSION
Definition: ImageHandler.h:104
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
static const string HDR_EXTENSION
Definition: ImageHandler.h:103
static const string PSD_EXTENSION
Definition: ImageHandler.h:108
StringSet _extensions
Definition: ImageHandler.h:139
void setFilenameResolver(StringResolverPtr resolver)
Set the filename resolver for images.
Definition: ImageHandler.h:202
static const string BMP_EXTENSION
Standard image file extensions.
Definition: ImageHandler.h:100
shared_ptr< const Image > ConstImagePtr
A shared pointer to a const image.
Definition: Image.h:26
MX_RENDER_API const string DEFAULT_COLOR_SUFFIX
std::unordered_map< string, std::vector< ImageLoaderPtr > > ImageLoaderMap
Map from strings to vectors of image loaders.
Definition: ImageHandler.h:38
virtual ~ImageLoader()
Definition: ImageHandler.h:97
IMFUTIL_EXPORT Image * loadImage(const std::string &fileName, Header &hdr)