MaterialX 1.39.2
Loading...
Searching...
No Matches
Image.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_IMAGE_H
7#define MATERIALX_IMAGE_H
8
11
13
15
16#include <MaterialXCore/Types.h>
17
18MATERIALX_NAMESPACE_BEGIN
19
20class Image;
21
23using ImagePtr = shared_ptr<Image>;
24
26using ConstImagePtr = shared_ptr<const Image>;
27
29using ImageMap = std::unordered_map<string, ImagePtr>;
30
32using ImageVec = std::vector<ImagePtr>;
33
35using ImagePair = std::pair<ImagePtr, ImagePtr>;
36
38using ImageBufferDeallocator = std::function<void(void*)>;
39
41using UnsignedIntPair = std::pair<unsigned int, unsigned int>;
42
45class MX_RENDER_API Image
46{
47 public:
48 enum class BaseType
49 {
50 UINT8,
51 INT8,
52 UINT16,
53 INT16,
54 HALF,
55 FLOAT
56 };
57
58 public:
60 static ImagePtr create(unsigned int width, unsigned int height, unsigned int channelCount, BaseType baseType = BaseType::UINT8)
61 {
62 return ImagePtr(new Image(width, height, channelCount, baseType));
63 }
64
65 ~Image();
66
69
71 unsigned int getWidth() const
72 {
73 return _width;
74 }
75
77 unsigned int getHeight() const
78 {
79 return _height;
80 }
81
83 unsigned int getChannelCount() const
84 {
85 return _channelCount;
86 }
87
89 BaseType getBaseType() const
90 {
91 return _baseType;
92 }
93
95 unsigned int getBaseStride() const;
96
98 unsigned int getRowStride() const
99 {
100 return _width * _channelCount * getBaseStride();
101 }
102
104 unsigned int getMaxMipCount() const;
105
109
112 void setTexelColor(unsigned int x, unsigned int y, const Color4& color);
113
116 Color4 getTexelColor(unsigned int x, unsigned int y) const;
117
121
124
127 bool isUniformColor(Color4* uniformColor = nullptr);
128
132
134 void setUniformColor(const Color4& color);
135
138
140 void applyGammaTransform(float gamma);
141
143 ImagePtr copy(unsigned int channelCount, BaseType baseType) const;
144
147
150
153 ImagePtr applyBoxDownsample(unsigned int factor);
154
157 ImagePair splitByLuminance(float luminance);
158
161 void writeTable(const FilePath& filePath, unsigned int channel);
162
166
168 void setResourceBuffer(void* buffer)
169 {
170 _resourceBuffer = buffer;
171 }
172
174 void* getResourceBuffer() const
175 {
176 return _resourceBuffer;
177 }
178
181
184
187 {
188 _resourceBufferDeallocator = deallocator;
189 }
190
193 {
194 return _resourceBufferDeallocator;
195 }
196
200
202 void setResourceId(unsigned int id)
203 {
204 _resourceId = id;
205 }
206
208 unsigned int getResourceId() const
209 {
210 return _resourceId;
211 }
212
214
215 protected:
216 Image(unsigned int width, unsigned int height, unsigned int channelCount, BaseType baseType);
217
218 protected:
219 unsigned int _width;
220 unsigned int _height;
221 unsigned int _channelCount;
222 BaseType _baseType;
223
224 void* _resourceBuffer;
225 ImageBufferDeallocator _resourceBufferDeallocator;
226 unsigned int _resourceId = 0;
227};
228
230MX_RENDER_API ImagePtr createUniformImage(unsigned int width, unsigned int height, unsigned int channelCount, Image::BaseType baseType, const Color4& color);
231
233MX_RENDER_API ImagePtr createImageStrip(const vector<ImagePtr>& imageVec);
234
236MX_RENDER_API UnsignedIntPair getMaxDimensions(const vector<ImagePtr>& imageVec);
237
238MATERIALX_NAMESPACE_END
239
240#endif
Cross-platform support for file and search paths.
shared_ptr< const Image > ConstImagePtr
A shared pointer to a const image.
Definition Image.h:26
MX_RENDER_API ImagePtr createUniformImage(unsigned int width, unsigned int height, unsigned int channelCount, Image::BaseType baseType, const Color4 &color)
Create a uniform-color image with the given properties.
MX_RENDER_API UnsignedIntPair getMaxDimensions(const vector< ImagePtr > &imageVec)
Compute the maximum width and height of all images in the given vector.
std::pair< unsigned int, unsigned int > UnsignedIntPair
A pair of unsigned integers.
Definition Image.h:41
std::function< void(void *)> ImageBufferDeallocator
A function to perform image buffer deallocation.
Definition Image.h:38
std::vector< ImagePtr > ImageVec
A vector of images.
Definition Image.h:32
MX_RENDER_API ImagePtr createImageStrip(const vector< ImagePtr > &imageVec)
Create a horizontal image strip from a vector of images with identical resolutions and formats.
std::unordered_map< string, ImagePtr > ImageMap
A map from strings to images.
Definition Image.h:29
std::pair< ImagePtr, ImagePtr > ImagePair
A pair of images.
Definition Image.h:35
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition Image.h:23
Data type classes.
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
Class representing an image in system memory.
Definition Image.h:46
void setTexelColor(unsigned int x, unsigned int y, const Color4 &color)
Set the texel color at the given coordinates.
void setResourceBufferDeallocator(ImageBufferDeallocator deallocator)
Set the resource buffer deallocator for this image.
Definition Image.h:186
BaseType getBaseType() const
Return the base type of the image.
Definition Image.h:89
unsigned int getMaxMipCount() const
Return the maximum number of mipmaps for this image.
unsigned int getHeight() const
Return the height of the image.
Definition Image.h:77
Color4 getAverageColor()
Compute the average color of the image.
void releaseResourceBuffer()
Release the resource buffer for this image.
ImagePtr applyBoxDownsample(unsigned int factor)
Downsample this image by an integer factor using a box filter, returning the new reduced image.
void applyGammaTransform(float gamma)
Apply the given gamma transform to all texels of this image.
void setUniformColor(const Color4 &color)
Set all texels of this image to a uniform color.
ImagePtr applyBoxBlur()
Apply a 3x3 box blur to this image, returning a new blurred image.
unsigned int getChannelCount() const
Return the channel count of the image.
Definition Image.h:83
ImagePtr applyGaussianBlur()
Apply a 7x7 Gaussian blur to this image, returning a new blurred image.
ImagePair splitByLuminance(float luminance)
Split this image by the given luminance threshold, returning the resulting underflow and overflow ima...
bool isUniformColor(Color4 *uniformColor=nullptr)
Return true if all texels of this image are identical in color.
unsigned int getResourceId() const
Return the resource ID for this image.
Definition Image.h:208
void setResourceBuffer(void *buffer)
Set the resource buffer for this image.
Definition Image.h:168
ImagePtr copy(unsigned int channelCount, BaseType baseType) const
Create a copy of this image with the given channel count and base type.
void createResourceBuffer()
Allocate a resource buffer for this image that matches its properties.
void writeTable(const FilePath &filePath, unsigned int channel)
Save a channel of this image to disk as a text table, in a format that can be used for curve and surf...
void setResourceId(unsigned int id)
Set the resource ID for this image.
Definition Image.h:202
unsigned int getWidth() const
Return the width of the image.
Definition Image.h:71
static ImagePtr create(unsigned int width, unsigned int height, unsigned int channelCount, BaseType baseType=BaseType::UINT8)
Create an empty image with the given properties.
Definition Image.h:60
void * getResourceBuffer() const
Return the resource buffer for this image.
Definition Image.h:174
unsigned int getBaseStride() const
Return the stride of our base type in bytes.
unsigned int getRowStride() const
Return the stride of an image row in bytes.
Definition Image.h:98
ImageBufferDeallocator getResourceBufferDeallocator() const
Return the resource buffer deallocator for this image.
Definition Image.h:192
Color4 getTexelColor(unsigned int x, unsigned int y) const
Return the texel color at the given coordinates.
void applyMatrixTransform(const Matrix33 &mat)
Apply the given matrix transform to all texels of this image.
A 3x3 matrix of floating-point values.
Definition Types.h:586