I have two images two images which are 8 bit indexed, I am trying to perform the following calculations on them:
1. Multiply both images
2. Total of all pixel values from result of step 1.
SrcBitmap1
http://i.stack.imgur.com/xQe1B.png[
^]
SrcBitmap2
http://i.stack.imgur.com/9mBHi.png[
^]
ResultBitmap:
http://i.stack.imgur.com/PFTna.png[
^]
below is my code, and for some reason, I am getting color images after multiplying two gray images. Can't figure out what is going on. Any advise is greatly appreciated.
public Bitmap MaskImage(Bitmap SrcBitmap1, Bitmap SrcBitmap2)
{
int width;
int height;
if (SrcBitmap1.Width < SrcBitmap2.Width)
width = SrcBitmap1.Width;
else
width = SrcBitmap2.Width;
if (SrcBitmap1.Height < SrcBitmap2.Height)
height = SrcBitmap1.Height;
else
height = SrcBitmap2.Height;
Bitmap bitmap = new Bitmap(width, height);
try
{
BitmapData Src1Data = SrcBitmap1.LockBits(new Rectangle(0, 0, SrcBitmap1.Width, SrcBitmap1.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
BitmapData Src2Data = SrcBitmap2.LockBits(new Rectangle(0, 0, SrcBitmap2.Width, SrcBitmap2.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
BitmapData DestData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
unsafe
{
int xOffset = 1;
for (int col = 0; col < bitmap.Height - 1; col++)
{
byte* Src1Ptr = (byte*)Src1Data.Scan0 + col * Src1Data.Stride;
byte* Src2Ptr = (byte*)Src2Data.Scan0 + col * Src2Data.Stride;
byte* DestPtr = (byte*)DestData.Scan0 + col * DestData.Stride;
for (int row = 0; row < bitmap.Width - 1; row++)
{
byte i1 = Src1Ptr[row * xOffset];
bbyte i2 = Src2Ptr[row * xOffset];
DestPtr[row * xOffset + 0] = (byte)((((ushort)(i1) * (ushort)(i2)) >> 8));
DestPtr[row * xOffset + 1] = (byte)((ushort)(((ushort)(i1) * (ushort)(i2)) >> 8));
DestPtr[row * xOffset + 2] = (byte)((ushort)(((ushort)(i1) * (ushort)(i2)) >> 8));
}
}
}
bitmap.UnlockBits(DestData);
SrcBitmap1.UnlockBits(Src1Data);
SrcBitmap2.UnlockBits(Src2Data);
SrcBitmap1.Dispose();
SrcBitmap2.Dispose();
}
catch (Exception ex)
{
Console.Write(ex);
}
return bitmap;
}