Ejemplo n.º 1
0
 /// <summary>
 /// Converts each element of the array by using the specified conversion function.
 /// </summary>
 /// <typeparam name="TSrc">Source element type.</typeparam>
 /// <typeparam name="TDst">Destination element type.</typeparam>
 /// <param name="source">Source array.</param>
 /// <param name="convert">Conversion function.</param>
 /// <returns>Destination array.</returns>
 public static TDst[,] Convert <TSrc, TDst>(this TSrc[,] source, Convert <TSrc, TDst> convert)
 where TSrc : struct
 where TDst : struct
 {
     var area = new Rectangle(0, 0, source.GetLength(1), source.GetLength(0));
     return(source.Convert <TSrc, TDst>(convert, area));
 }
Ejemplo n.º 2
0
        private static void convert <TSrc, TDst>(Convert <TSrc, TDst> convert, TSrc[,] source, TDst[,] destination)
            where TSrc : struct
            where TDst : struct
        {
            var gridX = source.GetLength(1);
            var gridY = source.GetLength(0);

            process(th =>
            {
                convert(ref source[th.Y, th.X], ref destination[th.Y, th.X]);
            },
                    gridX, gridY);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Applies the specified conversion function to each source pixel, producing the destination image.
        /// </summary>
        /// <typeparam name="TSrc">Source element type.</typeparam>
        /// <typeparam name="TDst">Destination element type.</typeparam>
        /// <param name="source">Source array.</param>
        /// <param name="convert">Pixel conversion function.</param>
        /// <returns>Destination array.</returns>
        public static TDst[,] Convert <TSrc, TDst>(this TSrc[,] source, Func <TSrc, TDst> convert)
        {
            Size imSize = source.Size();

            TDst[,] dest = new TDst[imSize.Height, imSize.Width];

            ParallelLauncher.Launch(thread =>
            {
                dest[thread.Y, thread.X] = convert(source[thread.Y, thread.X]);
            },
                                    source.Width(), source.Height());

            return(dest);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converts each element of the array by using the specified conversion function.
        /// </summary>
        /// <typeparam name="TSrc">Source element type.</typeparam>
        /// <typeparam name="TDst">Destination element type.</typeparam>
        /// <param name="source">Source array.</param>
        /// <param name="convert">Conversion function.</param>
        /// <param name="area">Working area.</param>
        public static TDst[,] Convert <TSrc, TDst>(this TSrc[,] source, Convert <TSrc, TDst> convert, Rectangle area)
        where TSrc : struct
        where TDst : struct
        {
            TDst[,] destination = new TDst[area.Height, area.Width];
            var offset = area.Location;

            ParallelLauncher.Launch(thread =>
            {
                convert(ref source[thread.Y + offset.Y, thread.X + offset.X], ref destination[thread.Y, thread.X]);
            },
                                    area.Width, area.Height);

            return(destination);
        }
Ejemplo n.º 5
0
 public static int Len <TSrc>(
     this TSrc [ , ] src,
     int order = 0)
 {
     if (order == 0)
     {
         return(src.GetLength(0));
     }
     if (order == 1)
     {
         return(src.GetLength(1));
     }
     else
     {
         return(src.GetLength(0));
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 2D array (matrix) to 2D jagged array
        /// </summary>
        /// <typeparam name="TSrc"></typeparam>
        /// <param name="src"></param>
        /// <returns></returns>
        public static TSrc [] [] ToJagged <TSrc>(
            this TSrc [ , ] src)
        {
            int rowL = src.Len(0), fcolL = src.Len(1);

            TSrc[][] output = new TSrc[rowL][];
            for (int j = 0; j < rowL; j++)
            {
                TSrc[] second = new TSrc[fcolL];
                for (int i = 0; i < fcolL; i++)
                {
                    second [i] = src[j, i];
                }
                output [j] = second;
            }
            return(output);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Flatten data
        /// </summary>
        /// <typeparam name="TSrc"></typeparam>
        /// <param name="src"></param>
        /// <returns></returns>
        public static TSrc [] Flatten <TSrc>(
            this TSrc [ , ] src)
        {
            int fsize = src.GetLength(0);
            int ssize = src.GetLength(1);


            int         totalSize = fsize + ssize;
            List <TSrc> result    = new List <TSrc>();

            for (int f = 0; f < fsize; f++)
            {
                for (int s = 0; s < ssize; s++)
                {
                    result.Add(src [f, s]);
                }
            }
            return(result.ToArray());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Padding 0 value to 2d jagged array
        /// Total size of row and col become [ rowLength + paddingSize*2 , colLength + paddingSize*2 ]
        /// </summary>
        /// <typeparam name="TSrc"></typeparam>
        /// <param name="src"></param>
        /// <param name="padSize">Size of padding exapanding each side</param>
        /// <returns></returns>
        public static TSrc[ , ] Padding <TSrc>(
            this TSrc [ , ] src,
            int padSize)
        {
            if (padSize == 0)
            {
                return(src);
            }

            var basedata
                = new TSrc[src.GetLength(0) + padSize * 2, src.GetLength(1) + padSize * 2];

            for (int j = padSize; j < basedata.GetLength(0) - padSize; j++)
            {
                for (int i = padSize; i < basedata.GetLength(1) - padSize; i++)
                {
                    basedata [j, i] = src [j - padSize, i - padSize];
                }
            }
            return(basedata);
        }
Ejemplo n.º 9
0
 public static IEnumerable <TRes> Select <TSrc, TRes>(this TSrc[,] source, Func <TSrc, (int, int), TRes> selector)
Ejemplo n.º 10
0
 public static TDst[,] MyConvert <TSrc, TDst>(this TSrc[,] source)
 where TSrc : IColor
 where TDst : IColor
 {
     return(null);
 }