forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
180 lines (159 loc) · 6.93 KB
/
Extensions.cs
File metadata and controls
180 lines (159 loc) · 6.93 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TensorStack.Common;
using TensorStack.Common.Tensor;
namespace TensorStack.Image
{
public static class Extensions
{
/// <summary>
/// Converts ImageTensor to BitmapSource.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>BitmapSource.</returns>
public static BitmapSource ToImage(this ImageTensor imageTensor)
{
return imageTensor.ToBitmapImage();
}
/// <summary>
/// Converts ImageTensorBase to ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>ImageTensor.</returns>
public static ImageInput ToImageInput(this ImageTensor imageTensor)
{
return new ImageInput(imageTensor);
}
/// <summary>
/// Converts ImageTensorBase to ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>ImageTensor.</returns>
public static async Task<ImageInput> ToImageInputAsync(this ImageTensor imageTensor)
{
return await Task.Run(() => new ImageInput(imageTensor));
}
/// <summary>
/// Saves the ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <param name="filename">The filename.</param>
public static Task SaveAsync(this ImageTensor imageTensor, string filename)
{
return Task.Run(() =>
{
using (var image = imageTensor.ToImageInput())
{
image.Save(filename);
}
});
}
/// <summary>
/// Saves the ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <param name="filename">The filename.</param>
public static Task SaveAsync(this ImageInput imageTensor, string filename)
{
return Task.Run(() => imageTensor.Save(filename));
}
/// <summary>
/// Saves the specified image to file.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="filePath">The file path.</param>
internal static void Save(this WriteableBitmap bitmap, string filePath)
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
encoder.Save(stream);
}
}
/// <summary>
/// Converts WriteableBitmap to ImageTensor.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <returns>ImageTensor.</returns>
internal static ImageTensor ToTensor(this WriteableBitmap bitmapSource)
{
var writeableBitmap = bitmapSource.ToWriteableBitmap();
var width = writeableBitmap.PixelWidth;
var height = writeableBitmap.PixelHeight;
var stride = writeableBitmap.BackBufferStride;
var buffer = new byte[stride * height];
writeableBitmap.CopyPixels(buffer, stride, 0);
var hw = height * width;
var tensor = new ImageTensor(height, width);
var dataSpan = tensor.Memory.Span;
var bufferSpan = buffer.AsSpan();
for (int y = 0; y < height; y++)
{
int rowStart = y * stride;
for (int x = 0; x < width; x++)
{
int offset = y * width + x;
int pixelIndex = rowStart + x * 4; // BGRA in buffer
dataSpan[offset] = bufferSpan[pixelIndex + 2].NormalizeToFloat(); // R
dataSpan[hw + offset] = bufferSpan[pixelIndex + 1].NormalizeToFloat(); // G
dataSpan[2 * hw + offset] = bufferSpan[pixelIndex + 0].NormalizeToFloat(); // B
dataSpan[3 * hw + offset] = bufferSpan[pixelIndex + 3].NormalizeToFloat(); // A
}
}
return tensor;
}
/// <summary>
/// Converts ImageTensor to WriteableBitmap.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>WriteableBitmap.</returns>
internal static WriteableBitmap ToBitmapImage(this ImageTensor imageTensor)
{
var height = imageTensor.Dimensions[2];
var width = imageTensor.Dimensions[3];
var stride = width * 4;
var pixelBuffer = new byte[height * stride];
var writeableBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int pixelIndex = (y * width + x) * 4;
pixelBuffer[pixelIndex + 0] = imageTensor[0, 2, y, x].DenormalizeToByte(); // B
pixelBuffer[pixelIndex + 1] = imageTensor[0, 1, y, x].DenormalizeToByte(); // G
pixelBuffer[pixelIndex + 2] = imageTensor[0, 0, y, x].DenormalizeToByte(); // R
pixelBuffer[pixelIndex + 3] = imageTensor[0, 3, y, x].DenormalizeToByte(); // A
}
}
writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), pixelBuffer, stride, 0);
writeableBitmap.Freeze();
return writeableBitmap;
}
internal static WriteableBitmap ToWriteableBitmap(this BitmapSource bitmapSource)
{
if (bitmapSource.Format == PixelFormats.Bgra32 || bitmapSource.Format == PixelFormats.Bgr32)
{
if (bitmapSource is WriteableBitmap writeableBitmap)
return writeableBitmap;
writeableBitmap = new WriteableBitmap(bitmapSource);
writeableBitmap.Freeze();
return writeableBitmap;
}
// Convert to BGRA32 WriteableBitmap
var convertTarget = new WriteableBitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, bitmapSource.DpiX, bitmapSource.DpiY, PixelFormats.Bgra32, null);
var stride = convertTarget.PixelWidth * (convertTarget.Format.BitsPerPixel / 8);
var buffer = new byte[stride * convertTarget.PixelHeight];
var convertSource = new FormatConvertedBitmap(bitmapSource, PixelFormats.Bgra32, null, 0);
convertSource.CopyPixels(buffer, stride, 0);
convertTarget.WritePixels(new Int32Rect(0, 0, convertTarget.PixelWidth, convertTarget.PixelHeight), buffer, stride, 0);
convertTarget.Freeze();
return convertTarget;
}
}
}