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 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 
9 /// @file
10 /// Image class
11 
12 #include <MaterialXRender/Export.h>
13 
14 #include <MaterialXFormat/File.h>
15 
16 #include <MaterialXCore/Types.h>
17 
19 
20 class Image;
21 
22 /// A shared pointer to an image
23 using ImagePtr = shared_ptr<Image>;
24 
25 /// A shared pointer to a const image
26 using ConstImagePtr = shared_ptr<const Image>;
27 
28 /// A map from strings to images.
29 using ImageMap = std::unordered_map<string, ImagePtr>;
30 
31 /// A vetor of images.
32 using ImageVec = std::vector<ImagePtr>;
33 
34 /// A pair of images.
35 using ImagePair = std::pair<ImagePtr, ImagePtr>;
36 
37 /// A function to perform image buffer deallocation
38 using ImageBufferDeallocator = std::function<void(void*)>;
39 
40 /// A pair of unsigned integers.
41 using UnsignedIntPair = std::pair<unsigned int, unsigned int>;
42 
43 /// @class Image
44 /// Class representing an image in system memory
46 {
47  public:
48  enum class BaseType
49  {
50  UINT8 = 0,
51  UINT16 = 1,
52  HALF = 2,
53  FLOAT = 3
54  };
55 
56  public:
57  /// Create an empty image with the given properties.
58  static ImagePtr create(unsigned int width, unsigned int height, unsigned int channelCount, BaseType baseType = BaseType::UINT8)
59  {
60  return ImagePtr(new Image(width, height, channelCount, baseType));
61  }
62 
63  ~Image();
64 
65  /// @name Property Accessors
66  /// @{
67 
68  /// Return the width of the image.
69  unsigned int getWidth() const
70  {
71  return _width;
72  }
73 
74  /// Return the height of the image.
75  unsigned int getHeight() const
76  {
77  return _height;
78  }
79 
80  /// Return the channel count of the image.
81  unsigned int getChannelCount() const
82  {
83  return _channelCount;
84  }
85 
86  /// Return the base type of the image.
88  {
89  return _baseType;
90  }
91 
92  /// Return the stride of our base type in bytes.
93  unsigned int getBaseStride() const;
94 
95  /// Return the stride of an image row in bytes.
96  unsigned int getRowStride() const
97  {
98  return _width * _channelCount * getBaseStride();
99  }
100 
101  /// Return the maximum number of mipmaps for this image.
102  unsigned int getMaxMipCount() const;
103 
104  /// @}
105  /// @name Texel Accessors
106  /// @{
107 
108  /// Set the texel color at the given coordinates. If the coordinates
109  /// or image resource buffer are invalid, then an exception is thrown.
110  void setTexelColor(unsigned int x, unsigned int y, const Color4& color);
111 
112  /// Return the texel color at the given coordinates. If the coordinates
113  /// or image resource buffer are invalid, then an exception is thrown.
114  Color4 getTexelColor(unsigned int x, unsigned int y) const;
115 
116  /// @}
117  /// @name Image Analysis
118  /// @{
119 
120  /// Compute the average color of the image.
121  Color4 getAverageColor();
122 
123  /// Return true if all texels of this image are identical in color.
124  /// @param uniformColor Return the uniform color of the image, if any.
125  bool isUniformColor(Color4* uniformColor = nullptr);
126 
127  /// @}
128  /// @name Image Processing
129  /// @{
130 
131  /// Set all texels of this image to a uniform color.
132  void setUniformColor(const Color4& color);
133 
134  /// Apply the given matrix transform to all texels of this image.
135  void applyMatrixTransform(const Matrix33& mat);
136 
137  /// Apply the given gamma transform to all texels of this image.
138  void applyGammaTransform(float gamma);
139 
140  /// Create a copy of this image with the given channel count and base type.
141  ImagePtr copy(unsigned int channelCount, BaseType baseType) const;
142 
143  /// Apply a 3x3 box blur to this image, returning a new blurred image.
144  ImagePtr applyBoxBlur();
145 
146  /// Apply a 7x7 Gaussian blur to this image, returning a new blurred image.
147  ImagePtr applyGaussianBlur();
148 
149  /// Split this image by the given luminance threshold, returning the
150  /// resulting underflow and overflow images.
151  ImagePair splitByLuminance(float luminance);
152 
153  /// Save a channel of this image to disk as a text table, in a format
154  /// that can be used for curve and surface fitting.
155  void writeTable(const FilePath& filePath, unsigned int channel);
156 
157  /// @}
158  /// @name Resource Buffers
159  /// @{
160 
161  /// Set the resource buffer for this image.
163  {
164  _resourceBuffer = buffer;
165  }
166 
167  /// Return the resource buffer for this image.
168  void* getResourceBuffer() const
169  {
170  return _resourceBuffer;
171  }
172 
173  /// Allocate a resource buffer for this image that matches its properties.
174  void createResourceBuffer();
175 
176  /// Release the resource buffer for this image.
177  void releaseResourceBuffer();
178 
179  /// Set the resource buffer deallocator for this image.
181  {
182  _resourceBufferDeallocator = deallocator;
183  }
184 
185  /// Return the resource buffer deallocator for this image.
187  {
188  return _resourceBufferDeallocator;
189  }
190 
191  /// @}
192  /// @name Resource IDs
193  /// @{
194 
195  /// Set the resource ID for this image.
196  void setResourceId(unsigned int id)
197  {
198  _resourceId = id;
199  }
200 
201  /// Return the resource ID for this image.
202  unsigned int getResourceId() const
203  {
204  return _resourceId;
205  }
206 
207  /// @}
208 
209  protected:
210  Image(unsigned int width, unsigned int height, unsigned int channelCount, BaseType baseType);
211 
212  protected:
213  unsigned int _width;
214  unsigned int _height;
215  unsigned int _channelCount;
217 
220  unsigned int _resourceId = 0;
221 };
222 
223 /// Create a uniform-color image with the given properties.
224 MX_RENDER_API ImagePtr createUniformImage(unsigned int width, unsigned int height, unsigned int channelCount, Image::BaseType baseType, const Color4& color);
225 
226 /// Create a horizontal image strip from a vector of images with identical resolutions and formats.
227 MX_RENDER_API ImagePtr createImageStrip(const vector<ImagePtr>& imageVec);
228 
229 /// Compute the maximum width and height of all images in the given vector.
230 MX_RENDER_API UnsignedIntPair getMaxDimensions(const vector<ImagePtr>& imageVec);
231 
233 
234 #endif
unsigned int getWidth() const
Return the width of the image.
Definition: Image.h:69
BaseType getBaseType() const
Return the base type of the image.
Definition: Image.h:87
Definition: File.h:26
std::unordered_map< string, ImagePtr > ImageMap
A map from strings to images.
Definition: Image.h:29
void setResourceBuffer(void *buffer)
Set the resource buffer for this image.
Definition: Image.h:162
HALF
Definition: ImfPixelType.h:25
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
FLOAT
Definition: ImfPixelType.h:26
unsigned int getRowStride() const
Return the stride of an image row in bytes.
Definition: Image.h:96
ImageBufferDeallocator getResourceBufferDeallocator() const
Return the resource buffer deallocator for this image.
Definition: Image.h:186
unsigned int _channelCount
Definition: Image.h:215
std::vector< ImagePtr > ImageVec
A vetor of images.
Definition: Image.h:32
GLint y
Definition: glcorearb.h:103
Definition: Image.h:45
GLuint buffer
Definition: glcorearb.h:660
unsigned int getChannelCount() const
Return the channel count of the image.
Definition: Image.h:81
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
BaseType
Definition: Image.h:48
Definition: core.h:760
std::function< void(void *)> ImageBufferDeallocator
A function to perform image buffer deallocation.
Definition: Image.h:38
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:58
void * _resourceBuffer
Definition: Image.h:218
GLuint id
Definition: glcorearb.h:655
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 ImagePtr createImageStrip(const vector< ImagePtr > &imageVec)
Create a horizontal image strip from a vector of images with identical resolutions and formats...
GLint GLenum GLint x
Definition: glcorearb.h:409
unsigned int _width
Definition: Image.h:213
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
#define MX_RENDER_API
Definition: Export.h:18
GLuint color
Definition: glcorearb.h:1261
void setResourceBufferDeallocator(ImageBufferDeallocator deallocator)
Set the resource buffer deallocator for this image.
Definition: Image.h:180
unsigned int _height
Definition: Image.h:214
ImageBufferDeallocator _resourceBufferDeallocator
Definition: Image.h:219
GLint GLsizei width
Definition: glcorearb.h:103
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
BaseType _baseType
Definition: Image.h:216
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
unsigned int getHeight() const
Return the height of the image.
Definition: Image.h:75
void * getResourceBuffer() const
Return the resource buffer for this image.
Definition: Image.h:168
unsigned int getResourceId() const
Return the resource ID for this image.
Definition: Image.h:202
void setResourceId(unsigned int id)
Set the resource ID for this image.
Definition: Image.h:196
shared_ptr< const Image > ConstImagePtr
A shared pointer to a const image.
Definition: Image.h:26
std::pair< ImagePtr, ImagePtr > ImagePair
A pair of images.
Definition: Image.h:35