i have 3 code!!they have no error but i dont know a problem!!time to run code1 and code2 is slower than code3 and the result image is not true but i think code1 and code2 are better than code 3!!Can you explain me for this problem??
Code 1:
public static void chinhdosang(Bitmap bm, int dosang)
{
Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
BitmapData bmdata = bm.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptr = bmdata.Scan0;
int stride = bmdata.Stride;
int offset = stride - bm.Width * 3;
int n = stride * bm.Height;
byte[] rgbvalue = new byte[n];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalue, 0, n);
int temp,x,y;
for (y = 0; y < bm.Height; y++)
{
for (x = 0; x < bm.Width; x++)
{
int i;
i = y * stride + x;
temp = (int)rgbvalue[i] + dosang;
if (temp < 0) temp = 0;
if (temp > 255) temp = 255;
rgbvalue[i] = (byte)temp;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgbvalue, 0, ptr, n);
bm.UnlockBits(bmdata);
}
Code 2:
public static void chinhdosang(Bitmap bm, double dosang)
{
Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
BitmapData bmdata = bm.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmdata.Stride;
IntPtr ptr = bmdata.Scan0;
int offset = stride - bm.Width * 3;
unsafe
{
byte* p = (byte*)ptr;
int x, y;
double value;
for (y =0;y<bm.Height;y++)
{
for (x = 0; x < bm.Width; x++)
{
value = (double)(p[0]) + dosang;
if (value < 0) value = 0;
if (value > 255) value = 255;
p[0] = (byte) value;
++p;
}
p += offset;
}
}
bm.UnlockBits(bmdata);
}
Code 3:
public static void chinhdosang(Bitmap bm, int dosang)
{
Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
BitmapData bmdata = bm.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr ptr = bmdata.Scan0;
int stride = bmdata.Stride;
int offset = stride - bm.Width * 3;
int n = bm.Width * bm.Height * 3;
byte[] rgbvalue = new byte[n];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalue, 0, n);
int temp,x,y;
for ( int i = 0; i < n; i++)
{
temp = (int)rgbvalue[i] + dosang;
if (temp < 0) temp = 0;
if (temp > 255) temp = 255;
rgbvalue[i] = (byte)temp;
}
System.Runtime.InteropServices.Marshal.Copy(rgbvalue, 0, ptr, n);
bm.UnlockBits(bmdata);
}