MImage Class Reference

#include <MImage.h>

Class Description

Image manipulation.

This class provides methods for reading file images stored on disk.

MImage is a class which provides access to some of Maya's image manipulation functionality. It has methods for loading and resizing image files in any Maya-supported raster format, including IFF, SGI, Softimage (pic), TIFF (tif), Alias PIX (als), GIF, RLA, JPEG (jpg). The image is stored as an uncompressed array of pixels, that can be read and manipulated directly. For simplicity, the pixels are stored in a RGBA format (4 bytes per pixel).

+ Examples:

Public Types

enum  MImageFilterFormat { kNoFormat, kHeightFieldBumpFormat, kNormalMapBumpFormat, kUnknownFormat }
 Filter formats. More...
 
enum  MPixelType { kUnknown, kByte, kFloat }
 Pixel formats. More...
 

Public Member Functions

 MImage ()
 The default class constructor. More...
 
 ~MImage ()
 Class destructor. More...
 
MStatus create (unsigned int width, unsigned int height, unsigned int channels=4, MPixelType type=kByte)
 Create a new MImage object. More...
 
MStatus readFromFile (MString pathname, MPixelType type=kByte)
 Attempt to identify and open the specified image file. More...
 
MStatus readFromTextureNode (const MObject &fileTextureObject, MPixelType type=kByte)
 Attempt to read the content of the given file texture node. More...
 
MStatus getSize (unsigned int &width, unsigned int &height) const
 Get the width and height of the currently opened image. More...
 
MPixelType pixelType () const
 Get the current pixel format of the image. More...
 
unsigned char * pixels () const
 Returns a pointer to the first pixel of the uncompressed pixels array. More...
 
float * floatPixels () const
 Returns a pointer to the first pixel of the uncompressed pixels array. More...
 
void setPixels (unsigned char *pixels, unsigned int width, unsigned int height)
 Set the pointer to the first pixel of the uncompressed pixels array. More...
 
void setFloatPixels (float *pixels, unsigned int width, unsigned int height, unsigned int channels=4)
 Set the pointer to the first pixel of the uncompressed pixels array. More...
 
unsigned int depth () const
 Get the color depth (in bytes) of the currently opened image. More...
 
MStatus getDepthMapSize (unsigned int &width, unsigned int &height) const
 Returns the size of the depth map buffer. More...
 
MStatus getDepthMapRange (float &minValue, float &maxValue) const
 Compute the minimum and maximum depth values (range) for any stored depth buffer. More...
 
MStatus setDepthMap (float *depth, unsigned width, unsigned height)
 Specifies the depth map resolution and data. More...
 
MStatus setDepthMap (const MFloatArray &depth, unsigned width, unsigned height)
 Specifies the depth map resolution and data. More...
 
float * depthMap (MStatus *ReturnStatus=NULL) const
 Returns a pointer to the depth data. More...
 
MStatus readDepthMap (MString pathname)
 Reads the depth map from the specified file and place the result into the depth map array of this MImage instance. More...
 
MStatus resize (int width, int height, bool preserveAspectRatio=true)
 Resize the currently opened image to the specified dimension, or to the closest width/height that is preserves the original aspect ratio. More...
 
MStatus filter (MImageFilterFormat sourceFormat, MImageFilterFormat targetFormat, double scale=1.0, double offset=0.0)
 Modify the content of the image by applying a filter. More...
 
MStatus writeToFile (MString pathname, MString outputFormat=kIffStringDefault) const
 Save the content of this image in a file. More...
 
MStatus writeToFileWithDepth (MString pathname, MString outputFormat=kIffStringDefault, bool writeDepth=false) const
 Save the content of this image in a file. More...
 
MStatus release ()
 Release the current image. More...
 
void verticalFlip ()
 Flips the image vertically.
 
void setRGBA (bool rgbaFormat)
 Sets a flag to indicate that pixel information is in RGBA sequence or BGRA sequence. More...
 
bool isRGBA () const
 Query flag which indicates whether the pixel information is in RGBA sequence or BGRA sequence. More...
 
bool haveDepth () const
 Returns true if this instance of MImage contains a depth map. More...
 
MStatus convertPixelFormat (MPixelType type, double scale=1.0, double offset=0.0)
 Convert the pixel format used by this image using the specified parameters. More...
 

Static Public Member Functions

static bool filterExists (MImageFilterFormat sourceFormat, MImageFilterFormat targetFormat)
 Return whether or not a given source format can be directly converted to a given target format. More...
 
static const char * className ()
 Returns the name of this class. More...
 

Friends

class MHWRender::MTextureManager
 

Member Enumeration Documentation

