AForge.Imaging.Filters.ExtractNormalizedRGBChannel.ProcessFilter C# (CSharp) Method

ProcessFilter() protected method

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage sourceData, UnmanagedImage destinationData ) : void
sourceData UnmanagedImage Source image data.
destinationData UnmanagedImage Destination image data.
return void
        protected override unsafe void ProcessFilter( UnmanagedImage sourceData, UnmanagedImage destinationData )
        {
            // get width and height
            int width  = sourceData.Width;
            int height = sourceData.Height;

            int pixelSize = Image.GetPixelFormatSize( sourceData.PixelFormat ) / 8;
            int sum;

            if ( pixelSize <= 4 )
            {
                int srcOffset = sourceData.Stride - width * pixelSize;
                int dstOffset = destinationData.Stride - width;

                // do the job
                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++ )
                    {
                        sum = ( src[RGB.R] + src[RGB.G] + src[RGB.B] );

                        *dst = ( sum != 0 ) ? (byte) ( 255 * src[channel] / sum ) : (byte) 0;
                    }
                    src += srcOffset;
                    dst += dstOffset;
                }
            }
            else
            {
                pixelSize /= 2;

                byte* srcBase   = (byte*) sourceData.ImageData.ToPointer( );
                byte* dstBase   = (byte*) destinationData.ImageData.ToPointer( );
                int srcStride = sourceData.Stride;
                int dstStride = destinationData.Stride;

                // for each line
                for ( int y = 0; y < height; y++ )
                {
                    ushort* src = (ushort*) ( srcBase + y * srcStride );
                    ushort* dst = (ushort*) ( dstBase + y * dstStride );

                    // for each pixel
                    for ( int x = 0; x < width; x++, src += pixelSize, dst++ )
                    {
                        sum = ( src[RGB.R] + src[RGB.G] + src[RGB.B] );

                        *dst = ( sum != 0 ) ? (ushort) ( 65535 * src[channel] / sum ) : (ushort) 0;
                    }
                }
            }
        }
    }