Welcome to MSDN Blogs Sign in | Join | Help

Premultiplied alpha content processor

As mentioned in my previous post, a Content Processor that converts textures into premultiplied alpha format:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

namespace PremultipliedAlpha
{
    [ContentProcessor]
    class PremultipliedAlphaTextureProcessor : TextureProcessor
    {
        public override TextureContent Process(TextureContent input, ContentProcessorContext context)
        {
            input.ConvertBitmapType(typeof(PixelBitmapContent<Color>));

            foreach (MipmapChain mipChain in input.Faces)
            {
                foreach (PixelBitmapContent<Color> bitmap in mipChain)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            Color c = bitmap.GetPixel(x, y);

                            c.R = (byte)(c.R * c.A / 255);
                            c.G = (byte)(c.G * c.A / 255);
                            c.B = (byte)(c.B * c.A / 255);

                            bitmap.SetPixel(x, y, c);
                        }
                    }
                }
            }

            return base.Process(input, context);
        }
    }
}
Published Wednesday, November 11, 2009 8:10 AM by ShawnHargreaves

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Wednesday, November 18, 2009 1:29 PM by Jack

# re: Premultiplied alpha content processor

Is there an issue with loss of precision

when storing the premultiplied color back into A8R8G8B8?

Wednesday, November 18, 2009 2:01 PM by ShawnHargreaves

# re: Premultiplied alpha content processor

> Is there an issue with loss of precision

Not in any way that matters.

Remember this is the same multiplication that would be happening later on if you used conventional alpha blending, and the output surface for that blending is itself typically just 8 bits per channel.

There can certainly be differences due to rounding if the GPU does blending with >8 bit precision internally (which some do, but not all), but I've never seen this make a visible difference to the end result.

Saturday, November 21, 2009 5:40 AM by Martin Sherburn

# re: Premultiplied alpha content processor

Maybe this should be an option in the default XNA TextureProcessor? Could this be added for the next version?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker