Patryk Patryk - 3 months ago 7
C++ Question

How to display few images (each in separate window) with CImg ?

How can one display several images - each in one window - with the use of CImg ?

When I try something like this

cimg_library::CImg<unsigned char> image(s.c_str());
cimg_library::CImgDisplay main_disp(image, s.c_str() );
while (!main_disp.is_closed() )
main_disp.wait();


I have to close each window to get to the nect one and with this :

cimg_library::CImg<unsigned char> image(s.c_str());
cimg_library::CImgDisplay main_disp(image, s.c_str() )


They disappear one after another.

Answer

The windows opened by CImg are meant be displayed inside of an event loop. The event loop in the code snippet above is the block inside the while statement.

while (!main_disp.is_closed() ) 
            main_disp.wait();

The code in the post draws the window as part of the constructor, then the code enters the event loop and calls wait(). The call to wait() makes the application to pause until an "event" occurs. The event is some kind of input. It could be a mouse click, mouse movement, a keystroke from the keyboard, or even a redraw request from the operating system. When an event is received, the application starts executing again.

I haven't had time to try the code, but this code should show two windows at the same time:

cimg_library::CImg<unsigned char> image1(f1.c_str());
cimg_library::CImgDisplay disp1(image1, f1.c_str()  );
cimg_library::CImg<unsigned char> image2(f2.c_str());
cimg_library::CImgDisplay disp2(image1, f2.c_str()  );

//start event loop
while(true) {
     //All the interactive code is inside the event loop
     cimg_library::CImgDisplay::wait(disp1, disp2);
}

The tutorial (http://cimg.eu/reference/group__cimg__tutorial.html) has an example of two windows open and shows how to check for things like mouse button clicks and mouse position.