Merge pull request #6458 from nielsmh/edgeeffect-cornerfix

Fix corner glitches in edge effects
This commit is contained in:
Jaex 2022-08-20 21:02:50 +03:00 committed by GitHub
commit 91aeefd452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1672,82 +1672,88 @@ public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorS
int horizontalWaveCount = Math.Max(2, (bmp.Width / waveRange + 1) / 2 * 2) - 1;
int verticalWaveCount = Math.Max(2, (bmp.Height / waveRange + 1) / 2 * 2) - 1;
int horizontalWaveRange = bmp.Width / horizontalWaveCount;
int verticalWaveRange = bmp.Height / verticalWaveCount;
int step = Math.Min(Math.Max(1, waveRange / waveDepth), 10);
Bitmap updateResult(Bitmap bmpIn, Point[] path)
{
Bitmap bmpResult = bmpIn.CreateEmptyBitmap();
using (bmpIn)
using (Graphics g = Graphics.FromImage(bmpResult))
using (TextureBrush brush = new TextureBrush(bmpIn))
{
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
g.FillPolygon(brush, path);
}
return bmpResult;
};
int waveFunction(int t, int max, int depth) => (int)((1 - Math.Cos(t * Math.PI / max)) * depth / 2);
if (sides.HasFlag(AnchorStyles.Top))
{
waveRange = bmp.Width / horizontalWaveCount;
points.Clear();
for (int x = 0; x < bmp.Width; x += step)
int startX = sides.HasFlag(AnchorStyles.Left) ? waveDepth : 0;
int endX = sides.HasFlag(AnchorStyles.Right) ? bmp.Width - waveDepth : bmp.Width;
for (int x = startX; x < endX; x += step)
{
points.Add(new Point(x, waveFunction(x, waveRange, waveDepth)));
points.Add(new Point(x, waveFunction(x, horizontalWaveRange, waveDepth)));
}
points.Add(new Point(bmp.Width, waveFunction(bmp.Width, waveRange, waveDepth)));
points.Add(new Point(bmp.Width, bmp.Height));
points.Add(new Point(0, bmp.Height));
bmp = updateResult(bmp, points.ToArray());
points.Add(new Point(endX, waveFunction(endX, horizontalWaveRange, waveDepth)));
}
else
{
points.Add(new Point(0, 0));
}
if (sides.HasFlag(AnchorStyles.Right))
{
waveRange = bmp.Height / verticalWaveCount;
points.Clear();
points.Add(new Point(0, 0));
for (int y = 0; y < bmp.Height; y += step)
int startY = sides.HasFlag(AnchorStyles.Top) ? waveDepth : 0;
int endY = sides.HasFlag(AnchorStyles.Bottom) ? bmp.Height - waveDepth : bmp.Height;
for (int y = startY; y < endY; y += step)
{
points.Add(new Point(bmp.Width - waveDepth + waveFunction(y, waveRange, waveDepth), y));
points.Add(new Point(bmp.Width - waveDepth + waveFunction(y, verticalWaveRange, waveDepth), y));
}
points.Add(new Point(bmp.Width - waveDepth + waveFunction(bmp.Height, waveRange, waveDepth), bmp.Height));
points.Add(new Point(0, bmp.Height));
bmp = updateResult(bmp, points.ToArray());
points.Add(new Point(bmp.Width - waveDepth + waveFunction(endY, verticalWaveRange, waveDepth), endY));
}
else
{
points.Add(new Point(bmp.Width, points[points.Count - 1].Y));
}
if (sides.HasFlag(AnchorStyles.Bottom))
{
waveRange = bmp.Width / horizontalWaveCount;
points.Clear();
points.Add(new Point(0, 0));
points.Add(new Point(bmp.Width, 0));
for (int x = bmp.Width; x >= 0; x -= step)
int startX = sides.HasFlag(AnchorStyles.Right) ? bmp.Width - waveDepth : bmp.Width;
int endX = sides.HasFlag(AnchorStyles.Left) ? waveDepth : 0;
for (int x = startX; x >= endX; x -= step)
{
points.Add(new Point(x, bmp.Height - waveDepth + waveFunction(x, waveRange, waveDepth)));
points.Add(new Point(x, bmp.Height - waveDepth + waveFunction(x, horizontalWaveRange, waveDepth)));
}
points.Add(new Point(0, bmp.Height - waveDepth + waveFunction(0, waveRange, waveDepth)));
bmp = updateResult(bmp, points.ToArray());
points.Add(new Point(endX, bmp.Height - waveDepth + waveFunction(endX, horizontalWaveRange, waveDepth)));
}
else
{
points.Add(new Point(points[points.Count - 1].X, bmp.Height));
}
if (sides.HasFlag(AnchorStyles.Left))
{
waveRange = bmp.Height / verticalWaveCount;
points.Clear();
points.Add(new Point(0, 0));
points.Add(new Point(bmp.Width, 0));
points.Add(new Point(bmp.Width, bmp.Height));
for (int y = bmp.Height; y >= 0; y -= step)
int startY = sides.HasFlag(AnchorStyles.Bottom) ? bmp.Height - waveDepth : bmp.Height;
int endY = sides.HasFlag(AnchorStyles.Top) ? waveDepth : 0;
for (int y = startY; y >= endY; y -= step)
{
points.Add(new Point(waveFunction(y, waveRange, waveDepth), y));
points.Add(new Point(waveFunction(y, verticalWaveRange, waveDepth), y));
}
bmp = updateResult(bmp, points.ToArray());
points.Add(new Point(waveFunction(endY, verticalWaveRange, waveDepth), endY));
}
else
{
points.Add(new Point(0, points[points.Count - 1].Y));
}
return bmp;
if (!sides.HasFlag(AnchorStyles.Top))
{
points[0] = new Point(points[points.Count - 1].X, 0);
}
Bitmap bmpResult = bmp.CreateEmptyBitmap();
using (bmp)
using (Graphics g = Graphics.FromImage(bmpResult))
using (TextureBrush brush = new TextureBrush(bmp))
{
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
g.FillPolygon(brush, points.ToArray());
}
return bmpResult;
}
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random)
@ -1769,7 +1775,9 @@ public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorS
if (sides.HasFlag(AnchorStyles.Top) && horizontalTornCount > 1)
{
for (int x = 0; x < bmp.Width; x += tornRange)
int startX = (sides.HasFlag(AnchorStyles.Left) && verticalTornCount > 1) ? tornDepth : 0;
int endX = (sides.HasFlag(AnchorStyles.Right) && verticalTornCount > 1) ? bmp.Width - tornDepth : bmp.Width;
for (int x = startX; x < endX; x += tornRange)
{
int y = random ? RandomFast.Next(0, tornDepth) : ((x / tornRange) & 1) * tornDepth;
points.Add(new Point(x, y));
@ -1783,7 +1791,9 @@ public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorS
if (sides.HasFlag(AnchorStyles.Right) && verticalTornCount > 1)
{
for (int y = 0; y < bmp.Height; y += tornRange)
int startY = (sides.HasFlag(AnchorStyles.Top) && horizontalTornCount > 1) ? tornDepth : 0;
int endY = (sides.HasFlag(AnchorStyles.Bottom) && horizontalTornCount > 1) ? bmp.Height - tornDepth : bmp.Height;
for (int y = startY; y < endY; y += tornRange)
{
int x = random ? RandomFast.Next(0, tornDepth) : ((y / tornRange) & 1) * tornDepth;
points.Add(new Point(bmp.Width - tornDepth + x, y));
@ -1797,7 +1807,9 @@ public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorS
if (sides.HasFlag(AnchorStyles.Bottom) && horizontalTornCount > 1)
{
for (int x = bmp.Width; x >= 0; x = (x / tornRange - 1) * tornRange)
int startX = (sides.HasFlag(AnchorStyles.Right) && verticalTornCount > 1) ? bmp.Width - tornDepth : bmp.Width;
int endX = (sides.HasFlag(AnchorStyles.Left) && verticalTornCount > 1) ? tornDepth : 0;
for (int x = startX; x >= endX; x = (x / tornRange - 1) * tornRange)
{
int y = random ? RandomFast.Next(0, tornDepth) : ((x / tornRange) & 1) * tornDepth;
points.Add(new Point(x, bmp.Height - tornDepth + y));
@ -1811,7 +1823,9 @@ public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorS
if (sides.HasFlag(AnchorStyles.Left) && verticalTornCount > 1)
{
for (int y = bmp.Height; y >= 0; y = (y / tornRange - 1) * tornRange)
int startY = (sides.HasFlag(AnchorStyles.Bottom) && horizontalTornCount > 1) ? bmp.Height - tornDepth : bmp.Height;
int endY = (sides.HasFlag(AnchorStyles.Top) && horizontalTornCount > 1) ? tornDepth : 0;
for (int y = startY; y >= endY; y = (y / tornRange - 1) * tornRange)
{
int x = random ? RandomFast.Next(0, tornDepth) : ((y / tornRange) & 1) * tornDepth;
points.Add(new Point(x, y));