예제 #1
0
        public VideoConvert(string[] split, IWebSocketConnection socket)
        {
            VideoCapture capture = new VideoCapture("./vidoe.mp4");
            int          width   = 64;
            int          height  = 64;

            ComputerCraftStuff       CCS    = new ComputerCraftStuff();
            Dictionary <Color, char> colors = CCS.colors;

            int i = 0;

            using (Mat image = new Mat("./test.png"))
            {
                while (true)
                {
                    if (!socket.IsAvailable)
                    {
                        break;
                    }
                    capture.Read(image);
                    if (image.Empty())
                    {
                        socket.Close();
                        break;
                    }

                    Console.WriteLine((i + 1) + " / " + capture.FrameCount);
                    var arr     = new List <byte>();
                    var indexer = new Mat <Vec3b>(image.Resize(new OpenCvSharp.Size(width, height), 0, 0, InterpolationFlags.Nearest)).GetIndexer();

                    for (byte x = 0; x < height; x += 1)
                    {
                        for (byte y = 0; y < width; y += 1)
                        {
                            Vec3b color = indexer[x, y]; // BGR
                            arr.Add(Convert.ToByte(colors[CCS.GetClosestColor(colors.Keys.ToArray(), Color.FromArgb(color.Item2, color.Item1, color.Item0))]));
                        }
                    }

                    byte[] compressed = CCS.compress(arr.ToArray());
                    Console.WriteLine(arr.Count + " -> " + compressed.Length);
                    socket.Send(compressed);
                    i++;
                }
            }
        }
예제 #2
0
        public AudioConvert()
        {
            ComputerCraftStuff CCS = new ComputerCraftStuff();

            string file    = "./data/banana.wav";
            string outfile = "./data/pang.dfpwm";

            var raw       = new MediaFoundationReader(file);
            var outFormat = new WaveFormat(48000, 8, 1);
            var reader    = new MediaFoundationResampler(raw, outFormat);

            List <byte> outFile = new List <byte>();

            byte[] readBuffer = new byte[1024];
            byte[] outBuffer  = new byte[readBuffer.Length / 8];

            int read;

            do
            {
                for (read = 0; read < readBuffer.Length;)
                {
                    int amt = reader.Read(readBuffer, read, readBuffer.Length - read);

                    if (amt == 0)
                    {
                        break;
                    }
                    read += amt;
                }
                read &= ~0x07;
                CCS.AudioCompress(outBuffer, readBuffer, 0, 0, read / 8);

                outFile.AddRange(outBuffer);
            } while (read == readBuffer.Length);

            File.WriteAllBytes(outfile, outFile.ToArray());
        }
예제 #3
0
        public void OldScreenshotConvert(string[] split, IWebSocketConnection socket)
        {
            int width  = int.Parse(split[1]);
            int height = int.Parse(split[2]);

            ComputerCraftStuff       CCS    = new ComputerCraftStuff();
            Dictionary <Color, char> colors = CCS.colors;

            int i = 0;

            while (i < 4000)
            {
                if (!socket.IsAvailable)
                {
                    break;
                }
                i++;
                Mat image = BitmapConverter.ToMat(makeScreenshot());

                var arr     = new List <byte>();
                var resized = image.Resize(new OpenCvSharp.Size(width, height), 0, 0, InterpolationFlags.Nearest);
                var indexer = new Mat <Vec3b>(resized).GetIndexer();

                for (int x = 0; x < height; x += 1)
                {
                    for (int y = 0; y < width; y += 1)
                    {
                        Vec3b color = indexer[x, y]; // BGR
                        arr.Add(Convert.ToByte(colors[CCS.GetClosestColor(colors.Keys.ToArray(), Color.FromArgb(color.Item2, color.Item1, color.Item0))]));
                    }
                }

                byte[] compressed = CCS.compress(arr.ToArray());
                Console.WriteLine(arr.Count + " -> " + compressed.Length);
                socket.Send(compressed);
                Cv2.WaitKey(1);
            }
        }