Filter formats.

Enumerator
kNoFormat 

 

kHeightFieldBumpFormat 

 

kNormalMapBumpFormat 

 

kUnknownFormat 

 

enum MPixelType

Pixel formats.

Enumerator
kUnknown 

Format not known or invalid.

kByte 

One byte per channel, ranging from 0 to 255.

kFloat 

One float per channel, ranging from 0.0 to 1.0.

Constructor & Destructor Documentation

OPENMAYA_MAJOR_NAMESPACE_OPEN MImage ( )

The default class constructor.

Initialize the image.

~MImage ( )

Class destructor.

Automatically releases the associated image array.

Member Function Documentation

MStatus create ( unsigned int  width,
unsigned int  height,
unsigned int  channels = 4,
MPixelType  type = kByte 
)

Create a new MImage object.

Allocates memory for an RGBA array of pixels of the given size. If an object was already in memory, it is released first.

Parameters
[in]widththe desired image's width in pixels.
[in]heightthe desired image's height in pixels.
[in]channelsthe desired number of channels per pixel. For byte typed images, 4 channels is the only value currently supported.
[in]typethe desired pixel format.
Returns
  • MS::kFailure if there is not enough memory to allocate the array.
  • MS::kSuccess otherwise.
+ Examples:
MStatus readFromFile ( MString  pathname,
MPixelType  type = kByte 
)

Attempt to identify and open the specified image file.

Please note that IFF and SGI are the fastest image file formats for reading.

Parameters
[in]pathnamethe full path of the image file that should be opened.
[in]typethe desired pixel format. kUnknown attempts to load the native pixel type.
Returns
  • MS::kSuccess if the image file was succesfully identified and opened.
  • MS::kFailure otherwise.
+ Examples:
MStatus readFromTextureNode ( const MObject fileTextureObject,
MPixelType  type = kByte 
)

Attempt to read the content of the given file texture node.

Please note that IFF and SGI are the fastest image file formats for reading.

Parameters
[in]fileTextureObjectan object that refers to the file texture node that should be read.
[in]typethe desired pixel format. kUnknown attempts to load the native image pixel type.
Returns
  • MS::kSuccess if the content of the file texture was read succesfully.
  • MS::kFailure otherwise.
+ Examples:
MStatus getSize ( unsigned int &  width,
unsigned int &  height 
) const
MImage::MPixelType pixelType ( ) const

Get the current pixel format of the image.

Returns
  • The current pixel format of the image.
  • kUnknown if there is no currently opened image.
unsigned char * pixels ( ) const

Returns a pointer to the first pixel of the uncompressed pixels array.

This array is tightly packed, of size (width * height * depth) bytes. For the moment, pixels are always stored in a RGBA (depth=4 bytes) pixel format.

Returns
  • Pointer to the first pixel of the array.
  • NULL if no image is currently opened, or the current image has a pixel type other than kByte
+ Examples:
float * floatPixels ( ) const

Returns a pointer to the first pixel of the uncompressed pixels array.

This array is tightly packed, of size (width * height * depth * sizeof( float)) bytes.

Returns
  • Pointer to the first pixel of the array.
  • NULL if no image is currently opened, or the current image has a pixel type other than kFloat
+ Examples:
void setPixels ( unsigned char *  inPixelPtr,
unsigned int  width,
unsigned int  height 
)

Set the pointer to the first pixel of the uncompressed pixels array.

This array is tightly packed, of size (width * height * depth) bytes. For the moment, pixels are always stored in a RGBA (depth=4 bytes) pixel format.

Parameters
[in]inPixelPtrthe variable containing a block of pixels.
[in]widththe variable that will be set to the image's width in pixels.
[in]heightthe variable that will be set to the image's height in pixels.
+ Examples:
void setFloatPixels ( float *  inPixelPtr,
unsigned int  width,
unsigned int  height,
unsigned int  channels = 4 
)

Set the pointer to the first pixel of the uncompressed pixels array.

This array is tightly packed, of size (width * height * depth) bytes. For the moment, pixels are always stored in a RGBA (depth=4 bytes) pixel format.

Parameters
[in]inPixelPtrthe variable containing a block of pixels.
[in]widththe variable that will be set to the image's width in pixels.
[in]heightthe variable that will be set to the image's height in pixels.
[in]channelsthe number of channels per pixel.
unsigned int depth ( ) const

Get the color depth (in bytes) of the currently opened image.

Returns
  • The number of bytes used for each pixel.
  • 0 if there is no currently opened image.
MStatus getDepthMapSize ( unsigned int &  width,
unsigned int &  height 
) const

Returns the size of the depth map buffer.

