-
Notifications
You must be signed in to change notification settings - Fork 194
/
np_.extensions.cs
377 lines (341 loc) · 18.5 KB
/
np_.extensions.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Drawing.Imaging;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
// ReSharper disable once CheckNamespace
namespace NumSharp
{
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class np_
{
/// <summary>
/// Creates <see cref="NDArray"/> from given <see cref="Bitmap"/>.
/// </summary>
/// <param name="image">The image to load data from.</param>
/// <param name="flat">
/// If true, returns NDArray be 1-d of pixels: `R1G1B1R2G2B2 ... RnGnBn` where n is the amount of pixels in the image.<br></br>
/// If false, returns a 4-d NDArray shaped: (1, bmpData.Height, bmpData.Width, bbp)<br></br>
/// or (1, bmpData.Height, bmpData.Width, 4) if alpha is present and <paramref name="discardAlpha"/> is false.
/// </param>
/// <param name="copy">
/// If true, performs <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
/// and then copies the data to a new <see cref="NDArray"/> then finally releasing the locked bits.<br></br>
/// If false, It'll call <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
/// , wraps the <see cref="BitmapData.Scan0"/> with an NDArray and call <see cref="Bitmap.UnlockBits"/> only when the NDArray will be collected by the <see cref="GC"/>.
/// </param>
/// <param name="discardAlpha">If the given <see cref="Bitmap"/> has an alpha pixel (transparency pixel), discard that data or return a slice without the alpha (depends on <paramref name="copy"/>).</param>
/// <returns>An NDArray that holds the pixel data of the given bitmap</returns>
[SuppressMessage("ReSharper", "SuggestVarOrType_Elsewhere")]
public static unsafe NDArray ToNDArray(this System.Drawing.Bitmap image, bool flat = false, bool copy = true, bool discardAlpha = false)
{
if (image == null) throw new ArgumentNullException(nameof(image));
BitmapData bmpData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
if (copy)
try
{
unsafe
{
//Create a 1d vector without filling it's values to zero (similar to np.empty)
var nd = new NDArray(NPTypeCode.Byte, Shape.Vector(bmpData.Stride * image.Height), fillZeros: false);
// Get the respective addresses
byte* src = (byte*)bmpData.Scan0;
byte* dst = (byte*)nd.Unsafe.Address; //we can use unsafe because we just allocated that array and we know for sure it is contagious.
// Copy the RGB values into the array.
Buffer.MemoryCopy(src, dst, bmpData.Stride * image.Height, nd.size); //faster than Marshal.Copy
var ret = nd.reshape(1, image.Height, image.Width, bmpData.Stride / bmpData.Width);
if (discardAlpha && ret.shape[3] == 4)
ret = ret[Slice.All, Slice.All, Slice.All, new Slice(stop: 3)];
return flat && ret.ndim != 1 ? ret.flat : ret;
}
}
finally
{
try
{
image.UnlockBits(bmpData);
}
catch (ArgumentException)
{
//swallow
}
}
else
{
var ret = new NDArray(new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>((byte*)bmpData.Scan0, bmpData.Stride * bmpData.Height, () =>
{
try
{
image.UnlockBits(bmpData);
}
catch (ArgumentException)
{
//swallow
}
})));
if (flat)
{
if (discardAlpha)
{
if (bmpData.Stride / bmpData.Width == 4) //1byte-per-color
{
return ReshapeFlatData(ret, bmpData) // reshape
[Slice.All, Slice.All, Slice.All, new Slice(stop: 3)] //slice
.flat; //flatten
}
if (bmpData.Stride / bmpData.Width == 8) //2bytes-per-color
{
return ReshapeFlatData(ret, bmpData) // reshape
[Slice.All, Slice.All, Slice.All, new Slice(stop: 6)] //slice
.flat; //flatten
}
throw new NotSupportedException($"Given bbp ({bmpData.Stride / bmpData.Width}) is not supported.");
}
return ret.flat;
}
else
{
ret = ReshapeFlatData(ret, bmpData); //reshape
if (discardAlpha)
{
if (ret.shape[3] == 4) //1byte-per-color
ret = ret[Slice.All, Slice.All, Slice.All, new Slice(stop: 3)]; //slice
else if (ret.shape[3] == 8) //2bytes-per-color
ret = ret[Slice.All, Slice.All, Slice.All, new Slice(stop: 6)]; //slice
else
throw new NotSupportedException($"Given bbp ({bmpData.Stride / bmpData.Width}) is not supported.");
}
return ret;
}
}
}
/// <summary>
/// Reshapes the flat data to match the size of the bitmap.
/// </summary>
/// <param name="ret">flat 1-dimensional array containing the bitmap data</param>
/// <param name="bmpData">Source bitmap</param>
/// <returns>An 4-dimensional NDArray that holds the bitmap data contained in `ret`</returns>
private static NDArray ReshapeFlatData(NDArray ret, BitmapData bmpData)
{
var colorBytes = bmpData.Stride / bmpData.Width;
var strideWidth = bmpData.Stride / colorBytes; // For odd widths, this width is not equal to bmpData.Width
ret = ret.reshape(1, bmpData.Height, strideWidth, bmpData.Stride / bmpData.Width);
if (strideWidth != bmpData.Width)
{
ret = ret[Slice.All, Slice.All, new Slice(stop: bmpData.Width), new Slice(stop: colorBytes)];
}
return ret;
}
/// <summary>
/// Creates <see cref="NDArray"/> from given <see cref="Image"/>.
/// </summary>
/// <param name="image">The image to load data from.</param>
/// <param name="flat">
/// If true, returns NDArray be 1-d of pixels: `R1G1B1R2G2B2 ... RnGnBn` where n is the amount of pixels in the image.<br></br>
/// If false, returns a 4-d NDArray shaped: (1, bmpData.Height, bmpData.Width, bbp)
/// </param>
/// <param name="copy">
/// If true, performs <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
/// and then copies the data to a new <see cref="NDArray"/> then finally releasing the locked bits.<br></br>
/// If false, It'll call <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
/// , wraps the <see cref="BitmapData.Scan0"/> with an NDArray and call <see cref="Bitmap.UnlockBits"/> only when the NDArray will be collected by the <see cref="GC"/>.
/// </param>
/// <param name="discardAlpha">If the given <see cref="Bitmap"/> has an alpha pixel (transparency pixel), discard that data or return a slice without the alpha (depends on <paramref name="copy"/>).</param>
/// <returns>An NDArray that holds the pixel data of the given bitmap</returns>
public static NDArray ToNDArray(this Image image, bool flat = false, bool copy = true, bool discardAlpha = false)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
return ToNDArray(new System.Drawing.Bitmap(image), flat, copy, discardAlpha);
}
/// <summary>
/// Wraps given <see cref="BitmapData"/> as n <see cref="NDArray"/> without performing copy.
/// </summary>
/// <param name="bmpData">Targetted bitmap data with reading capabilities.</param>
/// <param name="flat">
/// If true, returns NDArray be 1-d of pixels: R1G1B1R2G2B2 ... RnGnBn where n is the amount of pixels in the image.<br></br>
/// If false, returns a 4-d NDArray shaped: (1, bmpData.Height, bmpData.Width, bbp)
/// </param>
/// <param name="discardAlpha">If the given <see cref="Bitmap"/> has an alpha pixel (transparency pixel), return a slice without the alpha.</param>
/// <returns>An NDArray that wraps the given bitmap, doesn't copy</returns>
/// <remarks>If the BitmapData is unlocked via <see cref="Bitmap.UnlockBits"/> - the NDArray will point to an invalid address which will cause heap corruption. Use with caution!</remarks>
public static unsafe NDArray AsNDArray(this BitmapData bmpData, bool flat = false, bool discardAlpha = false)
{
if (bmpData == null)
throw new ArgumentNullException(nameof(bmpData));
var ret = new NDArray(new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>((byte*)bmpData.Scan0, bmpData.Stride * bmpData.Height)));
if (flat)
{
if (ret.shape[3] == 4 && discardAlpha)
{
return ret.reshape(1, bmpData.Height, bmpData.Width, bmpData.Stride / bmpData.Width) //reshape
[Slice.All, Slice.All, Slice.All, new Slice(stop: 3)] //slice
.flat; //flatten
}
return ret;
}
else
{
ret = ret.reshape(1, bmpData.Height, bmpData.Width, bmpData.Stride / bmpData.Width); //reshape
if (ret.shape[3] == 4 && discardAlpha)
ret = ret[Slice.All, Slice.All, Slice.All, new Slice(stop: 3)]; //slice
return ret;
}
}
/// <summary>
/// Converts <see cref="NDArray"/> to a <see cref="Bitmap"/>.
/// </summary>
/// <param name="nd">The <see cref="NDArray"/> to copy pixels from, <see cref="Shape"/> is ignored completely. If nd.Unsafe.Shape.IsContiguous == false then a copy is made.</param>
/// <param name="width">The height of the <see cref="Bitmap"/></param>
/// <param name="height">The width of the <see cref="Bitmap"/></param>
/// <param name="format">The format of the expected bitmap, Must be matching to given NDArray otherwise unexpected results might occur.</param>
/// <returns>A <see cref="Bitmap"/></returns>
/// <exception cref="ArgumentException">When nd.size != width*height, which means the ndarray be turned into the given bitmap size.</exception>
public static unsafe Bitmap ToBitmap(this NDArray nd, int width, int height, PixelFormat format = PixelFormat.DontCare)
{
if (nd == null)
throw new ArgumentNullException(nameof(nd));
//if flat then initialize based on given format
if (nd.ndim == 1 && format != PixelFormat.DontCare)
nd = nd.reshape(1, height, width, format.ToBytesPerPixel()); //theres a check internally for size mismatc
if (nd.ndim != 4)
throw new ArgumentException("ndarray was expected to be of 4-dimensions, (1, bmpData.Height, bmpData.Width, bytesPerPixel)");
if (nd.shape[0] != 1)
throw new ArgumentException($"ndarray has more than one picture in it ({nd.shape[0]}) based on the first dimension.");
var bbp = nd.shape[3]; //bytes per pixel.
if (bbp != extractFormatNumber())
throw new ArgumentException($"Given PixelFormat: {format} does not match the number of bytes per pixel in the 4th dimension of given ndarray.");
if (bbp * width * height != nd.size)
throw new ArgumentException($"The expected size does not match the size of given ndarray. (expected: {bbp * width * height}, actual: {nd.size})");
var ret = new Bitmap(width, height, format);
var bitdata = ret.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, format);
if (bitdata.Stride != width * format.ToBytesPerPixel())
nd = np.concatenate((nd, np.zeros((1, height, 1, format.ToBytesPerPixel())).astype(NPTypeCode.Byte)), 2);
try
{
var dst = new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>((byte*)bitdata.Scan0, bitdata.Stride * bitdata.Height));
if (nd.Shape.IsContiguous)
nd.CopyTo(dst);
else
MultiIterator.Assign(new UnmanagedStorage(dst, Shape.Vector(bitdata.Stride * bitdata.Height)), nd.Unsafe.Storage);
}
finally
{
try
{
ret.UnlockBits(bitdata);
}
catch (ArgumentException)
{
//swallow
}
}
return ret;
int extractFormatNumber()
{
if (format == PixelFormat.DontCare)
{
switch (bbp)
{
case 3:
format = PixelFormat.Format24bppRgb;
break;
case 4:
format = PixelFormat.Format32bppArgb;
break;
case 6:
format = PixelFormat.Format48bppRgb;
break;
case 8:
format = PixelFormat.Format64bppArgb;
break;
}
return bbp;
}
return format.ToBytesPerPixel();
}
}
/// <summary>
/// Converts <see cref="NDArray"/> to a <see cref="Bitmap"/>.
/// </summary>
/// <param name="nd">The <see cref="NDArray"/> to copy pixels from, <see cref="Shape"/> is ignored completely. If nd.Unsafe.Shape.IsContiguous == false then a copy is made.</param>
/// <param name="format">The format of the expected bitmap, Must be matching to given NDArray otherwise unexpected results might occur.</param>
/// <returns>A <see cref="Bitmap"/></returns>
/// <exception cref="ArgumentException">When nd.size != width*height, which means the ndarray be turned into the given bitmap size.</exception>
public static Bitmap ToBitmap(this NDArray nd, PixelFormat format = PixelFormat.DontCare)
{
if (nd == null)
throw new ArgumentNullException(nameof(nd));
if (nd.ndim != 4)
throw new ArgumentException("ndarray was expected to be of 4-dimensions, (1, bmpData.Height, bmpData.Width, bytesPerPixel)");
if (nd.shape[0] != 1)
throw new ArgumentException($"ndarray has more than one picture in it ({nd.shape[0]}) based on the first dimension.");
var height = nd.shape[1];
var width = nd.shape[2];
return ToBitmap(nd, width, height, format);
}
/// <summary>
/// Returns how many bytes are per pixel based on given <paramref name="format"/>.
/// </summary>
/// <exception cref="ArgumentException">The format is not supported by us.</exception>
/// <exception cref="ArgumentOutOfRangeException">Invalid PixelFormat enum value.</exception>
public static int ToBytesPerPixel(this PixelFormat format)
{
int ret;
switch (format)
{
case PixelFormat.Format16bppArgb1555:
ret = 16;
break;
case PixelFormat.Format16bppGrayScale:
ret = 16;
break;
case PixelFormat.Format16bppRgb555:
ret = 16;
break;
case PixelFormat.Format16bppRgb565:
ret = 16;
break;
case PixelFormat.Format24bppRgb:
ret = 24;
break;
case PixelFormat.Format32bppArgb:
ret = 32;
break;
case PixelFormat.Format32bppPArgb:
ret = 32;
break;
case PixelFormat.Format32bppRgb:
ret = 32;
break;
case PixelFormat.Format48bppRgb:
ret = 48;
break;
case PixelFormat.Format64bppArgb:
ret = 64;
break;
case PixelFormat.Format64bppPArgb:
ret = 64;
break;
case PixelFormat.Format1bppIndexed:
case PixelFormat.Format8bppIndexed:
case PixelFormat.Format4bppIndexed:
case PixelFormat.Alpha:
case PixelFormat.Canonical:
case PixelFormat.Extended:
case PixelFormat.Gdi:
case PixelFormat.Indexed:
case PixelFormat.Max:
case PixelFormat.PAlpha:
throw new ArgumentException($"Given PixelFormat: {format} is not supported.");
case PixelFormat.DontCare:
throw new ArgumentException("Given PixelFormat can't be DontCare.");
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
return ret / 8;
}
}
}