예제 #4
0
        public ImageConvert(IWebSocketConnection socket, string url)
        {
            int width  = 82;
            int height = 42;

            socket.Send($"{width},{height}");

            ComputerCraftStuff CCS = new ComputerCraftStuff();

            using (WebClient webClient = new WebClient())
            {
                byte[] dataArr = webClient.DownloadData(url);
                File.WriteAllBytes(@"path.png", dataArr);
            }

            Mat image   = new Mat("path.png");
            var resized = image.Resize(new OpenCvSharp.Size(width, height), 0, 0, InterpolationFlags.Nearest);

            var arr = new List <byte>();

            Mat outimg = new Mat();

            Kmeans(resized, outimg, 16);


            Dictionary <Vec3b, char> colores = new Dictionary <Vec3b, char>();

            var indexer = new Mat <Vec3b>(outimg).GetIndexer();

            for (byte x = 0; x < height; x += 1)
            {
                for (byte y = 0; y < width; y += 1)
                {
                    Vec3b color = indexer[x, y]; // BGR

                    if (!colores.ContainsKey(color))
                    {
                        int  lmao     = colores.Count;
                        char hexValue = lmao.ToString("X").ToLower().ToCharArray()[0];
                        colores.Add(color, hexValue);
                    }

                    arr.Add(Convert.ToByte(colores[color]));
                }
            }

            byte[] compressed = CCS.compress(arr.ToArray());
            Console.WriteLine(arr.Count + " -> " + compressed.Length);


            string palette = "";

            foreach (KeyValuePair <Vec3b, char> entry in colores)
            {
                string hex = entry.Key.Item0.ToString("X2") + entry.Key.Item1.ToString("X2") + entry.Key.Item2.ToString("X2");
                palette += hex + ",";
            }

            socket.Send(width + "|" + palette.Substring(0, palette.Length - 1));
            socket.Send(compressed);
        }
예제 #5
0
        public ScreenshotConvert(IWebSocketConnection socket)
        {
            //82 width, 41 height default
            int width  = 82 * 3;
            int height = 41 * 3;

            socket.Send($"{width},{height}");

            ComputerCraftStuff CCS = new ComputerCraftStuff();

            int i = 0;

            while (i < 4000)
            {
                if (!socket.IsAvailable)
                {
                    break;
                }
                i++;
                Mat image = BitmapConverter.ToMat(makeScreenshot());

                var arr     = new List <byte>();
                var resized = image.Resize(new OpenCvSharp.Size(width, height), 0, 0, InterpolationFlags.Nearest);


                Mat outimg = new Mat();
                Kmeans(resized, outimg, 16);

                var indexer = new Mat <Vec3b>(outimg).GetIndexer();

                Dictionary <Vec3b, char> colores = new Dictionary <Vec3b, char>();

                for (int x = 0; x < height; x += 1)
                {
                    for (int y = 0; y < width; y += 1)
                    {
                        Vec3b felcolor = indexer[x, y]; // BGR
                        Vec3b color    = new Vec3b(felcolor.Item2, felcolor.Item1, felcolor.Item0);

                        if (!colores.ContainsKey(color))
                        {
                            colores.Add(color, colores.Count.ToString("x")[0]);
                        }

                        arr.Add(Convert.ToByte(colores[color]));
                    }
                }
                byte[] compressed = CCS.compress(arr.ToArray());

                string palette = "";
                foreach (KeyValuePair <Vec3b, char> entry in colores)
                {
                    string hex = entry.Key.Item0.ToString("x2") + entry.Key.Item1.ToString("x2") + entry.Key.Item2.ToString("x2");
                    palette += hex + ",";
                }

                socket.Send(compressed);
                //socket.Send(arr.ToArray());
                socket.Send(palette.Substring(0, palette.Length - 1));

                Cv2.WaitKey(24);
            }
        }