Fast DX texture pixel color conversion?

hellhound_01 104 Nov 15, 2012 at 21:07 directx

I’ve an image loaded in 32 bit rgba format. But DirectX9 requires an argb texture format. What is the best way to convert my image data to this format?

I’ve tried to use D3DFMT_A8R8G8B8 directly, but in this case my color values are changed and my texture appears blue. If I use D3DFMT_A8B8G8R8 instead,
I get in trouble because the returned surface format is D3DFM_A8R8G8B8 instead of my requested format. So I’ve to iterate over each pixel and bring it to the
correct pixel format. But this is a huge bottleneck on large images and not usable.

How would you handle this missmatch? Is there a faster way to convert those pixel colors?

3 Replies

Please log in or register to post a reply.

Reedbeta 167 Nov 15, 2012 at 22:43

Iterating over each pixel and swapping values is how I would handle it. It shouldn’t be *that* slow, even for large images, and you should only have to do it once, on load. Why do you say this solution is not usable?

TheNut 179 Nov 16, 2012 at 01:00

Gotta love Microsoft’s backward ways :) ARGB for images stored in memory and stored as BGR in bitmaps.

As reed said, just convert it over at load time. I have to do a similar thing by flipping textures in OpenGL since top is bottom and bottom is top (o¿O). It’s pretty fast. 250MB worth of graphics and other stuff going on loads in just 6 seconds from a zip package. Most images are large, like upwards to 4096x4096, so you shouldn’t have any performance problems. A quick way to convert your bitmap over would be to use bitshift operators. Typecast your 4byte RGBA pixels into an integer pointer and: (rgba >> 8) | (rgba << 24). You could optionally store your images in ARGB format and directly load them in like that.

hellhound_01 104 Nov 16, 2012 at 06:54

I’ve performed my pixel convertion first time the texture is initialized. Actually it takes 8-10 seconds to convert a huge (1024x4096) texture. Same application using OpenGL is 3 times faster. Actually I perform a bitshift on each pixel color value, maybe I could boost up my convertion using complete color shift as advised by The Nut.

Thanks for your hints!