forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageInput.cs
More file actions
135 lines (114 loc) · 4.1 KB
/
ImageInput.cs
File metadata and controls
135 lines (114 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using TensorStack.Common;
using TensorStack.Common.Tensor;
namespace TensorStack.Image
{
/// <summary>
/// ImageInput implementation with System.Windows.Media.Imaging.WriteableBitmap.
/// </summary>
public class ImageInput : ImageInputBase
{
private readonly string _sourceFile;
private WriteableBitmap _image;
/// <summary>
/// Initializes a new instance of the <see cref="ImageInput"/> class.
/// </summary>
/// <param name="tensor">The tensor.</param>
public ImageInput(ImageTensor tensor) : base(tensor)
{
_image = tensor.ToBitmapImage();
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageInput"/> class.
/// </summary>
/// <param name="image">The image.</param>
public ImageInput(WriteableBitmap image)
: base(image.ToTensor())
{
_image = image;
if (!_image.IsFrozen)
_image.Freeze();
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageInput"/> class.
/// </summary>
/// <param name="image">The image.</param>
public ImageInput(BitmapSource image)
: this(new WriteableBitmap(image)) { }
/// <summary>
/// Initializes a new instance of the <see cref="ImageInput"/> class.
/// </summary>
/// <param name="filename">The filename.</param>
public ImageInput(string filename)
: this(ImageService.Load(filename))
{
_sourceFile = filename;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageInput"/> class.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="resizeMode">The resize mode.</param>
public ImageInput(string filename, int width, int height, ResizeMode resizeMode = ResizeMode.Stretch)
: this(ImageService.Load(filename))
{
Resize(width, height, resizeMode);
}
/// <summary>
/// Gets the image.
/// </summary>
public WriteableBitmap Image => _image;
/// <summary>
/// Gets the source Image filename.
/// </summary>
public override string SourceFile => _sourceFile;
/// <summary>
/// Saves the image.
/// </summary>
/// <param name="filename">The filename.</param>
public override void Save(string filename)
{
_image.Save(filename);
}
/// <summary>
/// Save the Image to file
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="cancellationToken">The cancellation token</param>
public override Task SaveAsync(string filename, CancellationToken cancellationToken = default)
{
return Task.Run(() => Save(filename), cancellationToken);
}
/// <summary>
/// Called when Tensor data has changed
/// </summary>
protected override void OnTensorDataChanged()
{
base.OnTensorDataChanged();
_image = this.ToBitmapImage();
}
/// <summary>
/// Releases resources.
/// </summary>
protected override void Dispose(bool disposing)
{
_image = null;
base.Dispose(disposing);
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageInput"/> class.
/// </summary>
/// <param name="filename">The filename.</param>
public static async Task<ImageInput> CreateAsync(string filename)
{
var bitmap = await ImageService.LoadFromFileAsync(filename);
return await Task.Run(() => new ImageInput(bitmap));
}
}
}