Thanks for the replies.
ok. So this is what I have now, which actually works ))
public static void ArgbOverlay(Bitmap bm2, Bitmap bm1, out Bitmap bm3)
{
int[,] a1 = new int[bm1.Width, bm1.Height];
int[,] a2 = new int[bm2.Width, bm2.Height];
int[,] a3 = new int[bm2.Width, bm2.Height];
int[,] r1 = new int[bm1.Width, bm1.Height];
int[,] r2 = new int[bm2.Width, bm2.Height];
int[,] r3 = new int[bm2.Width, bm2.Height];
int[,] g1 = new int[bm1.Width, bm1.Height];
int[,] g2 = new int[bm2.Width, bm2.Height];
int[,] g3 = new int[bm2.Width, bm2.Height];
int[,] b1 = new int[bm1.Width, bm1.Height];
int[,] b2 = new int[bm2.Width, bm2.Height];
int[,] b3 = new int[bm2.Width, bm2.Height];
Rectangle rect1 = new Rectangle(0, 0, bm1.Width, bm1.Height);
BitmapData bd1 = bm1.LockBits(rect1, ImageLockMode.ReadWrite, bm1.PixelFormat);
int bytes1 = bd1.Stride * bd1.Height;
byte[] argbValues1 = new byte[bytes1];
IntPtr ptr1 = bd1.Scan0;
Marshal.Copy(ptr1, argbValues1, 0, bytes1);
Rectangle rect2 = new Rectangle(0, 0, bm2.Width, bm2.Height);
BitmapData bd2 = bm2.LockBits(rect2, ImageLockMode.ReadWrite, bm2.PixelFormat);
int bytes2 = bd2.Stride * bd2.Height;
byte[] argbValues2 = new byte[bytes2];
IntPtr ptr2 = bd2.Scan0;
Marshal.Copy(ptr2, argbValues2, 0, bytes2);
bm3 = new Bitmap(bm1.Width, bm1.Height, PixelFormat.Format32bppArgb);
Rectangle rect3 = new Rectangle(0, 0, bm3.Width, bm3.Height);
BitmapData bd3 = bm3.LockBits(rect3, ImageLockMode.ReadWrite, bm3.PixelFormat);
int bytes3 = bd3.Stride * bd3.Height;
byte[] argbValues3 = new byte[bytes3];
for (int x = 0; x < bm1.Width; x++)
{
for (int y = 0; y < bm1.Height; y++)
{
int BitPos1 = (x * 4) + (y * bd1.Stride);
b1[x, y] = (int)argbValues1[BitPos1];
g1[x, y] = (int)argbValues1[BitPos1 + 1];
r1[x, y] = (int)argbValues1[BitPos1 + 2];
a1[x, y] = (int)argbValues1[BitPos1 + 3];
b2[x, y] = (int)argbValues2[BitPos1];
g2[x, y] = (int)argbValues2[BitPos1 + 1];
r2[x, y] = (int)argbValues2[BitPos1 + 2];
a2[x, y] = (int)argbValues2[BitPos1 + 3];
r3[x, y] = (r2[x, y] * a2[x, y]) / 256 + ((r1[x, y] * (256 - a2[x, y])) / 256);
g3[x, y] = (g2[x, y] * a2[x, y]) / 256 + ((g1[x, y] * (256 - a2[x, y])) / 256);
b3[x, y] = (b2[x, y] * a2[x, y]) / 256 + ((b1[x, y] * (256 - a2[x, y])) / 256);
if (a1[x, y] < a2[x, y])
{
a3[x, y] = a2[x, y];
}
else
{
a3[x, y] = a1[x, y];
}
argbValues3[BitPos1] = (byte)b3[x, y];
argbValues3[BitPos1 + 1] = (byte)g3[x, y];
argbValues3[BitPos1 + 2] = (byte)r3[x, y];
argbValues3[BitPos1 + 3] = (byte)a3[x, y];
}
}
IntPtr ptr3 = bd3.Scan0;
Marshal.Copy(argbValues3, 0, ptr3, bytes3);
bm3.UnlockBits(bd3);
return;
}
Was wondering if anyone can see any glaring ways to optimise this further to make it a less cumbersome piece of code and also to make it run faster if poss.
It is fine as it is, but less is more.
|