CreateGrayscaleImage() public static method

Create and initialize new grayscale image.

AForge.Imaging.Image.CreateGrayscaleImage() function could be used instead, which does the some. But it was not used to get rid of dependency on AForge.Imaing library.

public static CreateGrayscaleImage ( int width, int height ) : Bitmap
width int Image width.
height int Image height.
return System.Drawing.Bitmap
Beispiel #1
0
        // Load P5 PGM image (grayscale PNM image with binary encoding)
        private static unsafe Bitmap ReadP5Image(Stream stream, int width, int height, int maxValue)
        {
            var scalingFactor = (double)255 / maxValue;

            // create new bitmap and lock it
            var image     = Tools.CreateGrayscaleImage(width, height);
            var imageData = image.LockBits(new Rectangle(0, 0, width, height),
                                           ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            var stride = imageData.Stride;
            var ptr    = (byte *)imageData.Scan0.ToPointer();

            // prepare a buffer for one line
            var line = new byte[width];

            int totalBytesRead = 0, bytesRead = 0;

            // load all rows
            for (var y = 0; y < height; y++)
            {
                totalBytesRead = 0;
                bytesRead      = 0;

                // load next line
                while (totalBytesRead != width)
                {
                    bytesRead = stream.Read(line, totalBytesRead, width - totalBytesRead);

                    if (bytesRead == 0)
                    {
                        // if we've reached the end before complete image is loaded, then there should
                        // be something wrong
                        throw new Exception();
                    }

                    totalBytesRead += bytesRead;
                }

                // fill next image row
                var row = ptr + stride * y;

                for (var x = 0; x < width; x++, row++)
                {
                    *row = (byte)(scalingFactor * line[x]);
                }
            }

            // unlock image and return it
            image.UnlockBits(imageData);
            return(image);
        }
Beispiel #2
0
        private unsafe Bitmap ReadP5Image(Stream stream, int width, int height, int maxValue)
        {
            double     num        = 255.0 / (double)maxValue;
            Bitmap     bitmap     = Tools.CreateGrayscaleImage(width, height);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            int        stride     = bitmapData.Stride;
            byte *     ptr        = (byte *)bitmapData.Scan0.ToPointer();

            byte[] array = new byte[width];
            int    num2  = 0;
            int    num3  = 0;

            for (int i = 0; i < height; i++)
            {
                num2 = 0;
                num3 = 0;
                for (; num2 != width; num2 += num3)
                {
                    num3 = stream.Read(array, num2, width - num2);
                    if (num3 == 0)
                    {
                        throw new Exception();
                    }
                }
                byte *ptr2 = ptr + (long)stride * (long)i;
                int   num4 = 0;
                while (num4 < width)
                {
                    *ptr2 = (byte)(num * (double)(int)array[num4]);
                    num4++;
                    ptr2++;
                }
            }
            bitmap.UnlockBits(bitmapData);
            return(bitmap);
        }
Beispiel #3
0
        // Read image frame from the specified stream (current stream's position is used)
        private unsafe Bitmap ReadImageFrame(Stream stream, FITSImageInfo imageInfo)
        {
            int width  = imageInfo.Width;
            int height = imageInfo.Height;

            // create new bitmap
            Bitmap image = (imageInfo.BitsPerPixel == 8) ?
                           Tools.CreateGrayscaleImage(width, height) :
                           new Bitmap(width, height, PixelFormat.Format16bppGrayScale);

            // lock it
            BitmapData imageData = image.LockBits(new Rectangle(0, 0, width, height),
                                                  ImageLockMode.ReadWrite, image.PixelFormat);

            int   originalBitsPerPixel = imageInfo.OriginalBitsPerPixl;
            int   stride = imageData.Stride;
            byte *ptr    = (byte *)imageData.Scan0.ToPointer( );

            double min = imageInfo.MinDataValue;
            double max = imageInfo.MaxDataValue;

            // check number of bits per pixel and load image appropriately
            if (imageInfo.BitsPerPixel == 16)
            {
                // 16 bpp grayscale image
                double coef = 65535.0 / (max - min);

                // prepare a buffer for one line
                int    lineSize = width * Math.Abs(originalBitsPerPixel) / 8;
                byte[] line     = new byte[lineSize];
                byte[] temp     = new byte[8];

                // load all rows
                for (int y = height - 1; y >= 0; y--)
                {
                    // load next line
                    if (Tools.ReadStream(stream, line, 0, lineSize) < lineSize)
                    {
                        throw new ArgumentException("The stream does not contain valid FITS image.");
                    }

                    // fill next image row
                    ushort *row = (ushort *)(ptr + stride * y);

                    for (int x = 0, i = 0; x < width; x++, row++)
                    {
                        double value = 0;

                        switch (originalBitsPerPixel)
                        {
                        case 16:        // 16 bit signed integer
                        {
                            short tempValue = 0;
                            unchecked
                            {
                                tempValue = (short)((line[i++] << 8) + line[i++]);
                            }
                            value = tempValue;
                            break;
                        }

                        case 32:        // 32 bit signed integer
                        {
                            temp[3] = line[i++];
                            temp[2] = line[i++];
                            temp[1] = line[i++];
                            temp[0] = line[i++];

                            value = BitConverter.ToInt32(temp, 0);

                            break;
                        }

                        case -32:        // 32 bit float
                        {
                            temp[3] = line[i++];
                            temp[2] = line[i++];
                            temp[1] = line[i++];
                            temp[0] = line[i++];

                            value = BitConverter.ToSingle(temp, 0);
                            break;
                        }

                        case -64:        // 64 bit double
                        {
                            temp[7] = line[i++];
                            temp[6] = line[i++];
                            temp[5] = line[i++];
                            temp[4] = line[i++];
                            temp[3] = line[i++];
                            temp[2] = line[i++];
                            temp[1] = line[i++];
                            temp[0] = line[i++];

                            value = BitConverter.ToDouble(temp, 0);
                            break;
                        }
                        }

                        *row = (ushort)((value - min) * coef);
                    }
                }
            }
            else
            {
                // 8 bpp grayscale image
                double coef = 255.0 / (max - min);

                // prepare a buffer for one line
                byte[] line = new byte[width];

                // load all rows
                for (int y = height - 1; y >= 0; y--)
                {
                    // load next line
                    if (Tools.ReadStream(stream, line, 0, width) < width)
                    {
                        throw new ArgumentException("The stream does not contain valid FITS image.");
                    }

                    // fill next image row
                    byte *row = ptr + stride * y;

                    for (int x = 0; x < width; x++, row++)
                    {
                        *row = (byte)(((double)line[x] - min) * coef);
                    }
                }
            }

            // unlock image and return it
            image.UnlockBits(imageData);
            return(image);
        }