public void ForEachSetup(Point view, bool invertFocus, ParallelOptions po, Action <UnmanagedImage> action) { if (Images == null) { throw new InvalidOperationException("Light field not loaded."); } //var ar = new string[MaximumDepthOfField(view)+1-DEPTH_OF_FIELD_MIN, DEPTH_OF_FOCUS_STEPS]; var n = (MaximumDepthOfField(view) + 1) - DEPTH_OF_FIELD_MIN; var l = n * DEPTH_OF_FOCUS_STEPS; var lmax = l - 1; Parallel.For( 0, l, po ?? Preferences.PoOne, i => { i = lmax - i; var p = new LightfieldParam { ViewPoint = view, DepthOfField = (i % n) + DEPTH_OF_FIELD_MIN, DepthOfFocus = ((i / n) * DEPTH_OF_FOCUS_STEP) + DEPTH_OF_FOCUS_MIN, InvertFocus = invertFocus }; var img = GetUnmanagedImage(p); action(img); } ); }
public void ForEachSetup(Point view, bool invertFocus, ParallelOptions po, Action <UnmanagedImage, FocusSetup> action, Predicate <FocusSetup> condition) { if (Images == null) { throw new InvalidOperationException("Light field not loaded."); } var n = (MaximumDepthOfField(view) + 1) - DEPTH_OF_FIELD_MIN; var l = n * DEPTH_OF_FOCUS_STEPS; var lmax = l - 1; Parallel.For( 0, l, po ?? Preferences.PoOne, i => { i = lmax - i; var field = (i % n) + DEPTH_OF_FIELD_MIN; var focus = ((i / n) * DEPTH_OF_FOCUS_STEP) + DEPTH_OF_FOCUS_MIN; var f = new FocusSetup { Id = i, DepthOfField = field, DepthOfFocus = focus }; if (!condition(f)) { return; } var p = new LightfieldParam { ViewPoint = view, DepthOfField = field, DepthOfFocus = focus, InvertFocus = invertFocus }; var img = GetUnmanagedImage(p); action(img, f); } ); }
public Bitmap GetImage(LightfieldParam parameter) => GetUnmanagedImage(parameter).ToManagedAndDisposeThis();
/// <summary> /// Codes a image using the light field based on the given parameters. /// </summary> /// <param name="parameter"> /// <see cref="LightfieldParam" /> /// </param> /// <returns></returns> public unsafe UnmanagedImage GetUnmanagedImage(LightfieldParam parameter) { var lightFieldSize = ViewRows * ViewColumns; if (lightFieldSize == 0) { return(null); } var doField = Math.Min(MaximumDepthOfField(parameter.ViewPoint), parameter.DepthOfField); var focusPoint = parameter.ViewPoint; if (focusPoint == default) { focusPoint = ViewPoint; } var doFocus = parameter.DepthOfFocus; var x0Focus = Math.Min(Math.Max(focusPoint.X, doField), ViewColumns - doField - 1); var y0Focus = Math.Min(Math.Max(focusPoint.Y, doField), ViewRows - doField - 1); var nLightFields = MathCV.Pow2(doField + doField + 1); var invNormLightFields = 1 / (float)nLightFields; var img = DoGetUnmanImage(x0Focus, y0Focus); if (img == null) { return(null); } var format = img.PixelFormat; var h = img.Height; var s = img.Stride; var n = s * h; var dt = (byte *)img.ImageData; Preferences.Supported.CheckFormat(format); var dxFocus = doFocus; var dyFocus = parameter.InvertFocus ? -doFocus : doFocus; var sumData = new float[img.Stride * h]; for (var i = 0; i < sumData.Length; i++) { sumData[i] = dt[i] * invNormLightFields; } var result = UnmanagedImage.Create(img.Width, img.Height, format); dt = (byte *)result.ImageData; var xMax = x0Focus + doField; var yMin = y0Focus - doField; var yMax = y0Focus + doField; for (var x = x0Focus - doField; x <= xMax; x++) { for (var y = yMin; y <= yMax; y++) { if (x == x0Focus && y == y0Focus) { continue; } var view = DoGetUnmanImage(x, y); if (view == null) { return(null); } if (view.PixelFormat != format) { throw new BadImageFormatException(nameof(view)); } try { IntegrateImage( view, sumData, dxFocus * (x - x0Focus), dyFocus * (y - y0Focus), invNormLightFields ); } catch (OperationCanceledException) { return(null); } } } for (var i = 0; i < n; i++) { dt[i] = sumData[i].ClampToByte(); } // throw new NotSupportedException("Not tested use of (float).LimitToByte(), or is (float).ToBase256() correct?"); return(result); }