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

Author Topic: QImage -> sf::Image  (Read 2477 times)

0 Members and 1 Guest are viewing this topic.

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
QImage -> sf::Image
« on: August 18, 2015, 04:13:09 pm »
Hey,

I'm trying to convert the pixel array of QImages into the std::vector<Uint8> used by sf::Images. QImages can have many formats, but the two formats I could be using are QImage::Format_RGB32 and QImage::Format_ARGB32.

I was wondering if sf::Images always have the RGBA32 format, or if it's possible to simply use the RGB32 format instead. If that's not possible, the conversion can take much longer.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: QImage -> sf::Image
« Reply #1 on: August 18, 2015, 04:20:28 pm »
I don't know what a QImage is.

However, if they're in the format that seem to be suggested by their names, it shouldn't be too difficult to convert from a QImage to an sf::Image and vice versa.

I'm not sure what you mean by "the conversion can take much longer" since creating the conversion code would be simple and any actual conversions would - most likely - only be done once.

In answer to your specific question, yes, sf::Image is always in the format RGBA.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: QImage -> sf::Image
« Reply #2 on: August 18, 2015, 04:46:57 pm »
Well the sequence is either RGBRGB... or ARGBARGB..., which are both different from RGBARGBA... used by SFML, so the following won't work / isn't correct:

m_pixels = std::vector<Uint8>(array, array + n);

although it's fast and easy, which is all the more the pity.

Now I wonder if I'd rather make my QImage using the RGB format, then add zeroes in the vector: R, G, B, 0, R, G, B, 0... ; or if I'd rather make my QImage using the ARGB format, then make a lot of swaps so that the sequence becomes RGBA-formatted. Would you have a preference?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: QImage -> sf::Image
« Reply #3 on: August 18, 2015, 06:28:34 pm »
Using RGB and processing each pixel individually and just adding the alpha when setting the pixel of the sf::Image seems reasonable.

I think it would be possible to use ARGB to increase speed but I'm not sure why speed is so important.
Anyway, if I was going to attempt it, I would probably increase the size of the data by one byte, copy all the alphas over the following alpha (do this in reverse order starting from the added byte), the pass the data to sf::Image starting from the second element (the first R).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: QImage -> sf::Image
« Reply #4 on: August 18, 2015, 06:45:45 pm »
The thing I don't understand is why you don't let Qt do the conversion with convertToFormat and QImage::Format_RGBA8888? I never tried it but it seems to do what you want.
SFML / OS X developer

 

anything