- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

# ImageIOHandlerRAW/ImageIOHandlerRAW.cpp

    //**************************************************************************/
    // Copyright (c) 2011 Autodesk, Inc.
    // All rights reserved.
    //
    // Use of this software is subject to the terms of the Autodesk license
    // agreement provided at the time of installation or download, or which
    // otherwise accompanies this software in either electronic or hard copy form.
    //
    //**************************************************************************/
    // DESCRIPTION: ImageIOHandlerRAW - Implementation of the ImageIOHandler interface
    // CREATED: April 2011
    //**************************************************************************/

    #pragma warning( disable : 4311 4312 )

    #include "ImageIOHandlerRAW.h"
    #include <QtGui/QFileDialog>
    #include <QtGui/QMessageBox>
    #include <QtCore/QDir>

    // Register this plugin, use the ImageIOHandlerRAW::Initializer function as the initialized, this will be called when the plugin got loaded.
    MB_PLUGIN( "ImageIOHandlerRAW", "ImageIOHandler example", "Autodesk", "http://www.mudbox3d.com", ImageIOHandlerRAW::Initializer );
    IMPLEMENT_SCLASS( ImageIOHandlerRAW, ImageIOHandler, QT_TRANSLATE_NOOP("ImageIOHandlerRAW", "ImageIOHandler example class"), 1 );

    ImageIOHandlerRAW::ImageIOHandlerRAW( void )
    {
        m_sImageName = tr("RAW Output");

        m_sImageDescription = tr("Outputs image in a RAW format.");

        m_aExtensions.append(FileExtension(NTRQ("raw"), NTRQ("RAW file [8 bit, RGBA]"), Image::e8integer) );
        m_aExtensions.append(FileExtension(NTRQ("raw"), NTRQ("RAW file [16 bit Integer, RGBA]"), Image::e16integer) );
        m_aExtensions.append(FileExtension(NTRQ("raw"), NTRQ("RAW file [32 bit Floating Point, RGBA]"), Image::e32float) );

        m_aFormats.append(PixelDescriptor(4, PixelDescriptor::sChar, PixelDescriptor::orderRGBA, false));
        m_aFormats.append(PixelDescriptor(4, PixelDescriptor::sHalf, PixelDescriptor::orderRGBA, false));
        m_aFormats.append(PixelDescriptor(4, PixelDescriptor::sFloat, PixelDescriptor::orderRGBA, false));
    }

    void ImageIOHandlerRAW::Initializer( void )
    {
    }

    bool ImageIOHandlerRAW::Import(const QString &sFileName, mudbox::Image *pImage)
    {
        if (!pImage || !sFileName.size())
            return false;

        QFile f(sFileName);

        if (f.open(QIODevice::ReadOnly))
        {

            char magicString[256];
            f.read(magicString, 12);

            QString magic(magicString);
            if (magic != "MBRAWEXAMPLE")
            {
                f.close();
                return false;
            }

            int width = 0;
            f.read((char *) &width, sizeof(int));

            int height = 0;
            f.read((char *) &height, sizeof(int));

            int nChannels = 0;
            f.read((char *) &nChannels, sizeof(int));

            int pixelSize = 0;
            f.read((char *) &pixelSize, sizeof(int));

            enum Image::Format fmt = Image::e8integer;
            f.read((char *) &fmt, sizeof(int));

            ImageDescriptor::MemoryChannelOrder channelOrder = ImageDescriptor::orderRGBA;
            f.read((char *) &channelOrder, sizeof(int));

            int imageSize = pixelSize * width * height;

            char *pData = new char[imageSize];
            f.read(pData, imageSize);

            pImage->Create(width, height, nChannels, fmt, false, channelOrder);

            pImage->setTile(0, 0, width, height, pData);

            f.close();

            delete pData;

            return true;
        }

        return false;
    }

    bool ImageIOHandlerRAW::Export(const QString &sFileName, Image *pImage)
    {
        if (!pImage || !sFileName.size())
            return false;

        QFile f(sFileName);

        if (f.open(QIODevice::WriteOnly))
        {
            f.write("MBRAWEXAMPLE", 12);

            const ImageDescriptor  &desc = pImage->getDescriptor();

            int width = pImage->Width();
            f.write((char *) &width, sizeof(int));

            int height = pImage->Height();
            f.write((char *) &height, sizeof(int));

            int nChannels = pImage->ChannelCount();
            f.write((char *) &nChannels, sizeof(int));

            int pixelSize = pImage->BytesPerPixel();
            f.write((char *) &pixelSize, sizeof(int));

            // Get the format of the data, write it to disk
            int fmt = pImage->Format();
            f.write((char *) &fmt, sizeof(int));

            // Get the channel order (rgba, abgr, etc...)
            int channelOrder = pImage->channelOrder();
            f.write((char *) &channelOrder, sizeof(int));

            int imageSize = pixelSize * width * height;

            char *pData = new char[imageSize];

            pImage->getTile(0, 0, width, height, (void *) pData);

            f.write(pData, imageSize);

            f.close();

            delete pData;

            return true;
        }

        return false;
    }