C#转换为灰度图的算法

public static unsafe void GrayscaleImage(BitmapData sourceImage, IntPtr destBuffer)
       {
           int width = sourceImage.Width;
           int height = sourceImage.Height;

           // do the job
           var src = (byte*) sourceImage.Scan0.ToPointer();
           var dst = (byte*) destBuffer.ToPointer();
           int srcOffset = sourceImage.Stride - width*3;

           // for each line
           for (int y = 0; y < height; y++)
           {
               // for each pixel
               for (int x = 0; x < width; x++, src += 3, dst++)
               {
                   // use integer arithmetic to instead of doubles to speed up calculation
                   *dst = (byte) ((2125*src[2] + 7154*src[1] + 721*src[0])/10000);
               }
               src += srcOffset;
           }
       }

你可能感兴趣的:(C#)