Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Mutate/Clone APIs #275

Merged
merged 40 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f026778
migrate to a Image.Generate/Image.Mutate context api
tocsoft Jul 1, 2017
456e139
fix demo
tocsoft Jul 4, 2017
88e30a0
fix apply processors to frames
tocsoft Jul 4, 2017
a61fcc9
Rename Generate -> Clone
tocsoft Jul 5, 2017
001e3ca
Fix stack overflow
tocsoft Jul 10, 2017
b4fbf17
merge with master
tocsoft Jul 11, 2017
961e202
initail image operations tests
tocsoft Jul 11, 2017
c3f7f83
move methods
tocsoft Jul 11, 2017
ccee50b
Merge branch 'tocsoft/mutate-api' of https://github.com/JimBobSquareP…
tocsoft Jul 11, 2017
f7cbb27
test extensions methods
tocsoft Jul 11, 2017
04b6ad8
fix warnings
tocsoft Jul 11, 2017
c12f993
fix sandbox test
tocsoft Jul 11, 2017
6270568
blank to force rebuild
tocsoft Jul 12, 2017
ab30013
Renameing classes + simplifying Image<TPixel>
tocsoft Jul 14, 2017
9b86525
fix some comments
tocsoft Jul 15, 2017
cb165fa
Merge remote-tracking branch 'origin/master' into tocsoft/mutate-api
tocsoft Jul 15, 2017
41cadf9
fix broken tests
tocsoft Jul 15, 2017
c421e6d
Cloneing Image Processor
tocsoft Jul 15, 2017
7635558
internalise cloning processor + rename context
tocsoft Jul 16, 2017
3e23313
made FormatsDirectory a lazy value -> folders are no longer walked in…
antonfirsov Jul 17, 2017
d9fb5bd
clean up TestFile.cs
antonfirsov Jul 17, 2017
7d932dc
Merge branch 'master' into tocsoft/mutate-api
tocsoft Jul 17, 2017
06271fa
Fix spelling errors
JimBobSquarePants Jul 18, 2017
e824a1b
Merge remote-tracking branch 'origin/master' into tocsoft/mutate-api
tocsoft Jul 18, 2017
70892a1
Cleanup + Rename Run to Apply
JimBobSquarePants Jul 26, 2017
9c5798b
Intellisense docs cleanup
JimBobSquarePants Aug 14, 2017
15a7b5a
Merge branch 'master' into tocsoft/mutate-api
JimBobSquarePants Aug 14, 2017
f8a0ed3
Update rotate test to use new API
JimBobSquarePants Aug 14, 2017
de964e5
added comments, docs + a CloneAndConvertToAvatarWithoutApply() example
antonfirsov Aug 15, 2017
9245631
fix typo
antonfirsov Aug 15, 2017
94c60cb
inital rename packages and namespaces
tocsoft Aug 16, 2017
3653f1f
fix some tests and updated packages
tocsoft Aug 16, 2017
fbe9d25
add missing stylecop.json
tocsoft Aug 16, 2017
8f53b5b
tweek codecov cmd file
tocsoft Aug 16, 2017
9be5ba0
skip tests in build on CI server as they are ran from CodeCoverage.cmd
tocsoft Aug 17, 2017
9a4d048
publish both packages
tocsoft Aug 17, 2017
c9db035
ensure local running of build scripts runs tests from correct build
tocsoft Aug 17, 2017
4c8ba59
fix broken test
tocsoft Aug 17, 2017
4f38761
Fix skew
JimBobSquarePants Aug 18, 2017
9b34d09
Merge pull request #297 from SixLabors/tocsoft/sixlabors_rename
JimBobSquarePants Aug 19, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions samples/AvatarWithRoundedCorner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,68 @@ namespace AvatarWithRoundedCorner
using SixLabors.Primitives;
using SixLabors.Shapes;

class Program
static class Program
{
static void Main(string[] args)
{
System.IO.Directory.CreateDirectory("output");
using (var img = Image.Load("fb.jpg"))
{
// as generate returns a new IImage make sure we dispose of it
using (Image<Rgba32> dest = img.Clone(x => x.ConvertToAvatar(new Size(200, 200), 20)))
{
dest.Save("output/fb.png");
}

GenerateAvatar("fb.jpg", "output/fb.png", new Size(200, 200), 20);
GenerateAvatar("fb.jpg", "output/fb-round.png", new Size(200, 200), 100);
GenerateAvatar("fb.jpg", "output/fb-rounder.png", new Size(200, 200), 150);
using (Image<Rgba32> destRound = img.Clone(x => x.ConvertToAvatar(new Size(200, 200), 100)))
{
destRound.Save("output/fb-round.png");
}

using (Image<Rgba32> destRound = img.Clone(x => x.ConvertToAvatar(new Size(200, 200), 150)))
{
destRound.Save("output/fb-rounder.png");
}

// the original `img` object has not been altered at all.
}
}

private static void GenerateAvatar(string source, string destination, Size size, float cornerRadius)
// lets create our custom image mutating pipeline
private static IImageOperations<Rgba32> ConvertToAvatar(this IImageOperations<Rgba32> operations, Size size, float cornerRadius)
{
using (var image = Image.Load(source))
return operations.Resize(new ImageSharp.Processing.ResizeOptions
{
image.Resize(new ImageSharp.Processing.ResizeOptions
{
Size = size,
Mode = ImageSharp.Processing.ResizeMode.Crop
});

ApplyRoundedCourners(image, cornerRadius);
image.Save(destination);
}
Size = size,
Mode = ImageSharp.Processing.ResizeMode.Crop
}).Run(i => ApplyRoundedCourners(i, cornerRadius));
}

// the combination of `IImageOperations.Run()` + this could be replaced with an `IImageProcessor`
public static void ApplyRoundedCourners(Image<Rgba32> img, float cornerRadius)
{
var corners = BuildCorners(img.Width, img.Height, cornerRadius);
// now we have our corners time to draw them
img.Fill(Rgba32.Transparent, corners, new GraphicsOptions(true)

// mutating in here as we already have a cloned original
img.Mutate(x => x.Fill(Rgba32.Transparent, corners, new GraphicsOptions(true)
{
BlenderMode = ImageSharp.PixelFormats.PixelBlenderMode.Src // enforces that any part of this shape that has color is punched out of the background
});
}));
}

public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
{
// first create a square
var rect = new SixLabors.Shapes.RectangularePolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);

// then cut out of the square a circle so we are left with a corner
var cornerToptLeft = rect.Clip(new SixLabors.Shapes.EllipsePolygon(cornerRadius-0.5f, cornerRadius - 0.5f, cornerRadius));
var cornerToptLeft = rect.Clip(new SixLabors.Shapes.EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));

// corner is now a corner shape positions top left
//lets make 3 more positioned correctly, we cando that by translating the orgional artound the center of the image
var center = new Vector2(imageWidth / 2, imageHeight / 2);
var angle = Math.PI / 2f;

float rightPos = imageWidth - cornerToptLeft.Bounds.Width +1;
float rightPos = imageWidth - cornerToptLeft.Bounds.Width + 1;
float bottomPos = imageHeight - cornerToptLeft.Bounds.Height + 1;

// move it across the widthof the image - the width of the shape
Expand Down
14 changes: 7 additions & 7 deletions src/ImageSharp.Drawing/DrawImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static partial class ImageExtensions
/// <param name="location">The location to draw the blended image.</param>
/// <param name="options">The options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, Size size, Point location, GraphicsOptions options)
public static IImageOperations<TPixel> DrawImage<TPixel>(this IImageOperations<TPixel> source, Image<TPixel> image, Size size, Point location, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
if (size == default(Size))
Expand All @@ -37,7 +37,7 @@ public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<T
location = Point.Empty;
}

