Пример #1
0
        private static double GetBytesPerPixel(ImagePixelFormat format)
        {
            switch (format)
            {
            case ImagePixelFormat.Bgr24:
                return(3);

            case ImagePixelFormat.Bgra32:
                return(4);

            case ImagePixelFormat.Rgb24:
                return(3);

            case ImagePixelFormat.Rgba32:
                return(4);

            case ImagePixelFormat.Argb32:
                return(4);

            case ImagePixelFormat.Uyvy422:
                return(2);

            case ImagePixelFormat.Yuv420:
                return(1.5);

            case ImagePixelFormat.Yuv422:
                return(2);

            case ImagePixelFormat.Yuv444:
                return(3);

            default:
                return(0);
            }
        }
Пример #2
0
        private static int GetBitsPerPixel(ImagePixelFormat format)
        {
            switch (format)
            {
            case ImagePixelFormat.Bgr24:
                return(24);

            case ImagePixelFormat.Bgra32:
                return(32);

            case ImagePixelFormat.Rgb24:
                return(24);

            case ImagePixelFormat.Rgba32:
                return(32);

            case ImagePixelFormat.Argb32:
                return(32);

            case ImagePixelFormat.Uyvy422:
                return(16);

            case ImagePixelFormat.Yuv420:
                return(12);

            case ImagePixelFormat.Yuv422:
                return(16);

            case ImagePixelFormat.Yuv444:
                return(24);

            default:
                return(0);
            }
        }
Пример #3
0
        public static Array2D <T> LoadImageData <T>(ImagePixelFormat format, byte[] data, uint rows, uint columns, uint steps)
            where T : struct
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (!Array2D <T> .TryParse <T>(out var type))
            {
                throw new NotSupportedException();
            }

            var srcType   = ImageTypes.UInt8.ToNativeArray2DType();
            var dstType   = type.ToNativeArray2DType();
            var pixelType = format.ToImagePixelType();
            var ret       = NativeMethods.extensions_load_image_data2(dstType, srcType, pixelType, data, rows, columns, steps);

            if (ret == IntPtr.Zero)
            {
                throw new ArgumentException($"Can not import from {ImageTypes.UInt8} to {dstType}.");
            }

            return(new Array2D <T>(ret, type));
        }
Пример #4
0
        /// <summary>
        /// Converts this video frame to the <see cref="ImageData"/> with the specified pixel format.
        /// </summary>
        /// <param name="scaler">A <see cref="ImageConverter"/> object, used for caching the FFMpeg <see cref="SwsContext"/> when converting many frames of the same video.</param>
        /// <param name="targetFormat">The output bitmap pixel format.</param>
        /// <returns>A <see cref="ImageData"/> instance containg converted bitmap data.</returns>
        public ImageData ToBitmap(ImageConverter scaler, ImagePixelFormat targetFormat)
        {
            var bitmap = ImageData.CreatePooled(Layout, targetFormat);

            scaler.AVFrameToBitmap(this, bitmap);
            return(bitmap);
        }
Пример #5
0
        internal NetImage(Size size, Size resolution, ImagePixelFormat pixFormat)
        {
            var pf = System.Drawing.Imaging.PixelFormat.Format32bppArgb; // xlator.xlat(pixFormat);  20180108 SPOL: PixelFormat hardcoded for .NET

            m_Bitmap = new Bitmap(size.Width, size.Height, pf);
            m_Bitmap.SetResolution(resolution.Width, resolution.Height);
        }
Пример #6
0
        /// <summary>
        /// Rents a memory buffer from pool and creates a new instance of <see cref="ImageData"/> class from it.
        /// </summary>
        /// <param name="imageSize">The image dimensions.</param>
        /// <param name="pixelFormat">The bitmap pixel format.</param>
        /// <returns>The new <see cref="ImageData"/> instance.</returns>
        public static ImageData CreatePooled(Size imageSize, ImagePixelFormat pixelFormat)
        {
            var size   = EstimateStride(imageSize.Width, pixelFormat) * imageSize.Height;
            var pool   = MemoryPool <byte> .Shared;
            var memory = pool.Rent(size);

            return(new ImageData(memory, imageSize, pixelFormat));
        }
