Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Bitmaps.cs
1using SkiaSharp;
2using System;
3using System.Globalization;
4using System.IO;
5
6namespace IdApp.Cv
7{
11 public static class Bitmaps
12 {
18 public static IMatrix FromBitmapFile(string FileName)
19 {
20 return FromBitmapFile(FileName, int.MaxValue, int.MaxValue);
21 }
22
32 public static IMatrix FromBitmapFile(string FileName, int MaxWidth, int MaxHeight)
33 {
34 using SKBitmap Bmp = SKBitmap.Decode(FileName);
35 return FromBitmap(Bmp, MaxWidth, MaxHeight);
36 }
37
43 public static IMatrix FromBitmapFile(byte[] Data)
44 {
45 return FromBitmapFile(Data, int.MaxValue, int.MaxValue);
46 }
47
55 public static IMatrix FromBitmapFile(byte[] Data, int MaxWidth, int MaxHeight)
56 {
57 using SKBitmap Bmp = SKBitmap.Decode(Data);
58 return FromBitmap(Bmp, MaxWidth, MaxHeight);
59 }
60
66 public static IMatrix FromBitmap(SKBitmap Bmp)
67 {
68 return FromBitmap(Bmp, int.MaxValue, int.MaxValue);
69 }
70
78 public static IMatrix FromBitmap(SKBitmap Bmp, int MaxWidth, int MaxHeight)
79 {
80 if (Bmp.Width > MaxWidth || Bmp.Height > MaxHeight)
81 {
82 double Scale = ((double)MaxWidth) / Bmp.Width;
83 double Scale2 = ((double)MaxHeight) / Bmp.Height;
84
85 if (Scale2 < Scale)
86 Scale = Scale2;
87
88 int Width = (int)(Bmp.Width * Scale + 0.5);
89 int Height = (int)(Bmp.Height * Scale + 0.5);
90
91 using SKSurface Surface = SKSurface.Create(new SKImageInfo(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul));
92 SKCanvas Canvas = Surface.Canvas;
93 Canvas.DrawBitmap(Bmp, new SKRect(0, 0, Bmp.Width, Bmp.Height),
94 new SKRect(0, 0, Width, Height), new SKPaint()
95 {
96 IsAntialias = true,
97 FilterQuality = SKFilterQuality.High
98 });
99
100
101 using SKImage ScaledIamge = Surface.Snapshot();
102 using SKBitmap ScaledBitmap = SKBitmap.FromImage(ScaledIamge);
103
104 return FromBitmap(ScaledBitmap, int.MaxValue, int.MaxValue);
105 }
106 else
107 {
108 byte[] Data = Bmp.Bytes;
109
110 switch (Bmp.ColorType)
111 {
112 case SKColorType.Alpha8:
113 case SKColorType.Gray8:
114 return new Matrix<byte>(Bmp.Width, Bmp.Height, Data);
115
116 case SKColorType.Bgra8888:
117 int i, j, c = Data.Length;
118 uint[] Bin32 = new uint[c / 4];
119 uint ui32;
120
121 i = j = 0;
122 while (i < c)
123 {
124 ui32 = (uint)(Data[i++] << 16);
125 ui32 |= (uint)(Data[i++] << 8);
126 ui32 |= Data[i++];
127 ui32 |= (uint)(Data[i++] << 24);
128 Bin32[j++] = ui32;
129 }
130
131 return new Matrix<uint>(Bmp.Width, Bmp.Height, Bin32);
132
133 case SKColorType.Rgba8888:
134 c = Data.Length;
135 Bin32 = new uint[c / 4];
136
137 i = j = 0;
138 while (i < c)
139 {
140 ui32 = (uint)(Data[i++] << 16);
141 ui32 |= (uint)(Data[i++] << 24);
142 ui32 |= Data[i++];
143 ui32 |= (uint)(Data[i++] << 8);
144 Bin32[j++] = ui32;
145 }
146
147 return new Matrix<uint>(Bmp.Width, Bmp.Height, Bin32);
148
149 default:
150 throw new ArgumentException("Color type not supported: " + Bmp.ColorType.ToString(), nameof(Bmp));
151 }
152 }
153 }
154
160 public static void ToImageFile(IMatrix M, string FileName)
161 {
162 using SKImage Image = ToBitmap(M);
163
164 ToImageFile(Image, FileName);
165 }
166
172 public static void ToImageFile(SKImage Image, string FileName)
173 {
174 ToImageFile(Image, FileName, 100);
175 }
176
183 public static void ToImageFile(SKImage Image, string FileName, int Quality)
184 {
185 string Extension = Path.GetExtension(FileName).ToLower();
186 if (Extension.StartsWith("."))
187 Extension = Extension.Substring(1);
188
189 if (string.IsNullOrEmpty(Extension))
190 throw new ArgumentException("Missing file extension", nameof(FileName));
191
192 Extension = Extension.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture) + Extension.Substring(1).ToLower();
193 if (!Enum.TryParse<SKEncodedImageFormat>(Extension, out SKEncodedImageFormat Format))
194 throw new ArgumentException("Unsupported image type/extension: " + Extension, nameof(FileName));
195
196 string Folder = Path.GetDirectoryName(FileName);
197 if (!(Directory.Exists(Folder)))
198 Directory.CreateDirectory(Folder);
199
200 using SKData Data = Image.Encode(Format, Quality);
201 using FileStream fs = File.Create(FileName);
202
203 Data.SaveTo(fs);
204 }
205
211 public static SKImage ToBitmap(IMatrix M)
212 {
213 int x, w = M.Width;
214 int y, h = M.Height;
215 int RowBytes;
216 byte[] Dest;
217 SKColorType ColorType;
218
219 if (M is Matrix<uint> ColorMatrix)
220 {
221 uint[] Src = ColorMatrix.Data;
222 int SrcIndex = ColorMatrix.Start;
223 int SrcSkip = ColorMatrix.Skip;
224 Dest = new byte[w * h * 4];
225 int DestIndex = 0;
226 uint ui32;
227
228 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
229 {
230 for (x = 0; x < w; x++)
231 {
232 ui32 = Src[SrcIndex++];
233
234 Dest[DestIndex++] = (byte)(ui32 >> 16);
235 Dest[DestIndex++] = (byte)(ui32 >> 8);
236 Dest[DestIndex++] = (byte)ui32;
237 Dest[DestIndex++] = (byte)(ui32 >> 24);
238 }
239 }
240
241 RowBytes = w << 2;
242 ColorType = SKColorType.Bgra8888;
243 }
244 else if (M is Matrix<byte> GrayScaleMatrix)
245 {
246 byte[] Src = GrayScaleMatrix.Data;
247 int SrcIndex = GrayScaleMatrix.Start;
248 int SrcSkip = GrayScaleMatrix.Skip;
249 Dest = new byte[w * h];
250 int DestIndex = 0;
251
252 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
253 {
254 for (x = 0; x < w; x++)
255 Dest[DestIndex++] = Src[SrcIndex++];
256 }
257
258 RowBytes = w;
259 ColorType = SKColorType.Gray8;
260 }
261 else if (M is Matrix<float> ComputationMatrix)
262 {
263 float[] Src = ComputationMatrix.Data;
264 int SrcIndex = ComputationMatrix.Start;
265 int SrcSkip = ComputationMatrix.Skip;
266 Dest = new byte[w * h];
267 int DestIndex = 0;
268 float f;
269
270 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
271 {
272 for (x = 0; x < w; x++)
273 {
274 f = Src[SrcIndex++];
275 Dest[DestIndex++] = f < 0f ? (byte)0 : f > 1f ? (byte)255 : (byte)(f * 255f + 0.5f);
276 }
277 }
278
279 RowBytes = w;
280 ColorType = SKColorType.Gray8;
281 }
282 else if (M is Matrix<int> FixedPointMatrix)
283 {
284 int[] Src = FixedPointMatrix.Data;
285 int SrcIndex = FixedPointMatrix.Start;
286 int SrcSkip = FixedPointMatrix.Skip;
287 Dest = new byte[w * h];
288 int DestIndex = 0;
289 int f;
290
291 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
292 {
293 for (x = 0; x < w; x++)
294 {
295 f = Src[SrcIndex++] + 32768;
296 Dest[DestIndex++] = f < 0f ? (byte)0 : f >= 0x01000000 ? (byte)255 : (byte)(f >> 16);
297 }
298 }
299
300 RowBytes = w;
301 ColorType = SKColorType.Gray8;
302 }
303 else
304 throw new ArgumentException("Matrix type not supported: " + M.GetType().FullName);
305
306 using MemoryStream ms = new(Dest);
307 using SKData Data = SKData.Create(ms);
308
309 return SKImage.FromPixels(new SKImageInfo(w, h, ColorType), Data, RowBytes);
310 }
311
312
318 public static byte[] EncodeAsPng(IMatrix M)
319 {
320 using SKImage Image = ToBitmap(M);
321 using SKData Data = Image.Encode(SKEncodedImageFormat.Png, 100);
322
323 return Data.ToArray();
324 }
325 }
326}
Static methods managing conversion to and from bitmap representations.
Definition: Bitmaps.cs:12
static void ToImageFile(SKImage Image, string FileName)
Saves an image to a file.
Definition: Bitmaps.cs:172
static IMatrix FromBitmap(SKBitmap Bmp, int MaxWidth, int MaxHeight)
Craetes a matrix from a bitmap.
Definition: Bitmaps.cs:78
static byte[] EncodeAsPng(IMatrix M)
Encodes an image in a matrix using PNG.
Definition: Bitmaps.cs:318
static void ToImageFile(IMatrix M, string FileName)
Saves a matrix as an image to a file.
Definition: Bitmaps.cs:160
static IMatrix FromBitmapFile(byte[] Data, int MaxWidth, int MaxHeight)
Loads a bitmap from a binary representation, and returns a matrix.
Definition: Bitmaps.cs:55
static IMatrix FromBitmapFile(string FileName)
Loads a bitmap from a file, and returns a matrix.
Definition: Bitmaps.cs:18
static SKImage ToBitmap(IMatrix M)
Converts a matrix to an image.
Definition: Bitmaps.cs:211
static void ToImageFile(SKImage Image, string FileName, int Quality)
Saves an image to a file.
Definition: Bitmaps.cs:183
static IMatrix FromBitmapFile(byte[] Data)
Loads a bitmap from a binary representation, and returns a matrix.
Definition: Bitmaps.cs:43
static IMatrix FromBitmap(SKBitmap Bmp)
Craetes a matrix from a bitmap.
Definition: Bitmaps.cs:66
static IMatrix FromBitmapFile(string FileName, int MaxWidth, int MaxHeight)
Loads a bitmap from a file, and returns a matrix.
Definition: Bitmaps.cs:32
Implements a Matrix, basic component for computations in Image Processing and Computer Vision.
Definition: Matrix.cs:12
Interface for matrices.
Definition: IMatrix.cs:9
int Height
Height of matrix (number of rows)
Definition: IMatrix.cs:23
int Width
Width of matrix (number of columns)
Definition: IMatrix.cs:18
Definition: Abs.cs:2