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

ProcessFilter() protected method

Process the filter on the specified image.
Can not extract alpha channel from none ARGB image. The /// exception is throw, when alpha channel is requested from RGB 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;

            if ( ( channel == RGB.A ) && ( pixelSize != 4 ) && ( pixelSize != 8 ) )
            {
                throw new InvalidImagePropertiesException( "Can not extract alpha channel from none ARGB image." );
            }

            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( );

                // allign source pointer to the required channel
                src += channel;

                for ( int y = 0; y < height; y++ )
                {
                    for ( int x = 0; x < width; x++, src += pixelSize, dst++ )
                    {
                        *dst = *src;
                    }
                    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 );

                    // allign source pointer to the required channel
                    src += channel;

                    // for each pixel
                    for ( int x = 0; x < width; x++, src += pixelSize, dst++ )
                    {
                        *dst = *src;
                    }
                }

            }
        }
    }