Esempio n. 1
0
        protected internal mxNumericArray(byte[] values, int m, int n, mxNumericType numType)
        {
            SafeArrayPtr pa = matrix.mxCreateNumericArray(2, new int[] { m, n }, numType, mxComplexity.mxREAL);

            CreateArray(pa, mxArrayType.Numeric);

            this.NumericType = numType;
            IntPtr ptr = matrix.mxGetData(this.NativeObject);

            Marshal.Copy(values, 0, ptr, values.Length);
        }
Esempio n. 2
0
        protected internal mxNumericArray(byte[] realBytes, byte[] imgBytes, int m, int n, mxNumericType numType)
        {
            SafeArrayPtr pa = matrix.mxCreateNumericArray(2, new int[] { m, n }, numType, mxComplexity.mxCOMPLEX);

            CreateArray(pa, mxArrayType.Numeric);

            this.NumericType = numType;
            IntPtr realPtr = matrix.mxGetData(this.NativeObject);
            IntPtr imgPtr  = matrix.mxGetImagData(this.NativeObject);

            Marshal.Copy(realBytes, 0, realPtr, realBytes.Length);
            Marshal.Copy(imgBytes, 0, imgPtr, imgBytes.Length);
        }
Esempio n. 3
0
        internal mxNumericArray(SafeArrayPtr pa) : base(pa, mxArrayType.Numeric)
        {
            CheckActive();
            mxClassID clsId = matrix.mxGetClassID(this.NativeObject);

            int clsIdInt = (int)clsId;

            // isNumeric
            if (clsIdInt >= 6 && clsIdInt <= 15)
            {
                this.NumericType = (mxNumericType)clsIdInt;
            }
            else
            {
                throw new ArgumentException("The argument is not a numeric.");
            }
        }
Esempio n. 4
0
        public static Array ToArray(byte[] buffer, int m, int n, mxNumericType numType)
        {
            if (m < 1 || n < 1)
            {
                throw new ArgumentOutOfRangeException("The dimension must be larger than 1.");
            }

            switch (numType)
            {
            case mxNumericType.UINT8: {
                if (buffer.Length != m * n * byte_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                byte[,] values = new byte[m, n];

                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        values[r, c] = buffer[c * m + r];
                    }
                }

                return(values);
            }

            case mxNumericType.INT8: {
                if (buffer.Length != m * n * byte_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                sbyte[,] values = new sbyte[m, n];

                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        values[r, c] = (sbyte)buffer[c * m + r];
                    }
                }

                return(values);
            }

            case mxNumericType.INT16: {
                if (buffer.Length != m * n * short_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                short[,] values = new short[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * short_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * short_size, short_size);
                    }
                }

                return(values);
            }

            case mxNumericType.UINT16: {
                if (buffer.Length != m * n * ushort_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                ushort[,] values = new ushort[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * ushort_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * ushort_size, ushort_size);
                    }
                }

                return(values);
            }

            case mxNumericType.INT32: {
                if (buffer.Length != m * n * int_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                int[,] values = new int[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * int_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * int_size, int_size);
                    }
                }

                return(values);
            }

            case mxNumericType.UINT32: {
                if (buffer.Length != m * n * uint_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                uint[,] values = new uint[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * uint_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * uint_size, uint_size);
                    }
                }

                return(values);
            }

            case mxNumericType.INT64: {
                if (buffer.Length != m * n * long_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                long[,] values = new long[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * long_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * long_size, long_size);
                    }
                }

                return(values);
            }

            case mxNumericType.UINT64: {
                if (buffer.Length != m * n * ulong_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                ulong[,] values = new ulong[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * ulong_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * ulong_size, ulong_size);
                    }
                }

                return(values);
            }

            case mxNumericType.SINGLE: {
                if (buffer.Length != m * n * float_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                float[,] values = new float[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * float_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * float_size, float_size);
                    }
                }

                return(values);
            }

            case mxNumericType.DOUBLE: {
                if (buffer.Length != m * n * double_size)
                {
                    throw new ArgumentException("The buffer length is not match with m*n.");
                }

                double[,] values = new double[m, n];
                int offset;
                for (int r = 0; r < m; r++)
                {
                    for (int c = 0; c < n; c++)
                    {
                        offset = (c * m + r) * double_size;
                        Buffer.BlockCopy(buffer, offset, values, (r * n + c) * double_size, double_size);
                    }
                }

                return(values);
            }

            default:
                throw new ArgumentException();
            }
        }
Esempio n. 5
0
 public extern static SafeArrayPtr mxCreateNumericArray(int ndim, int[] dims, mxNumericType classId, mxComplexity complexFlag);
Esempio n. 6
0
 public extern static SafeArrayPtr mxCreateNumericMatrix(int m, int n, mxNumericType classId, mxComplexity complexFlag);
Esempio n. 7
0
 protected internal mxNumericArray(byte[] realBytes, byte[] imgBytes, mxNumericType numType) :
     this(realBytes, imgBytes, realBytes.Length, numType)
 {
 }
Esempio n. 8
0
 protected internal mxNumericArray(byte[] bytes, mxNumericType numType)
     : this(bytes, bytes.Length, numType)
 {
 }
Esempio n. 9
0
        public static Array ToArray(byte[] buffer, int m, int n, mxNumericType numType)
        {
            if (m < 1 || n < 1)
                throw new ArgumentOutOfRangeException("The dimension must be larger than 1.");

            switch (numType) {
                case mxNumericType.UINT8:{
                        if (buffer.Length != m * n * byte_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        byte[,] values = new byte[m, n];

                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                values[r, c] = buffer[c * m + r];
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT8: {
                        if (buffer.Length != m * n * byte_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        sbyte[,] values = new sbyte[m, n];

                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                values[r, c] = (sbyte)buffer[c * m + r];
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT16: {
                        if (buffer.Length != m * n * short_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        short[,] values = new short[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * short_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * short_size, short_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.UINT16: {
                        if (buffer.Length != m * n * ushort_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        ushort[,] values = new ushort[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * ushort_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * ushort_size, ushort_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT32: {
                        if (buffer.Length != m * n * int_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        int[,] values = new int[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * int_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * int_size, int_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.UINT32:{
                        if (buffer.Length != m * n * uint_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        uint[,] values = new uint[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * uint_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * uint_size, uint_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT64: {
                        if (buffer.Length != m * n * long_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        long[,] values = new long[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * long_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * long_size, long_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.UINT64: {
                        if (buffer.Length != m * n * ulong_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        ulong[,] values = new ulong[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * ulong_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * ulong_size, ulong_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.SINGLE: {
                        if (buffer.Length != m * n * float_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        float[,] values = new float[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * float_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * float_size, float_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.DOUBLE: {
                        if (buffer.Length != m * n * double_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        double[,] values = new double[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * double_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * double_size, double_size);
                            }
                        }

                        return values;
                    }
                default:
                    throw new ArgumentException();
            }
        }
Esempio n. 10
0
 public static extern SafeArrayPtr mxCreateNumericMatrix(int m, int n, mxNumericType classId, mxComplexity complexFlag);
Esempio n. 11
0
 public static extern SafeArrayPtr mxCreateNumericArray(int ndim, int[] dims, mxNumericType classId, mxComplexity complexFlag);