Alpha Masked copy is getting weird results.

Started by
4 comments, last by Dan Violet Sagmiller 11 years, 1 month ago
I have some code that adds a texture to another texture using the alpha mask of another texture. But I'm getting really wonky results.
First, All images are 64x64 PNG files, stored with no compression.
I have 4 masks, used for the 4 corners that would make up an image (The image is all white, but the blue shows the alpha. It was the easiest way to show that here.
03.09.2013-07.49.png
Next, I have 4 textures to mess with: (dirt, grass, concrete and pavement)
03.09.2013-07.52.png
I put together a C# App to Create images. I start with a black 64x64 image, and start adding the pieces I want. For instance, mixing NE with 2 gives me this:
03.09.2013-07.56.png
It looks pretty accurate, but for some reason much brighter.
When I combine 3 & NW, I get this:
03.09.2013-08.02.png
Again hard to notice, but some of the tones are brighter.
Now for the real kicker. Now I take the first image (green in the top right corner) and call the same method to add the concrete to the top left corner, and I get this:
03.09.2013-08.05.png
Obviously, my code is doing something very wrong, but I'm not sure what, because it starts out so close.
Here is my code (C#, which doesn't support AND/OR on Bytes, which is pretty dumb, so everything is cast to int and back.)


        public void Paint(Bitmap canvas, Bitmap mask, Bitmap newLayer)
        {
            var rect = new Rectangle(0, 0, newLayer.Width, newLayer.Height);
            var bitsMask = mask.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            var bitsInput = newLayer.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            var bitsOutput = canvas.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            unsafe
            {
                for (int y = 0; y < newLayer.Height; y++)
                {
                    byte* ptrMask = (byte*)bitsMask.Scan0 + y * bitsMask.Stride;
                    byte* ptrInput = (byte*)bitsInput.Scan0 + y * bitsInput.Stride;
                    byte* ptrOutput = (byte*)bitsOutput.Scan0 + y * bitsOutput.Stride;
                    for (int x = 0; x < newLayer.Width; x++)
                    {
                        int msk = (int)ptrMask[4 * x + 3];
                        //blue
                        ptrOutput[4 * x + 0] = (byte)
                        (
                            (int)ptrOutput[4 * x + 0] | 
                            (msk & (int)ptrInput[4 * x + 0])
                        );
                        //green
                        ptrOutput[4 * x + 1] = (byte)
                        (
                            (int)ptrOutput[4 * x * 1] | 
                            (msk & (int)ptrInput[4 * x + 1])
                        );
                        //red
                        ptrOutput[4 * x + 2] = (byte)
                        (
                            (int)ptrOutput[4 * x * 2] | 
                            (msk & (int)ptrInput[4 * x + 2])
                        );
                        //mask
                        ptrOutput[4 * x + 3] = 255;
                    }
                }
            }
            mask.UnlockBits(bitsMask);
            newLayer.UnlockBits(bitsInput);
            canvas.UnlockBits(bitsOutput);


        }
 

The general logic is canvas = canvas OR (mask AND cover)

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

Advertisement

I should include that I also tried casting to uint and back as well, but that had no difference. The negative flip isn't held where I'm translating to int and back to byte.

I should also include that speed is not important here. This is for an editing tool to put the textures together. if it takes 20 minutes for 525 permutations, I'm ok with that. I just need it to work.

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

Some things i noticed as unexerienced guy, don't hang me if i am totaly off:

Format is specified as Format32argb but it seems you are writing bgra, is the reversed order necessary?

Following your example, wouldn't it be possible that: ((INT)Byte) & ((INT)Byte) > Byte? In that case your cast back to byte would not give 11111111 which it should.

In general i am not sure you could use the same code for masking and for imageblending.

simple add would probably result in very bright colors in the overlapping area..

You have an inconsistency in your code here:

(int)ptrOutput[4 * x * 1] should be: (int)ptrOutput[4 * x + 1] (for both the green and the blue channels.)

...but the real problem is that you can't use bitwise masking to perform alpha blending (only 1-bit alpha testing).

Consider the color value 170 (10101010) and an alpha value of 85 (1010101). AND-ing them together would result in the value 0 (00000000), while the expected value (of a multiplication of the two) would be 170 * 85 / 255 = 56. An OR operation would result in the value 255 (11111111).

More correct would be: newcolor = ((basecolor * (255 - alpha)) + (covercolor * alpha)) / 255.

(int)ptrOutput[4 * x * 1] should be: (int)ptrOutput[4 * x + 1] (for both the green and the blue channels.)

Thanks! i had the misplaced multiple, instead of a +. that fixed the singular approaches.

03.10.2013-11.27.png

So now, the only issue is its combination of 2 areas. It turns pink/bright. Additive, which I suppose it is.

Which then brings me on to your next part:

the real problem is that you can't use bitwise masking to perform alpha blending

Which is what I am seeing.

And then your solution:

newcolor = ((basecolor * (255 - alpha)) + (covercolor * alpha)) / 255

Which solved this for me. Thanks!

Here is the end result of having 4 different tiles corners stamped together.

03.10.2013-11.37.png

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

Format is specified as Format32argb but it seems you are writing bgra, is the reversed order necessary?

Yes, it does appear blanked out, but I noticed piece by piece that when using the "A" I was getting red, "R" I was getting blue, etc... So I marked them the way I say them. I'm not sure why It came through this way, but that's how it worked. I agree it is messed up, and if anyone knows why, that would be great. It might have to do with locked bits or something.

In general i am not sure you could use the same code for masking and for imageblending.
simple add would probably result in very bright colors in the overlapping area..

- Bingo! as noted in my previous post.

Thanks for your help as well.

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

This topic is closed to new replies.

Advertisement