Example #1
0
        public Bitmap RunOneFrame(Bitmap frame, double multiplier, Functions function)//fix
        {
            CalcMaxValuesOneFrame(frame.Width, frame.Height, multiplier, function);
            var width           = frame.Width;
            var height          = frame.Height;
            var convertedBitmap = new FastBitmap(new Bitmap(width, height));

            convertedBitmap.LockBits();
            var currentBitmap = new FastBitmap(frame);

            currentBitmap.LockBits();
            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    var pixel = RunFunction(x, y, multiplier,
                                            function, width, height);
                    var convX = FastRoundInt(Map(pixel.Item1, minX, maxX, 0, width - 1));
                    var convY = FastRoundInt(Map(pixel.Item2, minY, maxY, 0, height - 1));
                    convertedBitmap.SetPixel(convX, convY, currentBitmap.GetPixel(x, y));
                }
            }
            currentBitmap.UnlockBits();
            currentBitmap.DisposeSource();
            convertedBitmap.UnlockBits();
            return(CompleteBitmap(convertedBitmap.GetSource()));
        }
Example #2
0
        public static void Run(VideoFileReader reader, VideoFileWriter writer, double multiplierFrom, double multiplierTo,
                               Functions function, BackgroundWorker renderWorker)
        {
            if (!reader.IsOpen || !writer.IsOpen)
            {
                throw new Exception("Файл не открыт.");
            }
            var numberOfFrames = (int)reader.FrameCount;

            Console.WriteLine("Считаем максимумы.");
            var maxValues = CalcMaxValuesParallel(reader.Width, reader.Height, multiplierFrom, multiplierTo, numberOfFrames, function);
            var maxX      = maxValues.Item1;
            var maxY      = maxValues.Item2;
            var minX      = maxValues.Item3;
            var minY      = maxValues.Item4;
            var width     = reader.Width;
            var height    = reader.Height;

            Console.WriteLine($"Maximum x: {maxX}; Maximum y: {maxY}; Minimum x: {minX}; Minimum y: {minY}");
            Console.WriteLine("Начинаем рендер");
            for (var t = 0; t < numberOfFrames; t++)
            {
                var convertedBitmap = new FastBitmap(new Bitmap(width, height));
                convertedBitmap.LockBits();
                FastBitmap currentBitmap;
                try
                {
                    currentBitmap = new FastBitmap(reader.ReadVideoFrame(t));
                }
                catch (Exception ignored)
                {
                    break;
                }
                currentBitmap.LockBits();
                for (var x = 0; x < width; x++)
                {
                    for (var y = 0; y < height; y++)
                    {
                        var pixel = RunFunction(x, y, GetMultiplier(t, numberOfFrames, multiplierFrom, multiplierTo),
                                                function, width, height);
                        var convX = FastRoundInt(Map(pixel.Item1, minX, maxX, 0, width - 1));
                        var convY = FastRoundInt(Map(pixel.Item2, minY, maxY, 0, height - 1));
                        convertedBitmap.SetPixel(convX, convY, currentBitmap.GetPixel(x, y));
                    }
                }
                currentBitmap.UnlockBits();
                currentBitmap.DisposeSource();
                convertedBitmap.UnlockBits();
                //timeLine.Value = t;
                var res = CompleteBitmap(convertedBitmap.GetSource());
                writer.WriteVideoFrame(res);
                AppForm.PreviewBitmap = (Bitmap)res.Clone();
                renderWorker.ReportProgress(FastRoundInt(t * 1000.0 / numberOfFrames));
                //Progress.ReportFastTime(t++, numberOfFrames);
            }
        }
Example #3
0
        public Bitmap RunOneFrame(VideoFileReader reader, int frameNum, double multiplierFrom,
                                  double multiplierTo, Functions function, int maxWidth, int maxHeight)
        {
            if (!reader.IsOpen)
            {
                throw new Exception("Файл не открыт.");
            }
            var        numberOfFrames = (int)reader.FrameCount;
            var        multiplier     = GetMultiplier(frameNum, numberOfFrames, multiplierFrom, multiplierTo);
            FastBitmap currentBitmap;

            try
            {
                currentBitmap = new FastBitmap(ScaleImage(reader.ReadVideoFrame(frameNum), maxWidth, maxHeight,
                                                          true, false));
            }
            catch (Exception ignored)
            {
                return(new Bitmap(maxWidth, maxHeight));
            }
            currentBitmap.LockBits();
            var width  = currentBitmap.Width;
            var height = currentBitmap.Height;

            CalcMaxValuesOneFrame(width, height, multiplier, function);
            var convertedBitmap = new FastBitmap(new Bitmap(width, height));

            convertedBitmap.LockBits();
            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    var pixel = RunFunction(x, y, multiplier,
                                            function, width, height);
                    var convX = FastRoundInt(Map(pixel.Item1, minX, maxX, 0, width - 1));
                    var convY = FastRoundInt(Map(pixel.Item2, minY, maxY, 0, height - 1));
                    convertedBitmap.SetPixel(convX, convY, currentBitmap.GetPixel(x, y));
                }
            }
            currentBitmap.UnlockBits();
            currentBitmap.DisposeSource();
            convertedBitmap.UnlockBits();
            return(CompleteBitmap(convertedBitmap.GetSource()));
        }
