src/Foundation/Media/Gimela.Media.Imaging/Filters/Color Filters/Grayscale.cs
/// <summary>
/// Process the filter on the specified image.
/// </summary>
/// 
/// <param name="sourceData">Source image data.</param>
/// <param name="destinationData">Destination image data.</param>
/// 
Process the filter on the specified image.
Process the filter on the specified image .
protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
    {
      int width = sourceData.Width;
      int height = sourceData.Height;
      PixelFormat srcPixelFormat = sourceData.PixelFormat;
      if (
          (srcPixelFormat == PixelFormat.Format24bppRgb) ||
          (srcPixelFormat == PixelFormat.Format32bppRgb) ||
          (srcPixelFormat == PixelFormat.Format32bppArgb))
      {
        int pixelSize = (srcPixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
        int srcOffset = sourceData.Stride - width * pixelSize;
        int dstOffset = destinationData.Stride - width;
        byte* src = (byte*)sourceData.ImageData.ToPointer();
        byte* dst = (byte*)destinationData.ImageData.ToPointer();
        for (int y = 0; y < height; y++)
        {
          for (int x = 0; x < width; x++, src += pixelSize, dst++)
          {
            *dst = (byte)(RedCoefficient * src[RGB.R] + GreenCoefficient * src[RGB.G] + BlueCoefficient * src[RGB.B]);
          }
          src += srcOffset;
          dst += dstOffset;
        }
      }
      else
      {
        int pixelSize = (srcPixelFormat == PixelFormat.Format48bppRgb) ? 3 : 4;
        byte* srcBase = (byte*)sourceData.ImageData.ToPointer();
        byte* dstBase = (byte*)destinationData.ImageData.ToPointer();
        int srcStride = sourceData.Stride;
        int dstStride = destinationData.Stride;
        for (int y = 0; y < height; y++)
        {
          ushort* src = (ushort*)(srcBase + y * srcStride);
          ushort* dst = (ushort*)(dstBase + y * dstStride);
          for (int x = 0; x < width; x++, src += pixelSize, dst++)
          {
            *dst = (ushort)(RedCoefficient * src[RGB.R] + GreenCoefficient * src[RGB.G] + BlueCoefficient * src[RGB.B]);
          }
        }
      }
    }
protected override unsafe void ProcessFilter ( UnmanagedImage sourceData , UnmanagedImage destinationData ) { int width = sourceData . Width ; int height = sourceData . Height ; PixelFormat srcPixelFormat = sourceData . PixelFormat ; if ( ( srcPixelFormat == PixelFormat . Format24bppRgb ) || ( srcPixelFormat == PixelFormat . Format32bppRgb ) || ( srcPixelFormat == PixelFormat . Format32bppArgb ) ) { int pixelSize = ( srcPixelFormat == PixelFormat . Format24bppRgb ) ? 3 : 4 ; int srcOffset = sourceData . Stride - width * pixelSize ; int dstOffset = destinationData . Stride - width ; byte * src = ( byte * ) sourceData . ImageData . ToPointer ( ) ; byte * dst = ( byte * ) destinationData . ImageData . ToPointer ( ) ; for ( int y = 0 ; y < height ; y ++ ) { for ( int x = 0 ; x < width ; x ++ , src += pixelSize , dst ++ ) { * dst = ( byte ) ( RedCoefficient * src [ RGB . R ] + GreenCoefficient * src [ RGB . G ] + BlueCoefficient * src [ RGB . B ] ) ; } src += srcOffset ; dst += dstOffset ; } } else { int pixelSize = ( srcPixelFormat == PixelFormat . Format48bppRgb ) ? 3 : 4 ; byte * srcBase = ( byte * ) sourceData . ImageData . ToPointer ( ) ; byte * dstBase = ( byte * ) destinationData . ImageData . ToPointer ( ) ; int srcStride = sourceData . Stride ; int dstStride = destinationData . Stride ; for ( int y = 0 ; y < height ; y ++ ) { ushort * src = ( ushort * ) ( srcBase + y * srcStride ) ; ushort * dst = ( ushort * ) ( dstBase + y * dstStride ) ; for ( int x = 0 ; x < width ; x ++ , src += pixelSize , dst ++ ) { * dst = ( ushort ) ( RedCoefficient * src [ RGB . R ] + GreenCoefficient * src [ RGB . G ] + BlueCoefficient * src [ RGB . B ] ) ; } } } }
Process the filter on the specified image.
Process the filter on the specified image .
// get width and height // do the job // for each line // for each pixel // for each line // for each pixel
sourceData
UnmanagedImage
Source image data.
Source image data .
destinationData
UnmanagedImage
Destination image data.
Destination image data .