/// <summary> /// Allocate pixel data for this Image. /// </summary> /// <param name="format"> /// A <see cref="PixelLayout"/> indicating the image pixel format. /// </param> /// <param name="w"> /// A <see cref="Int32"/> indicating the image width, in pixels. /// </param> /// <param name="h"> /// A <see cref="Int32"/> indicating the image height, in pixels. /// </param> public void Create(PixelLayout format, uint w, uint h) { switch (format) { // Single plane formats case PixelLayout.R8: case PixelLayout.R16: case PixelLayout.GRAY16S: case PixelLayout.RF: case PixelLayout.RHF: //case PixelLayout.GRAYAF: case PixelLayout.RGB8: case PixelLayout.RGB15: case PixelLayout.RGB16: case PixelLayout.RGB24: case PixelLayout.RGB48: case PixelLayout.RGBF: case PixelLayout.RGBHF: case PixelLayout.RGBD: case PixelLayout.SRGB24: case PixelLayout.SBGR24: //case PixelLayout.RGB30A2: case PixelLayout.RGBA32: case PixelLayout.RGBA64: case PixelLayout.RGBAF: case PixelLayout.RGBAHF: case PixelLayout.BGR8: case PixelLayout.BGR15: case PixelLayout.BGR16: case PixelLayout.BGR24: case PixelLayout.BGR48: case PixelLayout.BGRF: case PixelLayout.BGRHF: //case PixelLayout.BGR30A2: case PixelLayout.BGRA32: case PixelLayout.BGRA64: case PixelLayout.BGRAF: case PixelLayout.BGRAHF: case PixelLayout.CMY24: case PixelLayout.CMYK32: case PixelLayout.CMYK64: case PixelLayout.CMYKA40: case PixelLayout.Depth16: case PixelLayout.Depth24: case PixelLayout.Depth32: case PixelLayout.DepthF: case PixelLayout.Depth24Stencil8: case PixelLayout.Depth32FStencil8: case PixelLayout.Integer1: case PixelLayout.Integer2: case PixelLayout.Integer3: case PixelLayout.Integer4: case PixelLayout.UInteger1: case PixelLayout.UInteger2: case PixelLayout.UInteger3: case PixelLayout.UInteger4: _PixelBuffers = new AlignedMemoryBuffer(w * h * format.GetBytesCount(), 16); // Define planes _PixelPlanes = new IntPtr[] { _PixelBuffers.AlignedBuffer }; break; case PixelLayout.YUYV: case PixelLayout.YYUV: case PixelLayout.YVYU: case PixelLayout.UYVY: case PixelLayout.VYUY: if (((w % 2) != 0) || ((h % 2) != 0)) { throw new InvalidOperationException(String.Format("invalid image extents for pixel format {0}", format)); } // Define planes _PixelBuffers = new AlignedMemoryBuffer(w * h * format.GetBytesCount(), 16); _PixelPlanes = new IntPtr[] { _PixelBuffers.AlignedBuffer }; break; case PixelLayout.YVU410: case PixelLayout.YUV410: if (((w % 16) != 0) || ((h % 16) != 0)) { throw new InvalidOperationException(String.Format("invalid image extents for pixel format {0}", format)); } _PixelBuffers = new AlignedMemoryBuffer(w * h + (w * h / 16) * 2, 16); // Define planes _PixelPlanes = new IntPtr[3]; _PixelPlanes[0] = _PixelBuffers.AlignedBuffer; _PixelPlanes[1] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h); _PixelPlanes[2] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h + (w * h / 16)); break; case PixelLayout.YVU420: case PixelLayout.YUV420: if (((w % 4) != 0) || ((h % 4) != 0)) { throw new InvalidOperationException(String.Format("invalid image extents for pixel format {0}", format)); } _PixelBuffers = new AlignedMemoryBuffer(w * h + (w * h / 4), 16); // Define planes _PixelPlanes = new IntPtr[3]; _PixelPlanes[0] = _PixelBuffers.AlignedBuffer; _PixelPlanes[1] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h); _PixelPlanes[2] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h + (w * h / 4)); break; case PixelLayout.YUV422P: if ((w % 2) != 0) { throw new InvalidOperationException(String.Format("invalid image extents for pixel format {0}", format)); } _PixelBuffers = new AlignedMemoryBuffer(w * h + (w * h / 2), 16); // Define planes _PixelPlanes = new IntPtr[3]; _PixelPlanes[0] = _PixelBuffers.AlignedBuffer; _PixelPlanes[1] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h); _PixelPlanes[2] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h + (w * h / 2)); break; case PixelLayout.YUV411P: if ((w % 4) != 0) { throw new InvalidOperationException(String.Format("invalid image extents for pixel format {0}", format)); } _PixelBuffers = new AlignedMemoryBuffer(w * h + (w * h / 4), 16); // Define planes _PixelPlanes = new IntPtr[3]; _PixelPlanes[0] = _PixelBuffers.AlignedBuffer; _PixelPlanes[1] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h); _PixelPlanes[2] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h + (w * h / 4)); break; case PixelLayout.Y41P: if ((w % 8) != 0) { throw new InvalidOperationException(String.Format("invalid image extents for pixel format {0}", format)); } _PixelBuffers = new AlignedMemoryBuffer(w * h * 12 / 8, 16); // Define planes _PixelPlanes = new IntPtr[3]; _PixelPlanes[0] = _PixelBuffers.AlignedBuffer; _PixelPlanes[1] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h); _PixelPlanes[2] = new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + w * h + (w * h / 4)); break; default: throw new NotSupportedException(String.Format("pixel format {0} is not supported", format)); } // Set image information _ImageInfo.PixelType = format; _ImageInfo.Width = w; _ImageInfo.Height = h; Debug.Assert(_PixelPlanes != null); Debug.Assert(_PixelPlanes.Length != 0); Debug.Assert(Array.TrueForAll(_PixelPlanes, delegate(IntPtr pixelPlane) { return(pixelPlane != IntPtr.Zero); })); }
private IntPtr GetPixelDataOffset(uint w, uint h) { switch (PixelLayout) { // Single plane formats case PixelLayout.R8: case PixelLayout.R16: case PixelLayout.GRAY16S: //case PixelLayout.GRAYF: case PixelLayout.RHF: //case PixelLayout.GRAYAF: case PixelLayout.RGB8: case PixelLayout.RGB15: case PixelLayout.RGB16: case PixelLayout.RGB24: case PixelLayout.RGB48: case PixelLayout.RGBF: case PixelLayout.RGBHF: case PixelLayout.RGBD: case PixelLayout.SRGB24: case PixelLayout.SBGR24: //case PixelLayout.RGB30A2: case PixelLayout.RGBA32: case PixelLayout.RGBA64: case PixelLayout.RGBAF: case PixelLayout.RGBAHF: case PixelLayout.BGR8: case PixelLayout.BGR15: case PixelLayout.BGR16: case PixelLayout.BGR24: case PixelLayout.BGR48: case PixelLayout.BGRF: case PixelLayout.BGRHF: //case PixelLayout.BGR30A2: case PixelLayout.BGRA32: case PixelLayout.BGRA64: case PixelLayout.BGRAF: case PixelLayout.BGRAHF: case PixelLayout.CMY24: case PixelLayout.CMYK32: case PixelLayout.CMYK64: case PixelLayout.CMYKA40: case PixelLayout.Depth16: case PixelLayout.Depth24: case PixelLayout.Depth32: case PixelLayout.DepthF: case PixelLayout.Depth24Stencil8: case PixelLayout.Depth32FStencil8: case PixelLayout.Integer1: case PixelLayout.Integer2: case PixelLayout.Integer3: case PixelLayout.Integer4: case PixelLayout.UInteger1: case PixelLayout.UInteger2: case PixelLayout.UInteger3: case PixelLayout.UInteger4: return(new IntPtr(_PixelBuffers.AlignedBuffer.ToInt64() + (w * PixelLayout.GetBytesCount()) + (h * Width * PixelLayout.GetBytesCount()))); default: throw new NotSupportedException(String.Format("pixel format {0} is not supported", PixelLayout)); } }