Accord.Imaging.ImageStatistics.ProcessImage C# (CSharp) Method

ProcessImage() private method

private ProcessImage ( UnmanagedImage image, byte mask, int maskLineSize ) : void
image UnmanagedImage
mask byte
maskLineSize int
return void
        private unsafe void ProcessImage(UnmanagedImage image, byte* mask, int maskLineSize)
        {
            // get image dimension
            int width = image.Width;
            int height = image.Height;

            pixels = pixelsWithoutBlack = 0;

            red = green = blue = gray = null;
            redWithoutBlack = greenWithoutBlack = blueWithoutBlack = grayWithoutBlack = null;

            int maskOffset = maskLineSize - width;

            // check pixel format
            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // alloc arrays
                int[] g = new int[256];
                int[] gwb = new int[256];

                byte value;
                int offset = image.Stride - width;

                // do the job
                byte* p = (byte*)image.ImageData.ToPointer();

                if (mask == null)
                {
                    // for each pixel
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, p++)
                        {
                            // get pixel value
                            value = *p;

                            g[value]++;
                            pixels++;

                            if (value != 0)
                            {
                                gwb[value]++;
                                pixelsWithoutBlack++;
                            }
                        }
                        p += offset;
                    }
                }
                else
                {
                    // for each pixel
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, p++, mask++)
                        {
                            if (*mask == 0)
                                continue;

                            // get pixel value
                            value = *p;

                            g[value]++;
                            pixels++;

                            if (value != 0)
                            {
                                gwb[value]++;
                                pixelsWithoutBlack++;
                            }
                        }
                        p += offset;
                        mask += maskOffset;
                    }
                }

                // create historgram for gray level
                gray = new Histogram(g);
                grayWithoutBlack = new Histogram(gwb);
            }
            else
            {
                // alloc arrays
                int[] r = new int[256];
                int[] g = new int[256];
                int[] b = new int[256];

                int[] rwb = new int[256];
                int[] gwb = new int[256];
                int[] bwb = new int[256];

                byte rValue, gValue, bValue;
                int pixelSize = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
                int offset = image.Stride - width * pixelSize;

                // do the job
                byte* p = (byte*)image.ImageData.ToPointer();

                if (mask == null)
                {
                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, p += pixelSize)
                        {
                            // get pixel values
                            rValue = p[RGB.R];
                            gValue = p[RGB.G];
                            bValue = p[RGB.B];

                            r[rValue]++;
                            g[gValue]++;
                            b[bValue]++;
                            pixels++;

                            if ((rValue != 0) || (gValue != 0) || (bValue != 0))
                            {
                                rwb[rValue]++;
                                gwb[gValue]++;
                                bwb[bValue]++;
                                pixelsWithoutBlack++;
                            }
                        }
                        p += offset;
                    }
                }
                else
                {
                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, p += pixelSize, mask++)
                        {
                            if (*mask == 0)
                                continue;

                            // get pixel values
                            rValue = p[RGB.R];
                            gValue = p[RGB.G];
                            bValue = p[RGB.B];

                            r[rValue]++;
                            g[gValue]++;
                            b[bValue]++;
                            pixels++;

                            if ((rValue != 0) || (gValue != 0) || (bValue != 0))
                            {
                                rwb[rValue]++;
                                gwb[gValue]++;
                                bwb[bValue]++;
                                pixelsWithoutBlack++;
                            }
                        }
                        p += offset;
                        mask += maskOffset;
                    }
                }

                // create histograms
                red = new Histogram(r);
                green = new Histogram(g);
                blue = new Histogram(b);

                redWithoutBlack = new Histogram(rwb);
                greenWithoutBlack = new Histogram(gwb);
                blueWithoutBlack = new Histogram(bwb);
            }
        }