#include <maya/MPxImageFile.h>
#include <maya/MImageFileInfo.h>
#include <maya/MImage.h>
#include <maya/MFnPlugin.h>
#include <maya/MStringArray.h>
#include <maya/MIOStream.h>
#if _WIN32   
#pragma warning( disable : 4290 )       // Disable STL warnings.
#pragma warning( disable : 4244 )       // Disable conversion from unsigned int to unsigned short 
#endif
#undef min
#undef max
#include <OpenEXR/ImfRgbaFile.h>
#include <OpenEXR/ImfArray.h>
#include <half.h>
#define INVALID_PIXEL_TYPE Imf::NUM_PIXELTYPES
MString kImagePluginName( 
"OpenEXR");
 
{
public:
                    OpenEXRImageFile();
    virtual         ~OpenEXRImageFile();
    static void*    creator();
protected:
    int             fWidth;
    int             fHeight;
    int             fChannels;
    int             fLayers;
    Imf::PixelType  fPixelType;
    Imf::RgbaInputFile* fInputFile;
};
OpenEXRImageFile::OpenEXRImageFile()
: fInputFile( NULL), fChannels( 0), fLayers( 0), fPixelType( INVALID_PIXEL_TYPE)
{
}
OpenEXRImageFile::~OpenEXRImageFile()
{
    if( fInputFile)
        delete fInputFile;
}
void * OpenEXRImageFile::creator()
{
    return new OpenEXRImageFile();
}
{
    if( fInputFile) 
        delete fInputFile;
    try
    {
        fInputFile = 
new Imf::RgbaInputFile( pathname.
asChar());
    }
    catch( ... )
    {
    }
    if( !fInputFile)
    if( info)
    {
        const Imf::Header& header = fInputFile->header();
        fWidth = header.dataWindow().max.x - header.dataWindow().min.x + 1;
        fHeight = header.dataWindow().max.y - header.dataWindow().min.y + 1;
        const Imf::RgbaChannels channels = fInputFile->channels();
        
        fChannels = channels & Imf::WRITE_A ? 4 : 3;
        
    }
}
MStatus OpenEXRImageFile::load( 
MImage& image, 
unsigned int imageNumber)
 
{
    Imf::Array<Imf::Rgba> pixels;
    try
    {
        
        int dw = fWidth;
        int dh = fHeight;
        int dx = fInputFile->dataWindow().min.x;
        int dy = fInputFile->dataWindow().min.y;
        pixels.resizeErase (dw * dh);
        fInputFile->setFrameBuffer (pixels - dx - dy * dw, 1, dw);
        fInputFile->readPixels( fInputFile->dataWindow().min.y, fInputFile->dataWindow().max.y);
        
        
        bool flipVertically = true;
        if( flipVertically)
        {
            Imf::Rgba* src = pixels + (fHeight - 1) * fWidth;
            for( int y = 0; y < fHeight; y++)
            {
                for( int x = 0; x < fWidth; x++)
                {
                    *dest++ = src->r;
                    *dest++ = src->g;
                    *dest++ = src->b;
                    if( fChannels == 4)
                        *dest++ = src->a;
                    *src++;
                }
                src -= fWidth * 2;
            }
        }
        else
        {
        }
    }
    catch (...)
    {
        
        
        
        
        
        cerr << "OpenEXRImageFile::load() failed to load image." << endl;
    }
    return rval;
}
{
    MFnPlugin plugin( obj, PLUGIN_COMPANY, 
"8.0", 
"Any" );
 
                    kImagePluginName,
                    OpenEXRImageFile::creator, 
                    extensions));
    
}
{
}