[Managed C#] masking using a paletted mask-map
Moderator: MaxCoderz Staff
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
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).
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).
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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;
}
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
Code: Select all
BitmapData OutputData = Output.LockBits(FrameRectangle, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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.
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.
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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
http://www.freewebs.com/lawcompany/WithARGB.png
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
Replace
with
Code: Select all
Bitmap Output = (Bitmap)sourceImage.Clone();
int Width = Output.Width;
int Height = Output.Height;
Code: Select all
int Width = sourceImage.Width;
int Height = sourceImage.Height;
Bitmap Output = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
THANKS!! that worked!!
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..
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.
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
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?
Edit: random? How do you mean? Do you mean that the "parts" don't seem to be in a sensible order?
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
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
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
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
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.
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.