#include <maya/MString.h>
#include <maya/MStatus.h>
#include "iffreader.h"
IFFimageReader::IFFimageReader ()
{
    fImage = NULL;
    fBuffer = NULL;
    fZBuffer = NULL;
}
IFFimageReader::~IFFimageReader ()
{
    close ();
}
{
    close ();
   fImage = ILopen (filename.
asChar (), 
"rb");
    if (NULL == fImage)
    
    ILctrl (fImage, ILF_Updown, 1);
    
    ILctrl (fImage, ILF_Pack, 0);
    
    
    
    if (ILgetbpp (fImage) == 2)
        ILctrl (fImage, ILF_Full, 1);
    else
        ILctrl (fImage, ILF_Full, 0);
    
    ILctrl (fImage, ILF_NoMask, 0);
}
{
    if (NULL == fImage)
    if (ILclose (fImage))
    {
        fImage = NULL;
    }
    fImage = NULL;
    if (NULL != fBuffer)
    {
        delete [] fBuffer;
        fBuffer = NULL;
    }
    if (NULL != fZBuffer)
    {
        delete [] fZBuffer;
        fZBuffer = NULL;
    }
}
MStatus IFFimageReader::getSize (
int &x, 
int &y)
 
{
    if (NULL == fImage)
    if (ILgetsize (fImage, &x, &y))
}
int IFFimageReader::getBytesPerChannel ()
{
    return ILgetbpp (fImage);
}
bool IFFimageReader::isRGB ()
{
    if (NULL == fImage)
        return false;
    int type = ILgettype (fImage);
    if (type == -1)
        return false;
    return (type&ILH_RGB) ? true : false;
}
bool IFFimageReader::isGrayscale ()
{
    if (NULL == fImage)
        return false;
    int type = ILgettype (fImage);
    if (type == -1)
        return false;
    return (type&ILH_Black) ? true : false;
}
bool IFFimageReader::hasAlpha ()
{
    if (NULL == fImage)
        return false;
    int type = ILgettype (fImage);
    if (type == -1)
        return false;
    return (type&ILH_Alpha) ? true : false;
}
bool IFFimageReader::hasDepthMap ()
{
    if (NULL == fImage)
        return false;
    int type = ILgettype (fImage);
    if (type == -1)
        return false;
    return (type&ILH_Zbuffer) ? true : false;
}
MStatus IFFimageReader::readImage ()
 
{
    if (NULL == fImage)
    if (NULL != fBuffer || NULL != fZBuffer)
    int width,height;
    ILgetsize (fImage, &width, &height);
    int bpp; 
    bpp = ILgetbpp (fImage);
    int type;
    type = ILgettype (fImage);
    if ((type & ILH_RGB) || (type & ILH_Black))
        fBuffer = new byte [width * height * bpp * 4];
    if (type & ILH_Zbuffer)
        fZBuffer = new float [width * height];
    if (ILload (fImage, fBuffer, fZBuffer))
}
MStatus IFFimageReader::getPixel (
int x, 
int y, 
int *r, 
int *g, 
int *b,
 
                                  int *a)
{
    if (NULL == fBuffer)
    int width,height;
    ILgetsize (fImage, &width, &height);
    if (x >= width || y >= height || x < 0 || y < 0)
    if (ILgetbpp (fImage) == 2) {
        int *ptr = (int *)&fBuffer [(y * width + x) * 4];
        
#if     defined(_WIN32) || defined(LINUX)
        if (NULL != r)
            *r = ptr [2];
        if (NULL != g)
            *g = ptr [1];
        if (NULL != b)
            *b = ptr [0];
        if (NULL != a)
            *a = ptr [3];
#else
        if (NULL != r)
            *r = ptr [3];
        if (NULL != g)
            *g = ptr [2];
        if (NULL != b)
            *b = ptr [1];
        if (NULL != a)
            *a = ptr [0];
#endif
    } else {
        byte *ptr = &fBuffer [(y * width + x) * 4];
#if     defined(_WIN32) || defined(LINUX)
        if (NULL != r)
            *r = ptr [2];
        if (NULL != g)
            *g = ptr [1];
        if (NULL != b)
            *b = ptr [0];
        if (NULL != a)
            *a = ptr [3];
#else
        if (NULL != r)
            *r = ptr [3];
        if (NULL != g)
            *g = ptr [2];
        if (NULL != b)
            *b = ptr [1];
        if (NULL != a)
            *a = ptr [0];
#endif
    }
}
MStatus IFFimageReader::getDepth (
int x, 
int y, 
float *d)
 
{
    if (NULL == fZBuffer || NULL == d)
    int width,height;
    ILgetsize (fImage, &width, &height);
    if (x >= width || y >= height || x < 0 || y < 0)
    float depth = fZBuffer [y * width + x];
    if (depth == 0.)
        *d = 0.;
    else
        *d = -1.0f/depth;
}
MString IFFimageReader::errorString ()
 
{
    return FLstrerror (FLerror ());
}
const byte *IFFimageReader::getPixelMap () const
{
    return fBuffer;
}
const float *IFFimageReader::getDepthMap () const
{
    return fZBuffer;
}