Create() public static method

Allocate new image in unmanaged memory.

Allocate new image with specified attributes in unmanaged memory.

The method supports only Format8bppIndexed, Format16bppGrayScale, Format24bppRgb, Format32bppRgb, Format32bppArgb, Format32bppPArgb, Format48bppRgb, Format64bppArgb and Format64bppPArgb pixel formats. In the case if Format8bppIndexed format is specified, pallete is not not created for the image (supposed that it is 8 bpp grayscale image).

Unsupported pixel format was specified. Invalid image size was specified.
public static Create ( int width, int height, PixelFormat pixelFormat ) : UnmanagedImage
width int Image width.
height int Image height.
pixelFormat PixelFormat Image pixel format.
return UnmanagedImage
コード例 #1
0
        public unsafe void ExtractBlobsImage(UnmanagedImage image, Blob blob, bool extractInOriginalSize)
        {
            if (objectLabels == null)
            {
                throw new ApplicationException("Image should be processed before to collect objects map.");
            }
            if (image.PixelFormat != PixelFormat.Format24bppRgb && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppArgb && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppPArgb)
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the provided image.");
            }
            int width   = image.Width;
            int height  = image.Height;
            int stride  = image.Stride;
            int num     = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int width2  = blob.Rectangle.Width;
            int height2 = blob.Rectangle.Height;
            int width3  = extractInOriginalSize ? width : width2;
            int height3 = extractInOriginalSize ? height : height2;
            int left    = blob.Rectangle.Left;
            int num2    = left + width2 - 1;
            int top     = blob.Rectangle.Top;
            int num3    = top + height2 - 1;
            int iD      = blob.ID;

            blob.Image        = UnmanagedImage.Create(width3, height3, image.PixelFormat);
            blob.OriginalSize = extractInOriginalSize;
            byte *ptr  = (byte *)image.ImageData.ToPointer() + (long)top * (long)stride + (long)left * (long)num;
            byte *ptr2 = (byte *)blob.Image.ImageData.ToPointer();
            int   num4 = top * width + left;

            if (extractInOriginalSize)
            {
                ptr2 += top * blob.Image.Stride + left * num;
            }
            int num5 = stride - width2 * num;
            int num6 = blob.Image.Stride - width2 * num;
            int num7 = width - width2;

            for (int i = top; i <= num3; i++)
            {
                int num8 = left;
                while (num8 <= num2)
                {
                    if (objectLabels[num4] == iD)
                    {
                        *ptr2 = *ptr;
                        if (num > 1)
                        {
                            ptr2[1] = ptr[1];
                            ptr2[2] = ptr[2];
                            if (num > 3)
                            {
                                ptr2[3] = ptr[3];
                            }
                        }
                    }
                    num8++;
                    num4++;
                    ptr2 += num;
                    ptr  += num;
                }
                ptr  += num5;
                ptr2 += num6;
                num4 += num7;
            }
        }
        public unsafe Blob[] GetObjects(UnmanagedImage image, bool extractInOriginalSize)
        {
            if (this.objectLabels == null)
            {
                throw new ApplicationException("Image should be processed before to collect objects map.");
            }
            if ((((image.PixelFormat != PixelFormat.Format24bppRgb) && (image.PixelFormat != PixelFormat.Format8bppIndexed)) && ((image.PixelFormat != PixelFormat.Format32bppArgb) && (image.PixelFormat != PixelFormat.Format32bppRgb))) && (image.PixelFormat != PixelFormat.Format32bppPArgb))
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the provided image.");
            }
            int width  = image.Width;
            int height = image.Height;
            int stride = image.Stride;
            int num4   = Image.GetPixelFormatSize(image.PixelFormat) / 8;

            Blob[] blobArray = new Blob[this.objectsCount];
            for (int i = 0; i < this.objectsCount; i++)
            {
                int            num6    = this.blobs[i].Rectangle.Width;
                int            num7    = this.blobs[i].Rectangle.Height;
                int            num8    = extractInOriginalSize ? width : num6;
                int            num9    = extractInOriginalSize ? height : num7;
                int            x       = this.blobs[i].Rectangle.X;
                int            num11   = (x + num6) - 1;
                int            y       = this.blobs[i].Rectangle.Y;
                int            num13   = (y + num7) - 1;
                int            iD      = this.blobs[i].ID;
                UnmanagedImage image2  = UnmanagedImage.Create(num8, num9, image.PixelFormat);
                byte *         numPtr  = (byte *)((image.ImageData.ToPointer() + (y * stride)) + (x * num4));
                byte *         numPtr2 = (byte *)image2.ImageData.ToPointer();
                int            index   = (y * width) + x;
                if (extractInOriginalSize)
                {
                    numPtr2 += (y * image2.Stride) + (x * num4);
                }
                int num16 = stride - (num6 * num4);
                int num17 = image2.Stride - (num6 * num4);
                int num18 = width - num6;
                for (int j = y; j <= num13; j++)
                {
                    int num20 = x;
                    while (num20 <= num11)
                    {
                        if (this.objectLabels[index] == iD)
                        {
                            numPtr2[0] = numPtr[0];
                            if (num4 > 1)
                            {
                                numPtr2[1] = numPtr[1];
                                numPtr2[2] = numPtr[2];
                                if (num4 > 3)
                                {
                                    numPtr2[3] = numPtr[3];
                                }
                            }
                        }
                        num20++;
                        index++;
                        numPtr2 += num4;
                        numPtr  += num4;
                    }
                    numPtr  += num16;
                    numPtr2 += num17;
                    index   += num18;
                }
                blobArray[i]              = new Blob(this.blobs[i]);
                blobArray[i].Image        = image2;
                blobArray[i].OriginalSize = extractInOriginalSize;
            }
            return(blobArray);
        }
