MaterialX 1.39.0
Loading...
Searching...
No Matches
ImageHandler.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_IMAGEHANDLER_H
7#define MATERIALX_IMAGEHANDLER_H
8
11
14
16
18
19MATERIALX_NAMESPACE_BEGIN
20
21extern MX_RENDER_API const string IMAGE_PROPERTY_SEPARATOR;
22extern MX_RENDER_API const string UADDRESS_MODE_SUFFIX;
23extern MX_RENDER_API const string VADDRESS_MODE_SUFFIX;
24extern MX_RENDER_API const string FILTER_TYPE_SUFFIX;
25extern MX_RENDER_API const string DEFAULT_COLOR_SUFFIX;
26
27class ImageHandler;
28class ImageLoader;
29class VariableBlock;
30
32using ImageHandlerPtr = std::shared_ptr<ImageHandler>;
33
35using ImageLoaderPtr = std::shared_ptr<ImageLoader>;
36
38using ImageLoaderMap = std::unordered_map<string, std::vector<ImageLoaderPtr>>;
39
42class MX_RENDER_API ImageSamplingProperties
43{
44 public:
49 void setProperties(const string& fileNameUniform,
50 const VariableBlock& uniformBlock);
51
52 bool operator==(const ImageSamplingProperties& r) const;
53
56 enum class AddressMode : int
57 {
58 UNSPECIFIED = -1,
59 CONSTANT = 0,
60 CLAMP = 1,
61 PERIODIC = 2,
62 MIRROR = 3
63 };
64
66 AddressMode uaddressMode = AddressMode::UNSPECIFIED;
68 AddressMode vaddressMode = AddressMode::UNSPECIFIED;
69
72 enum class FilterType : int
73 {
74 UNSPECIFIED = -1,
75 CLOSEST = 0,
76 LINEAR = 1,
77 CUBIC = 2
78 };
79
81 FilterType filterType = FilterType::UNSPECIFIED;
82
84 bool enableMipmaps = true;
85
88 Color4 defaultColor = { 0.0f, 0.0f, 0.0f, 1.0f };
89};
90
93struct MX_RENDER_API ImageSamplingKeyHasher
94{
95 size_t operator()(const ImageSamplingProperties& k) const
96 {
97 return (size_t) k.enableMipmaps + // 1 bit
98 (((size_t) k.filterType & 3) << 1) + // 2 bit
99 ((((size_t) k.uaddressMode + 1) & 7) << 3) + // 3 bit
100 ((((size_t) k.vaddressMode + 1) & 7) << 6) + // 3 bit
101 ((((size_t) k.defaultColor[0] + 1)) << 9) ;
102 }
103};
104
107class MX_RENDER_API ImageLoader
108{
109 public:
111 {
112 }
113 virtual ~ImageLoader() { }
114
116 static const string BMP_EXTENSION;
117 static const string EXR_EXTENSION;
118 static const string GIF_EXTENSION;
119 static const string HDR_EXTENSION;
120 static const string JPG_EXTENSION;
121 static const string JPEG_EXTENSION;
122 static const string PIC_EXTENSION;
123 static const string PNG_EXTENSION;
124 static const string PSD_EXTENSION;
125 static const string TGA_EXTENSION;
126 static const string TIF_EXTENSION;
127 static const string TIFF_EXTENSION;
128 static const string TXT_EXTENSION;
129 static const string TX_EXTENSION;
130 static const string TXR_EXTENSION;
131
135 {
136 return _extensions;
137 }
138
144 virtual bool saveImage(const FilePath& filePath,
145 ConstImagePtr image,
146 bool verticalFlip = false);
147
151 virtual ImagePtr loadImage(const FilePath& filePath);
152
153 protected:
154 // List of supported string extensions
155 StringSet _extensions;
156};
157
163class MX_RENDER_API ImageHandler
164{
165 public:
166 static ImageHandlerPtr create(ImageLoaderPtr imageLoader)
167 {
168 return ImageHandlerPtr(new ImageHandler(imageLoader));
169 }
170 virtual ~ImageHandler() { }
171
175
178
185 bool saveImage(const FilePath& filePath, ConstImagePtr image, bool verticalFlip = false);
186
194 ImagePtr acquireImage(const FilePath& filePath, const Color4& defaultColor = Color4(0.0f));
195
199 virtual bool bindImage(ImagePtr image, const ImageSamplingProperties& samplingProperties);
200
203 virtual bool unbindImage(ImagePtr image);
204
207
210 {
211 _searchPath = path;
212 }
213
216 {
217 return _searchPath;
218 }
219
222 {
223 _resolver = resolver;
224 }
225
228 {
229 return _resolver;
230 }
231
233 virtual bool createRenderResources(ImagePtr image, bool generateMipMaps, bool useAsRenderTarget = false);
234
237 virtual void releaseRenderResources(ImagePtr image = nullptr);
238
242 {
243 releaseRenderResources();
244 _imageCache.clear();
245 }
246
249 {
250 return _zeroImage;
251 }
252
256
257 protected:
258 // Protected constructor.
259 ImageHandler(ImageLoaderPtr imageLoader);
260
261 // Load an image from the file system.
262 ImagePtr loadImage(const FilePath& filePath);
263
264 // Add an image to the cache.
265 void cacheImage(const string& filePath, ImagePtr image);
266
267 // Return the cached image, if found; otherwise return an empty
268 // shared pointer.
269 ImagePtr getCachedImage(const FilePath& filePath);
270
271 protected:
272 ImageLoaderMap _imageLoaders;
273 ImageMap _imageCache;
274 FileSearchPath _searchPath;
275 StringResolverPtr _resolver;
276 ImagePtr _zeroImage;
277};
278
279MATERIALX_NAMESPACE_END
280
281#endif
The top-level Document class.
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:24
shared_ptr< StringResolver > StringResolverPtr
A shared pointer to a StringResolver.
Definition: Element.h:66
Cross-platform support for file and search paths.
Image class.
shared_ptr< const Image > ConstImagePtr
A shared pointer to a const image.
Definition: Image.h:26
std::vector< ImagePtr > ImageVec
A vetor of images.
Definition: Image.h:32
std::unordered_map< string, ImagePtr > ImageMap
A map from strings to images.
Definition: Image.h:29
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
std::shared_ptr< ImageLoader > ImageLoaderPtr
Shared pointer to an ImageLoader.
Definition: ImageHandler.h:35
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
std::unordered_map< string, std::vector< ImageLoaderPtr > > ImageLoaderMap
Map from strings to vectors of image loaders.
Definition: ImageHandler.h:38
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
Macros for declaring imported and exported symbols.
A four-component color value.
Definition: Types.h:364
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:219
Base image handler class.
Definition: ImageHandler.h:164
ImagePtr acquireImage(const FilePath &filePath, const Color4 &defaultColor=Color4(0.0f))
Acquire an image from the cache or file system.
virtual bool createRenderResources(ImagePtr image, bool generateMipMaps, bool useAsRenderTarget=false)
Create rendering resources for the given image.
virtual bool unbindImage(ImagePtr image)
Unbind an image, making it no longer active for rendering.
void clearImageCache()
Clear the contents of the image cache, first releasing any render resources associated with cached im...
Definition: ImageHandler.h:241
ImageVec getReferencedImages(ConstDocumentPtr doc)
Acquire all images referenced by the given document, and return the images in a vector.
const FileSearchPath & getSearchPath() const
Return the image search path.
Definition: ImageHandler.h:215
StringResolverPtr getFilenameResolver() const
Return the filename resolver for images.
Definition: ImageHandler.h:227
virtual bool bindImage(ImagePtr image, const ImageSamplingProperties &samplingProperties)
Bind an image for rendering.
void setSearchPath(const FileSearchPath &path)
Set the search path to be used for finding images on the file system.
Definition: ImageHandler.h:209
void addLoader(ImageLoaderPtr loader)
Add another image loader to the handler, which will be invoked if existing loaders cannot load a give...
void unbindImages()
Unbind all images that are currently stored in the cache.
virtual void releaseRenderResources(ImagePtr image=nullptr)
Release rendering resources for the given image, or for all cached images if no image pointer is spec...
ImagePtr getZeroImage() const
Return a fallback image with zeroes in all channels.
Definition: ImageHandler.h:248
StringSet supportedExtensions()
Get a list of extensions supported by the handler.
void setFilenameResolver(StringResolverPtr resolver)
Set the filename resolver for images.
Definition: ImageHandler.h:221
bool saveImage(const FilePath &filePath, ConstImagePtr image, bool verticalFlip=false)
Save image to disk.
Abstract base class for file-system image loaders.
Definition: ImageHandler.h:108
const StringSet & supportedExtensions() const
Returns a list of supported extensions.
Definition: ImageHandler.h:134
virtual bool saveImage(const FilePath &filePath, ConstImagePtr image, bool verticalFlip=false)
Save an image to the file system.
virtual ImagePtr loadImage(const FilePath &filePath)
Load an image from the file system.
static const string BMP_EXTENSION
Standard image file extensions.
Definition: ImageHandler.h:116
Interface to describe sampling properties for images.
Definition: ImageHandler.h:43
FilterType
Filter type options.
Definition: ImageHandler.h:73
AddressMode
Address mode options.
Definition: ImageHandler.h:57
FilterType filterType
Filter type.
Definition: ImageHandler.h:81
void setProperties(const string &fileNameUniform, const VariableBlock &uniformBlock)
Set the properties based on data in a uniform block.
Color4 defaultColor
Default color.
Definition: ImageHandler.h:88
AddressMode uaddressMode
Address mode in U.
Definition: ImageHandler.h:66
bool enableMipmaps
Enable mipmaps.
Definition: ImageHandler.h:84
AddressMode vaddressMode
Address mode in V.
Definition: ImageHandler.h:68
A block of variables in a shader stage.
Definition: ShaderStage.h:61
Class used for hashing ImageSamplingProperties in an unordered_map.
Definition: ImageHandler.h:94