TGUI Forum

General Category => Help requests => Topic started by: DJuego on 28 March 2020, 15:57:08

Title: Conversion from CImg to TGUI
Post by: DJuego on 28 March 2020, 15:57:08
Windows 10. Microsoft Visual Studio 2019

Hi. I'm new to CImg.

I'm trying to load images into SFML from CImg (http://cimg.eu/). My idea is that CImg will take care of the image processing while the interface is developed based on SFML (more specifically with TGUI. For example in a PictureBox.). Has anyone tried this conversion successfully?

My attempts have been unsuccessful. For example; this code produces an exception.

Code: [Select]
cimg_library::CImg<unsigned char> image_cimg("recursos/imagen_png.png");

sf::Image image_sfml;
image_sfml.create(image_cimg.width(), image_cimg.height(), image_cimg.data());

Any idea?
Thank you!

DJuego
Title: Re: Conversion from CImg to TGUI
Post by: texus on 28 March 2020, 22:32:05
I don't really know CImg, but if the exception is coming from the sf::Image::create function then you may want to check what image_cimg.data() is returning.

According to the SFML documentation (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Image.php#a1c2b960ea12bdbb29e80934ce5268ebf), the pixel data must be in 32-bits RGBA. This means that it will try to read 4*width*height bytes. Does image_cimg.size() return that amount? If not, then you should figure out how to get CImg to return the data in the correct format.

Alternatively, you can create the sf::Image without providing the pixel data and then use sf::Image::getPixelPtr (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Image.php#ad9562b126fc8d5efcf608166992865c7) and manually loop over the pixels from CImg and change them in the pixel data.

Either way, you need to understand exactly how the pixel data is formatted in your CImg image in order to figure out how to convert it to SFML (which stores the pixels as 4-byte RGBA values).
Title: Re: Conversion from CImg to TGUI
Post by: DJuego on 28 March 2020, 22:59:23
Indeed! Thank you for your answer, texus!

Very soon after asking the question I managed to code a satisfactory solution myself.
 I planned to publish the solution I found, and thus contribute to the community. But the life delayed me. I'm sorry. :-(

 Anyway, it's not perfect, because I can't load the alpha channel of the PNG images. But I know that the problem has nothing to do with SFML or TGUI.

I paste the current code (for now):

Code: [Select]
m_imagen.load("recursos/imagen_png.png");
//m_imagen.load("recursos/imagenBN_jpg.jpg");
//m_imagen.load("recursos/imagen_jpg.jpg");

sf::Image imagen;
imagen.create(m_imagen.width(), m_imagen.height(), sf::Color::Black);

switch (m_imagen.spectrum())
{
case 0:
{
//The image is empty


}
break;

case 1:  //Grayscale image
{
for (std::size_t i = 0; i < m_imagen.width(); ++i)
{
for (std::size_t j = 0; j < m_imagen.height(); ++j)
{
sf::Color color = sf::Color(m_imagen(i, j, 0, 0), m_imagen(i, j, 0, 0), m_imagen(i, j, 0, 0), 255);
imagen.setPixel(i, j, color);
}
}
}
break;
case 3:   //The image is RGB
{
for (std::size_t i = 0; i < m_imagen.width(); ++i)
{
for (std::size_t j = 0; j < m_imagen.height(); ++j)
{
sf::Color color = sf::Color(m_imagen(i, j, 0, 0), m_imagen(i, j, 0, 1), m_imagen(i, j, 0, 2), 255);
imagen.setPixel(i, j, color);
}
}
}
break;

case 4: //The image is RGBA?
{
for (std::size_t i = 0; i < m_imagen.width(); ++i)
{
for (std::size_t j = 0; j < m_imagen.height(); ++j)
{
sf::Color color = sf::Color(m_imagen(i, j, 0, 0), m_imagen(i, j, 0, 1), m_imagen(i, j, 0, 2), m_imagen(i, j, 0, 3));
imagen.setPixel(i, j, color);
}
}
}
break;

default:

break;
}



sf::Texture textura;
textura.loadFromImage(imagen);

tgui::Picture::Ptr panoramica = m_gui_principal.get<tgui::Picture>("Picture1");
panoramica->getRenderer()->setTexture(textura);



DJuego

P.S: Thank you for your commitment to the forum.