AForge.Imaging.UnmanagedImage.Collect16bppPixelValues C# (CSharp) Method

Collect16bppPixelValues() public method

Collect pixel values from the specified list of coordinates.

The method goes through the specified list of points and for each point retrievs corresponding pixel's value from the unmanaged image.

For grayscale image the output array has the same length as number of points in the specified list of points. For color image the output array has triple length, containing pixels' values in RGB order.

The method does not make any checks for valid coordinates and leaves this up to user. If specified coordinates are out of image's bounds, the result is not predictable (crash in most cases).

This method is supposed for images with 16 bpp channels only (16 bpp grayscale image and 48/64 bpp color images).

Unsupported pixel format of the source image. Use Collect8bppPixelValues() method for /// images with 8 bpp channels.
public Collect16bppPixelValues ( List points ) : ushort[]
points List List of coordinates to collect pixels' value from.
return ushort[]
        public ushort[] Collect16bppPixelValues( List<IntPoint> points )
        {
            int pixelSize = Bitmap.GetPixelFormatSize( pixelFormat ) / 8;

            if ( ( pixelFormat == PixelFormat.Format8bppIndexed ) || ( pixelSize == 3 ) || ( pixelSize == 4 ) )
            {
                throw new UnsupportedImageFormatException( "Unsupported pixel format of the source image. Use Collect8bppPixelValues() method for it." );
            }

            ushort[] pixelValues = new ushort[points.Count * ( ( pixelFormat == PixelFormat.Format16bppGrayScale ) ? 1 : 3 )];

            unsafe
            {
                byte* basePtr = (byte*) imageData.ToPointer( );
                ushort* ptr;

                if ( pixelFormat == PixelFormat.Format16bppGrayScale )
                {
                    int i = 0;

                    foreach ( IntPoint point in points )
                    {
                        ptr = (ushort*) ( basePtr + stride * point.Y + point.X * pixelSize );
                        pixelValues[i++] = *ptr;
                    }
                }
                else
                {
                    int i = 0;

                    foreach ( IntPoint point in points )
                    {
                        ptr = (ushort*) ( basePtr + stride * point.Y + point.X * pixelSize );
                        pixelValues[i++] = ptr[RGB.R];
                        pixelValues[i++] = ptr[RGB.G];
                        pixelValues[i++] = ptr[RGB.B];
                    }
                }
            }

            return pixelValues;
        }
    }