2

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

Logica
  • 977
  • 4
  • 16
Anton Stafeyev
  • 761
  • 1
  • 7
  • 20
  • Any GUI toolkit can do this: Qt or GTK for example. – Thomas Mar 20 '20 at 17:31
  • @Thomas can u be more specific about the way to do it, which calls can perform the task. So far yeah i have window open with GTK but cant find a way to draw on to screen my self – Anton Stafeyev Mar 20 '20 at 17:34
  • thats a draw widget, that i am ware of. What i am asking for is how to do it without widgets – Anton Stafeyev Mar 20 '20 at 17:44
  • What is your aim here? To write as little code as possible? To update the screen as fast as possible? What is most important? – Mark Setchell Mar 20 '20 at 18:39
  • @MarkSetchell Basically i have some input coming from the external device, input is parsed and graphical representation is computed. i need to take this array of pixels and display them in a window. So pretty much i have a buffer, and it needs to be displayed. So i am looking for a way to modify pixel buffer of a current window Preferably using smth like libx11 without going to deep into rabbit hole of widget libraryes and cairo – Anton Stafeyev Mar 20 '20 at 18:40
  • With **OpenCV** you can create a `Mat` , i.e. image matrix, like this https://stackoverflow.com/a/46576614/2836621 and display a window with it in using `cv2::imshow("Title", mat)` and wait for a keypress with `cv2::waitKey(0)`. – Mark Setchell Mar 20 '20 at 20:03
  • @MarkSetchell yeah but linking in opencv just for image displaying is not good idea :) – Anton Stafeyev Mar 20 '20 at 20:24
  • Ok, so what are your priorities here? It's not that hard to link OpenCV. – Mark Setchell Mar 20 '20 at 20:34
  • for example libX11 and XImage. yeah it is not hard to link a library that does stuff, but at what cost ? Priority is to open a window and draw a buffer. As i said earlier it is adevice that sends data which needs to be displayed. So device could send 100 frames a second or 1. So a frame rate is involved. means that displaying mats with opencv is nto the best idea. – Anton Stafeyev Mar 20 '20 at 20:37

2 Answers2

5

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

enter image description here

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks that solves the problem, will look into SFML – Anton Stafeyev Mar 23 '20 at 22:28
  • @AntonStafeyev I don't know why you accepted this answer if you didn't like using OpenGL, I examined SFML source and it looks like that's exactly what it's doing. I'm still trying to figure out how to do this directly. – YoTengoUnLCD Dec 11 '21 at 21:21
4

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.

enter image description here

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432