gpt4 book ai didi

c++ - FreeImage 图像颜色错误

转载 作者:行者123 更新时间:2023-11-28 04:30:06 24 4
gpt4 key购买 nike

我正在尝试从使用 Gstreamer 创建的流中提取帧,并尝试使用 FreeImageQImage 保存它们(这个用于测试)。

    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 - Format_ARGB32 [绿色像蓝色或蓝色像橙色等。] 但是当我用 测试时>Qt - Format_RGBA8888 我可以获得正确的输出。我需要使用 FreeImage,我希望了解如何更正此问题。

最佳答案

既然你说 Qt 使用 Format_RGBA8888 成功,我只能猜测:gstreamer 帧有 RGBA 顺序的字节,而 FreeImage 需要 ARGB。

快速修复:

//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);

关于c++ - FreeImage 图像颜色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com