[Managed C#] masking using a paletted mask-map

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

can bitmap even be ARGB? i thought that 1 byte was allways set to 0x00?
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

A bitmap is just the name chosen to represent a 2D grid of pixels, each pixel having a unique colour associated with it. Whether each pixel holds its own colour information, or holds an index into a palette, or indeed what format is used to represent colours - it doesn't matter. That's why I pick to use ARGB (and take advantage of an alpha channel) instead of RGB.

This might be useful reading.

I think you're referring to the 32-bit RGB format (as opposed to 32-bit ARGB), where one byte is left unused?

Unless you are referring to the Windows ".bmp" format, which is nothing to do with the Bitmap class (up to the extent that it can load and save .bmp files, as well as a variety of others).
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

well during debugging i noticed the first byte was allways 0x00 so i thought.. well.. you know.. but i guess that was just because i wrote crappy code
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

If you lock an opaque Bitmap as 32-bit ARGB, you might well find that the first byte is always 0xFF now... ;)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

i hope so (and it should but you never know)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

For some reason it allways returns the whole image - not a partly transparent image.. It doesn't have any reason not to work. Or actually it probebly does but I made a mistake somewhere, but I can't find it.

Code: Select all

public Image BuildMasker(Bitmap sourceImage, Bitmap sourceMask, int index)
        {
            Bitmap Output = (Bitmap)sourceImage.Clone();
            int Width = Output.Width;
            int Height = Output.Height;

            int[] SourceImageArray = new int[Width * Height];
            byte[] SourceMaskArray = new byte[Width * Height];

            System.Drawing.Rectangle FrameRectangle = new System.Drawing.Rectangle(0, 0, Width, Height);

            BitmapData SourceImageData = sourceImage.LockBits(FrameRectangle, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            Marshal.Copy(SourceImageData.Scan0, SourceImageArray, 0, Width * Height);
            sourceImage.UnlockBits(SourceImageData);
            BitmapData SourceMaskData = sourceMask.LockBits(FrameRectangle, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
            Marshal.Copy(SourceMaskData.Scan0, SourceMaskArray, 0, Width * Height);
            sourceMask.UnlockBits(SourceMaskData);

            for (int i = 0; i < Width * Height; ++i)
            {
                if (SourceMaskArray[i] >= index) SourceImageArray[i] = 0;
            }
            BitmapData OutputData = Output.LockBits(FrameRectangle, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            Marshal.Copy(SourceImageArray, 0, OutputData.Scan0, Width * Height);
            Output.UnlockBits(OutputData);
            return (Image)Output;
        }
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Code: Select all

BitmapData OutputData = Output.LockBits(FrameRectangle, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Locking an image with a ReadOnly mode when you really want to write to it? :)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

well you see..
http://www.freewebs.com/lawcompany/Lock ... teonly.PNG
It didn't like WriteOnly, so.. I messed up and forgot what I changed, and.. well. I suck.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Seeing as SourceImageArray contains 32bpp ARGB data, try locking as Format32bppArgb instead of Indexed (which is the invalid parameter that causes an exception to be thrown).
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

That did the same, I tried to solve it by locking as indexed (the origional image was indexed, but locked as Argb)
http://www.freewebs.com/lawcompany/WithARGB.png
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Replace

Code: Select all

Bitmap Output = (Bitmap)sourceImage.Clone(); 
int Width = Output.Width; 
int Height = Output.Height;
with

Code: Select all

int Width = sourceImage.Width; 
int Height = sourceImage.Height;
Bitmap Output = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

THANKS!! that worked!! :D
You're the best!
How did you figure out I would have to do this btw?

edit: well, it doesn't error, but it seems to go random :?
edit2: alright its the bloody picture again - I'm going to have a long talk with the artist..
Last edited by King Harold on Tue 31 Oct, 2006 5:59 pm, edited 1 time in total.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Guesswork, really. All I know is that the graphics-related functions tend not to like indexed pixel formats, so by creating a fresh output Bitmap with a non-indexed pixel format, rather than simply duplicating the original, it was less likely to complain.

Edit: random? How do you mean? Do you mean that the "parts" don't seem to be in a sensible order?
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Well first thing i tried was completely messed up, completely, bad palette I guess (I can't help it, I didn't draw them)
The second is less bad but still.. kinda chaotic..
So I guess I will have them redrawn or something, I checked them with the GIMP and they really are chaotic
http://www.freewebs.com/lawcompany/BadPalette.PNG
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Can you post an example of the output? Do the output images sort of match the originals, but have the parts that are meant to steadily appear more and more in completely the wrong order (or are they completely garbled?)

EDIT: Oh, I see. Have you modified the original indexed images (the "mask-map") at all? Even reloading and saving in an image editor might have been enough to scramble the order of the colours in the palette.

If the colours are in some sort of order (so, ordered by brightness for example) it should be relatively easy to write a tool to reorder the palette. If not, then... it could be difficult.
Last edited by benryves on Tue 31 Oct, 2006 6:09 pm, edited 1 time in total.
Post Reply