Linux draw pixel buffer

匆匆过客 提交于 2020-06-17 13:36:07

问题


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.

What would be the solution to:

  1. Open window
  2. Replace all pixels in this window with my pixels RGB888

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.

Platform: Ubuntu 18


回答1:


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;
}

On my Mac, I achieved the following frame rates:

  • 700 fps @ 640x480 resolution
  • 384 fps @ 1024x768 resolution

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.

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




回答2:


You could use CImg which is a small, fast, modern C++ library. It is "header only" so no complicated linking or dependencies.

// http://cimg.eu/reference/group__cimg__tutorial.html

#include <iostream>
#include <string>
#include "CImg.h"

using namespace cimg_library;

int main(int argc,char **argv) {

   const unsigned char white[] = { 255,255,255 };
   const int width  = 320;
   const int height = 240;
   // Create 3-channel RGB image
   CImg<> img(width,height,1,3);

   // Create main window
   CImgDisplay main_window(img,"Random Data",0);
   int frame = 0;

   while (!main_window.is_closed()) {
     // Fill image with random noise
     img.rand(0,255);
     // Draw in frame counter
     std::string text = "Frame: " + std::to_string(frame);
     img.draw_text(10,10,text.c_str(),white,0,1,32);
     main_window.display(img);
     frame++;
     std::cout << "Frame: " << frame << std::endl;
   }
}

Here it is in action - the quality is not best because random data is poorly compressible and Stack Overflow has a 2MB image limit. It is good in real-life.

Note that as I am using X11 underneath here, the compilation command must define cimg_display so will look something like:

g++ -Dcimg_display=1 -std=c++11 -I /opt/X11/include -L /opt/X11/lib -lx11 ...

Note also that I am using img.rand() to fill the image with data, you will want to get img.data() which is a pointer to the pixel buffer and then memcpy() your image data into the buffer at that address.

Note that I also did some stuff with writing to the framebuffer directly in another answer. That was in Python but it is easily adapted.



来源:https://stackoverflow.com/questions/60778907/linux-draw-pixel-buffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!