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

Author Topic: Conversion from BGRA to RGBA  (Read 4511 times)

0 Members and 1 Guest are viewing this topic.

roccio

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Conversion from BGRA to RGBA
« on: November 03, 2015, 09:16:37 am »
Hello,
I have a byte array in BGRA format and I use it to update a sf::Texture. The problem is that sf::Texture only supports RGBA forrmat. Is there a fast solution for this conversion? Or (like I do now) I have to swap the bytes manually?

Thank you

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Conversion from BGRA to RGBA
« Reply #1 on: November 03, 2015, 09:34:06 am »
You can hack a faster solution with an OpenGL call:

sf::Texture::bind(&texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, bytes);

Depending on how the driver implements the conversion, the speed difference may be more or less noticeable.

However this may break the internal cache of SFML. You can make it almost safe by preserving the texture binding:

GLint textureBinding;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);
sf::Texture::bind(&texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, bytes);
glBindTexture(GL_TEXTURE_2D, textureBinding);

Try with caution ;)
Laurent Gomila - SFML developer

roccio

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Conversion from BGRA to RGBA
« Reply #2 on: November 03, 2015, 12:12:26 pm »
I will try. Thank you Laurent!

Aravol

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Conversion from BGRA to RGBA
« Reply #3 on: February 22, 2016, 07:55:54 pm »
Looking at needing to do this exact thing for a current project. Would the rebind be necessary if the texture is only edited from a dedicated thread (ie only this thread edits this image, this thread only edits this image), or does the mentioned internal cache cross threads? If there's a difference, I'm also using the .NET wrappers rather than the C++ libraries.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Conversion from BGRA to RGBA
« Reply #4 on: February 24, 2016, 08:44:58 pm »
What's important is in which OpenGL context you perform the operation. If the thread uses the window's context (ie. you called window.SetActive(true) in the thread), then you need to preserve OpenGL states. If you're using a dedicated dummy context (ie. you instanciate the Context class at the beginning of your thread) then you don't have to do it.
Laurent Gomila - SFML developer