Note, depth maps can differ in resolution from the color map. This is mainly for oversampling support in image planes. In all other cases, you should insure that depth size matches the color buffer size. You cannot write out an image where the depth map and image map has different resolutions.

Note, depth maps are not support with HDR, or floating point, images.

Parameters
[out]width- width of the depth map
[out]height- height of the depth map
Returns
  • MS::kSuccess - height / width successfully returned.
  • MS::kFailure - invalid depth map data.
+ Examples:
MStatus getDepthMapRange ( float &  minValue,
float &  maxValue 
) const

Compute the minimum and maximum depth values (range) for any stored depth buffer.

The depth values returned are positive distances from the camera, with higher values indicating points further from the camera.

Parameters
[out]minValueMinimum value
[out]maxValueMaximum value
Returns
Status code
Status Codes:
  • MS::kSuccess Data values could be retrieved.
  • MS::kFailure Data values could not be retrieved. A value of 0 is returned for each argument.
MStatus setDepthMap ( float *  depth,
unsigned  width,
unsigned  height 
)

Specifies the depth map resolution and data.

Note, it is possible for the depth map to be a different resolution from the color map. This is to support oversampling with image planes. You cannot save image data to a file if the color map and image maps are different resolutions.

Parameters
[in]depth- float buffer that contains depth values. The depth values should be stored as -1.0/eyeDistance, where eyeDistance == 0.0 implies no depth is at that location.
[in]width- the width of the depth buffer
[in]height- the height of the depth buffer.
Returns
  • MS::kSuccess - depth values successfully loaded.
  • MS::kFailure - invalid object.
  • MS::kInvalidParameter - invalid parameter values.
+ Examples:
MStatus setDepthMap ( const MFloatArray depth,
unsigned  width,
unsigned  height 
)

Specifies the depth map resolution and data.

Note, it is possible for the depth map to be a different resolution from the color map. This is to support oversampling with image planes. You cannot save image data to a file if the color map and image maps are different resolutions.

Parameters
[in]depth- float array that contains depth values. The depth values should be stored as -1.0/eyeDistance, where eyeDistance == 0.0 implies no depth is at that location.
[in]width- the width of the depth buffer
[in]height- the height of the depth buffer.
Returns
  • MS::kSuccess - depth values successfully loaded.
  • MS::kFailure - invalid object.
  • MS::kInvalidParameter - invalid parameter values.
float * depthMap ( MStatus ReturnStatus = NULL) const

Returns a pointer to the depth data.

Parameters
[in]ReturnStatus- return code.
Returns
  • MS::kFailure - invalid object, or no depth data exists for this image instance.
  • MS::kSuccess - depth buffer successfully returned.
MStatus readDepthMap ( MString  pathname)

Reads the depth map from the specified file and place the result into the depth map array of this MImage instance.

Parameters
[in]pathname- the path to the depth file.
Returns
  • MS::kFailure - invalid object, or a depth buffer does not exist in the specified file.
  • MS::kSuccess - depth map successfully read.
+ Examples:
MStatus resize ( int  width,
int  height,
bool  preserveAspectRatio = true 
)

Resize the currently opened image to the specified dimension, or to the closest width/height that is preserves the original aspect ratio.

Resizing the image doesn't affect the file it was loaded from; it only affects the pixels in memory. The pixels are automatically filtered and antialiased to avoid a pixelated look.

Parameters
[in]widththe desired width in pixels.
[in]heightthe desired height in pixels.
[in]preserveAspectRatioa boolean specifying whether the aspect ratio should be preserved or not. If this flag is set, the given width and height are interpreted as the maximum dimensions allowable.
Returns
  • MS::kSuccess if the operation was succesful.
  • MS::kFailure if there is no currently opened image, the image has a pixel type other than kByte, there is not enough memory to allocate the new pixels array, or in case of any other error.
+ Examples:
bool filterExists ( MImageFilterFormat  sourceFormat,
MImageFilterFormat  targetFormat 
)
static

Return whether or not a given source format can be directly converted to a given target format.

See MImage::filter() for more information.

Parameters
[in]targetFormatthe format of the resulting image.
[in]sourceFormatthe format of the source image.
Returns
  • true if the filter exists.
  • false otherwise.
+ Examples:
MStatus filter ( MImageFilterFormat  sourceFormat,
MImageFilterFormat  targetFormat,
double  scale = 1.0,
double  offset = 0.0 
)

Modify the content of the image by applying a filter.

The dimension of the image remains the same; only the RGBA components get affected.

Currently, only one filter is supported:

