3using System.Globalization;
34 using SKBitmap Bmp = SKBitmap.Decode(FileName);
57 using SKBitmap Bmp = SKBitmap.Decode(Data);
68 return FromBitmap(Bmp,
int.MaxValue,
int.MaxValue);
80 if (Bmp.Width > MaxWidth || Bmp.Height > MaxHeight)
82 double Scale = ((double)MaxWidth) / Bmp.
Width;
83 double Scale2 = ((double)MaxHeight) / Bmp.Height;
88 int Width = (int)(Bmp.Width * Scale + 0.5);
89 int Height = (int)(Bmp.Height * Scale + 0.5);
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()
97 FilterQuality = SKFilterQuality.High
101 using SKImage ScaledIamge = Surface.Snapshot();
102 using SKBitmap ScaledBitmap = SKBitmap.FromImage(ScaledIamge);
104 return FromBitmap(ScaledBitmap,
int.MaxValue,
int.MaxValue);
108 byte[] Data = Bmp.Bytes;
110 switch (Bmp.ColorType)
112 case SKColorType.Alpha8:
113 case SKColorType.Gray8:
116 case SKColorType.Bgra8888:
117 int i, j, c = Data.Length;
118 uint[] Bin32 =
new uint[c / 4];
124 ui32 = (uint)(Data[i++] << 16);
125 ui32 |= (uint)(Data[i++] << 8);
127 ui32 |= (uint)(Data[i++] << 24);
133 case SKColorType.Rgba8888:
135 Bin32 =
new uint[c / 4];
140 ui32 = (uint)(Data[i++] << 16);
141 ui32 |= (uint)(Data[i++] << 24);
143 ui32 |= (uint)(Data[i++] << 8);
150 throw new ArgumentException(
"Color type not supported: " + Bmp.ColorType.ToString(), nameof(Bmp));
183 public static void ToImageFile(SKImage Image,
string FileName,
int Quality)
185 string Extension = Path.GetExtension(FileName).ToLower();
186 if (Extension.StartsWith(
"."))
187 Extension = Extension.Substring(1);
189 if (
string.IsNullOrEmpty(Extension))
190 throw new ArgumentException(
"Missing file extension", nameof(FileName));
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));
196 string Folder = Path.GetDirectoryName(FileName);
197 if (!(Directory.Exists(Folder)))
198 Directory.CreateDirectory(Folder);
200 using SKData Data = Image.Encode(Format, Quality);
201 using FileStream fs = File.Create(FileName);
217 SKColorType ColorType;
221 uint[] Src = ColorMatrix.Data;
222 int SrcIndex = ColorMatrix.Start;
223 int SrcSkip = ColorMatrix.Skip;
224 Dest =
new byte[w * h * 4];
228 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
230 for (x = 0; x < w; x++)
232 ui32 = Src[SrcIndex++];
234 Dest[DestIndex++] = (byte)(ui32 >> 16);
235 Dest[DestIndex++] = (byte)(ui32 >> 8);
236 Dest[DestIndex++] = (byte)ui32;
237 Dest[DestIndex++] = (byte)(ui32 >> 24);
242 ColorType = SKColorType.Bgra8888;
246 byte[] Src = GrayScaleMatrix.Data;
247 int SrcIndex = GrayScaleMatrix.Start;
248 int SrcSkip = GrayScaleMatrix.Skip;
249 Dest =
new byte[w * h];
252 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
254 for (x = 0; x < w; x++)
255 Dest[DestIndex++] = Src[SrcIndex++];
259 ColorType = SKColorType.Gray8;
263 float[] Src = ComputationMatrix.Data;
264 int SrcIndex = ComputationMatrix.Start;
265 int SrcSkip = ComputationMatrix.Skip;
266 Dest =
new byte[w * h];
270 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
272 for (x = 0; x < w; x++)
275 Dest[DestIndex++] = f < 0f ? (byte)0 : f > 1f ? (
byte)255 : (byte)(f * 255f + 0.5f);
280 ColorType = SKColorType.Gray8;
284 int[] Src = FixedPointMatrix.Data;
285 int SrcIndex = FixedPointMatrix.Start;
286 int SrcSkip = FixedPointMatrix.Skip;
287 Dest =
new byte[w * h];
291 for (y = 0; y < h; y++, SrcIndex += SrcSkip)
293 for (x = 0; x < w; x++)
295 f = Src[SrcIndex++] + 32768;
296 Dest[DestIndex++] = f < 0f ? (byte)0 : f >= 0x01000000 ? (
byte)255 : (byte)(f >> 16);
301 ColorType = SKColorType.Gray8;
304 throw new ArgumentException(
"Matrix type not supported: " + M.GetType().FullName);
306 using MemoryStream ms =
new(Dest);
307 using SKData Data = SKData.Create(ms);
309 return SKImage.FromPixels(
new SKImageInfo(w, h, ColorType), Data, RowBytes);
321 using SKData Data = Image.Encode(SKEncodedImageFormat.Png, 100);
323 return Data.ToArray();
Static methods managing conversion to and from bitmap representations.
static void ToImageFile(SKImage Image, string FileName)
Saves an image to a file.
static IMatrix FromBitmap(SKBitmap Bmp, int MaxWidth, int MaxHeight)
Craetes a matrix from a bitmap.
static byte[] EncodeAsPng(IMatrix M)
Encodes an image in a matrix using PNG.
static void ToImageFile(IMatrix M, string FileName)
Saves a matrix as an image to a file.
static IMatrix FromBitmapFile(byte[] Data, int MaxWidth, int MaxHeight)
Loads a bitmap from a binary representation, and returns a matrix.
static IMatrix FromBitmapFile(string FileName)
Loads a bitmap from a file, and returns a matrix.
static SKImage ToBitmap(IMatrix M)
Converts a matrix to an image.
static void ToImageFile(SKImage Image, string FileName, int Quality)
Saves an image to a file.
static IMatrix FromBitmapFile(byte[] Data)
Loads a bitmap from a binary representation, and returns a matrix.
static IMatrix FromBitmap(SKBitmap Bmp)
Craetes a matrix from a bitmap.
static IMatrix FromBitmapFile(string FileName, int MaxWidth, int MaxHeight)
Loads a bitmap from a file, and returns a matrix.
Implements a Matrix, basic component for computations in Image Processing and Computer Vision.
int Height
Height of matrix (number of rows)
int Width
Width of matrix (number of columns)