Пример #7
0
        private ImageData(IMemoryOwner <byte> memory, Size size, ImagePixelFormat pixelFormat)
        {
            span         = null;
            pooledMemory = memory;

            ImageSize   = size;
            PixelFormat = pixelFormat;
        }
Пример #8
0
        /// <summary>
        /// Constructor takes the pixelData byte array, width and height dimensions in pixels and the <see cref="ImagePixelFormat"/>.
        /// </summary>
        /// <param name="pixelData">The pixelData byte array contains all bitmap pixels.</param>
        /// <param name="width">Width in pixels.</param>
        /// <param name="height">Height in pixels.</param>
        /// <param name="colorFormat"><see cref="ImagePixelFormat"/> provides additional information about pixel Encoding.</param>
        public ImageData(byte[] pixelData, int width, int height, ImagePixelFormat colorFormat)
        {
            PixelData = pixelData;

            Width       = width;
            Height      = height;
            PixelFormat = colorFormat;
        }
Пример #9
0
 /// <summary>
 /// Constructor initializes a Texture from a pixelData byte buffer, width and height in pixels and <see cref="ImagePixelFormat"/>.
 /// </summary>
 /// <param name="pixelData">The raw pixelData byte buffer that makes up the texture.</param>
 /// <param name="width">Width in pixels.</param>
 /// <param name="height">Height in pixels.</param>
 /// <param name="colorFormat">Provides additional information about pixel encoding.</param>
 /// <param name="generateMipMaps">Defines if mipmaps are created.</param>
 /// <param name="filterMode">Defines the filter mode <see cref="TextureFilterMode"/>.</param>
 /// <param name="wrapMode">Defines the wrapping mode <see cref="TextureWrapMode"/>.</param>
 public Texture(byte[] pixelData, int width, int height, ImagePixelFormat colorFormat, bool generateMipMaps = true, TextureFilterMode filterMode = TextureFilterMode.Linear, TextureWrapMode wrapMode = TextureWrapMode.Repeat)
 {
     SessionUniqueIdentifier = Suid.GenerateSuid();
     _imageData        = new ImageData(pixelData, width, height, colorFormat);
     DoGenerateMipMaps = generateMipMaps;
     FilterMode        = filterMode;
     WrapMode          = wrapMode;
 }
Пример #10
0
        /// <summary>
        /// Creates a new instance of type ImageData.
        /// </summary>
        /// <param name="format">The <see cref="ColorFormat"/>.</param>
        /// <param name="width">Width in px.</param>
        /// <param name="height">Height in px.</param>
        public ImageData(ColorFormat format, int width = 2048, int height = 2048)
        {
            Width  = width;
            Height = height;

            PixelFormat = new ImagePixelFormat(format);

            int byteSize = Width * Height * PixelFormat.BytesPerPixel;

            PixelData = new byte[byteSize];
        }
Пример #11
0
        public ScanLineEnumerator(byte[] pixelData, ImagePixelFormat pixelFormat, int xSrc, int ySrc, int width, int height, int maxWidth)
        {
            _pixelData   = pixelData;
            _pixelFormat = pixelFormat;
            _xSrc        = xSrc;
            _ySrc        = ySrc;
            _width       = width;
            _height      = height;
            _maxWidth    = maxWidth;

            _currentPosition = _ySrc - 1; // decreased by one for initial call of MoveNext
        }
Пример #12
0
        /// <summary>
        /// Converts this video frame to the <see cref="ImageData"/> with the specified pixel format.
        /// </summary>
        /// <param name="converter">A <see cref="ImageConverter"/> object, used for caching the FFMpeg <see cref="SwsContext"/> when converting many frames of the same video.</param>
        /// <param name="targetFormat">The output bitmap pixel format.</param>
        /// /// <param name="targetSize">The output bitmap size.</param>
        /// <returns>A <see cref="ImageData"/> instance containing converted bitmap data.</returns>
        public ImageData ToBitmap(ImageConverter converter, ImagePixelFormat targetFormat, Size targetSize)
        {
            var bitmap = ImageData.CreatePooled(targetSize, targetFormat); // Rents memory for the output bitmap.

            fixed(byte *ptr = bitmap.Data)
            {
                // Converts the raw video frame using the given size and pixel format and writes it to the ImageData bitmap.
                converter.AVFrameToBitmap(this, ptr, bitmap.Stride);
            }

            return(bitmap);
        }