sourceFormat = kHeightFieldBumpFormat, targetFormat = kNormalMapBumpFormat: this filter performs a conversion from the height-field bump format (generally used by Maya) to a normal map bump format suitable for interactive display. This filter is typically most useful to support bump mapping in a MPxHwShaderNode-derived hardware shader plug-in, since most high-end graphics adapter support it. For more information regarding this filter, see "A Practical and Robust Bump-mapping Technique for Today's GPUs", by Mark J. Kilgard, NVIDIA Corporation.

The scale argument for this filter can vary from -256.0 to 256.0, although typical values range from 1.0 to 10.0. The offset argument is currently ignored and should be left to the default value of 0.0.

Parameters
[in]sourceFormatthe source format.
[in]targetFormatdesired target format.
[in]scalevary depending on the source/target format.
[in]offsetvary depending on the source/target format.
Returns
  • MS::kSuccess if the operation was succesful.
  • MS::kFailure if this image is empty or uses a format other than kByte.
+ Examples:
MStatus writeToFile ( MString  pathname,
MString  outputFormat = kIffStringDefault 
) const

Save the content of this image in a file.

By default, the file is saved in IFF format. Optionally, the file can also be converted in a variety of image formats.

Parameters
[in]pathnamea path to the desired file. This path can be absolute (eg: "C:/temp/bump.iff") or relative (eg: "temp/bump.iff").
[in]outputFormatthe desired image format to save into. This follows the same convention found in the Render Globals window. Examples of valid formats include: als, bmp, cin, gif, jpg, rla, sgi, tga, tif. "iff" is the default, and is the fastest format to save into since it doesn't need any internal conversion.
Returns
  • MS::kFailure if an error occured. Common potential errors include: empty image, invalid output format, invalid pathname, not enough disk space or read-only file.
  • MS::kSuccess otherwise.
+ Examples:
MStatus writeToFileWithDepth ( MString  pathname,
MString  outputFormat = kIffStringDefault,
bool  writeDepth = false 
) const

Save the content of this image in a file.

By default, the file is saved in IFF format. Optionally, the file can also be converted in a variety of image formats. If the writeDepth parameter is true then any depth information stored in MImage will be written to file.

IMPORTANT: Depth map is only supported if the outputFormat is 'iff'. If the depth map and color map size differ in resolution then the file cannot be written.

Parameters
[in]pathnamea path to the desired file. This path can be absolute (eg: "C:/temp/bump.iff") or relative (eg: "temp/bump.iff").
[in]outputFormatthe desired image format to save into. This follows the same convention found in the Render Globals window. Examples of valid formats include: als, bmp, cin, gif, jpg, rla, sgi, tga, tif. "iff" is the default, and is the fastest format to save into since it doesn't need any internal conversion.
[in]writeDepthwrites the depth channel information if it stored in the class data.
Returns
  • MS::kFailure if an error occured. Common potential errors include: empty image, invalid output format, invalid pathname, not enough disk space, read-only file, or depth map resolution does not match color map resolution.
  • MS::kSuccess otherwise.
MStatus release ( )

Release the current image.

If there is no current image, the call is ignored. An image is always released automatically during destruction, so generally there is no need to call this function manually.

Returns
MS::kSuccess always.
+ Examples:
void setRGBA ( bool  rgbaFormat)

Sets a flag to indicate that pixel information is in RGBA sequence or BGRA sequence.

Pixel data must have been allocated before this call is made.

Parameters
[in]rgbaFormattrue value indicates RGBA format
+ Examples:
bool isRGBA ( ) const

Query flag which indicates whether the pixel information is in RGBA sequence or BGRA sequence.

If no pixel data exists, then false will be returned.

Returns
  • true if in RGBA format.
  • false if in BGRA format.
bool haveDepth ( ) const

Returns true if this instance of MImage contains a depth map.

Returns
  • true - depth map exists for this class instance.
  • false - depth map does not exist for this class instance.
MStatus convertPixelFormat ( MPixelType  format,
double  scale = 1.0,
double  offset = 0.0 
)

Convert the pixel format used by this image using the specified parameters.

Parameters
[in]formatthe desired pixel format. If this matches the current format, this method will do nothing.
[in]scaleif conversion is required, this specifies the scaling applied to every channel of every pixel. A scale of 0 will automatically rescale based on the minimum and maximum values in the image.
[in]offsetif conversion is required, this specifies the offset applied to every channel of every pixel.
Returns
  • MS::kFailure if the image is empty, or not in the requested format.
  • MS::kSuccess if the image is in the requested format.
const char * className ( )
static

Returns the name of this class.

Returns
The name of this class.

The documentation for this class was generated from the following files:
  • MImage.h
  • MImage.cpp