25 internal static class Utils
30 public static readonly
int[] AcceptableDivisors =
new[] { 2, 3, 5, 7 };
37 public static int GetAcceptableNumber(
int n)
39 for (
int i = n; i >= 1; i--)
41 if (IsAcceptableNumber(i))
47 throw new ArgumentOutOfRangeException(nameof(n), n,
"The number must be strictly higher than 0!");
55 public static bool IsAcceptableNumber(
int n)
67 for (
int i = 0; i < AcceptableDivisors.Length; i++)
71 while (n % AcceptableDivisors[i] == 0)
73 n /= AcceptableDivisors[i];
79 return IsAcceptableNumber(n);
95 public static void ClipImage(IntPtr image, RoundedSize imageSize, Rectangle imageArea, Rectangle clipArea,
PixelFormats pixelFormat)
97 int clipLeft = Math.Max(0, (
int)Math.Ceiling((clipArea.X0 - imageArea.X0) / imageArea.Width * imageSize.Width - 0.001));
98 int clipRight = Math.Max(0, (
int)Math.Floor(imageSize.Width - (imageArea.X1 - clipArea.X1) / imageArea.Width * imageSize.Width + 0.001));
100 int clipTop = Math.Max(0, (
int)Math.Ceiling((clipArea.Y0 - imageArea.Y0) / imageArea.Height * imageSize.Height - 0.001));
101 int clipBottom = Math.Max(0, (
int)Math.Floor(imageSize.Height - (imageArea.Y1 - clipArea.Y1) / imageArea.Height * imageSize.Height + 0.001));
120 int stride = imageSize.Width * pixelSize;
122 if (clipLeft > 0 || clipRight < imageSize.Width || clipTop > 0 || clipBottom < imageSize.Height)
126 byte* imageData = (
byte*)image;
128 for (
int y = 0; y < imageSize.Height; y++)
130 if (y < clipTop || y >= clipBottom)
132 for (
int x = 0; x < imageSize.Width; x++)
134 for (
int i = 0; i < pixelSize; i++)
136 imageData[y * stride + x * pixelSize + i] = clearValue;
142 for (
int x = 0; x < Math.Min(clipLeft, imageSize.Width); x++)
144 for (
int i = 0; i < pixelSize; i++)
146 imageData[y * stride + x * pixelSize + i] = clearValue;
150 for (
int x = Math.Max(0, clipRight); x < imageSize.Width; x++)
152 for (
int i = 0; i < pixelSize; i++)
154 imageData[y * stride + x * pixelSize + i] = clearValue;
168 public static void UnpremultiplyAlpha(IntPtr image, RoundedSize imageSize)
170 int stride = imageSize.Width * 4;
174 byte* imageData = (
byte*)image;
176 for (
int y = 0; y < imageSize.Height; y++)
178 for (
int x = 0; x < imageSize.Width; x++)
180 if (imageData[y * stride + x * 4 + 3] > 0)
182 imageData[y * stride + x * 4] = (byte)(imageData[y * stride + x * 4] * 255 / imageData[y * stride + x * 4 + 3]);
183 imageData[y * stride + x * 4 + 1] = (byte)(imageData[y * stride + x * 4 + 1] * 255 / imageData[y * stride + x * 4 + 3]);
184 imageData[y * stride + x * 4 + 2] = (byte)(imageData[y * stride + x * 4 + 2] * 255 / imageData[y * stride + x * 4 + 3]);
PixelFormats
Pixel formats supported by the library.