A grayscale image is used to discard all the color information of the image and display the 24-Bit Bitmap information in 8 bits. The grayscale image has a total gray level of 256, that is to say, to convert a point of a 24-Bit Bitmap (such as (255,255,255) to 255, so the coefficients multiplied by the values of R, G, and B are 1.
The pseudo statement can be used to represent the following:
Public bitmap GrayScal (bitmap orgbmp)
{
Create a large 8-bit image with the original image
Retrieve each vertex in the original image
New image points = original image points Red amount * coefficient 1 + green amount * coefficient 2 + yellow amount * system 3
Return New Image
}
Code PointTrans { cb; cg; cr; PointTrans( cr, cg, cb) { .cr = cr; .cg = cg; .cb = cb; } Bitmap GrayScaleBmp(Bitmap orgData) { bmpWidth = orgData.Width, bmpHeight = orgData.Height; Bitmap destData = ImageTools.CreateGrayscaleImage(bmpWidth, bmpHeight); Rectangle bmpRect= Rectangle(0,0,bmpWidth,bmpHeight); BitmapData orgBmpData = orgData.LockBits(bmpRect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData destBmpData = destData.LockBits(bmpRect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); ProcessFilter(orgBmpData,destBmpData); orgData.UnlockBits(orgBmpData); destData.UnlockBits(destBmpData); destData; } ProcessFilter(BitmapData sourceData, BitmapData destinationData) { width = sourceData.Width; height = sourceData.Height; srcOffset = sourceData.Stride - width*3; dstOffset = destinationData.Stride - width; * src = (*) sourceData.Scan0.ToPointer(); * dst = (*) destinationData.Scan0.ToPointer(); ( y = 0; y < height; y++) { ( x = 0; x < width; x++, src += 3, dst++) { *dst = () (cr*src[RGB.R] + cg*src[RGB.G] + cb*src[RGB.B]); } src += srcOffset; dst += dstOffset; } } }