Premultiplied alpha content processor

Premultiplied alpha content processor

  • Comments 7

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);
        }
    }
}
  • Is there an issue with loss of precision

    when storing the premultiplied color back into A8R8G8B8?

  • > 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.

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

  • I discovered a little program which has the option to save images with premultiplied alpha and other helpful things, so I thought I'd leave a link here for anyone who's interested.

    http://www.qualibyte.com/pixelformer/

  • How does one convince the ModelProcessor to use this content processor? Do you have to make a custom ModelProcessor that overrides ConvertMaterial to use a custom material processor that overrides BuildTexture to use the PremultipliedAlphaTextureProcessor, or is there some way to outright override TextureProcessor in the ContentProcessorContext?

    Also, you forgot a "using Microsoft.Xna.Framework.Graphics;", as that's where Color is located.

  • > How does one convince the ModelProcessor to use this content processor? Do you have to make a custom ModelProcessor that overrides ConvertMaterial to use a custom material processor

    Exactly right. This post has some example code:

    http://blogs.msdn.com/shawnhar/archive/2006/11/22/customizing-how-models-build-their-textures.aspx

    > Also, you forgot a "using Microsoft.Xna.Framework.Graphics;", as that's where Color is located.

    Oops, good catch!  My bad :-)

  • Hi Shawn,

    I checked out one of the XNA tutorial "Particles" (http://creators.xna.com/en-US/sample/particle) and found out that the BMP somehow "smoke.bmp" has embedded alpha channel. Do you know what paint software could do this?

    Thanks,

Page 1 of 1 (7 items)
Leave a Comment
  • Please add 6 and 5 and type the answer here:
  • Post