private async void AttemptSave() { if (Processing) { return; } Processing = true; GC.Collect(); var lowMemory = false; try { var result = (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit"); lowMemory = result / 1024 / 1024 < 300; } catch (ArgumentOutOfRangeException) { } IBuffer buffer; Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = lowMemory ? 0.5 : 1; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); using (var effect = new LensBlurEffect(source, new LensBlurPredefinedKernel(Shape, (uint)SizeSlider.Value))) using (var renderer = new JpegRenderer(effect)) { effect.KernelMap = segmenter; buffer = await renderer.RenderAsync(); } } using (var library = new MediaLibrary()) using (var stream = buffer.AsStream()) { library.SavePicture("lensblur_" + DateTime.Now.Ticks, stream); Model.Saved = true; AdaptButtonsToState(); } Processing = false; }
private async void AttemptUpdatePreviewAsync() { if (!Processing) { Processing = true; AdaptButtonsToState(); Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = 0.5; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); do { _processingPending = false; var previewBitmap = new WriteableBitmap((int)Model.AnnotationsBitmap.Dimensions.Width, (int)Model.AnnotationsBitmap.Dimensions.Height); using (var effect = new LensBlurEffect(source, new LensBlurPredefinedKernel(Model.KernelShape, (uint)Model.KernelSize))) using (var renderer = new WriteableBitmapRenderer(effect, previewBitmap)) { effect.KernelMap = segmenter; try { await renderer.RenderAsync(); PreviewImage.Source = previewBitmap; previewBitmap.Invalidate(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("AttemptUpdatePreviewAsync rendering failed: " + ex.Message); } } }while (_processingPending); } Processing = false; AdaptButtonsToState(); } else { _processingPending = true; } }
public async Task RenderFullQualityImage() { using (var source = KnownImages.Nurse.ImageSource) using (var annotations = KnownImages.NurseScribbles.ImageSource) using (var segmenter = new InteractiveForegroundSegmenter(source, Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 255, 255, 0), annotations)) using (var effect = new FocusObjectDepthOfFieldEffect(source, segmenter, new Point(0, 0.8), new Point(1, 0.8), 1.0, 1.0, DepthOfFieldQuality.Full)) using (var renderer = new JpegRenderer(effect)) { var buffer = await renderer.RenderAsync(); ImageResults.Instance.SaveToPicturesLibrary(buffer); } }
protected override MaybeTask <IImageProvider> GetEffectInternalAsync(IImageProvider source, Windows.Foundation.Size sourceSize, Windows.Foundation.Size renderSize) { if (m_effectEffect == null) { m_segmenter = new InteractiveForegroundSegmenter(source, foreground, background, m_scribbles); m_segmenter.Quality = 0.5; m_effectEffect = new FocusObjectDepthOfFieldEffect(source, m_segmenter, new Point(0.0, 0.7), new Point(1.0, 0.75), 1.0, 1.0, DepthOfFieldQuality.Preview); } else if (m_effectEffect.Source != source) { m_effectEffect.Source = source; } return(new MaybeTask <IImageProvider>(m_effectEffect)); }
public void CreatesSegmenterGraph() { var bitmap = new Bitmap(new Size(10, 10), ColorMode.Argb8888); using (var source = new BitmapImageSource(bitmap)) using (var source2 = new BitmapImageSource(bitmap)) using (var kernel2 = new BitmapImageSource(bitmap)) using (var segmenter = new InteractiveForegroundSegmenter(source)) { segmenter.AnnotationsSource = source2; string result = CreateGraph(segmenter); Assert.AreEqual(2, new Regex(Regex.Escape(NodeId(source))).Matches(result).Count); Assert.AreEqual(2, new Regex(Regex.Escape(NodeId(source2))).Matches(result).Count); Assert.AreEqual(0, new Regex(Regex.Escape(NodeId(kernel2))).Matches(result).Count); Assert.AreEqual(3, new Regex(Regex.Escape(NodeId(segmenter))).Matches(result).Count); } }
protected override MaybeTask<IImageProvider> GetEffectInternalAsync(IImageProvider source, Windows.Foundation.Size sourceSize, Windows.Foundation.Size renderSize) { if (m_effectEffect == null) { m_segmenter = new InteractiveForegroundSegmenter(source, foreground, background, m_scribbles); m_segmenter.Quality = 0.5; m_effectEffect = new FocusObjectDepthOfFieldEffect(source, m_segmenter, new Point(0.0, 0.7), new Point(1.0, 0.75), 1.0, 1.0, DepthOfFieldQuality.Preview); } else if (m_effectEffect.Source != source) { m_effectEffect.Source = source; } return new MaybeTask<IImageProvider>(m_effectEffect); }
public void CreatesQuiteComplexGraph() { var bitmap = new Bitmap(new Size(10, 10), ColorMode.Argb8888); using (var source1 = new BitmapImageSource(bitmap)) using (var source2 = new BitmapImageSource(bitmap)) using (var source3 = new BitmapImageSource(bitmap)) using (var source4 = new BitmapImageSource(bitmap)) using (var segmenter = new InteractiveForegroundSegmenter(source1, Color.FromArgb(255, 255, 0, 0), Color.FromArgb(255, 0, 255, 0), source2)) using (var bokeh = new LensBlurEffect(source1, segmenter)) using (var blendEffect = new BlendEffect(bokeh, source3)) { string result = CreateGraph(blendEffect); Assert.AreEqual(3, new Regex(Regex.Escape(NodeId(source1))).Matches(result).Count); Assert.AreEqual(2, new Regex(Regex.Escape(NodeId(source2))).Matches(result).Count); Assert.AreEqual(2, new Regex(Regex.Escape(NodeId(source3))).Matches(result).Count); Assert.AreEqual(0, new Regex(Regex.Escape(NodeId(source4))).Matches(result).Count); Assert.AreEqual(4, new Regex(Regex.Escape(NodeId(segmenter))).Matches(result).Count); Assert.AreEqual(5, new Regex(Regex.Escape(NodeId(bokeh))).Matches(result).Count); Assert.AreEqual(3, new Regex(Regex.Escape(NodeId(blendEffect))).Matches(result).Count); } }
private async void AttemptSaveAsync() { if (!Processing) { Processing = true; AdaptButtonsToState(); GC.Collect(); var lowMemory = false; try { long result = (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit"); lowMemory = result / 1024 / 1024 < 300; } catch (ArgumentOutOfRangeException) { } IBuffer buffer = null; Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = lowMemory ? 0.5 : 1; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); using (var effect = new LensBlurEffect(source, new LensBlurPredefinedKernel(Model.KernelShape, (uint)Model.KernelSize))) using (var renderer = new JpegRenderer(effect)) { effect.KernelMap = segmenter; try { buffer = await renderer.RenderAsync(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("AttemptSave rendering failed: " + ex.Message); } } } if (buffer != null) { using (var library = new MediaLibrary()) using (var stream = buffer.AsStream()) { library.SavePicture("lensblur_" + DateTime.Now.Ticks, stream); Model.Saved = true; AdaptButtonsToState(); } } Processing = false; AdaptButtonsToState(); } }
private async void AttemptUpdatePreviewAsync() { if (!Processing) { Processing = true; AdaptButtonsToState(); Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = 0.5; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); do { _processingPending = false; var previewBitmap = new WriteableBitmap((int)Model.AnnotationsBitmap.Dimensions.Width, (int)Model.AnnotationsBitmap.Dimensions.Height); using (var effect = new LensBlurEffect(source, new LensBlurPredefinedKernel(Model.KernelShape, (uint)Model.KernelSize))) using (var renderer = new WriteableBitmapRenderer(effect, previewBitmap)) { effect.KernelMap = segmenter; try { await renderer.RenderAsync(); PreviewImage.Source = previewBitmap; previewBitmap.Invalidate(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("AttemptUpdatePreviewAsync rendering failed: " + ex.Message); } } } while (_processingPending); } Processing = false; AdaptButtonsToState(); } else { _processingPending = true; } }
private async void AttemptUpdatePreviewAsync() { if (!Processing) { Processing = true; do { _processingPending = false; if (Model.OriginalImage != null && ForegroundAnnotationsDrawn && BackgroundAnnotationsDrawn) { Model.OriginalImage.Position = 0; var maskBitmap = new WriteableBitmap((int)AnnotationsCanvas.ActualWidth, (int)AnnotationsCanvas.ActualHeight); var annotationsBitmap = new WriteableBitmap((int)AnnotationsCanvas.ActualWidth, (int)AnnotationsCanvas.ActualHeight); annotationsBitmap.Render(AnnotationsCanvas, new ScaleTransform { ScaleX = 1, ScaleY = 1 }); annotationsBitmap.Invalidate(); Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var renderer = new WriteableBitmapRenderer(segmenter, maskBitmap)) using (var annotationsSource = new BitmapImageSource(annotationsBitmap.AsBitmap())) { var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); segmenter.Quality = 0.5; segmenter.AnnotationsSource = annotationsSource; await renderer.RenderAsync(); MaskImage.Source = maskBitmap; maskBitmap.Invalidate(); Model.AnnotationsBitmap = (Bitmap)annotationsBitmap.AsBitmap(); } } else { MaskImage.Source = null; } }while (_processingPending && !_manipulating); Processing = false; } else { _processingPending = true; } }
private async void AttemptUpdatePreviewAsync() { if (!Processing) { Processing = true; do { _processingPending = false; if (Model.OriginalImage != null && ForegroundAnnotationsDrawn && BackgroundAnnotationsDrawn) { Model.OriginalImage.Position = 0; var maskBitmap = new WriteableBitmap((int)AnnotationsCanvas.ActualWidth, (int)AnnotationsCanvas.ActualHeight); var annotationsBitmap = new WriteableBitmap((int)AnnotationsCanvas.ActualWidth, (int)AnnotationsCanvas.ActualHeight); annotationsBitmap.Render(AnnotationsCanvas, new ScaleTransform { ScaleX = 1, ScaleY = 1 }); annotationsBitmap.Invalidate(); Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var renderer = new WriteableBitmapRenderer(segmenter, maskBitmap)) using (var annotationsSource = new BitmapImageSource(annotationsBitmap.AsBitmap())) { var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); segmenter.Quality = 0.5; segmenter.AnnotationsSource = annotationsSource; await renderer.RenderAsync(); MaskImage.Source = maskBitmap; maskBitmap.Invalidate(); Model.AnnotationsBitmap = (Bitmap)annotationsBitmap.AsBitmap(); } } else { MaskImage.Source = null; } } while (_processingPending && !_manipulating); Processing = false; } else { _processingPending = true; } }
private async void AttemptUpdatePreviewAsync2() { if (!Processing) { Processing = true; Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = 0.5; segmenter.Source = source; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); do { _processingPending = false; var previewBitmap = new WriteableBitmap((int)Model.AnnotationsBitmap.Dimensions.Width, (int)Model.AnnotationsBitmap.Dimensions.Height); using (var backgroundSource = new StreamImageSource(Model.OriginalImage2)) using (var filterEffect = new FilterEffect(backgroundSource)) using (var blendFilter = new BlendFilter(source)) using (var renderer = new WriteableBitmapRenderer(filterEffect, previewBitmap)) { blendFilter.BlendFunction = BlendFunction.Normal; blendFilter.MaskSource = segmenter; filterEffect.Filters = new IFilter[] { blendFilter }; try { await renderer.RenderAsync(); } catch { } var wb = previewBitmap; var fileStream = new MemoryStream(); wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 100, 100); fileStream.Seek(0, SeekOrigin.Begin); var effect = new LensBlurEffect(new StreamImageSource(fileStream), new LensBlurPredefinedKernel(LensBlurPredefinedKernelShape.Circle, 10)); var renderer2 = new WriteableBitmapRenderer(effect, previewBitmap); effect.KernelMap = segmenter; try { await renderer2.RenderAsync(); } catch { } PreviewImage.Source = previewBitmap; wb = previewBitmap; fileStream = new MemoryStream(); wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 100, 100); fileStream.Seek(0, SeekOrigin.Begin); // var m = new MediaLibrary(); // m.SavePictureToCameraRoll("test", fileStream); Model.MixedStream = fileStream; // Model.MixedStream = ConvertToStream(previewBitmap); previewBitmap.Invalidate(); } } while (_processingPending); } Processing = false; } else { _processingPending = true; } }
private async void AttemptUpdateImageAsync() { if (!Processing) { Processing = true; GC.Collect(); var lowMemory = false; try { long result = (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit"); lowMemory = result / 1024 / 1024 < 300; } catch (ArgumentOutOfRangeException) { } var maxSide = lowMemory ? 2048.0 : 4096.0; Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = lowMemory ? 0.5 : 1; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); var info = await source.GetInfoAsync(); double scaler, rotation; var width = info.ImageSize.Width; var height = info.ImageSize.Height; if (width > height) { scaler = maxSide / width; rotation = 90; var t = width; // We're rotating the image, so swap width and height width = height; height = t; } else { scaler = maxSide / height; rotation = 0; } scaler = Math.Max(1, scaler); _bitmap = new WriteableBitmap((int)(width * scaler), (int)(height * scaler)); using (var blurEffect = new LensBlurEffect(source, new LensBlurPredefinedKernel(Model.KernelShape, (uint)Model.KernelSize))) using (var filterEffect = new FilterEffect(blurEffect) { Filters = new[] { new RotationFilter(rotation) }}) using (var renderer = new WriteableBitmapRenderer(filterEffect, _bitmap)) { blurEffect.KernelMap = segmenter; try { await renderer.RenderAsync(); Image.Source = _bitmap; _bitmap.Invalidate(); ConfigureViewport(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("AttemptUpdateImageAsync rendering failed: " + ex.Message); } } } Processing = false; } }
private async void AttemptUpdateImageAsync() { if (!Processing) { Processing = true; GC.Collect(); var lowMemory = false; try { long result = (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit"); lowMemory = result / 1024 / 1024 < 300; } catch (ArgumentOutOfRangeException) { } var maxSide = lowMemory ? 2048.0 : 4096.0; Model.OriginalImage.Position = 0; using (var source = new StreamImageSource(Model.OriginalImage)) using (var segmenter = new InteractiveForegroundSegmenter(source)) using (var annotationsSource = new BitmapImageSource(Model.AnnotationsBitmap)) { segmenter.Quality = lowMemory ? 0.5 : 1; segmenter.AnnotationsSource = annotationsSource; var foregroundColor = Model.ForegroundBrush.Color; var backgroundColor = Model.BackgroundBrush.Color; segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B); segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B); var info = await source.GetInfoAsync(); double scaler, rotation; var width = info.ImageSize.Width; var height = info.ImageSize.Height; if (width > height) { scaler = maxSide / width; rotation = 90; var t = width; // We're rotating the image, so swap width and height width = height; height = t; } else { scaler = maxSide / height; rotation = 0; } scaler = Math.Max(1, scaler); _bitmap = new WriteableBitmap((int)(width * scaler), (int)(height * scaler)); using (var blurEffect = new LensBlurEffect(source, new LensBlurPredefinedKernel(Model.KernelShape, (uint)Model.KernelSize))) using (var filterEffect = new FilterEffect(blurEffect) { Filters = new[] { new RotationFilter(rotation) } }) using (var renderer = new WriteableBitmapRenderer(filterEffect, _bitmap)) { blurEffect.KernelMap = segmenter; try { await renderer.RenderAsync(); Image.Source = _bitmap; _bitmap.Invalidate(); ConfigureViewport(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("AttemptUpdateImageAsync rendering failed: " + ex.Message); } } } Processing = false; } }