Пример #13
0
 /// <summary>
 /// Creates a new instance of type "WritableTexture".
 /// </summary>
 /// <param name="texType">Defines the type of the render texture.</param>
 /// <param name="colorFormat">The color format of the texture, <see cref="ImagePixelFormat"/></param>
 /// <param name="width">Width in px.</param>
 /// <param name="height">Height in px.</param>
 /// <param name="generateMipMaps">Defines if mipmaps are created.</param>
 /// <param name="filterMode">Defines the filter mode <see cref="TextureFilterMode"/>.</param>
 /// <param name="wrapMode">Defines the wrapping mode <see cref="TextureWrapMode"/>.</param>
 /// <param name="compareMode">The textures compare mode. If uncertain, leaf on NONE, this is only important for depth (shadow) textures (<see cref="TextureCompareMode"/>).</param>
 /// <param name="compareFunc">The textures compare function. If uncertain, leaf on LEESS, this is only important for depth (shadow) textures and if the CompareMode isn't NONE (<see cref="Compare"/>)</param>
 public WritableTexture(RenderTargetTextureTypes texType, ImagePixelFormat colorFormat, int width, int height, bool generateMipMaps = true, TextureFilterMode filterMode = TextureFilterMode.Linear, TextureWrapMode wrapMode = TextureWrapMode.Repeat, TextureCompareMode compareMode = TextureCompareMode.None, Compare compareFunc = Compare.Less)
 {
     SessionUniqueIdentifier = Suid.GenerateSuid();
     PixelFormat             = colorFormat;
     Width             = width;
     Height            = height;
     DoGenerateMipMaps = generateMipMaps;
     FilterMode        = filterMode;
     WrapMode          = wrapMode;
     TextureType       = texType;
     CompareMode       = compareMode;
     CompareFunc       = compareFunc;
 }
Пример #14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ImageData"/> struct using a <see cref="Span{T}"/> as the data source.
        /// </summary>
        /// <param name="data">The bitmap data.</param>
        /// <param name="pixelFormat">The pixel format.</param>
        /// <param name="imageSize">The image dimensions.</param>
        /// <exception cref="ArgumentException">When data span size doesn't match size calculated from width, height and the pixel format.</exception>
        public ImageData(Span <byte> data, ImagePixelFormat pixelFormat, Size imageSize)
        {
            var size = EstimateStride(imageSize.Width, pixelFormat) * imageSize.Height;

            if (data.Length != size)
            {
                throw new ArgumentException("Pixel buffer size doesn't match size required by this image format.");
            }

            span         = data;
            pooledMemory = null;

            ImageSize   = imageSize;
            PixelFormat = pixelFormat;
        }
Пример #15
0
        public static Image ToImage(this Color[,] map, ImagePixelFormat format = ImagePixelFormat.RGBA32)
        {
            var H      = map.GetLength(0);
            var W      = map.GetLength(1);
            var result = Image.Of(W, H);

            for (int h = 0; h < H; h++)
            {
                for (int w = 0; w < W; w++)
                {
                    result.SetPixel(w, h, map[h, w]);
                }
            }

            return(result);
        }
Пример #16
0
        /// <summary>
        /// Creates a new instance of type ImageData.
        /// </summary>
        /// <param name="width">Width in px.</param>
        /// <param name="height">Height in px.</param>
        public ImageData(int width = 2048, int height = 2048)
        {
            Width  = width;
            Height = height;

            PixelFormat = new ImagePixelFormat(ColorFormat.RGBA);

            int byteSize = Width * Height * PixelFormat.BytesPerPixel;

            PixelData = new byte[byteSize]; //4k = 67108864

            for (int i = 0; i < byteSize; i += PixelFormat.BytesPerPixel)
            {
                PixelData[i]     = 255;
                PixelData[i + 1] = 127;
                PixelData[i + 2] = 127;
                PixelData[i + 3] = 255;
            }
        }
