0

I'm loading in a PNG to use as a texture in Unity. When I use ConvertToRawBits() it outputs to BGRA32. Is it possible to output to ARGB32 or RGBA32 instead?

https://gist.github.com/ciwolsey/24635ea523b4a6b758783688be53bf86

ciwolsey
  • 97
  • 1
  • 1
  • 13
  • I'm using the FreeImageNet version of FreeImage but I'll accept answers as if it was just FreeImage. – ciwolsey Mar 03 '18 at 04:32
  • Yes I know that but I've decided to handle texture loading via threads to stop the render thread blocking. I'm just giving someone the option to answer with a C++ code snippet if they want and I'll convert it over to C#. – ciwolsey Mar 03 '18 at 17:27
  • Load the file with what? I don't want to read a PNG manually which is why I'm using FreeImage - isn't that what FreeImage is for? I guess SO isn't great for these kinds of discussions. My only aim here is to load a PNG without blocking the render thread. Currently I'm not using any C++ at all. – ciwolsey Mar 03 '18 at 23:15
  • It's not like it's only reading in the image that blocks the render thread, it's also the decompression of the PNG when you use Texture2D.LoadImage(). So my idea was to use FreeImage to read in, decode the PNG to a suitable format and then pass the result back to main thread where I can then use Texture2D.LoadRawTextureData() – ciwolsey Mar 03 '18 at 23:25
  • LoadImage will block the render thread as far as I understand because it will perform decompression of PNG on main thread. Which is why I'm using FreeImage on another thread - to avoid that. – ciwolsey Mar 03 '18 at 23:25
  • I've added my current code to the question. – ciwolsey Mar 03 '18 at 23:33
  • As far as I can tell OpenGL does not provide functionality for decoding PNGs though. You'd still need something like libpng or freeimage. Even the OpenGL wiki states: "Loading images from files is outside the scope of the OpenGL specification. It is handled by user-made code or one of the image handling libraries below.". Below which are several image loading libraries - one of which is FreeImage. My code targets desktop-only VR so mobile is of no concern. – ciwolsey Mar 03 '18 at 23:50
  • The code I posted works just fine. I don't really need advice beyond the original question which is on the subject of FreeImage. – ciwolsey Mar 04 '18 at 00:20
  • It doesn't look like it's possible as FreeImage uses your platform's endian-ness to decide that. However, if you are in control of building FreeImage yourself, you may be able to compile it to force a certain native format. This post covers this issue pretty well: https://stackoverflow.com/questions/31103121/why-freeimage-loads-image-in-bgr-format – avariant Mar 09 '18 at 22:08

1 Answers1

2

You have two options for this, you can:

  1. Recompile Freeimage with FREEIMAGE_COLORORDER set to FREEIMAGE_COLORORDER_RGB value (1). Then your loaded images will be always in RGBA32 format. If this FREEIMAGE_COLORORDER is not specified at compile time, the actual color order setting is determined from the target system's endianess.

  2. Use Freeimage SwapRedBlue32 function to convert loaded image from BGRA32 to RGBA32 format.

Here is a nice discussion on this subject.

SergeyLebedev
  • 3,673
  • 15
  • 29