SFML community forums

Help => Graphics => Topic started by: Alia5 on May 25, 2011, 09:22:46 pm

Title: qimage to sf::image conversion
Post by: Alia5 on May 25, 2011, 09:22:46 pm
hi
i need help to convert a qimage to an sf::image...
i just cant get it to work...

yeah, i can read and set every pixel, but you cant read the alpha of an pixel from an qimage :O
Title: qimage to sf::image conversion
Post by: Laurent on May 25, 2011, 10:05:46 pm
You must convert the image to ARGB32 (QImage::convertToFormat), get the pixels (QImage::bits), and play with bitwise operators to convert from ARGB to RGBA.
Title: qimage to sf::image conversion
Post by: Lokk on May 25, 2011, 10:09:49 pm
Take a look at here :
http://doc.qt.nokia.com/latest/qglwidget.html#convertToGLFormat

So, maybe something like this...
Code: [Select]
QImage source;
...

QImage converted = QGLWidget::convertToGLFormat(source);
sf::Image final;
final.LoadFromPixels(source.width(), source.height(), reinterpret_cast<const sf::Uint8*>(converted.bits()));
Title: qimage to sf::image conversion
Post by: Alia5 on May 25, 2011, 10:34:35 pm
@Lokk
With your method i have to include the whole qt-opengl-library...

instead i convert using qimage.convertToFormat(QImage::Format_ARGB32);
like laurent said...

works like a charm <3

Code: [Select]
qimage.convertToFormat(QImage::Format_ARGB32);
image.LoadFromPixels(qimage.width(), qimage.height(), reinterpret_cast<const sf::Uint8*>(qimage.bits()));


much thanks to both of you :)
Title: qimage to sf::image conversion
Post by: Laurent on May 25, 2011, 10:55:28 pm
Really? SFML wants RGBA, not ARGB. Colors should be wrong in the final texture.
Title: qimage to sf::image conversion
Post by: Lokk on May 25, 2011, 11:15:08 pm
Yeah, it's really weird lol...
Title: qimage to sf::image conversion
Post by: Alia5 on May 25, 2011, 11:16:58 pm
I only tested it with an black/transparent image....

will try...

edit: your right... :/

edit2: OpenGLWidget conversion keeps the colours but flips the y of my image xD  not that big deal, but kinda sucks :D
Title: qimage to sf::image conversion
Post by: Lokk on May 25, 2011, 11:52:48 pm
Quote
i can read and set every pixel, but you cant read the alpha of an pixel from an qimage :O

In fact, it is possible :

Code: [Select]
QImage image;
...
QRgb pixel = image.pixel(x, y);
int alpha = qAlpha(pixel); // return your alpha component
Title: qimage to sf::image conversion
Post by: Alia5 on May 26, 2011, 02:23:32 pm
woot? why i didn't find the qAlpha function in the doc? :O
Title: qimage to sf::image conversion
Post by: caracal on January 02, 2012, 06:33:16 am
Not to be a necromancer on this topic but I just ran into the same problem
here is how I solved it.

Code: [Select]

     QImage res = QImage(320, 320, QImage::Format_ARGB32);
     res.load("test.png");
     res = res.rgbSwapped();


     sf::Image image;
     image.Create(res.width(), res.height(), reinterpret_cast<const sf::Uint8*>(res.bits()));