Linux画像素缓冲区 [英] Linux draw pixel buffer

查看:126
本文介绍了Linux画像素缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单,我有一个生成像素缓冲区的代码.现在,我需要呈现此像素缓冲区,而不是保存图像,然后再对其进行分析.

The problem is simple enough, i have a code that generates a pixel buffer. Now i need to present this pixel buffer instead of saving image and then analyzing it after.

解决方案是什么?

  1. 打开窗口
  2. 用我的像素RGB888替换此窗口中的所有像素

到目前为止,建议是:若要使用opengl,请为覆盖窗口的矩形创建顶点缓冲区,然后使用像素着色器绘制像素.显然,这不是在窗口中交换像素缓冲区的最佳方法.

So far suggestion were: To use opengl, create a vertex buffer for a rect covering a window, and use pixel shader to draw your pixels. Which clearly is not the best way to swap pixel buffers in window.

平台:Ubuntu 18

Platform: Ubuntu 18

推荐答案

您还可以使用 SFML .实际上,在我的其他答案中,它似乎比 CImg 快得多.我不是这方面的专家,但是以下代码可以满足您的需求:

You can also display bitmapped images in a window pretty easily with SFML. In fact, it seems considerably faster than CImg in my other answer. I am no expert in this, but the following code does what you seem to want:

// g++ -std=c++11 main.cpp $(pkg-config --libs --cflags sfml-graphics sfml-window)

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdint>

int main()
{
    const unsigned width = 1024;
    const unsigned height= 768;

    // create the window
    sf::RenderWindow window(sf::VideoMode(width, height), "Some Funky Title");

    // create a texture
    sf::Texture texture;
    texture.create(width, height);

    // Create a pixel buffer to fill with RGBA data
    unsigned char *pixbuff = new unsigned char[width * height * 4];
    // Create uint32_t pointer to above for easy access as RGBA
    uint32_t * intptr = (uint32_t *)pixbuff;

    // The colour we will fill the window with
    unsigned char red  = 0;
    unsigned char blue = 255;

    // run the program as long as the window is open
    int frame = 0;
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // Create RGBA value to fill screen with.
        // Increment red and decrement blue on each cycle. Leave green=0, and make opaque
        uint32_t RGBA;
        RGBA = (red++ << 24) | (blue-- << 16) | 255;
        // Stuff data into buffer
        for(int i=0;i<width*height;i++){
           intptr[i] = RGBA;
        }
        // Update screen
        texture.update(pixbuff);
        sf::Sprite sprite(texture);
        window.draw(sprite);

        // end the current frame
        window.display();
        std::cout << "Frame: " << frame << std::endl;
        frame++;
        if(frame==1000)break;
    }

    return 0;
}

在Mac上,我达到了以下帧速率:

On my Mac, I achieved the following frame rates:

  • 700 fps @ 640x480分辨率
  • 384 fps @ 1024x768分辨率

如果您想提高性能,则可以/可以在第二个线程外创建并填充屏幕外的纹理,但这已经非常快了.

You can/could create and fill a texture off-screen in a second thread if you want to improve performance, but this is already pretty fast.

关键字:C ++,图像处理,显示,位图图形,像素缓冲区,SFML,imshow,质数.

Keywords: C++, Image Processing, display, bitmapped graphics, pixel buffer, SFML, imshow, prime.

这篇关于Linux画像素缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