summaryrefslogtreecommitdiff
path: root/Models/ImageModel.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Models/ImageModel.cs')
-rw-r--r--Models/ImageModel.cs69
1 files changed, 59 insertions, 10 deletions
diff --git a/Models/ImageModel.cs b/Models/ImageModel.cs
index e0bb1af..047d2e0 100644
--- a/Models/ImageModel.cs
+++ b/Models/ImageModel.cs
@@ -1,28 +1,61 @@
using Avalar.Models.Interfaces;
+using Avalar.Utils;
+using Avalar.Services.Interfaces;
using Avalonia.Media.Imaging;
using ReactiveUI;
using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
namespace Avalar.Models
{
public class ImageModel : ReactiveObject, IImageModel, ISettingsModel, IDisposable
{
-
+ private readonly IResizer m_Resizer;
+ private readonly IBrightness m_Brightness;
+ private CancellationTokenSource m_CancellationTokenSource;
+ private AvalarPipeline m_pipeline;
private IBitmap m_OriginalBitmap;
- private IBitmap m_LatestResult;
+ public ImageModel(IServiceProvider serviceProvider)
+ {
+ m_Resizer = serviceProvider.GetService<IResizer>();
+ m_Brightness = serviceProvider.GetService<IBrightness>();
- private double m_Width;
- public double Width {
- get => m_Width;
- set => this.RaiseAndSetIfChanged(ref m_Width, value);
+ m_pipeline = new AvalarPipeline(new List<IPipelineStep>
+ {
+ m_Resizer.AsPipelineStep(),
+ m_Brightness.AsPipelineStep()
+ });
+
+ m_pipeline.PipelineChanged += OnPipelineChanged;
}
- private double m_Height;
- public double Height
+ public async Task RunPipeline()
{
- get => m_Height;
- set => this.RaiseAndSetIfChanged(ref m_Height, value);
+ try
+ {
+ LatestResult = await m_pipeline.Run(m_OriginalBitmap, m_CancellationTokenSource.Token).ConfigureAwait(false);
+ } catch (OperationCanceledException)
+ {
+
+ }
+ }
+
+ private async void OnPipelineChanged(object sender, EventArgs e)
+ {
+ m_CancellationTokenSource?.Cancel();
+ m_CancellationTokenSource = new CancellationTokenSource();
+
+ await RunPipeline().ConfigureAwait(false);
+ }
+
+ private IBitmap m_LatestResult;
+ public IBitmap LatestResult
+ {
+ get => m_LatestResult;
+ set => this.RaiseAndSetIfChanged(ref m_LatestResult, value);
}
public IBitmap LoadImage(string imagePath)
@@ -48,9 +81,25 @@ namespace Avalar.Models
{
m_OriginalBitmap.Dispose();
m_LatestResult.Dispose();
+ m_CancellationTokenSource.Dispose();
}
disposed = true;
}
+
+ public void SetWidth(uint width)
+ {
+ m_Resizer.Width = width;
+ }
+
+ public void SetHeight(uint height)
+ {
+ m_Resizer.Height = height;
+ }
+
+ public void SetBrightnessDelta(int brightnessDelta)
+ {
+ m_Brightness.Brightness = brightnessDelta;
+ }
}
}