-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathMLImage.cs
More file actions
520 lines (442 loc) · 18 KB
/
MLImage.cs
File metadata and controls
520 lines (442 loc) · 18 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.ML.Transforms.Image;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator;
namespace Microsoft.ML.Data
{
/// <summary>
/// Provide interfaces for imaging operations.
/// </summary>
public sealed class MLImage : IDisposable
{
private SKBitmap _image;
private MLPixelFormat _pixelFormat;
private string _tag;
// disallow instantiating image object from the default constructor
private MLImage()
{
}
/// <summary>
/// Create a new MLImage instance from a stream.
/// </summary>
/// <param name="imageStream">The stream to create the image from.</param>
/// <returns>MLImage object.</returns>
public static MLImage CreateFromStream(Stream imageStream)
{
if (imageStream is null)
{
throw new ArgumentNullException(nameof(imageStream));
}
SKBitmap image = SKBitmap.Decode(imageStream);
if (image is null)
{
throw new ArgumentException($"Invalid input stream contents", nameof(imageStream));
}
return new MLImage(image);
}
/// <summary>
/// Create a new MLImage instance from a stream.
/// </summary>
/// <param name="imagePath">The image file path to create the image from.</param>
/// <returns>MLImage object.</returns>
public static MLImage CreateFromFile(string imagePath)
{
if (imagePath is null)
{
throw new ArgumentNullException(nameof(imagePath));
}
SKBitmap image = SKBitmap.Decode(imagePath);
if (image is null)
{
throw new ArgumentException($"Invalid path", nameof(imagePath));
}
return new MLImage(image);
}
/// <summary>
/// Creates MLImage object from the pixel data span.
/// </summary>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="pixelFormat">The pixel format to create the image with.</param>
/// <param name="imagePixelData">The pixels data to create the image from.</param>
/// <returns>MLImage object.</returns>
public static unsafe MLImage CreateFromPixels(int width, int height, MLPixelFormat pixelFormat, ReadOnlySpan<byte> imagePixelData)
{
if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)
{
throw new ArgumentException($"Unsupported pixel format", nameof(pixelFormat));
}
if (width <= 0)
{
throw new ArgumentException($"Invalid width value.", nameof(width));
}
if (height <= 0)
{
throw new ArgumentException($"Invalid height value.", nameof(height));
}
if (imagePixelData.Length != width * height * 4)
{
throw new ArgumentException($"Invalid {nameof(imagePixelData)} buffer size.");
}
SKBitmap image = new SKBitmap(new SKImageInfo(width, height, pixelFormat == MLPixelFormat.Bgra32 ? SKColorType.Bgra8888 : SKColorType.Rgba8888));
Debug.Assert(image.Info.BitsPerPixel == 32);
Debug.Assert(image.RowBytes * image.Height == width * height * 4);
imagePixelData.CopyTo(new Span<byte>(image.GetPixels().ToPointer(), image.Width * image.Height * 4));
return new MLImage(image);
}
/// <summary>
/// Gets the pixel format for this Image.
/// </summary>
public MLPixelFormat PixelFormat
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
return _pixelFormat;
}
private set
{
Debug.Assert(_image is not null);
_pixelFormat = value;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "MSML_GeneralName:This name should be PascalCased", Justification = "<Pending>")]
public byte[] GetBGRPixels
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
// 3 is because we only want RGB not alpha channels
byte[] pixels = new byte[Height * Width * 3];
var pixelData = _image.Pixels;
int idx = 0;
for (int i = 0; i < Height * Width * 3;)
{
pixels[i++] = pixelData[idx].Blue;
pixels[i++] = pixelData[idx].Green;
pixels[i++] = pixelData[idx++].Red;
}
return pixels;
}
}
/// <summary>
/// Gets the image pixel data.
/// </summary>
public unsafe ReadOnlySpan<byte> Pixels
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
Debug.Assert(_image.Info.BytesPerPixel == 4);
var pixelsPtr = _image.GetPixels();
if (pixelsPtr == IntPtr.Zero || _image.ByteCount <= 0)
throw new InvalidOperationException("Pixel data is unavailable.");
return new ReadOnlySpan<byte>(pixelsPtr.ToPointer(), _image.ByteCount);
}
}
/// <summary>
/// Gets or sets the image tag.
/// </summary>
public string Tag
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
return _tag;
}
set
{
ThrowInvalidOperationExceptionIfDisposed();
_tag = value;
}
}
/// <summary>
/// Gets the image width in pixels.
/// </summary>
public int Width
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
return _image.Width;
}
}
/// <summary>
/// Gets the image height in pixels.
/// </summary>
public int Height
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
return _image.Height;
}
}
/// <summary>
/// Gets how many bits per pixel used by current image object.
/// </summary>
public int BitsPerPixel
{
get
{
ThrowInvalidOperationExceptionIfDisposed();
Debug.Assert(_image.Info.BitsPerPixel == 32);
return _image.Info.BitsPerPixel;
}
}
/// <summary>
/// Save the current image to a file.
/// </summary>
/// <param name="imagePath">The path of the file to save the image to.</param>
/// <remarks>The saved image encoding will be detected from the file extension.</remarks>
public void Save(string imagePath)
{
ThrowInvalidOperationExceptionIfDisposed();
string ext = Path.GetExtension(imagePath);
if (!_extensionToEncodingFormat.TryGetValue(ext, out SKEncodedImageFormat encodingFormat))
{
throw new ArgumentException($"Path has invalid image file extension.", nameof(imagePath));
}
using SKData data = _image.Encode(encodingFormat, 100);
if (data is null)
{
throw new ArgumentException($"Saving image with the format '{ext}' is not supported. Try save it with `Jpeg`, `Png`, or `Webp` format.", nameof(imagePath));
}
using var stream = new FileStream(imagePath, FileMode.Create, FileAccess.Write);
data.SaveTo(stream);
}
/// <summary>
/// Disposes the image.
/// </summary>
public void Dispose()
{
if (_image != null)
{
_image.Dispose();
_image = null;
}
}
private MLImage(SKBitmap image)
{
_image = image;
PixelFormat = _image.Info.ColorType switch
{
SKColorType.Bgra8888 => MLPixelFormat.Bgra32,
SKColorType.Rgba8888 => MLPixelFormat.Rgba32,
_ => CloneImageToSupportedFormat()
};
}
private MLPixelFormat CloneImageToSupportedFormat()
{
Debug.Assert(_image.Info.ColorType != SKColorType.Bgra8888 && _image.Info.ColorType != SKColorType.Rgba8888);
if (!_image.CanCopyTo(SKColorType.Bgra8888))
{
throw new InvalidOperationException("Unsupported image format.");
}
SKBitmap image1 = _image.Copy(SKColorType.Bgra8888);
_image.Dispose();
_image = image1;
return MLPixelFormat.Bgra32;
}
internal MLImage CloneWithResizing(int width, int height, ImageResizeMode mode)
{
ThrowInvalidOperationExceptionIfDisposed();
SKBitmap image = mode switch
{
ImageResizeMode.Pad => ResizeWithPadding(width, height),
ImageResizeMode.Fill => ResizeFull(width, height),
>= ImageResizeMode.CropAnchorTop and <= ImageResizeMode.CropAnchorCentral => ResizeWithCrop(width, height, mode),
_ => throw new ArgumentException($"Invalid resize mode value.", nameof(mode))
};
if (image is null)
{
throw new InvalidOperationException($"Couldn't resize the image");
}
return new MLImage(image);
}
private SKBitmap ResizeFull(int width, int height) => _image.Resize(new SKSizeI(width, height), new SKSamplingOptions(SKFilterMode.Nearest));
private SKBitmap ResizeWithPadding(int width, int height)
{
float widthAspect = (float)width / _image.Width;
float heightAspect = (float)height / _image.Height;
int destX = 0;
int destY = 0;
float aspect;
if (heightAspect < widthAspect)
{
aspect = heightAspect;
destX = (int)((width - (_image.Width * aspect)) / 2);
}
else
{
aspect = widthAspect;
destY = (int)((height - (_image.Height * aspect)) / 2);
}
int destWidth = (int)(_image.Width * aspect);
int destHeight = (int)(_image.Height * aspect);
SKBitmap destBitmap = new SKBitmap(width, height, isOpaque: true);
SKRect srcRect = new SKRect(0, 0, _image.Width, _image.Height);
SKRect destRect = new SKRect(destX, destY, destX + destWidth, destY + destHeight);
using SKCanvas canvas = new SKCanvas(destBitmap);
using SKImage image = SKImage.FromBitmap(_image);
canvas.DrawImage(image, srcRect, destRect, new SKSamplingOptions(SKCubicResampler.Mitchell));
return destBitmap;
}
private SKBitmap ResizeWithCrop(int width, int height, ImageResizeMode mode)
{
float widthAspect = (float)width / _image.Width;
float heightAspect = (float)height / _image.Height;
int destX = 0;
int destY = 0;
float aspect;
if (heightAspect < widthAspect)
{
aspect = widthAspect;
switch (mode)
{
case ImageResizeMode.CropAnchorTop:
destY = 0;
break;
case ImageResizeMode.CropAnchorBottom:
destY = (int)(height - (_image.Height * aspect));
break;
default:
destY = (int)((height - (_image.Height * aspect)) / 2);
break;
}
}
else
{
aspect = heightAspect;
switch (mode)
{
case ImageResizeMode.CropAnchorLeft:
destX = 0;
break;
case ImageResizeMode.CropAnchorRight:
destX = (int)(width - (_image.Width * aspect));
break;
default:
destX = (int)((width - (_image.Width * aspect)) / 2);
break;
}
}
int destWidth = (int)(_image.Width * aspect);
int destHeight = (int)(_image.Height * aspect);
SKBitmap dst = new SKBitmap(width, height, isOpaque: true);
SKRect srcRect = new SKRect(0, 0, _image.Width, _image.Height);
SKRect destRect = new SKRect(destX, destY, destX + destWidth, destY + destHeight);
using SKCanvas canvas = new SKCanvas(dst);
using SKImage image = SKImage.FromBitmap(_image);
canvas.DrawImage(image, srcRect, destRect, new SKSamplingOptions(SKCubicResampler.Mitchell));
return dst;
}
// This matrix get multiplied to every pixel matrix [R G B A W] to average the colors values to get the grayscale effect.
private static readonly SKColorFilter _grayscaleColorMatrix = SKColorFilter.CreateColorMatrix(new float[]
{
0.3f, 0.59f, 0.11f, 0, 0,
0.3f, 0.59f, 0.11f, 0, 0,
0.3f, 0.59f, 0.11f, 0, 0,
0, 0, 0, 1, 0
});
internal MLImage CloneWithGrayscale()
{
ThrowInvalidOperationExceptionIfDisposed();
SKBitmap dst = new SKBitmap(_image.Width, _image.Height, isOpaque: true);
using SKPaint paint = new SKPaint()
{
ColorFilter = _grayscaleColorMatrix,
};
SKBitmap destBitmap = new SKBitmap(_image.Width, _image.Height, isOpaque: true);
using SKCanvas canvas = new SKCanvas(destBitmap);
canvas.DrawBitmap(_image, 0f, 0f, paint: paint);
return new MLImage(destBitmap);
}
private static readonly Dictionary<string, SKEncodedImageFormat> _extensionToEncodingFormat = new Dictionary<string, SKEncodedImageFormat>(StringComparer.OrdinalIgnoreCase)
{
{ ".bmp", SKEncodedImageFormat.Bmp },
{ ".png", SKEncodedImageFormat.Png },
{ ".jpg", SKEncodedImageFormat.Jpeg },
{ ".jpeg", SKEncodedImageFormat.Jpeg },
{ ".gif", SKEncodedImageFormat.Gif },
{ ".ico", SKEncodedImageFormat.Ico },
{ ".astc", SKEncodedImageFormat.Astc },
{ ".avif", SKEncodedImageFormat.Avif },
{ ".dng", SKEncodedImageFormat.Dng },
{ ".heif", SKEncodedImageFormat.Heif },
{ ".ktx", SKEncodedImageFormat.Ktx },
{ ".pkm", SKEncodedImageFormat.Pkm },
{ ".wbmp", SKEncodedImageFormat.Wbmp },
{ ".webp", SKEncodedImageFormat.Webp }
};
private void ThrowInvalidOperationExceptionIfDisposed()
{
if (_image is null)
{
throw new InvalidOperationException("Object is disposed.");
}
}
}
/// <summary>
/// Specifies the format of the color data for each pixel in the image.
/// </summary>
public enum MLPixelFormat
{
/// <summary>
/// The pixel format is unknown.
/// </summary>
Unknown,
/// <summary>
/// Specifies that the format is 32 bits per pixel; 8 bits each are used for the blue, green, red, and alpha components.
/// The color components are stored in blue, green, red, and alpha order
/// </summary>
Bgra32,
/// <summary>
/// Specifies that the format is 32 bits per pixel; 8 bits each are used for the red, green, blue, and alpha components.
/// The color components are stored in red, green, blue, and alpha order
/// </summary>
Rgba32
}
/// <summary>
/// The mode to decide how the image should be resized.
/// </summary>
internal enum ImageResizeMode
{
/// <summary>
/// Pads the resized image to fit the bounds of its container.
/// </summary>
Pad,
/// <summary>
/// Ignore aspect ratio and squeeze/stretch into target dimensions.
/// </summary>
Fill,
/// <summary>
/// Resized image to fit the bounds of its container using cropping with top anchor.
/// </summary>
CropAnchorTop,
/// <summary>
/// Resized image to fit the bounds of its container using cropping with bottom anchor.
/// </summary>
CropAnchorBottom,
/// <summary>
/// Resized image to fit the bounds of its container using cropping with left anchor.
/// </summary>
CropAnchorLeft,
/// <summary>
/// Resized image to fit the bounds of its container using cropping with right anchor.
/// </summary>
CropAnchorRight,
/// <summary>
/// Resized image to fit the bounds of its container using cropping with central anchor.
/// </summary>
CropAnchorCentral
}
}