用偽語句可以表示如下
public bitmap GrayScal(bitmap orgbmp)
{
建立一個與原圖片等大的8位的圖片
取出原圖像中的每一個點
新圖像的點=原圖像點的紅色量*系數1+綠色量*系數2+黃色量*系統3
返回新圖像
}
/// <summary>
/// 做點運算,要給每一個偏量,做一下設置,比如做圖像的灰度圖就需要現設置
/// </summary>
/// <param name="cr"></param>
/// <param name="cg"></param>
/// <param name="cb"></param>
public PointTrans(double cr, double cg, double cb)
{
this.cr = cr;
this.cg = cg;
this.cb = cb;
}
public Bitmap GrayScaleBmp(Bitmap orgData)
{
int bmpWidth = orgData.Width, bmpHeight = orgData.Height;
Bitmap destData = ImageTools.CreateGrayscaleImage(bmpWidth, bmpHeight);
Rectangle bmpRect=new 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);
return destData;
}
protected unsafe void ProcessFilter(BitmapData sourceData, BitmapData destinationData)
{
// get width and height
int width = sourceData.Width;
int height = sourceData.Height;
int srcOffset = sourceData.Stride - width*3;
int dstOffset = destinationData.Stride - width;
// do the job
byte* src = (byte*) sourceData.Scan0.ToPointer();
byte* dst = (byte*) destinationData.Scan0.ToPointer();
// for each line
for (int y = 0; y < height; y++)
{
// for each pixel
for (int x = 0; x < width; x++, src += 3, dst++)
{
*dst = (byte) (cr*src[RGB.R] + cg*src[RGB.G] + cb*src[RGB.B]);
}
src += srcOffset;
dst += dstOffset;
}
}
}
新聞熱點
疑難解答