#include <maya/MStatus.h>
#include <math.h>
#include "MNormalMapConverter.h"
bool MNormalMapConverter::convertToNormalMap(
unsigned char* inImagePtr,
unsigned int width,
unsigned int height,
OutFormatType outputPixelFormat,
float bumpScale,
unsigned char* outImagePtr )
{
bool isOK = true;
if( NULL == inImagePtr ) return false;
if( NULL == outImagePtr )
{
convertToNormalMap_InPlace( inImagePtr, width, height, outputPixelFormat, bumpScale );
}
else
{
if( outputPixelFormat == RGBA ) {
}
else if( outputPixelFormat == HILO ) {
}
else {
isOK = false;
}
}
return isOK;
}
bool MNormalMapConverter::convertToNormalMap_InPlace(
unsigned char* inImagePtr,
unsigned int width,
unsigned int height,
OutFormatType outputPixelFormat,
float bumpScale )
{
bool isOK = true;
if( outputPixelFormat == RGBA ) {
bumpScale /= 255.0f;
unsigned int widthMinus1 = width - 1;
unsigned int heightMinus1 = height - 1;
unsigned int offset = (4 * width);
unsigned char* imagePtr = inImagePtr;
unsigned int m, n;
for( m=0; m<heightMinus1; m++ )
{
for( n=0; n<widthMinus1; n++ )
{
float deltaU = bumpScale * (float)(imagePtr[0] - imagePtr[4]);
float deltaV = bumpScale * (float)(imagePtr[0] - imagePtr[offset]);
float sqlen = deltaU*deltaU + deltaV*deltaV + 1.0f;
float rclen = 1.0f / sqrtf( sqlen );
float nx = deltaU * rclen;
float ny = deltaV * rclen;
float nz = rclen;
*(imagePtr++) = (unsigned char) ((nx + 1.0f) * 127.5f);
*(imagePtr++) = (unsigned char) ((ny + 1.0f) * 127.5f);
*(imagePtr++) = (unsigned char) ((nz + 1.0f) * 127.5f);
*(imagePtr++) = 255;
}
*(imagePtr++) = imagePtr[-4];
*(imagePtr++) = imagePtr[-4];
*(imagePtr++) = imagePtr[-4];
*(imagePtr++) = 255;
}
memcpy(imagePtr, imagePtr-offset, width*4);
}
else if( outputPixelFormat == HILO ) {
}
else {
isOK = false;
}
return isOK;
}
bool MNormalMapConverter::convertToHeightMap(
unsigned char* inImagePtr,
unsigned int width,
unsigned int height,
OutFormatType outputPixelFormat,
float heightScale,
unsigned char* outImagePtr)
{
return false;
}