Ejemplo n.º 1
0
        /// <summary>
        ///     Copies values from one array to another, broadcasting as necessary.
        /// </summary>
        /// <param name="dst">The array into which values are copied.</param>
        /// <param name="src">The array from which values are copied.</param>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.copyto.html</remarks>
        public static void copyto(NDArray dst, NDArray src) //todo! add where argument
        {
            if (dst == null)
            {
                throw new ArgumentNullException(nameof(dst));
            }

            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }

            //try to perform memory copy
            if (dst.Shape.IsContiguous && src.Shape.IsContiguous && dst.dtype == src.dtype && src.size == dst.size)
            {
                unsafe
                {
                    src.CopyTo(dst.Address);
                    return;
                }
            }

            //perform manual copy with automatic casting
            MultiIterator.Assign(dst.Storage, src.Storage);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Converts <see cref="NDArray"/> to a <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="nd">The <see cref="NDArray"/> to copy pixels from, <see cref="Shape"/> is ignored completely. If nd.Unsafe.Shape.IsContiguous == false then a copy is made.</param>
        /// <param name="width">The height of the <see cref="Bitmap"/></param>
        /// <param name="height">The width of the <see cref="Bitmap"/></param>
        /// <returns>A <see cref="Bitmap"/></returns>
        /// <exception cref="ArgumentException">When nd.size != width*height, which means the ndarray be turned into the given bitmap size.</exception>
        public static unsafe Bitmap ToBitmap(this NDArray nd, int width, int height)
        {
            if (nd == null)
            {
                throw new ArgumentNullException(nameof(nd));
            }

            if (width * height != nd.size)
            {
                throw new ArgumentException("Given nd.size != width*height");
            }

            if (!nd.Unsafe.Shape.IsContiguous)
            {
                nd = nd.Clone();
            }

            var ret     = new Bitmap(height, width);
            var bitdata = ret.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

            try
            {
                nd.CopyTo(new UnmanagedMemoryBlock <byte>((byte *)bitdata.Scan0, bitdata.Stride * bitdata.Height));
            }
            finally
            {
                ret.UnlockBits(bitdata);
            }

            return(ret);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Converts <see cref="NDArray"/> to a <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="nd">The <see cref="NDArray"/> to copy pixels from, <see cref="Shape"/> is ignored completely. If nd.Unsafe.Shape.IsContiguous == false then a copy is made.</param>
        /// <param name="width">The height of the <see cref="Bitmap"/></param>
        /// <param name="height">The width of the <see cref="Bitmap"/></param>
        /// <param name="format">The format of the expected bitmap, Must be matching to given NDArray otherwise unexpected results might occur.</param>
        /// <returns>A <see cref="Bitmap"/></returns>
        /// <exception cref="ArgumentException">When nd.size != width*height, which means the ndarray be turned into the given bitmap size.</exception>
        public static unsafe Bitmap ToBitmap(this NDArray nd, int width, int height, PixelFormat format = PixelFormat.DontCare)
        {
            if (nd == null)
            {
                throw new ArgumentNullException(nameof(nd));
            }

            //if flat then initialize based on given format
            if (nd.ndim == 1 && format != PixelFormat.DontCare)
            {
                nd = nd.reshape(1, height, width, format.ToBytesPerPixel()); //theres a check internally for size mismatch.
            }
            if (nd.ndim != 4)
            {
                throw new ArgumentException("ndarray was expected to be of 4-dimensions, (1, bmpData.Height, bmpData.Width, bytesPerPixel)");
            }

            if (nd.shape[0] != 1)
            {
                throw new ArgumentException($"ndarray has more than one picture in it ({nd.shape[0]}) based on the first dimension.");
            }


            var bbp = nd.shape[3]; //bytes per pixel.

            if (bbp != extractFormatNumber())
            {
                throw new ArgumentException($"Given PixelFormat: {format} does not match the number of bytes per pixel in the 4th dimension of given ndarray.");
            }

            if (bbp * width * height != nd.size)
            {
                throw new ArgumentException($"The expected size does not match the size of given ndarray. (expected: {bbp * width * height}, actual: {nd.size})");
            }


            var ret     = new Bitmap(width, height, format);
            var bitdata = ret.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, format);

            try
            {
                var dst = new ArraySlice <byte>(new UnmanagedMemoryBlock <byte>((byte *)bitdata.Scan0, bitdata.Stride * bitdata.Height));
                if (nd.Shape.IsContiguous)
                {
                    nd.CopyTo(dst);
                }
                else
                {
                    MultiIterator.Assign(new UnmanagedStorage(dst, Shape.Vector(bitdata.Stride * bitdata.Height)), nd.Unsafe.Storage);
                }
            }
            finally
            {
                ret.UnlockBits(bitdata);
            }

            return(ret);

            int extractFormatNumber()
            {
                if (format == PixelFormat.DontCare)
                {
                    switch (bbp)
                    {
                    case 3:
                        format = PixelFormat.Format24bppRgb;
                        break;

                    case 4:
                        format = PixelFormat.Format32bppArgb;
                        break;

                    case 6:
                        format = PixelFormat.Format48bppRgb;
                        break;

                    case 8:
                        format = PixelFormat.Format64bppArgb;
                        break;
                    }

                    return(bbp);
                }

                return(format.ToBytesPerPixel());
            }
        }