FreeImage неправильный цвет изображения - PullRequest
0 голосов
/ 07 ноября 2018

Я пытаюсь извлечь кадры из потока, который я создаю с помощью Gstreamer, и пытаюсь сохранить их с помощью FreeImage или QImage (это для тестирования).

    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, и я хочу узнать, как это исправить.

1 Ответ

0 голосов
/ 07 ноября 2018

Поскольку вы говорите, что 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);
...