コード例 #3
0
        public unsafe Blob[] GetObjects(UnmanagedImage image, bool extractInOriginalSize)
        {
            if (objectLabels == null)
            {
                throw new ApplicationException("Image should be processed before to collect objects map.");
            }
            if (image.PixelFormat != PixelFormat.Format24bppRgb && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppArgb && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppPArgb)
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the provided image.");
            }
            int width  = image.Width;
            int height = image.Height;
            int stride = image.Stride;
            int num    = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;

            Blob[] array = new Blob[objectsCount];
            for (int i = 0; i < objectsCount; i++)
            {
                int            width2         = blobs[i].Rectangle.Width;
                int            height2        = blobs[i].Rectangle.Height;
                int            width3         = extractInOriginalSize ? width : width2;
                int            height3        = extractInOriginalSize ? height : height2;
                int            x              = blobs[i].Rectangle.X;
                int            num2           = x + width2 - 1;
                int            y              = blobs[i].Rectangle.Y;
                int            num3           = y + height2 - 1;
                int            iD             = blobs[i].ID;
                UnmanagedImage unmanagedImage = UnmanagedImage.Create(width3, height3, image.PixelFormat);
                byte *         ptr            = (byte *)image.ImageData.ToPointer() + (long)y * (long)stride + (long)x * (long)num;
                byte *         ptr2           = (byte *)unmanagedImage.ImageData.ToPointer();
                int            num4           = y * width + x;
                if (extractInOriginalSize)
                {
                    ptr2 += y * unmanagedImage.Stride + x * num;
                }
                int num5 = stride - width2 * num;
                int num6 = unmanagedImage.Stride - width2 * num;
                int num7 = width - width2;
                for (int j = y; j <= num3; j++)
                {
                    int num8 = x;
                    while (num8 <= num2)
                    {
                        if (objectLabels[num4] == iD)
                        {
                            *ptr2 = *ptr;
                            if (num > 1)
                            {
                                ptr2[1] = ptr[1];
                                ptr2[2] = ptr[2];
                                if (num > 3)
                                {
                                    ptr2[3] = ptr[3];
                                }
                            }
                        }
                        num8++;
                        num4++;
                        ptr2 += num;
                        ptr  += num;
                    }
                    ptr  += num5;
                    ptr2 += num6;
                    num4 += num7;
                }
                array[i]              = new Blob(blobs[i]);
                array[i].Image        = unmanagedImage;
                array[i].OriginalSize = extractInOriginalSize;
            }
            return(array);
        }
        public unsafe void ExtractBlobsImage(UnmanagedImage image, Blob blob, bool extractInOriginalSize)
        {
            if (this.objectLabels == null)
            {
                throw new ApplicationException("Image should be processed before to collect objects map.");
            }
            if ((((image.PixelFormat != PixelFormat.Format24bppRgb) && (image.PixelFormat != PixelFormat.Format8bppIndexed)) && ((image.PixelFormat != PixelFormat.Format32bppArgb) && (image.PixelFormat != PixelFormat.Format32bppRgb))) && (image.PixelFormat != PixelFormat.Format32bppPArgb))
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the provided image.");
            }
            int width  = image.Width;
            int height = image.Height;
            int stride = image.Stride;
            int num4   = Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int num5   = blob.Rectangle.Width;
            int num6   = blob.Rectangle.Height;
            int num7   = extractInOriginalSize ? width : num5;
            int num8   = extractInOriginalSize ? height : num6;
            int left   = blob.Rectangle.Left;
            int num10  = (left + num5) - 1;
            int top    = blob.Rectangle.Top;
            int num12  = (top + num6) - 1;
            int iD     = blob.ID;

            blob.Image        = UnmanagedImage.Create(num7, num8, image.PixelFormat);
            blob.OriginalSize = extractInOriginalSize;
            byte *numPtr  = (byte *)((image.ImageData.ToPointer() + (top * stride)) + (left * num4));
            byte *numPtr2 = (byte *)blob.Image.ImageData.ToPointer();
            int   index   = (top * width) + left;

            if (extractInOriginalSize)
            {
                numPtr2 += (top * blob.Image.Stride) + (left * num4);
            }
            int num15 = stride - (num5 * num4);
            int num16 = blob.Image.Stride - (num5 * num4);
            int num17 = width - num5;

            for (int i = top; i <= num12; i++)
            {
                int num19 = left;
                while (num19 <= num10)
                {
                    if (this.objectLabels[index] == iD)
                    {
                        numPtr2[0] = numPtr[0];
                        if (num4 > 1)
                        {
                            numPtr2[1] = numPtr[1];
                            numPtr2[2] = numPtr[2];
                            if (num4 > 3)
                            {
                                numPtr2[3] = numPtr[3];
                            }
                        }
                    }
                    num19++;
                    index++;
                    numPtr2 += num4;
                    numPtr  += num4;
                }
                numPtr  += num15;
                numPtr2 += num16;
                index   += num17;
            }
        }