Skip to content

Commit de0ec98

Browse files
committed
Refactor Image projects
1 parent 4b50c6f commit de0ec98

File tree

8 files changed

+161
-16
lines changed

8 files changed

+161
-16
lines changed

TensorStack.Image.Bitmap/ImageInput.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) TensorStack. All rights reserved.
22
// Licensed under the Apache 2.0 License.
33
using System.Drawing;
4+
using System.Threading;
5+
using System.Threading.Tasks;
46
using TensorStack.Common;
57
using TensorStack.Common.Tensor;
68

@@ -9,8 +11,9 @@ namespace TensorStack.Image
911
/// <summary>
1012
/// ImageInput implementation with System.Drawing.Bitmap.
1113
/// </summary>
12-
public class ImageInput : ImageTensor
14+
public class ImageInput : ImageInputBase
1315
{
16+
private readonly string _sourceFile;
1417
private Bitmap _image;
1518

1619
/// <summary>
@@ -38,7 +41,10 @@ public ImageInput(Bitmap image)
3841
/// </summary>
3942
/// <param name="filename">The filename.</param>
4043
public ImageInput(string filename)
41-
: this(new Bitmap(filename)) { }
44+
: this(new Bitmap(filename))
45+
{
46+
_sourceFile = filename;
47+
}
4248

4349

4450
/// <summary>
@@ -59,17 +65,34 @@ public ImageInput(string filename, int width, int height, ResizeMode resizeMode
5965
/// </summary>
6066
public Bitmap Image => _image;
6167

68+
/// <summary>
69+
/// Gets the source filename.
70+
/// </summary>
71+
public override string SourceFile => _sourceFile;
72+
6273

6374
/// <summary>
6475
/// Saves the image.
6576
/// </summary>
6677
/// <param name="filename">The filename.</param>
67-
public void Save(string filename)
78+
public override void Save(string filename)
6879
{
6980
_image.Save(filename);
7081
}
7182

7283

84+
/// <summary>
85+
/// Save the Image to file
86+
/// </summary>
87+
/// <param name="filename">The filename.</param>
88+
/// <param name="cancellationToken">The cancellation token</param>
89+
/// <returns>Task.</returns>
90+
public override Task SaveAsync(string filename, CancellationToken cancellationToken = default)
91+
{
92+
return Task.Run(() => Save(filename), cancellationToken);
93+
}
94+
95+
7396
/// <summary>
7497
/// Called when Tensor data has changed
7598
/// </summary>

TensorStack.Image.Bitmap/TensorStack.Image.Bitmap.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
<!--Projects-->
1313
<ItemGroup Condition=" '$(Configuration)' == 'Debug'">
14-
<ProjectReference Include="..\TensorStack.Common\TensorStack.Common.csproj" />
14+
<ProjectReference Include="..\TensorStack.Image\TensorStack.Image.csproj" />
1515
</ItemGroup>
1616

1717
<!--Packages-->
1818
<ItemGroup Condition=" '$(Configuration)' == 'Release'">
19-
<PackageReference Include="TensorStack.Common" Version="$(Version)" />
19+
<PackageReference Include="TensorStack.Image" Version="$(Version)" />
2020
</ItemGroup>
2121

2222
<!--Common Packages-->

TensorStack.Image.BitmapImage/ImageInput.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) TensorStack. All rights reserved.
22
// Licensed under the Apache 2.0 License.
3+
using System.Threading;
4+
using System.Threading.Tasks;
35
using System.Windows.Media.Imaging;
46
using TensorStack.Common;
57
using TensorStack.Common.Tensor;
@@ -9,8 +11,9 @@ namespace TensorStack.Image
911
/// <summary>
1012
/// ImageInput implementation with System.Windows.Media.Imaging.WriteableBitmap.
1113
/// </summary>
12-
public class ImageInput : ImageTensor
14+
public class ImageInput : ImageInputBase
1315
{
16+
private readonly string _sourceFile;
1417
private WriteableBitmap _image;
1518

1619
/// <summary>
@@ -47,7 +50,10 @@ public ImageInput(BitmapSource image)
4750
/// </summary>
4851
/// <param name="filename">The filename.</param>
4952
public ImageInput(string filename)
50-
: this(ImageService.Load(filename)) { }
53+
: this(ImageService.Load(filename))
54+
{
55+
_sourceFile = filename;
56+
}
5157

5258

5359
/// <summary>
@@ -69,17 +75,33 @@ public ImageInput(string filename, int width, int height, ResizeMode resizeMode
6975
/// </summary>
7076
public WriteableBitmap Image => _image;
7177

78+
/// <summary>
79+
/// Gets the source Image filename.
80+
/// </summary>
81+
public override string SourceFile => _sourceFile;
82+
7283

7384
/// <summary>
7485
/// Saves the image.
7586
/// </summary>
7687
/// <param name="filename">The filename.</param>
77-
public void Save(string filename)
88+
public override void Save(string filename)
7889
{
7990
_image.Save(filename);
8091
}
8192

8293

94+
/// <summary>
95+
/// Save the Image to file
96+
/// </summary>
97+
/// <param name="filename">The filename.</param>
98+
/// <param name="cancellationToken">The cancellation token</param>
99+
public override Task SaveAsync(string filename, CancellationToken cancellationToken = default)
100+
{
101+
return Task.Run(() => Save(filename), cancellationToken);
102+
}
103+
104+
83105
/// <summary>
84106
/// Called when Tensor data has changed
85107
/// </summary>

TensorStack.Image.BitmapImage/TensorStack.Image.BitmapImage.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
<!--Projects-->
1313
<ItemGroup Condition=" '$(Configuration)' == 'Debug'">
14-
<ProjectReference Include="..\TensorStack.Common\TensorStack.Common.csproj" />
14+
<ProjectReference Include="..\TensorStack.Image\TensorStack.Image.csproj" />
1515
</ItemGroup>
1616

1717
<!--Packages-->
1818
<ItemGroup Condition=" '$(Configuration)' == 'Release'">
19-
<PackageReference Include="TensorStack.Common" Version="$(Version)" />
19+
<PackageReference Include="TensorStack.Image" Version="$(Version)" />
2020
</ItemGroup>
2121

2222

TensorStack.Image.ImageSharp/ImageInput.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
// Licensed under the Apache 2.0 License.
33
using SixLabors.ImageSharp;
44
using SixLabors.ImageSharp.PixelFormats;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using TensorStack.Common.Tensor;
68

79
namespace TensorStack.Image
810
{
911
/// <summary>
1012
/// ImageInput implementation with ImageSharp Image<Rgba32>.
1113
/// </summary>
12-
public class ImageInput : ImageTensor
14+
public class ImageInput : ImageInputBase
1315
{
16+
private readonly string _sourceFile;
1417
private Image<Rgba32> _image;
1518

1619
/// <summary>
@@ -38,7 +41,10 @@ public ImageInput(Image<Rgba32> image)
3841
/// </summary>
3942
/// <param name="filename">The filename.</param>
4043
public ImageInput(string filename)
41-
: this(Image<Rgba32>.Load<Rgba32>(filename)) { }
44+
: this(Image<Rgba32>.Load<Rgba32>(filename))
45+
{
46+
_sourceFile = filename;
47+
}
4248

4349

4450
/// <summary>
@@ -59,17 +65,32 @@ public ImageInput(string filename, int width, int height, Common.ResizeMode resi
5965
/// </summary>
6066
public Image<Rgba32> Image => _image;
6167

68+
/// <summary>
69+
/// Gets the source Image filename.
70+
/// </summary>
71+
public override string SourceFile => _sourceFile;
72+
6273

6374
/// <summary>
6475
/// Saves the specified image.
6576
/// </summary>
66-
/// <param name="filename">The filename.</param>
67-
public void Save(string filename)
77+
public override void Save(string filename)
6878
{
6979
_image.SaveAsPng(filename);
7080
}
7181

7282

83+
/// <summary>
84+
/// Save the Image to file
85+
/// </summary>
86+
/// <param name="filename">The filename.</param>
87+
/// <param name="cancellationToken">The cancellation token</param>
88+
public override async Task SaveAsync(string filename, CancellationToken cancellationToken = default)
89+
{
90+
await _image.SaveAsPngAsync(filename);
91+
}
92+
93+
7394
/// <summary>
7495
/// Called when Tensor data has changed
7596
/// </summary>

TensorStack.Image.ImageSharp/TensorStack.Image.ImageSharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
<!--Projects-->
1010
<ItemGroup Condition=" '$(Configuration)' == 'Debug'">
11-
<ProjectReference Include="..\TensorStack.Common\TensorStack.Common.csproj" />
11+
<ProjectReference Include="..\TensorStack.Image\TensorStack.Image.csproj" />
1212
</ItemGroup>
1313

1414
<!--Packages-->
1515
<ItemGroup Condition=" '$(Configuration)' == 'Release'">
16-
<PackageReference Include="TensorStack.Common" Version="$(Version)" />
16+
<PackageReference Include="TensorStack.Image" Version="$(Version)" />
1717
</ItemGroup>
1818

1919
<!--Common Packages-->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) TensorStack. All rights reserved.
2+
// Licensed under the Apache 2.0 License.
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using TensorStack.Common.Tensor;
6+
7+
namespace TensorStack.Image
8+
{
9+
public abstract class ImageInputBase : ImageTensor
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="ImageInputBase"/> class.
13+
/// </summary>
14+
/// <param name="imageTensor">The Image tensor.</param>
15+
protected ImageInputBase(ImageTensor imageTensor)
16+
: base(imageTensor) { }
17+
18+
/// <summary>
19+
/// Gets the source Image filename.
20+
/// </summary>
21+
public abstract string SourceFile { get; }
22+
23+
/// <summary>
24+
/// Save the Image to file
25+
/// </summary>
26+
/// <param name="filename">The filename.</param>
27+
public abstract void Save(string filename);
28+
29+
/// <summary>
30+
/// Save the Image to file
31+
/// </summary>
32+
/// <param name="filename">The filename.</param>
33+
/// <param name="cancellationToken">The cancellation token</param>
34+
public abstract Task SaveAsync(string filename, CancellationToken cancellationToken = default);
35+
}
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<PlatformTarget>x64</PlatformTarget>
6+
<Description></Description>
7+
</PropertyGroup>
8+
9+
<!--Projects-->
10+
<ItemGroup Condition=" '$(Configuration)' == 'Debug'">
11+
<ProjectReference Include="..\TensorStack.Common\TensorStack.Common.csproj" />
12+
</ItemGroup>
13+
14+
<!--Packages-->
15+
<ItemGroup Condition=" '$(Configuration)' == 'Release'">
16+
<PackageReference Include="TensorStack.Common" Version="$(Version)" />
17+
</ItemGroup>
18+
19+
20+
<!--Nuget Settings-->
21+
<PropertyGroup>
22+
<Title>$(AssemblyName)</Title>
23+
<PackageId>$(AssemblyName)</PackageId>
24+
<Product>$(AssemblyName)</Product>
25+
<PackageIcon>Icon.png</PackageIcon>
26+
</PropertyGroup>
27+
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
28+
<None Remove="README.md" />
29+
<None Remove="Icon.png" />
30+
</ItemGroup>
31+
<ItemGroup Condition="'$(Configuration)' == 'Release'">
32+
<None Remove="Add.png" />
33+
<None Update="README.md">
34+
<Pack>True</Pack>
35+
<PackagePath>\</PackagePath>
36+
</None>
37+
<None Include="..\Assets\Icon.png">
38+
<Pack>True</Pack>
39+
<PackagePath>\</PackagePath>
40+
</None>
41+
</ItemGroup>
42+
43+
</Project>

0 commit comments

Comments
 (0)