AForge.Imaging.Filters.HistogramEqualization.ProcessFilter C# (CSharp) Метод

ProcessFilter() защищенный Метод

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage image, Rectangle rect ) : void
image UnmanagedImage Source image data.
rect System.Drawing.Rectangle Image rectangle for processing by the filter.
Результат void
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            int pixelSize = ( image.PixelFormat == PixelFormat.Format8bppIndexed ) ? 1 :
                ( image.PixelFormat == PixelFormat.Format24bppRgb ) ? 3 : 4;

            int startX = rect.Left;
            int startY = rect.Top;
            int stopX  = startX + rect.Width;
            int stopY  = startY + rect.Height;
            int stride = image.Stride;
            int offset = stride - rect.Width * pixelSize;

            int numberOfPixels = ( stopX - startX ) * ( stopY - startY );

            // check image format
            if ( image.PixelFormat == PixelFormat.Format8bppIndexed )
            {
                // grayscale image
                byte* ptr = (byte*) image.ImageData.ToPointer( );
                // allign pointer to the first pixel to process
                ptr += ( startY * stride + startX );

                // calculate histogram
                int[] histogram = new int[256];
                for ( int y = startY; y < stopY; y++ )
                {
                    for ( int x = startX; x < stopX; x++, ptr++ )
                    {
                        histogram[*ptr]++;
                    }
                    ptr += offset;
                }

                // calculate new intensity levels
                byte[] equalizedHistogram = Equalize( histogram, numberOfPixels );

                // update pixels' intensities
                ptr = (byte*) image.ImageData.ToPointer( );
                // allign pointer to the first pixel to process
                ptr += ( startY * stride + startX );

                for ( int y = startY; y < stopY; y++ )
                {
                    for ( int x = startX; x < stopX; x++, ptr++ )
                    {
                        *ptr = equalizedHistogram[*ptr];
                    }
                    ptr += offset;
                }
            }
            else
            {
                // color image
                byte* ptr = (byte*) image.ImageData.ToPointer( );
                // allign pointer to the first pixel to process
                ptr += ( startY * stride + startX * pixelSize );

                // calculate histogram
                int[] histogramR = new int[256];
                int[] histogramG = new int[256];
                int[] histogramB = new int[256];

                for ( int y = startY; y < stopY; y++ )
                {
                    for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                    {
                        histogramR[ptr[RGB.R]]++;
                        histogramG[ptr[RGB.G]]++;
                        histogramB[ptr[RGB.B]]++;
                    }
                    ptr += offset;
                }

                // calculate new intensity levels
                byte[] equalizedHistogramR = Equalize( histogramR, numberOfPixels );
                byte[] equalizedHistogramG = Equalize( histogramG, numberOfPixels );
                byte[] equalizedHistogramB = Equalize( histogramB, numberOfPixels );

                // update pixels' intensities
                ptr = (byte*) image.ImageData.ToPointer( );
                // allign pointer to the first pixel to process
                ptr += ( startY * stride + startX * pixelSize );

                for ( int y = startY; y < stopY; y++ )
                {
                    for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                    {
                        ptr[RGB.R] = equalizedHistogramR[ptr[RGB.R]];
                        ptr[RGB.G] = equalizedHistogramG[ptr[RGB.G]];
                        ptr[RGB.B] = equalizedHistogramB[ptr[RGB.B]];
                    }
                    ptr += offset;
                }
            }
        }