首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FreeImage错误图像颜色

FreeImage错误图像颜色
EN

Stack Overflow用户
提问于 2018-11-07 10:00:16
回答 1查看 373关注 0票数 0

我试图从我用Gstreamer创建的流中提取框架,并尝试用FreeImageQImage保存它们(这是用于测试的)。

代码语言:javascript
复制
    GstMapInfo bufferInfo;
    GstBuffer *sampleBuffer;
    GstStructure *capsStruct;
    GstSample *sample;
    GstCaps *caps;

    int width, height;
    const int BitsPP = 32;

    /* Retrieve the buffer */
    g_signal_emit_by_name (sink, "pull-sample", &sample);

    if (sample) {

        sampleBuffer = gst_sample_get_buffer(sample);
        gst_buffer_map(sampleBuffer,&bufferInfo,GST_MAP_READ);

        if (!bufferInfo.data) {
            g_printerr("Warning: could not map GStreamer buffer!\n");
            throw;
        }

        caps = gst_sample_get_caps(sample);
        capsStruct= gst_caps_get_structure(caps,0);

        gst_structure_get_int(capsStruct,"width",&width);
        gst_structure_get_int(capsStruct,"height",&height);


        auto bitmap = FreeImage_Allocate(width, height, BitsPP,0,0,0);
        memcpy( FreeImage_GetBits( bitmap ), bufferInfo.data, width * height * (BitsPP/8));

//        int pitch = ((((BitsPP * width) + 31) / 32) * 4);
//        auto bitmap = FreeImage_ConvertFromRawBits(bufferInfo.data,width,height,pitch,BitsPP,0, 0, 0);

        FreeImage_FlipHorizontal(bitmap);
        bitmap = FreeImage_RotateClassic(bitmap,180);

        static int id = 0;
        std::string name = "/home/stadmin/pic/sample" + std::to_string(id++) + ".png";

#ifdef FREE_SAVE
        FreeImage_Save(FIF_PNG,bitmap,name.c_str());
#endif

#ifdef QT_SAVE
        //Format_ARGB32
        QImage image(bufferInfo.data,width,height,QImage::Format_ARGB32);
        image.save(QString::fromStdString(name));
#endif

        fibPipeline.push(bitmap);

        gst_sample_unref(sample);
        gst_buffer_unmap(sampleBuffer, &bufferInfo);

        return GST_FLOW_OK;

FreeImage中的颜色输出是完全错误的,比如Qt -F、、ormat_ARGB32、绿色如蓝色或蓝色如桔子等。,但是当我使用Qt - Format_RGBA8888进行测试时,我可以得到正确的输出。我需要使用FreeImage,我希望学习如何纠正这个问题。

EN

回答 1

Stack Overflow用户

发布于 2018-11-07 14:11:12

由于您说Qt使用Format_RGBA8888成功,我只能猜测: gstreamer框架有按RGBA顺序排列的字节,而FreeImage则期望ARGB。

快速修复:

代码语言:javascript
复制
//have a buffer the same length of the incoming bytes
size_t length = width * height * (BitsPP/8);
BYTE * bytes = (BYTE *) malloc(length);

//copy the incoming bytes to it, in the right order:
int index = 0;
while(index < length)
{
    bytes[index]     = bufferInfo.data[index + 2];  //B
    bytes[index + 1] = bufferInfo.data[index + 1];  //G
    bytes[index + 2] = bufferInfo.data[index];      //R
    bytes[index + 3] = bufferInfo.data[index + 3];  //A
    index += 4;
}

//fill the bitmap using the buffer
auto bitmap = FreeImage_Allocate(width, height, BitsPP,0,0,0);
memcpy( FreeImage_GetBits( bitmap ), bytes, length);

//don't forget to
free(bytes);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53187161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档