OpenEXR 图像加载

发布于 2024-08-24 18:40:18 字数 2750 浏览 3 评论 0原文

我已经开始使用 OpenEXR 加载 EXR 图像。我必须使用浮点类型获取 RGB 像素。

对于 RGB 图像,使用此代码加载没有问题:

ImfInputFile *iFile = ImfOpenInputFile(filename);
        FrameBuffer fb;
        const Header &iHeader = iFile.header();
        bool hasRed = false, hasGreen = false, hasBlue = false;
        bool hasY = false;
        Box2i dw = iHeader.dataWindow();
        int width = dw.max.x-dw.min.x+1;
        int height = dw.max.y-dw.min.y+1;

        for (ChannelList::ConstIterator it = iHeader.channels().begin(), ite = iHeader.channels().end(); it != ite; it++) {
            if ((strcmp(it.name(), "R") == 0)) { hasRed = true; }
            if ((strcmp(it.name(), "G") == 0)) { hasGreen = true; }
            if ((strcmp(it.name(), "B") == 0)) { hasBlue = true; }
            if (it.channel().type != HALF) {
                HDR_LOG("Unable to open EXR file \"%s\" (unsupported data type %s)", filename, it.channel().type);
                return (IEFileCantOpen);
            }
        }

        if ((hasRed == true) || (hasGreen == true) || (hasBlue == true)) {
            fb.insert("R", Slice(
                Imf::FLOAT, (char*)((char*)image->data + (sizeof(float) * 0)),
                sizeof(float) * 3,
                sizeof(float) * width * 3,
                1, 1,
                0.0
                )
            );
            fb.insert("G", Slice(
                Imf::FLOAT, (char*)((char*)image->data + (sizeof(float) * 1)),
                sizeof(float) * 3,
                sizeof(float) * width * 3,
                1, 1,
                0.0
                )
            );

            fb.insert("B", Slice(
                Imf::FLOAT, (char*)((char*)image->data + (sizeof(float) * 2)),
                sizeof(float) * 3,
                sizeof(float) * width * 3,
                1, 1,
                0.0
                )
            );

            iFile.setFrameBuffer(fb);

            if (ReadImage(filename, iFile, dw.min.y, dw.max.y) == IEFileReadError) {
                HDR_LOG("There was a generic error on loading the EXR image \"%s\". Image could be corrupted.", filename);
                return (IEFileReadError);
            }

            image->unproc = 1;

            return (IENoError);
        } else {
            char sChannels[2048] = { '\0' };

            for (ChannelList::ConstIterator it = iHeader.channels().begin(), ite = iHeader.channels().end(); it != ite; it++) {
                strcat(sChannels, it.name());
                strcat(sChannels, " ");
            }

            HDR_LOG("Unable to open EXR file (unknown channels set: %s)", sChannels);
            return (IEFileReadError);
        }
    }

但我想知道这个库如何解码/转换 Y-RY-GY 图像(亮度 + 色度通道)并获取浮点 RGB 像素数据。

I've started to load EXR images using OpenEXR. I have to get the RGB pixels using floating point type.

For RGB images, there was no problem on loading, using this code:

ImfInputFile *iFile = ImfOpenInputFile(filename);
        FrameBuffer fb;
        const Header &iHeader = iFile.header();
        bool hasRed = false, hasGreen = false, hasBlue = false;
        bool hasY = false;
        Box2i dw = iHeader.dataWindow();
        int width = dw.max.x-dw.min.x+1;
        int height = dw.max.y-dw.min.y+1;

        for (ChannelList::ConstIterator it = iHeader.channels().begin(), ite = iHeader.channels().end(); it != ite; it++) {
            if ((strcmp(it.name(), "R") == 0)) { hasRed = true; }
            if ((strcmp(it.name(), "G") == 0)) { hasGreen = true; }
            if ((strcmp(it.name(), "B") == 0)) { hasBlue = true; }
            if (it.channel().type != HALF) {
                HDR_LOG("Unable to open EXR file \"%s\" (unsupported data type %s)", filename, it.channel().type);
                return (IEFileCantOpen);
            }
        }

        if ((hasRed == true) || (hasGreen == true) || (hasBlue == true)) {
            fb.insert("R", Slice(
                Imf::FLOAT, (char*)((char*)image->data + (sizeof(float) * 0)),
                sizeof(float) * 3,
                sizeof(float) * width * 3,
                1, 1,
                0.0
                )
            );
            fb.insert("G", Slice(
                Imf::FLOAT, (char*)((char*)image->data + (sizeof(float) * 1)),
                sizeof(float) * 3,
                sizeof(float) * width * 3,
                1, 1,
                0.0
                )
            );

            fb.insert("B", Slice(
                Imf::FLOAT, (char*)((char*)image->data + (sizeof(float) * 2)),
                sizeof(float) * 3,
                sizeof(float) * width * 3,
                1, 1,
                0.0
                )
            );

            iFile.setFrameBuffer(fb);

            if (ReadImage(filename, iFile, dw.min.y, dw.max.y) == IEFileReadError) {
                HDR_LOG("There was a generic error on loading the EXR image \"%s\". Image could be corrupted.", filename);
                return (IEFileReadError);
            }

            image->unproc = 1;

            return (IENoError);
        } else {
            char sChannels[2048] = { '\0' };

            for (ChannelList::ConstIterator it = iHeader.channels().begin(), ite = iHeader.channels().end(); it != ite; it++) {
                strcat(sChannels, it.name());
                strcat(sChannels, " ");
            }

            HDR_LOG("Unable to open EXR file (unknown channels set: %s)", sChannels);
            return (IEFileReadError);
        }
    }

But I wonder how this library could decode/transform Y-RY-GY images (luminance + chroma channels) and get floating point RGB pixel data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2024-08-31 18:40:18

查看技术简介。有一个关于亮度/色度图像的部分。

似乎有三个通道:Y(亮度,单独使用,用于灰度图像,或与 RY 和 BY 组合使用,用于彩色图像。),RY,BY(色度,用于亮度/色度)。

类似的事情

 fb.insert("Y" /* <- channel name */, Slice(...)

应该解决。

Take a look at the Technical Introduction. There is a section about Luminance/Chroma Images.

It seems that there are three Channels: Y (Luminance, used either alone, for gray-scale images, or in combination with RY and BY for color images.), RY, BY (Chroma for luminance/chroma).

Something like

 fb.insert("Y" /* <- channel name */, Slice(...)

should work out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文