예제 #1
0
파일: Pix.cs 프로젝트: unitay/leptonica.net
        /// <summary>
        /// Convert Pix to 3-dimensional byte array
        /// </summary>
        /// <param name="pixs">Pix Source</param>
        /// <param name="parallel">true to run in parallel, false to run sequentially single threaded</param>
        /// <returns>byte[height][width][3] (r,g,b) </returns>
        public static byte[][][] pixTo3dByteArrayByLine(this Pix pixs, bool parallel = true)
        {
            var h      = pixs.Height;
            var w      = pixs.Width;
            var nImage = new byte[h][][];

            if (!parallel)
            {
                var rLine = new byte[w];
                var gLine = new byte[w];
                var bLine = new byte[w];
                for (int y = 0; y < h; y++)
                {
                    nImage[y] = new byte[w][];

                    rLine = new byte[w];
                    gLine = new byte[w];
                    bLine = new byte[w];
                    pixs.pixGetRGBLine(y, rLine, gLine, bLine);

                    for (int x = 0; x < rLine.Length; x++)
                    {
                        nImage[y][x] = new byte[] { rLine[x], gLine[x], bLine[x] }
                    }
                    ;
                }
                rLine = null;
                gLine = null;
                bLine = null;
            }
            else
            {
                var parallelOpts = new System.Threading.Tasks.ParallelOptions()
                {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                };
                System.Threading.Tasks.Parallel.For(0, h, parallelOpts, y =>
                {
                    nImage[y] = new byte[w][];

                    byte[] rLine = new byte[w];
                    byte[] gLine = new byte[w];
                    byte[] bLine = new byte[w];
                    pixs.pixGetRGBLine(y, rLine, gLine, bLine);

                    for (int x = 0; x < rLine.Length; x++)
                    {
                        nImage[y][x] = new byte[] { rLine[x], gLine[x], bLine[x] }
                    }
                    ;
                });
            }
            return(nImage);
        }