internal MatrixRangeExp(IntPtr ptr) { Matrix <T> .TryParse <T>(out var type); this._NativeMatrixElementType = type.ToNativeMatrixElementType(); this._MatrixElementType = type; this.NativePtr = ptr; }
internal MatrixOp(Dlib.Native.ElementType elementType, MatrixElementTypes type, IntPtr ptr, int templateRows = 0, int temlateColumns = 0) { this._ElementType = elementType; this._MatrixElementType = type.ToNativeMatrixElementType(); this.NativePtr = ptr; this.TemplateRows = templateRows; this.TemplateColumns = temlateColumns; }
internal Matrix(IntPtr ptr, MatrixElementTypes type) { if (ptr == IntPtr.Zero) { throw new ArgumentException("Can not pass IntPtr.Zero", nameof(ptr)); } this.NativePtr = ptr; this._MatrixElementTypes = type; }
private static void ToManaged(MatrixElementTypes type, IntPtr src, WriteableBitmap bitmap, bool rgbReverse, int channels) { var width = bitmap.PixelWidth; var height = bitmap.PixelHeight; var buffer = bitmap.BackBuffer; var stride = bitmap.BackBufferStride; var srcType = type.ToNativeMatrixElementType(); NativeMethods.extensions_convert_matrix_to_managed_image(srcType, src, buffer, rgbReverse, (uint)height, (uint)width, (uint)stride, (uint)channels); }
public StdVectorMatrixImp(int templateRows, int templateColumns) { Matrix <TElement> .TryParse <TElement>(out var type); this._Type = type; this._NativeType = this._Type.ToNativeMatrixElementType(); this._TemplateRows = templateRows; this._TemplateColumns = templateColumns; }
public MatrixRangeExp(T start, T end, int num) { Matrix <T> .TryParse <T>(out var type); this._Bridge = CreateBridge(type); this._NativeMatrixElementType = type.ToNativeMatrixElementType(); this._MatrixElementType = type; this.NativePtr = this._Bridge.Create3(start, end, num); }
private object GetMatrixItem(MatrixElementTypes type, uint index) { var err = NativeMethods.array_matrix_getitem(this._MatrixElementType.ToNativeMatrixElementType(), this.NativePtr, index, out var mat); var templateRow = 0; var templateColumn = 0; switch (type) { case MatrixElementTypes.UInt8: return(new Matrix <byte>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.UInt16: return(new Matrix <ushort>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.UInt32: return(new Matrix <uint>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.Int8: return(new Matrix <sbyte>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.Int16: return(new Matrix <short>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.Int32: return(new Matrix <int>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.Float: return(new Matrix <float>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.Double: return(new Matrix <double>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.RgbPixel: return(new Matrix <RgbPixel>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.BgrPixel: return(new Matrix <BgrPixel>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.RgbAlphaPixel: return(new Matrix <RgbAlphaPixel>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.HsiPixel: return(new Matrix <HsiPixel>(mat, templateRow, templateColumn, false)); case MatrixElementTypes.LabPixel: return(new Matrix <LabPixel>(mat, templateRow, templateColumn, false)); default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
public Matrix(MatrixTemplateSizeParameter parameter, TElement[] array) : base(parameter.TemplateRows, parameter.TemplateColumns) { if (!TryParse(typeof(TElement), out var type)) throw new NotSupportedException($"{typeof(TElement).Name} does not support"); if (parameter == null) throw new ArgumentNullException(nameof(parameter)); if (array == null) throw new ArgumentNullException(nameof(array)); if (parameter.TemplateRows < 0) throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateRows)}", $"{nameof(parameter.TemplateRows)} should be positive value."); if (parameter.TemplateColumns < 0) throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateColumns)}", $"{nameof(parameter.TemplateColumns)} should be positive value."); var templateRows = parameter.TemplateRows; var templateColumns = parameter.TemplateColumns; if (templateRows * templateColumns != 0) { if (templateRows * templateColumns != array.Length) throw new ArgumentOutOfRangeException(); } else if (templateRows != 0) { if (array.Length % templateRows != 0) throw new ArgumentOutOfRangeException(); } else if (templateColumns != 0) { if (array.Length % templateColumns != 0) throw new ArgumentOutOfRangeException(); } using (var vector = new StdVector<TElement>(array)) { var error = NativeMethods.matrix_new5(type.ToNativeMatrixElementType(), (uint)templateRows, (uint)templateColumns, vector.NativePtr, out var ret); switch (error) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"Input {type} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(templateRows)} or {nameof(templateRows)} is not supported."); } this.NativePtr = ret; this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this._Indexer = this.CreateIndexer(type); } }
public Matrix() { if (!SupportTypes.TryGetValue(typeof(T), out var type)) { throw new NotSupportedException($"{typeof(T).Name} does not support"); } this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this.NativePtr = Dlib.Native.matrix_new(this._ElementType); }
public Matrix() { if (!TryParse(typeof(TElement), out var type)) throw new NotSupportedException($"{typeof(TElement).Name} does not support"); this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this.NativePtr = NativeMethods.matrix_new(this._ElementType); this._Indexer = this.CreateIndexer(type); }
internal MatrixRangeExp(IntPtr ptr) { if (!SupportTypes.TryGetValue(typeof(T), out var type)) { throw new NotSupportedException($"{typeof(T).Name} does not support"); } this._NativeMatrixElementType = type.ToNativeMatrixElementType(); this._MatrixElementType = type; this.NativePtr = ptr; }
internal static NativeMethods.MatrixElementType ToNativeMatrixElementType(this MatrixElementTypes matrixElementTypes) { switch (matrixElementTypes) { case MatrixElementTypes.UInt8: return(NativeMethods.MatrixElementType.UInt8); case MatrixElementTypes.UInt16: return(NativeMethods.MatrixElementType.UInt16); case MatrixElementTypes.UInt32: return(NativeMethods.MatrixElementType.UInt32); case MatrixElementTypes.UInt64: return(NativeMethods.MatrixElementType.UInt64); case MatrixElementTypes.Int8: return(NativeMethods.MatrixElementType.Int8); case MatrixElementTypes.Int16: return(NativeMethods.MatrixElementType.Int16); case MatrixElementTypes.Int32: return(NativeMethods.MatrixElementType.Int32); case MatrixElementTypes.Int64: return(NativeMethods.MatrixElementType.Int64); case MatrixElementTypes.Float: return(NativeMethods.MatrixElementType.Float); case MatrixElementTypes.Double: return(NativeMethods.MatrixElementType.Double); case MatrixElementTypes.RgbPixel: return(NativeMethods.MatrixElementType.RgbPixel); case MatrixElementTypes.BgrPixel: return(NativeMethods.MatrixElementType.BgrPixel); case MatrixElementTypes.RgbAlphaPixel: return(NativeMethods.MatrixElementType.RgbAlphaPixel); case MatrixElementTypes.HsiPixel: return(NativeMethods.MatrixElementType.HsiPixel); case MatrixElementTypes.LabPixel: return(NativeMethods.MatrixElementType.LabPixel); default: throw new ArgumentOutOfRangeException(nameof(matrixElementTypes), matrixElementTypes, null); } }
private Imp <TElement> CreateImp(MatrixElementTypes type) { switch (type) { case MatrixElementTypes.UInt8: return(new MatrixUInt8Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.Int8: return(new MatrixInt8Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.UInt16: return(new MatrixUInt16Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.Int16: return(new MatrixInt16Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.UInt32: return(new MatrixUInt32Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.Int32: return(new MatrixInt32Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.UInt64: return(new MatrixUInt64Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.Int64: return(new MatrixInt64Imp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.Double: return(new MatrixDoubleImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.Float: return(new MatrixFloatImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.RgbPixel: return(new MatrixRgbPixelImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.BgrPixel: return(new MatrixBgrPixelImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.HsiPixel: return(new MatrixHsiPixelImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.LabPixel: return(new MatrixLabPixelImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); case MatrixElementTypes.RgbAlphaPixel: return(new MatrixRgbAlphaPixelImp(this, type.ToNativeMatrixElementType()) as Imp <TElement>); default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } }
private Indexer <TElement> CreateIndexer(MatrixElementTypes types) { switch (types) { case MatrixElementTypes.UInt8: return(new IndexerUInt8(this) as Indexer <TElement>); case MatrixElementTypes.UInt16: return(new IndexerUInt16(this) as Indexer <TElement>); case MatrixElementTypes.UInt32: return(new IndexerUInt32(this) as Indexer <TElement>); case MatrixElementTypes.UInt64: return(new IndexerUInt64(this) as Indexer <TElement>); case MatrixElementTypes.Int8: return(new IndexerInt8(this) as Indexer <TElement>); case MatrixElementTypes.Int16: return(new IndexerInt16(this) as Indexer <TElement>); case MatrixElementTypes.Int32: return(new IndexerInt32(this) as Indexer <TElement>); case MatrixElementTypes.Int64: return(new IndexerInt64(this) as Indexer <TElement>); case MatrixElementTypes.Float: return(new IndexerFloat(this) as Indexer <TElement>); case MatrixElementTypes.Double: return(new IndexerDouble(this) as Indexer <TElement>); case MatrixElementTypes.RgbPixel: return(new IndexerRgbPixel(this) as Indexer <TElement>); case MatrixElementTypes.BgrPixel: return(new IndexerBgrPixel(this) as Indexer <TElement>); case MatrixElementTypes.RgbAlphaPixel: return(new IndexerRgbAlphaPixel(this) as Indexer <TElement>); case MatrixElementTypes.HsiPixel: return(new IndexerHsiPixel(this) as Indexer <TElement>); case MatrixElementTypes.LabPixel: return(new IndexerLabPixel(this) as Indexer <TElement>); default: throw new ArgumentOutOfRangeException(nameof(types), types, null); } }
public LinearKernel(int templateRow = 0, int templateColumn = 0) { using (var tmp = new T()) { this._MatrixElementTypes = tmp.MatrixElementType; this._ElementType = this._MatrixElementTypes.ToNativeMatrixElementType(); this.TemplateRows = templateRow; this.TemplateColumns = templateColumn; this.NativePtr = NativeMethods.linear_kernel_new(this._ElementType, templateRow, templateColumn); } }
internal Matrix(IntPtr ptr, MatrixElementTypes type) { if (ptr == IntPtr.Zero) { throw new ArgumentException("Can not pass IntPtr.Zero", nameof(ptr)); } this.NativePtr = ptr; this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this._Indexer = this.CreateIndexer(type); }
public Matrix(IntPtr array, int row, int column, int stride) { if (array == IntPtr.Zero) { throw new ArgumentException($"{nameof(array)} must not be {nameof(IntPtr)}.{nameof(IntPtr.Zero)}", nameof(array)); } if (row < 0) { throw new ArgumentOutOfRangeException($"{nameof(row)}", $"{nameof(row)} should be positive value."); } if (column < 0) { throw new ArgumentOutOfRangeException($"{nameof(column)}", $"{nameof(column)} should be positive value."); } if (column < 0) { throw new ArgumentOutOfRangeException($"{nameof(stride)} should be positive value."); } if (!TryParse(typeof(TElement), out var type)) { throw new NotSupportedException($"{typeof(TElement).Name} does not support"); } this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); unsafe { switch (this._ElementType) { case NativeMethods.MatrixElementType.UInt8: case NativeMethods.MatrixElementType.UInt16: case NativeMethods.MatrixElementType.UInt32: case NativeMethods.MatrixElementType.Int8: case NativeMethods.MatrixElementType.Int16: case NativeMethods.MatrixElementType.Int32: case NativeMethods.MatrixElementType.Float: case NativeMethods.MatrixElementType.Double: case NativeMethods.MatrixElementType.RgbPixel: case NativeMethods.MatrixElementType.BgrPixel: case NativeMethods.MatrixElementType.RgbAlphaPixel: case NativeMethods.MatrixElementType.HsiPixel: case NativeMethods.MatrixElementType.LabPixel: this.NativePtr = NativeMethods.matrix_new6(this._ElementType, row, column, stride, array); break; } } this._Indexer = this.CreateIndexer(type); }
public MatrixRangeExp(T start, T end, uint num) { if (!SupportTypes.TryGetValue(typeof(T), out var type)) { throw new NotSupportedException($"{typeof(T).Name} does not support"); } this._NativeMatrixElementType = type.ToNativeMatrixElementType(); this._MatrixElementType = type; var param = this.CreataParam(start, end, false, default(T), true, num); this.NativePtr = Dlib.Native.matrix_range_exp_create(this._NativeMatrixElementType, ref param); }
public VectorOfMatrix(int size) { if (size < 0) { throw new ArgumentOutOfRangeException(nameof(size)); } using (var matrix = new Matrix <T>()) { this._Type = matrix.MatrixElementType; var type = this._Type.ToNativeMatrixElementType(); this.NativePtr = Dlib.Native.vector_matrix_new2(type, new IntPtr(size)); } }
public Matrix(int row, int column) { if (!TryParse(typeof(TElement), out var type)) throw new NotSupportedException($"{typeof(TElement).Name} does not support"); if (row < 0) throw new ArgumentOutOfRangeException($"{nameof(row)}", $"{nameof(row)} should be positive value."); if (column < 0) throw new ArgumentOutOfRangeException($"{nameof(column)}", $"{nameof(column)} should be positive value."); this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this.NativePtr = NativeMethods.matrix_new1(this._ElementType, row, column); this._Indexer = this.CreateIndexer(type); }
public VectorOfMatrix(IEnumerable <MatrixBase> data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } using (var matrix = new Matrix <T>()) { this._Type = matrix.MatrixElementType; var array = data.Select(rectangle => rectangle.NativePtr).ToArray(); var type = this._Type.ToNativeMatrixElementType(); this.NativePtr = Dlib.Native.vector_matrix_new3(type, array, new IntPtr(array.Length)); } }
internal Matrix(IntPtr ptr, int templateRows = 0, int templateColumns = 0, bool isEnabledDispose = true) : base(templateRows, templateColumns, isEnabledDispose) { if (ptr == IntPtr.Zero) throw new ArgumentException("Can not pass IntPtr.Zero", nameof(ptr)); if (!TryParse(typeof(TElement), out var type)) throw new NotSupportedException($"{typeof(TElement).Name} does not support"); this.NativePtr = ptr; this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this._Indexer = this.CreateIndexer(type); }
public Matrix(byte[] array, int row, int column, int elementSize) { if (array == null) { throw new ArgumentNullException(nameof(array)); } if (row < 0) { throw new ArgumentOutOfRangeException($"{nameof(row)}", $"{nameof(row)} should be positive value."); } if (column < 0) { throw new ArgumentOutOfRangeException($"{nameof(column)}", $"{nameof(column)} should be positive value."); } if (elementSize < 1) { throw new ArgumentOutOfRangeException($"{nameof(elementSize)} should be greater than or equal to 1."); } if (array.Length != row * column * elementSize) { throw new ArgumentOutOfRangeException($"{nameof(array)}.Length should equal to {nameof(row)} x {nameof(column)} x {nameof(elementSize)}."); } if (!TryParse(typeof(TElement), out var type)) { throw new NotSupportedException($"{typeof(TElement).Name} does not support"); } this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); var size = ElementSizeDictionary[this._ElementType]; if (size != elementSize) { throw new ArgumentOutOfRangeException($"The size of {typeof(TElement).Name} does not equalt to {nameof(elementSize)}."); } unsafe { fixed(byte *src = &array[0]) this.NativePtr = NativeMethods.matrix_new3(this._ElementType, row, column, src); } this._Indexer = this.CreateIndexer(type); }
internal static ImageTypes ToImageTypes(this MatrixElementTypes matrixElementTypes) { switch (matrixElementTypes) { case MatrixElementTypes.UInt8: return(ImageTypes.UInt8); case MatrixElementTypes.UInt16: return(ImageTypes.UInt16); //case MatrixElementTypes.UInt32: // return ImageTypes.UInt32; //case MatrixElementTypes.Int8: // return ImageTypes.Int8; //case MatrixElementTypes.Int16: // return ImageTypes.Int16; case MatrixElementTypes.Int32: return(ImageTypes.Int32); case MatrixElementTypes.Float: return(ImageTypes.Float); case MatrixElementTypes.Double: return(ImageTypes.Double); case MatrixElementTypes.RgbPixel: return(ImageTypes.RgbPixel); case MatrixElementTypes.BgrPixel: return(ImageTypes.BgrPixel); case MatrixElementTypes.RgbAlphaPixel: return(ImageTypes.RgbAlphaPixel); case MatrixElementTypes.HsiPixel: return(ImageTypes.HsiPixel); case MatrixElementTypes.LabPixel: return(ImageTypes.LabPixel); default: throw new ArgumentOutOfRangeException(nameof(matrixElementTypes), matrixElementTypes, null); } }
public Matrix(int row, int column) { if (!SupportTypes.TryGetValue(typeof(T), out var type)) { throw new NotSupportedException($"{typeof(T).Name} does not support"); } if (row < 0) { throw new ArgumentOutOfRangeException($"{nameof(row)}", $"{nameof(row)} should be positive value."); } if (column < 0) { throw new ArgumentOutOfRangeException($"{nameof(column)}", $"{nameof(column)} should be positive value."); } this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this.NativePtr = Dlib.Native.matrix_new1(this._ElementType, row, column); }
public Matrix(MatrixTemplateSizeParameter parameter) : base(parameter.TemplateRows, parameter.TemplateColumns) { if (!TryParse(typeof(TElement), out var type)) { throw new NotSupportedException($"{typeof(TElement).Name} does not support"); } if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } if (parameter.TemplateRows < 0) { throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateRows)}", $"{nameof(parameter.TemplateRows)} should be positive value."); } if (parameter.TemplateColumns < 0) { throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateColumns)}", $"{nameof(parameter.TemplateColumns)} should be positive value."); } var templateRows = parameter.TemplateRows; var templateColumns = parameter.TemplateColumns; var error = NativeMethods.matrix_new4(type.ToNativeMatrixElementType(), (uint)templateRows, (uint)templateColumns, out var ret); switch (error) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"Input {type} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(templateRows)} or {nameof(templateRows)} is not supported."); } this.NativePtr = ret; this._MatrixElementTypes = type; this._ElementType = type.ToNativeMatrixElementType(); this._Indexer = this.CreateIndexer(type); }
private static void ToNative(WriteableBitmap bitmap, MatrixElementTypes dstType, IntPtr dst, bool rgbReverse, int channels) { var width = bitmap.PixelWidth; var height = bitmap.PixelHeight; var buffer = bitmap.BackBuffer; var stride = bitmap.BackBufferStride; var usePalette = bitmap.Palette != null && bitmap.Palette.Colors.Count == 256; if (!usePalette) { NativeMethods.extensions_convert_managed_image_to_matrix(buffer, dstType.ToNativeMatrixElementType(), dst, rgbReverse, (uint)height, (uint)width, (uint)stride, (uint)channels); } else { var p = bitmap.Palette.Colors.Select(c => new RgbPixel { Blue = c.B, Green = c.G, Red = c.R }).ToArray(); NativeMethods.extensions_convert_managed_image_to_matrix_by_palette(buffer, dstType.ToNativeMatrixElementType(), dst, p, (uint)height, (uint)width, (uint)stride, (uint)channels); } }
internal static Array2DMatrixBase CreateArray2DMatrix(MatrixElementTypes elementType) { switch (elementType) { case MatrixElementTypes.UInt8: return(new Array2DMatrix <byte>()); case MatrixElementTypes.UInt16: return(new Array2DMatrix <ushort>()); case MatrixElementTypes.UInt32: return(new Array2DMatrix <uint>()); case MatrixElementTypes.Int8: return(new Array2DMatrix <sbyte>()); case MatrixElementTypes.Int16: return(new Array2DMatrix <short>()); case MatrixElementTypes.Int32: return(new Array2DMatrix <int>()); case MatrixElementTypes.Float: return(new Array2DMatrix <float>()); case MatrixElementTypes.Double: return(new Array2DMatrix <double>()); case MatrixElementTypes.RgbPixel: return(new Array2DMatrix <RgbPixel>()); case MatrixElementTypes.RgbAlphaPixel: return(new Array2DMatrix <RgbAlphaPixel>()); case MatrixElementTypes.HsiPixel: return(new Array2DMatrix <HsiPixel>()); default: throw new ArgumentOutOfRangeException(nameof(elementType), elementType, null); } }
internal static TwoDimentionObjectBase CreateMatrix(MatrixElementTypes elementTypes, int rows = 0, int columns = 0) { switch (elementTypes) { case MatrixElementTypes.UInt8: return(new Matrix <byte>(rows, columns)); case MatrixElementTypes.UInt16: return(new Matrix <ushort>(rows, columns)); case MatrixElementTypes.UInt32: return(new Matrix <uint>(rows, columns)); case MatrixElementTypes.Int8: return(new Matrix <sbyte>(rows, columns)); case MatrixElementTypes.Int16: return(new Matrix <short>(rows, columns)); case MatrixElementTypes.Int32: return(new Matrix <int>(rows, columns)); case MatrixElementTypes.Float: return(new Matrix <float>(rows, columns)); case MatrixElementTypes.Double: return(new Matrix <double>(rows, columns)); case MatrixElementTypes.RgbPixel: return(new Matrix <RgbPixel>(rows, columns)); case MatrixElementTypes.RgbAlphaPixel: return(new Matrix <RgbAlphaPixel>(rows, columns)); case MatrixElementTypes.HsiPixel: return(new Matrix <HsiPixel>(rows, columns)); default: throw new ArgumentOutOfRangeException(nameof(elementTypes), elementTypes, null); } }
private static void ToNative(Bitmap bitmap, MatrixElementTypes dstType, IntPtr dst, bool rgbReverse, int channels) { var format = bitmap.PixelFormat; var width = bitmap.Width; var height = bitmap.Height; var palette = bitmap.Palette; var usePalette = bitmap.Palette.Entries.Length == 256; BitmapData bitmapData = null; try { bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, format); var scan0 = bitmapData.Scan0; var stride = bitmapData.Stride; if (!usePalette) { NativeMethods.extensions_convert_managed_image_to_matrix(scan0, dstType.ToNativeMatrixElementType(), dst, rgbReverse, (uint)height, (uint)width, (uint)stride, (uint)channels); } else { var p = palette.Entries.Select(c => new RgbPixel { Blue = c.B, Green = c.G, Red = c.R }).ToArray(); NativeMethods.extensions_convert_managed_image_to_matrix_by_palette(scan0, dstType.ToNativeMatrixElementType(), dst, p, (uint)height, (uint)width, (uint)stride, (uint)channels); } } finally { if (bitmapData != null) { bitmap.UnlockBits(bitmapData); } } }