DontYouWantaFanta / nvidia-texture-tools

Automatically exported from code.google.com/p/nvidia-texture-tools
Other
0 stars 0 forks source link

ImageEXR::loadFloatEXR fix #45

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The function ImageEXR::loadFloatEXR assumes that the channels in the EXR
file are in the order R, G, B, A. This does not seem to be generally the
case. I have a file with the channels in the order A, B, G, R.

My fix is below. It pulls out the name of each channel and uses that to
figure out the channel index.

namespace
{
    int channelIndexFromName(const char* name)
    {
        char c = tolower(name[0]);
        switch (c)
        {
        case 'r':
            return 0;
        case 'g':
            return 1;
        case 'b':
            return 2;
        default:
            return 3;
        }
    }
}

FloatImage * nv::ImageIO::loadFloatEXR(const char * fileName, Stream & s)
{
    nvCheck(s.isLoading());
    nvCheck(!s.isError());

    ExrStream stream(fileName, s);
    Imf::InputFile inputFile(stream);

    Imath::Box2i box = inputFile.header().dataWindow();

    int width = box.max.x - box.min.y + 1;
    int height = box.max.x - box.min.y + 1;

    const Imf::ChannelList & channels = inputFile.header().channels();

    // Count channels.
    uint channelCount= 0;
    for (Imf::ChannelList::ConstIterator it = channels.begin(); it !=
channels.end(); ++it)
    {
        channelCount++;
    }

    // Allocate FloatImage.
    AutoPtr<FloatImage> fimage(new FloatImage());
    fimage->allocate(channelCount, width, height);

    // Describe image's layout with a framebuffer.
    Imf::FrameBuffer frameBuffer;
    for (Imf::ChannelList::ConstIterator it = channels.begin(); it !=
channels.end(); ++it)
    {
        int channelIndex = channelIndexFromName(it.name());
        frameBuffer.insert(it.name(), Imf::Slice(Imf::FLOAT, (char
*)fimage->channel(channelIndex), sizeof(float), sizeof(float) * width));
    }

    // Read it.
    inputFile.setFrameBuffer (frameBuffer);
    inputFile.readPixels (box.min.y, box.max.y);

    return fimage.release();
}

Original issue reported on code.google.com by alastair...@gmail.com on 19 May 2008 at 7:14

GoogleCodeExporter commented 9 years ago
Thanks! I checked in the fix with some minor changes.

Original comment by cast...@gmail.com on 21 May 2008 at 7:18