Example #4
0
        private static void PhotoFinish(VideoFileReader reader, VideoFileWriter writer, BackgroundWorker renderWorker)
        {
            var numberOfFrames = (int)reader.FrameCount;
            var width          = reader.Width;
            var height         = reader.Height;

            if (numberOfFrames > width)        //if user want -> fit to original width
            {
                numberOfFrames = width;
            }
            //writer.Width = numberOfFrames; //rewrite for change resolution, open file in method
            for (var x = 0; x < width; x++)
            {
                var convertedBitmap = new FastBitmap(new Bitmap(numberOfFrames, height)); //photofinish одновременная обработка нескольких кадров
                convertedBitmap.LockBits();
                for (var f = 0; f < numberOfFrames; f++)
                {
                    FastBitmap currentBitmap;
                    try
                    {
                        currentBitmap = new FastBitmap(reader.ReadVideoFrame(f));
                    }
                    catch (Exception ignored)
                    {
                        break;
                    }
                    currentBitmap.LockBits();
                    for (var y = 0; y < height; y++)
                    {
                        convertedBitmap.SetPixel(f, y, currentBitmap.GetPixel(x, y));
                    }
                    currentBitmap.DisposeSource();
                }
                convertedBitmap.UnlockBits();
                writer.WriteVideoFrame(convertedBitmap.GetSource());

                AppForm.PreviewBitmap = (Bitmap)convertedBitmap.GetSource().Clone();
                convertedBitmap.DisposeSource();
                renderWorker.ReportProgress(FastUtils.FastRoundInt(x * 1000.0 / width));
            }
        }
Example #5
0
        public static void Run(string videoPath, string videoName)
        {
            var reader = new VideoFileReader();

            reader.Open(videoPath + videoName);
            var writer         = new VideoFileWriter();
            var numberOfFrames = (int)reader.FrameCount;
            var probeBitmap    = new FastBitmap(reader.ReadVideoFrame(0));

            probeBitmap.LockBits();
            var maxValues = CalcMaxValues(probeBitmap.Width, probeBitmap.Height);
            var maxX      = maxValues.Item1;
            var maxY      = maxValues.Item2;

            //Console.WriteLine($"{maxX}:{maxY}");
            writer.Height     = probeBitmap.Height;
            writer.Width      = probeBitmap.Width;
            writer.FrameRate  = 30;
            writer.VideoCodec = VideoCodec.Mpeg4;
            writer.BitRate    = reader.BitRate;
            writer.Open(videoPath + "out.avi");
            for (var t = 70; t < numberOfFrames; t++)
            {
                var        convertedBitmap = new FastBitmap(new Bitmap(probeBitmap.Width, probeBitmap.Height));
                FastBitmap currentBitmap;
                try
                {
                    currentBitmap = new FastBitmap(reader.ReadVideoFrame(t));
                }
                catch (Exception ignored)
                {
                    break;
                }
                convertedBitmap.LockBits();
                currentBitmap.LockBits();
                for (var x = 0; x < probeBitmap.Width; x++)
                {
                    for (var y = 0; y < probeBitmap.Height; y++)
                    {
                        var pixel = ConvertToPolar(x - probeBitmap.Width / 2, y - probeBitmap.Height / 2);
                        var convX = FastUtils.FastAbs((int)(pixel.Item2 / maxY * (probeBitmap.Width - 1)));
                        var convY = FastUtils.FastAbs((int)(pixel.Item1 / maxX * (probeBitmap.Height - 1)));
                        convertedBitmap.SetPixel(convX, convY, currentBitmap.GetPixel(x, y));
                    }
                }
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 4; i++)
                {
                    convertedBitmap = CompleteBitmap(convertedBitmap);
                }
                sw.Stop();
                Console.WriteLine(sw.Elapsed);
                currentBitmap.UnlockBits();
                currentBitmap.DisposeSource();
                convertedBitmap.GetSource().Save(videoPath + "test.png");
                break;
                //convertedBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
                writer.WriteVideoFrame(convertedBitmap.GetSource());
                convertedBitmap.DisposeSource();
            }
            probeBitmap.UnlockBits();
            writer.Flush();
        }