Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: qimage to sf::image conversion  (Read 8221 times)

0 Members and 1 Guest are viewing this topic.

Alia5

  • Newbie
  • *
  • Posts: 34
    • View Profile
qimage to sf::image conversion
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
qimage to sf::image conversion
« Reply #1 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.
Laurent Gomila - SFML developer

Lokk

  • Full Member
  • ***
  • Posts: 228
    • View Profile
    • http://betadineproject.wordpress.com/
qimage to sf::image conversion
« Reply #2 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()));

Alia5

  • Newbie
  • *
  • Posts: 34
    • View Profile
qimage to sf::image conversion
« Reply #3 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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
qimage to sf::image conversion
« Reply #4 on: May 25, 2011, 10:55:28 pm »
Really? SFML wants RGBA, not ARGB. Colors should be wrong in the final texture.
Laurent Gomila - SFML developer

Lokk

  • Full Member
  • ***
  • Posts: 228
    • View Profile
    • http://betadineproject.wordpress.com/
qimage to sf::image conversion
« Reply #5 on: May 25, 2011, 11:15:08 pm »
Yeah, it's really weird lol...

Alia5

  • Newbie
  • *
  • Posts: 34
    • View Profile
qimage to sf::image conversion
« Reply #6 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

Lokk

  • Full Member
  • ***
  • Posts: 228
    • View Profile
    • http://betadineproject.wordpress.com/
qimage to sf::image conversion
« Reply #7 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

Alia5

  • Newbie
  • *
  • Posts: 34
    • View Profile
qimage to sf::image conversion
« Reply #8 on: May 26, 2011, 02:23:32 pm »
woot? why i didn't find the qAlpha function in the doc? :O

caracal

  • Newbie
  • *
  • Posts: 19
    • View Profile
qimage to sf::image conversion
« Reply #9 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()));