Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageEXR::loadFloatEXR fix #45

Closed
GoogleCodeExporter opened this issue Apr 5, 2015 · 1 comment
Closed

ImageEXR::loadFloatEXR fix #45

GoogleCodeExporter opened this issue Apr 5, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

Thanks! I checked in the fix with some minor changes.

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

  • Changed state: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant