Use bitmap in filter image effects

This commit is contained in:
Jaex 2020-03-22 00:41:08 +03:00
parent cfdad2c27c
commit e419281240
16 changed files with 98 additions and 109 deletions

View file

@ -35,12 +35,12 @@ namespace ShareX.HelpersLib
{
public static class ConvolutionMatrixManager
{
public static Image Apply(this ConvolutionMatrix kernel, Image img)
public static Bitmap Apply(this ConvolutionMatrix kernel, Bitmap bmp)
{
Bitmap result = (Bitmap)img.Clone();
Bitmap bmpResult = (Bitmap)bmp.Clone();
using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly))
using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly))
{
int originX = (kernel.Width - 1) / 2;
int originY = (kernel.Height - 1) / 2;
@ -96,23 +96,12 @@ public static Image Apply(this ConvolutionMatrix kernel, Image img)
a = a.Clamp(0, 255);
}
dest.SetPixel(
x,
y,
new ColorBgra(
(byte)b,
(byte)g,
(byte)r,
kernel.ConsiderAlpha
? (byte)a
: source.GetPixel(x, y).Alpha
)
);
dest.SetPixel(x, y, new ColorBgra((byte)b, (byte)g, (byte)r, kernel.ConsiderAlpha ? (byte)a : source.GetPixel(x, y).Alpha));
});
});
}
return result;
return bmpResult;
}
public static ConvolutionMatrix Smooth(int weight = 1)

View file

@ -469,9 +469,9 @@ public static Image RoundedCorners(Image img, int cornerRadius)
return bmp;
}
public static Image Outline(Image img, int borderSize, Color borderColor)
public static Bitmap Outline(Bitmap bmp, int borderSize, Color borderColor)
{
Bitmap result = img.CreateEmptyBitmap(borderSize * 2, borderSize * 2);
Bitmap bmpResult = bmp.CreateEmptyBitmap(borderSize * 2, borderSize * 2);
ColorMatrix maskMatrix = new ColorMatrix();
maskMatrix.Matrix00 = 0;
@ -482,9 +482,9 @@ public static Image Outline(Image img, int borderSize, Color borderColor)
maskMatrix.Matrix41 = ((float)borderColor.G).Remap(0, 255, 0, 1);
maskMatrix.Matrix42 = ((float)borderColor.B).Remap(0, 255, 0, 1);
using (img)
using (Image shadow = maskMatrix.Apply(img))
using (Graphics g = Graphics.FromImage(result))
using (bmp)
using (Image shadow = maskMatrix.Apply(bmp))
using (Graphics g = Graphics.FromImage(bmpResult))
{
for (int i = 0; i <= borderSize * 2; i++)
{
@ -494,33 +494,33 @@ public static Image Outline(Image img, int borderSize, Color borderColor)
g.DrawImage(shadow, new Rectangle(borderSize * 2, i, shadow.Width, shadow.Height));
}
g.DrawImage(img, new Rectangle(borderSize, borderSize, img.Width, img.Height));
g.DrawImage(bmp, new Rectangle(borderSize, borderSize, bmp.Width, bmp.Height));
}
return result;
return bmpResult;
}
public static Image DrawReflection(Image img, int percentage, int maxAlpha, int minAlpha, int offset, bool skew, int skewSize)
public static Bitmap DrawReflection(Bitmap bmp, int percentage, int maxAlpha, int minAlpha, int offset, bool skew, int skewSize)
{
Bitmap reflection = AddReflection(img, percentage, maxAlpha, minAlpha);
Bitmap reflection = AddReflection(bmp, percentage, maxAlpha, minAlpha);
if (skew)
{
reflection = AddSkew(reflection, skewSize, 0);
}
Bitmap result = new Bitmap(reflection.Width, img.Height + reflection.Height + offset);
Bitmap bmpResult = new Bitmap(reflection.Width, bmp.Height + reflection.Height + offset);
using (Graphics g = Graphics.FromImage(result))
using (img)
using (bmp)
using (reflection)
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SetHighQuality();
g.DrawImage(img, 0, 0, img.Width, img.Height);
g.DrawImage(reflection, 0, img.Height + offset, reflection.Width, reflection.Height);
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
g.DrawImage(reflection, 0, bmp.Height + offset, reflection.Width, reflection.Height);
}
return result;
return bmpResult;
}
public static Bitmap AddSkew(Image img, int x, int y)
@ -910,18 +910,18 @@ public static Bitmap RotateImage(Image img, float angleDegrees, bool upsize, boo
return newBitmap;
}
public static Bitmap AddShadow(Image img, float opacity, int size)
public static Bitmap AddShadow(Bitmap bmp, float opacity, int size)
{
return AddShadow(img, opacity, size, 1, Color.Black, new Point(0, 0));
return AddShadow(bmp, opacity, size, 1, Color.Black, new Point(0, 0));
}
public static Bitmap AddShadow(Image img, float opacity, int size, float darkness, Color color, Point offset)
public static Bitmap AddShadow(Bitmap bmp, float opacity, int size, float darkness, Color color, Point offset)
{
Bitmap shadowImage = null;
try
{
shadowImage = img.CreateEmptyBitmap(size * 2, size * 2);
shadowImage = bmp.CreateEmptyBitmap(size * 2, size * 2);
ColorMatrix maskMatrix = new ColorMatrix();
maskMatrix.Matrix00 = 0;
@ -932,8 +932,8 @@ public static Bitmap AddShadow(Image img, float opacity, int size, float darknes
maskMatrix.Matrix41 = ((float)color.G).Remap(0, 255, 0, 1);
maskMatrix.Matrix42 = ((float)color.B).Remap(0, 255, 0, 1);
Rectangle shadowRectangle = new Rectangle(size, size, img.Width, img.Height);
maskMatrix.Apply(img, shadowImage, shadowRectangle);
Rectangle shadowRectangle = new Rectangle(size, size, bmp.Width, bmp.Height);
maskMatrix.Apply(bmp, shadowImage, shadowRectangle);
if (size > 0)
{
@ -945,25 +945,25 @@ public static Bitmap AddShadow(Image img, float opacity, int size, float darknes
ColorMatrix alphaMatrix = new ColorMatrix();
alphaMatrix.Matrix33 = darkness;
Bitmap shadowImage2 = (Bitmap)alphaMatrix.Apply(shadowImage);
Bitmap shadowImage2 = alphaMatrix.Apply(shadowImage);
shadowImage.Dispose();
shadowImage = shadowImage2;
}
Bitmap result = shadowImage.CreateEmptyBitmap(Math.Abs(offset.X), Math.Abs(offset.Y));
Bitmap bmpResult = shadowImage.CreateEmptyBitmap(Math.Abs(offset.X), Math.Abs(offset.Y));
using (Graphics g = Graphics.FromImage(result))
using (Graphics g = Graphics.FromImage(bmpResult))
{
g.SetHighQuality();
g.DrawImage(shadowImage, Math.Max(0, offset.X), Math.Max(0, offset.Y), shadowImage.Width, shadowImage.Height);
g.DrawImage(img, Math.Max(size, -offset.X + size), Math.Max(size, -offset.Y + size), img.Width, img.Height);
g.DrawImage(bmp, Math.Max(size, -offset.X + size), Math.Max(size, -offset.Y + size), bmp.Width, bmp.Height);
}
return result;
return bmpResult;
}
finally
{
if (img != null) img.Dispose();
if (bmp != null) bmp.Dispose();
if (shadowImage != null) shadowImage.Dispose();
}
}
@ -1416,17 +1416,17 @@ public static void FastBoxBlur(Bitmap bmp, int radius)
}
}
public static Image TornEdges(Image img, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges)
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges)
{
if (tornDepth < 1 || tornRange < 1 || sides == AnchorStyles.None)
{
return img;
return bmp;
}
List<Point> points = new List<Point>();
int horizontalTornCount = img.Width / tornRange;
int verticalTornCount = img.Height / tornRange;
int horizontalTornCount = bmp.Width / tornRange;
int verticalTornCount = bmp.Height / tornRange;
if (sides.HasFlag(AnchorStyles.Top))
{
@ -1438,53 +1438,53 @@ public static Image TornEdges(Image img, int tornDepth, int tornRange, AnchorSty
else
{
points.Add(new Point(0, 0));
points.Add(new Point(img.Width - 1, 0));
points.Add(new Point(bmp.Width - 1, 0));
}
if (sides.HasFlag(AnchorStyles.Right))
{
for (int y = 0; y < verticalTornCount - 1; y++)
{
points.Add(new Point(img.Width - 1 - MathHelpers.Random(0, tornDepth), tornRange * y));
points.Add(new Point(bmp.Width - 1 - MathHelpers.Random(0, tornDepth), tornRange * y));
}
}
else
{
points.Add(new Point(img.Width - 1, 0));
points.Add(new Point(img.Width - 1, img.Height - 1));
points.Add(new Point(bmp.Width - 1, 0));
points.Add(new Point(bmp.Width - 1, bmp.Height - 1));
}
if (sides.HasFlag(AnchorStyles.Bottom))
{
for (int x = 0; x < horizontalTornCount - 1; x++)
{
points.Add(new Point(img.Width - 1 - (tornRange * x), img.Height - 1 - MathHelpers.Random(0, tornDepth)));
points.Add(new Point(bmp.Width - 1 - (tornRange * x), bmp.Height - 1 - MathHelpers.Random(0, tornDepth)));
}
}
else
{
points.Add(new Point(img.Width - 1, img.Height - 1));
points.Add(new Point(0, img.Height - 1));
points.Add(new Point(bmp.Width - 1, bmp.Height - 1));
points.Add(new Point(0, bmp.Height - 1));
}
if (sides.HasFlag(AnchorStyles.Left))
{
for (int y = 0; y < verticalTornCount - 1; y++)
{
points.Add(new Point(MathHelpers.Random(0, tornDepth), img.Height - 1 - (tornRange * y)));
points.Add(new Point(MathHelpers.Random(0, tornDepth), bmp.Height - 1 - (tornRange * y)));
}
}
else
{
points.Add(new Point(0, img.Height - 1));
points.Add(new Point(0, bmp.Height - 1));
points.Add(new Point(0, 0));
}
Bitmap result = img.CreateEmptyBitmap();
Bitmap bmpResult = bmp.CreateEmptyBitmap();
using (img)
using (Graphics g = Graphics.FromImage(result))
using (TextureBrush brush = new TextureBrush(img))
using (bmp)
using (Graphics g = Graphics.FromImage(bmpResult))
using (TextureBrush brush = new TextureBrush(bmp))
{
g.SetHighQuality();
@ -1498,22 +1498,22 @@ public static Image TornEdges(Image img, int tornDepth, int tornRange, AnchorSty
{
g.FillPolygon(brush, fillPoints);
}
return result;
}
return bmpResult;
}
public static Bitmap Slice(Image img, int minSliceHeight, int maxSliceHeight, int minSliceShift, int maxSliceShift)
public static Bitmap Slice(Bitmap bmp, int minSliceHeight, int maxSliceHeight, int minSliceShift, int maxSliceShift)
{
Bitmap result = img.CreateEmptyBitmap();
Bitmap bmpResult = bmp.CreateEmptyBitmap();
using (Graphics g = Graphics.FromImage(result))
using (Graphics g = Graphics.FromImage(bmpResult))
{
int y = 0;
while (y < img.Height)
while (y < bmp.Height)
{
Rectangle sourceRect = new Rectangle(0, y, img.Width, MathHelpers.Random(minSliceHeight, maxSliceHeight));
Rectangle sourceRect = new Rectangle(0, y, bmp.Width, MathHelpers.Random(minSliceHeight, maxSliceHeight));
Rectangle destRect = sourceRect;
if (MathHelpers.Random(1) == 0) // Shift left
@ -1525,13 +1525,13 @@ public static Bitmap Slice(Image img, int minSliceHeight, int maxSliceHeight, in
destRect.X = MathHelpers.Random(minSliceShift, maxSliceShift);
}
g.DrawImage(img, destRect, sourceRect, GraphicsUnit.Pixel);
g.DrawImage(bmp, destRect, sourceRect, GraphicsUnit.Pixel);
y += sourceRect.Height;
}
}
return result;
return bmpResult;
}
public static string OpenImageFileDialog(Form form = null)

View file

@ -56,10 +56,10 @@ public Blur()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
ImageHelpers.BoxBlur((Bitmap)img, Radius);
return img;
ImageHelpers.BoxBlur(bmp, Radius);
return bmp;
}
}
}

View file

@ -32,11 +32,11 @@ namespace ShareX.ImageEffectsLib
[Description("Edge detect")]
internal class EdgeDetect : ImageEffect
{
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
using (img)
using (bmp)
{
return ConvolutionMatrixManager.EdgeDetect().Apply(img);
return ConvolutionMatrixManager.EdgeDetect().Apply(bmp);
}
}
}

View file

@ -30,11 +30,11 @@ namespace ShareX.ImageEffectsLib
{
internal class Emboss : ImageEffect
{
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
using (img)
using (bmp)
{
return ConvolutionMatrixManager.Emboss().Apply(img);
return ConvolutionMatrixManager.Emboss().Apply(bmp);
}
}
}

View file

@ -63,7 +63,7 @@ public GaussianBlur()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
ConvolutionMatrix kernelHoriz = ConvolutionMatrixManager.GaussianBlur(1, size, sigma);
@ -77,8 +77,8 @@ public override Image Apply(Image img)
kernelVert[i, 0] = kernelHoriz[0, i];
}
using (img)
using (Image horizPass = kernelHoriz.Apply(img))
using (bmp)
using (Bitmap horizPass = kernelHoriz.Apply(bmp))
{
return kernelVert.Apply(horizPass);
}

View file

@ -64,9 +64,9 @@ public MatrixConvolution()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
using (img)
using (bmp)
{
ConvolutionMatrix cm = new ConvolutionMatrix();
cm[0, 0] = X0Y0 / Factor;
@ -79,7 +79,7 @@ public override Image Apply(Image img)
cm[2, 1] = X1Y2 / Factor;
cm[2, 2] = X2Y2 / Factor;
cm.Offset = Offset;
return cm.Apply(img);
return cm.Apply(bmp);
}
}
}

View file

@ -32,11 +32,11 @@ namespace ShareX.ImageEffectsLib
[Description("Mean removal")]
internal class MeanRemoval : ImageEffect
{
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
using (img)
using (bmp)
{
return ConvolutionMatrixManager.MeanRemoval().Apply(img);
return ConvolutionMatrixManager.MeanRemoval().Apply(bmp);
}
}
}

View file

@ -55,9 +55,9 @@ public Outline()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.Outline(img, Size, Color);
return ImageHelpers.Outline(bmp, Size, Color);
}
}
}

View file

@ -51,10 +51,10 @@ public Pixelate()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
ImageHelpers.Pixelate((Bitmap)img, Size);
return img;
ImageHelpers.Pixelate(bmp, Size);
return bmp;
}
}
}

View file

@ -90,9 +90,9 @@ public Reflection()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.DrawReflection(img, Percentage, MaxAlpha, MinAlpha, Offset, Skew, SkewSize);
return ImageHelpers.DrawReflection(bmp, Percentage, MaxAlpha, MinAlpha, Offset, Skew, SkewSize);
}
}
}

View file

@ -76,9 +76,9 @@ public Shadow()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.AddShadow(img, Opacity, Size, Darkness + 1, Color, Offset);
return ImageHelpers.AddShadow(bmp, Opacity, Size, Darkness + 1, Color, Offset);
}
}
}

View file

@ -30,13 +30,13 @@ namespace ShareX.ImageEffectsLib
{
internal class Sharpen : ImageEffect
{
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
//return ImageHelpers.Sharpen(img, Strength);
//return ImageHelpers.Sharpen(bmp, Strength);
using (img)
using (bmp)
{
return ConvolutionMatrixManager.Sharpen().Apply(img);
return ConvolutionMatrixManager.Sharpen().Apply(bmp);
}
}
}

View file

@ -50,16 +50,16 @@ public Slice()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
int minSliceHeight = Math.Min(MinSliceHeight, MaxSliceHeight);
int maxSliceHeight = Math.Max(MinSliceHeight, MaxSliceHeight);
int minSliceShift = Math.Min(MinSliceShift, MaxSliceShift);
int maxSliceShift = Math.Max(MinSliceShift, MaxSliceShift);
using (img)
using (bmp)
{
return ImageHelpers.Slice(img, minSliceHeight, maxSliceHeight, minSliceShift, maxSliceShift);
return ImageHelpers.Slice(bmp, minSliceHeight, maxSliceHeight, minSliceShift, maxSliceShift);
}
}
}

View file

@ -30,11 +30,11 @@ namespace ShareX.ImageEffectsLib
{
internal class Smooth : ImageEffect
{
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
using (img)
using (bmp)
{
return ConvolutionMatrixManager.Smooth().Apply(img);
return ConvolutionMatrixManager.Smooth().Apply(bmp);
}
}
}

View file

@ -50,9 +50,9 @@ public TornEdge()
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.TornEdges(img, Depth, Range, Sides, CurvedEdges);
return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges);
}
}
}