Jump to content


Fast DX texture pixel color conversion?

directx

3 replies to this topic

#1 hellhound_01

    New Member

  • Members
  • PipPip
  • 58 posts

Posted 15 November 2012 - 09:07 PM

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?
"There is only one god and his name is death. And there is only one thing we have to say to Death: Not today!" -- Syrio Forel (from Game of Thrones)

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 15 November 2012 - 10:43 PM

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?
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 TheNut

    Senior Member

  • Moderators
  • 1699 posts
  • LocationThornhill, ON

Posted 16 November 2012 - 01:00 AM

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.
http://www.nutty.ca - Being a nut has its advantages.

#4 hellhound_01

    New Member

  • Members
  • PipPip
  • 58 posts

Posted 16 November 2012 - 06:54 AM

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!
"There is only one god and his name is death. And there is only one thing we have to say to Death: Not today!" -- Syrio Forel (from Game of Thrones)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users