source.ApplyProcessor(new DrawImageProcessor<TPixel>(image, size, location, options), source.Bounds);
source.ApplyProcessor(new DrawImageProcessor<TPixel>(image, size, location, options));
return source;
}

Expand All @@ -49,7 +49,7 @@ public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<T
/// <param name="image">The image to blend with the currently processing image.</param>
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 1.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixel> image, float percent)
public static IImageOperations<TPixel> Blend<TPixel>(this IImageOperations<TPixel> source, Image<TPixel> image, float percent)
where TPixel : struct, IPixel<TPixel>
{
GraphicsOptions options = GraphicsOptions.Default;
Expand All @@ -66,7 +66,7 @@ public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixe
/// <param name="blender">The blending mode.</param>
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 1.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixel> image, PixelBlenderMode blender, float percent)
public static IImageOperations<TPixel> Blend<TPixel>(this IImageOperations<TPixel> source, Image<TPixel> image, PixelBlenderMode blender, float percent)
where TPixel : struct, IPixel<TPixel>
{
GraphicsOptions options = GraphicsOptions.Default;
Expand All @@ -83,7 +83,7 @@ public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixe
/// <param name="image">The image to blend with the currently processing image.</param>
/// <param name="options">The options, including the blending type and belnding amount.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixel> image, GraphicsOptions options)
public static IImageOperations<TPixel> Blend<TPixel>(this IImageOperations<TPixel> source, Image<TPixel> image, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return DrawImage(source, image, default(Size), default(Point), options);
Expand All @@ -99,7 +99,7 @@ public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixe
/// <param name="size">The size to draw the blended image.</param>
/// <param name="location">The location to draw the blended image.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, float percent, Size size, Point location)
public static IImageOperations<TPixel> DrawImage<TPixel>(this IImageOperations<TPixel> source, Image<TPixel> image, float percent, Size size, Point location)
where TPixel : struct, IPixel<TPixel>
{
GraphicsOptions options = GraphicsOptions.Default;
Expand All @@ -118,7 +118,7 @@ public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<T
/// <param name="size">The size to draw the blended image.</param>
/// <param name="location">The location to draw the blended image.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, PixelBlenderMode blender, float percent, Size size, Point location)
public static IImageOperations<TPixel> DrawImage<TPixel>(this IImageOperations<TPixel> source, Image<TPixel> image, PixelBlenderMode blender, float percent, Size size, Point location)
where TPixel : struct, IPixel<TPixel>
{
GraphicsOptions options = GraphicsOptions.Default;
Expand Down
14 changes: 7 additions & 7 deletions src/ImageSharp.Drawing/FillRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static partial class ImageExtensions
/// <param name="brush">The details how to fill the region of interest.</param>
/// <param name="options">The graphics options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, GraphicsOptions options)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, IBrush<TPixel> brush, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return source.Apply(new FillProcessor<TPixel>(brush, options));
Expand All @@ -36,7 +36,7 @@ public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixe
/// <param name="source">The image this method extends.</param>
/// <param name="brush">The details how to fill the region of interest.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, IBrush<TPixel> brush)
where TPixel : struct, IPixel<TPixel>
{
return source.Fill(brush, GraphicsOptions.Default);
Expand All @@ -49,7 +49,7 @@ public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixe
/// <param name="source">The image this method extends.</param>
/// <param name="color">The color.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, TPixel color)
where TPixel : struct, IPixel<TPixel>
{
return source.Fill(new SolidBrush<TPixel>(color));
Expand All @@ -64,7 +64,7 @@ public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color
/// <param name="region">The region.</param>
/// <param name="options">The graphics options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, Region region, GraphicsOptions options)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, IBrush<TPixel> brush, Region region, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return source.Apply(new FillRegionProcessor<TPixel>(brush, region, options));
Expand All @@ -78,7 +78,7 @@ public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixe
/// <param name="brush">The brush.</param>
/// <param name="region">The region.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, Region region)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, IBrush<TPixel> brush, Region region)
where TPixel : struct, IPixel<TPixel>
{
return source.Fill(brush, region, GraphicsOptions.Default);
Expand All @@ -93,7 +93,7 @@ public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixe
/// <param name="region">The region.</param>
/// <param name="options">The options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color, Region region, GraphicsOptions options)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, TPixel color, Region region, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return source.Fill(new SolidBrush<TPixel>(color), region, options);
Expand All @@ -107,7 +107,7 @@ public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color
/// <param name="color">The color.</param>
/// <param name="region">The region.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color, Region region)
public static IImageOperations<TPixel> Fill<TPixel>(this IImageOperations<TPixel> source, TPixel color, Region region)
where TPixel : struct, IPixel<TPixel>
{
return source.Fill(new SolidBrush<TPixel>(color), region);
Expand Down
12 changes: 6 additions & 6 deletions src/ImageSharp.Drawing/Paths/DrawBeziers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static partial class ImageExtensions
/// <param name="points">The points.</param>
/// <param name="options">The options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, float thickness, PointF[] points, GraphicsOptions options)
public static IImageOperations<TPixel> DrawBeziers<TPixel>(this IImageOperations<TPixel> source, IBrush<TPixel> brush, float thickness, PointF[] points, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return source.Draw(new Pen<TPixel>(brush, thickness), new Path(new CubicBezierLineSegment(points)), options);
Expand All @@ -43,7 +43,7 @@ public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IBrus
/// <param name="thickness">The thickness.</param>
/// <param name="points">The points.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, float thickness, PointF[] points)
public static IImageOperations<TPixel> DrawBeziers<TPixel>(this IImageOperations<TPixel> source, IBrush<TPixel> brush, float thickness, PointF[] points)
where TPixel : struct, IPixel<TPixel>
{
return source.Draw(new Pen<TPixel>(brush, thickness), new Path(new CubicBezierLineSegment(points)));
Expand All @@ -58,7 +58,7 @@ public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IBrus
/// <param name="thickness">The thickness.</param>
/// <param name="points">The points.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, TPixel color, float thickness, PointF[] points)
public static IImageOperations<TPixel> DrawBeziers<TPixel>(this IImageOperations<TPixel> source, TPixel color, float thickness, PointF[] points)
where TPixel : struct, IPixel<TPixel>
{
return source.DrawBeziers(new SolidBrush<TPixel>(color), thickness, points);
Expand All @@ -74,7 +74,7 @@ public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, TPixe
/// <param name="points">The points.</param>
/// <param name="options">The options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, TPixel color, float thickness, PointF[] points, GraphicsOptions options)
public static IImageOperations<TPixel> DrawBeziers<TPixel>(this IImageOperations<TPixel> source, TPixel color, float thickness, PointF[] points, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return source.DrawBeziers(new SolidBrush<TPixel>(color), thickness, points, options);
Expand All @@ -89,7 +89,7 @@ public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, TPixe
/// <param name="points">The points.</param>
/// <param name="options">The options.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IPen<TPixel> pen, PointF[] points, GraphicsOptions options)
public static IImageOperations<TPixel> DrawBeziers<TPixel>(this IImageOperations<TPixel> source, IPen<TPixel> pen, PointF[] points, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return source.Draw(pen, new Path(new CubicBezierLineSegment(points)), options);
Expand All @@ -103,7 +103,7 @@ public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IPen<
/// <param name="pen">The pen.</param>
/// <param name="points">The points.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> DrawBeziers<TPixel>(this Image<TPixel> source, IPen<TPixel> pen, PointF[] points)
public static IImageOperations<TPixel> DrawBeziers<TPixel>(this IImageOperations<TPixel> source, IPen<TPixel> pen, PointF[] points)
where TPixel : struct, IPixel<TPixel>
{
return source.Draw(pen, new Path(new CubicBezierLineSegment(points)));
Expand Down
Loading