Пример #17
0
        internal static NativeMethods.ImagePixelType ToImagePixelType(this ImagePixelFormat type)
        {
            switch (type)
            {
            case ImagePixelFormat.Bgr:
                return(NativeMethods.ImagePixelType.Bgr);

            case ImagePixelFormat.Bgra:
                return(NativeMethods.ImagePixelType.Bgra);

            case ImagePixelFormat.Rgb:
                return(NativeMethods.ImagePixelType.Rgb);

            case ImagePixelFormat.Rgba:
                return(NativeMethods.ImagePixelType.Rgba);

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Пример #18
0
        private static int GetBytesPerPixel(ImagePixelFormat format)
        {
            switch (format)
            {
            case ImagePixelFormat.Bgr24:
                return(3);

            case ImagePixelFormat.Bgra32:
                return(4);

            case ImagePixelFormat.Rgb24:
                return(3);

            case ImagePixelFormat.Argb32:
                return(4);

            default:
                return(0);
            }
        }
Пример #19
0
        internal static System.Drawing.Imaging.PixelFormat xlat(ImagePixelFormat pf)
        {
            switch (pf)
            {
            case ImagePixelFormat.BPP1Indexed: return(System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

            case ImagePixelFormat.BPP4Indexed: return(System.Drawing.Imaging.PixelFormat.Format4bppIndexed);

            case ImagePixelFormat.BPP8Indexed: return(System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

            case ImagePixelFormat.BPP16Gray:   return(System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

            case ImagePixelFormat.RGB24:       return(System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            case ImagePixelFormat.RGB32:       return(System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            case ImagePixelFormat.RGBA32:      return(System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            default: return(System.Drawing.Imaging.PixelFormat.Canonical);
            }
        }
Пример #20
0
        public static Bitmap GetBitmap(int width, int height, ImagePixelFormat format, byte[] data)
        {
            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            if (!pixelDecoders.ContainsKey(format))
            {
                throw new NotImplementedException(string.Format("Unimplemented format {0}", format));
            }

            using (BinaryReader reader = new BinaryReader(new MemoryStream(data)))
            {
                if (format == ImagePixelFormat.ETC1 || format == ImagePixelFormat.ETC1A4)
                {
                    throw new NotImplementedException("FIXME: ETC1/ETC1A4 not implemented");
                }
                else
                {
                    BitmapData bmpData   = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                    byte[]     pixelData = new byte[bmpData.Height * bmpData.Stride];
                    Marshal.Copy(bmpData.Scan0, pixelData, 0, pixelData.Length);

                    for (int y = 0; y < bitmap.Height; y += 8)
                    {
                        for (int x = 0; x < bitmap.Width; x += 8)
                        {
                            for (int t = 0; t < tileOrder.Length; t++)
                            {
                                pixelDecoders[format](reader, pixelData, (int)(((((tileOrder[t] / 8) + y) * bitmap.Width) + ((tileOrder[t] % 8) + x)) * 4));
                            }
                        }
                    }

                    Marshal.Copy(pixelData, 0, bmpData.Scan0, pixelData.Length);
                    bitmap.UnlockBits(bmpData);
                }
            }

            return(bitmap);
        }
Пример #21
0
        /// <summary>
        /// Creates a new instance of the <see cref="ImageData"/> class using a pointer to the unmanaged memory as the data source.
        /// </summary>
        /// <param name="pointer">The byte array containing bitmap data.</param>
        /// <param name="pixelFormat">The bitmap pixel format.</param>
        /// <param name="imageSize">The image dimensions.</param>
        /// <returns>A new <see cref="ImageData"/> instance.</returns>
        public static ImageData FromPointer(IntPtr pointer, ImagePixelFormat pixelFormat, Size imageSize)
        {
            var span = CreateSpan(pointer, imageSize, pixelFormat);

            return(new ImageData(span, pixelFormat, imageSize));
        }
Пример #22
0
 /// <summary>Creates a new image instance of the specified properties</summary>
 public static Image Of(Size size, Size resolution, ImagePixelFormat format) => new Image(PlatformAbstractionLayer.Graphics.CreateImage(size, resolution, format));
Пример #23
0
 /// <summary>
 /// Creates a new instance of the <see cref="ImageData"/> class using a byte array as the data source.
 /// </summary>
 /// <param name="pixels">The byte array containing bitmap data.</param>
 /// <param name="pixelFormat">The bitmap pixel format.</param>
 /// <param name="width">The image width.</param>
 /// <param name="height">The image height.</param>
 /// <returns>A new <see cref="ImageData"/> instance.</returns>
 public static ImageData FromArray(byte[] pixels, ImagePixelFormat pixelFormat, int width, int height)
 => FromArray(pixels, pixelFormat, new Size(width, height));
Пример #24
0
 /// <summary>Creates a new image instance of the specified properties</summary>
 public static Image Of(int width, int height, int xDPI, int yDPI, ImagePixelFormat pixFormat) => new Image(PlatformAbstractionLayer.Graphics.CreateImage(
                                                                                                                new Size(width, height),
                                                                                                                new Size(xDPI, yDPI),
                                                                                                                pixFormat));
Пример #25
0
 /// <summary>Creates a new image instance of the specified properties</summary>
 public static Image Of(Size size, ImagePixelFormat pixFormat) => new Image(PlatformAbstractionLayer.Graphics.CreateImage(
                                                                                size,
                                                                                new Size(DEFAULT_RESOLUTION_PPI, DEFAULT_RESOLUTION_PPI),
                                                                                pixFormat));
Пример #26
0
 /// <summary>Creates a new image instance of the specified properties</summary>
 public static Image Of(int width, int height, ImagePixelFormat pixFormat) => new Image(PlatformAbstractionLayer.Graphics.CreateImage(
                                                                                            new Size(width, height),
                                                                                            new Size(DEFAULT_RESOLUTION_PPI, DEFAULT_RESOLUTION_PPI),
                                                                                            pixFormat));
Пример #27
0
 /// <summary>
 /// Creates a new instance of the <see cref="ImageData"/> class using a pointer to the unmanaged memory as the data source.
 /// </summary>
 /// <param name="pointer">The byte array containing bitmap data.</param>
 /// <param name="pixelFormat">The bitmap pixel format.</param>
 /// <param name="width">The image width.</param>
 /// <param name="height">The image height.</param>
 /// <returns>A new <see cref="ImageData"/> instance.</returns>
 public static ImageData FromPointer(IntPtr pointer, ImagePixelFormat pixelFormat, int width, int height)
 => FromPointer(pointer, pixelFormat, new Size(width, height));
Пример #28
0
 /// <summary>
 /// Gets the estimated image line size based on the pixel format and width.
 /// </summary>
 /// <param name="width">The image width.</param>
 /// <param name="format">The image pixel format.</param>
 /// <returns>The size of a single line of the image measured in bytes.</returns>
 public static int EstimateStride(int width, ImagePixelFormat format) => (int)(GetBytesPerPixel(format) * width);
Пример #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ImageData"/> struct using a <see cref="Span{T}"/> as the data source.
 /// </summary>
 /// <param name="data">The bitmap data.</param>
 /// <param name="pixelFormat">The pixel format.</param>
 /// <param name="width">The image width.</param>
 /// <param name="height">The image height.</param>
 /// <exception cref="ArgumentException">When data span size doesn't match size calculated from width, height and the pixel format.</exception>
 public ImageData(Span <byte> data, ImagePixelFormat pixelFormat, int width, int height)
     : this(data, pixelFormat, new Size(width, height))
 {
 }
Пример #30
0
        private static unsafe Span <byte> CreateSpan(IntPtr pointer, Size imageSize, ImagePixelFormat pixelFormat)
        {
            var size = EstimateStride(imageSize.Width, pixelFormat) * imageSize.Height;

            return(new Span <byte>((void *)pointer, size));
        }