Pixel format conversions

Is there a good way to convert pixel data between formats?

I have an byte array of pixel data in RGB format - if I want to add an alpha channel, or convert it into BGR format etc, is there a better way of doing it than manually processing the array pixel by pixel, pixel component by pixel component?

Its depends. If you want to change the pixelformat before you upload it as a texture to the GPU, I would let the GPU/driver do the work since todays GPU’s are good in data swizzling.

If you mean pixel format conversions for normal CPU use, you need an appropriate algorithm (or library) that does the conversion. Some conversions can be speeded up by using extended instructions like “MMX-SSE3” or “3dNow!” others cant.

Yeah, this is separate from the GPU, unfortunately. Also, I don’t have access to MMX et al, which leaves me with hideous code similar to:

</font><blockquote><font size=“1” face=“Verdana, Arial”>code:</font><hr /><pre style=“font-size:x-small; font-family: monospace;”>if((in == RGB && out == BGR)

Well, that ate half my reply…

Let’s try again:

</font><blockquote><font size=“1” face=“Verdana, Arial”>code:</font><hr /><pre style=“font-size:x-small; font-family: monospace;”>if((in == RGB && out == BGR)

Heh, how embarrasing… I really wish they’d allow “preview” for anon posters… Looks like it doesn’t like pipes.

Yeah, this is separate from the GPU, unfortunately. Also, I don’t have access to MMX et al, which leaves me with hideous code similar to:

if((in == RGB && out == BGR) OR (in == BGR && out == RGB))
  for(int i = 0 ; i < pixelCount * 3 ; i++)
  {
    out[i+2] = in[ i ] ;
    out[i+1] = in[i+1] ;
    out[ i ] = in[i+2] ;
  }
else if((in == RGBA && out == ...

No sequential indices, no bulk array access… a complete performance nightmare. And you don’t even gain any readibility through verboseness! :frowning:

Well, thanks anyway - I guess I’m not missing anything. There really isn’t a clever way to do this!

Hurrah! :rolleyes:

(Note to self: don’t use pipe characters on this forum…)