以下程序是我在网上查到的对于图象做灰度处理的算法,我运行了,速度很快!但是我不很明白这段程序的意思!麻烦高手帮我解释下!
另外,还有一个问题,用C#编写的图形处理程序(不涉及指针)运行速度很慢,每次都得等那么一会,才能看到效果!这是为何?
// GDI+ return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
int nVal;
byte * p = (byte *)(void )Scan0;
int nOffset = stride - b.Width3;
int nWidth = b.Width * 3;
for(int y=0;y
1<b.height;++y) (int="" (nval="" (p[0]="" +="" ++x)="" 0)="" <="" brightness);="" for="" if="" nval="0;" nwidth;="" x="" {=""> 255) nVal = 255;
2p[0] = (byte)nVal;
3++p;
4}
5p += nOffset;
6}
7}
8b.UnlockBits(bmData);
9pictureBox2.Image=b;
10
11\---------------------------------------------------------------
12
13\---------------------------------------------------------------
14
15这段时间,我一直在查看这方面的信息,终于让我在这个网址看到可以解释我的问题的答案了:http://www.bobpowell.net/lockingbits.htm
16里面有一段,这么说的:
17The Stride property, as shown in figure 1, holds the width of one row in bytes. The size of a row however may not be an exact multiple of the pixel size because for efficiency, the system ensures that the data is packed into rows that begin on a four byte boundary and are padded out to a multiple of four bytes. This means for example that a 24 bit per pixel image 17 pixels wide would have a stride of 52. The used data in each row would take up 3*17 = 51 bytes and the padding of 1 byte would expand each row to 52 bytes or 13*4 bytes.
18它的意思是说,一行stride除了包括像素的信息外,还有个padding,它占的空间很少,为了更加形象的说明问题,它举了例:说24bit/per的图象一行有17个像素,但这行的宽度是:17*3+1=52!这个1就是padding的宽度!</b.height;++y)>