private TimeSpan ProvideDelayDuration(int index) { if (index < 10) { // First second return(TimeSpan.FromMilliseconds(100)); } if (index < 10 + 256) { // Steps where there is also led dimming SpotSet.IndicateMissingValues(); return(TimeSpan.FromMilliseconds(5000d / 256)); } return(TimeSpan.FromMilliseconds(1000)); }
public void Run(CancellationToken token) { if (IsRunning) { throw new Exception(nameof(DesktopDuplicatorReader) + " is already running!"); } IsRunning = true; _log.Debug("Started Desktop Duplication Reader."); try { BitmapData bitmapData = new BitmapData(); while (!token.IsCancellationRequested) { var frame = _retryPolicy.Execute <DesktopFrame>(GetNextFrame); TraceFrameDetails(frame); if (frame == null) { //there was a timeout before there was the next frame, simply retry! continue; } var image = frame.DesktopImage; image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData); lock (SpotSet.Lock) { var useLinearLighting = Settings.UseLinearLighting; var imageRectangle = new Rectangle(0, 0, image.Width, image.Height); Parallel.ForEach(SpotSet.Spots , spot => { const int numberOfSteps = 15; int stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps); int stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps); int sumR; int sumG; int sumB; int count; if (imageRectangle.Width != SpotSet.ExpectedScreenBound.Width || imageRectangle.Height != SpotSet.ExpectedScreenBound.Height) { //the screen was resized or this is some kind of powersaving state SpotSet.IndicateMissingValues(); return; } GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData, out sumR, out sumG, out sumB, out count); var countInverse = 1f / count; byte finalR, finalG, finalB; ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse, out finalR, out finalG, out finalB, useLinearLighting , Settings.SaturationTreshold, spot.Red, spot.Green, spot.Blue); spot.SetColor(finalR, finalG, finalB); }); } image.UnlockBits(bitmapData); } } finally { _desktopDuplicator?.Dispose(); _desktopDuplicator = null; _log.Debug("Stopped Desktop Duplication Reader."); IsRunning = false; } }
public async void Run(CancellationToken token) { while (RunState == RunStateEnum.Stopping) { await Task.Yield(); } if (RunState != RunStateEnum.Stopped) { throw new Exception($"{nameof(DesktopDuplicatorReader)} is already running!"); } RunState = RunStateEnum.Running; _log.Debug("Started Desktop Duplication Reader."); Bitmap image = null; try { var bitmapData = new BitmapData(); while (!token.IsCancellationRequested) { var frameTime = Stopwatch.StartNew(); var context = new Context { { "image", image } }; var newImage = _retryPolicy.Execute(c => GetNextFrame(c["image"] as Bitmap), context); TraceFrameDetails(newImage); if (newImage == null) { // There was a timeout before there was the next frame, simply retry! continue; } image = newImage; var isPreviewRunning = SettingsViewModel.IsSettingsWindowOpen && SettingsViewModel.IsPreviewTabOpen; if (isPreviewRunning) { SettingsViewModel.SetPreviewImage(image); } image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData); lock (SpotSet.Lock) { var useLinearLighting = UserSettings.UseLinearLighting; if (image.Width != SpotSet.ExpectedScreenWidth || image.Height != SpotSet.ExpectedScreenHeight) { // The screen was resized or this is some kind of powersaving state SpotSet.IndicateMissingValues(); return; } else { Parallel.ForEach(SpotSet.Spots, spot => { const int numberOfSteps = 15; var stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps); var stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps); GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData, out var sumR, out var sumG, out var sumB, out var count); var countInverse = 1f / count; ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse, out var finalR, out var finalG, out var finalB, useLinearLighting, UserSettings.SaturationTreshold, spot.Red, spot.Green, spot.Blue); spot.SetColor(finalR, finalG, finalB, isPreviewRunning); }); }
public void Run(CancellationToken token) { if (IsRunning) { throw new Exception(nameof(DesktopDuplicatorReader) + " is already running!"); } IsRunning = true; _log.Debug("Started Desktop Duplication Reader."); Bitmap image = null; try { BitmapData bitmapData = new BitmapData(); while (!token.IsCancellationRequested) { var newImage = _retryPolicy.Execute(() => GetNextFrame(image)); TraceFrameDetails(newImage); if (newImage == null) { //there was a timeout before there was the next frame, simply retry! continue; } image = newImage; bool isPreviewRunning = SettingsViewModel.IsSettingsWindowOpen && SettingsViewModel.IsPreviewTabOpen; if (isPreviewRunning) { SettingsViewModel.SetPreviewImage(image); } image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData); lock (SpotSet.Lock) { var useLinearLighting = UserSettings.UseLinearLighting; var imageRectangle = new Rectangle(0, 0, image.Width, image.Height); Parallel.ForEach(SpotSet.Spots , spot => { const int numberOfSteps = 15; int stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps); int stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps); if (imageRectangle.Width != SpotSet.ExpectedScreenBound.Width || imageRectangle.Height != SpotSet.ExpectedScreenBound.Height) { //the screen was resized or this is some kind of powersaving state SpotSet.IndicateMissingValues(); return; } GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData, out int sumR, out int sumG, out int sumB, out int count); var countInverse = 1f / count; ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse , out byte finalR, out byte finalG, out byte finalB, useLinearLighting , UserSettings.SaturationTreshold, spot.Red, spot.Green, spot.Blue); spot.SetColor(finalR, finalG, finalB, isPreviewRunning); }); if (isPreviewRunning) { //copy all color data to the preview var needsNewArray = SettingsViewModel.PreviewSpots?.Length != SpotSet.Spots.Length; SettingsViewModel.PreviewSpots = SpotSet.Spots; } }