Skip to content

Commit ad094ff

Browse files
Merge pull request #2322 from SixLabors/js/2112
Add DrawImage(Image, Point, Rectangle) method to the DrawImage processor
2 parents df13097 + 493ad9f commit ad094ff

File tree

8 files changed

+253
-111
lines changed

8 files changed

+253
-111
lines changed

src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs

Lines changed: 207 additions & 95 deletions
Large diffs are not rendered by default.

src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public DrawImageProcessor(
6363
public IImageProcessor<TPixelBg> CreatePixelSpecificProcessor<TPixelBg>(Configuration configuration, Image<TPixelBg> source, Rectangle sourceRectangle)
6464
where TPixelBg : unmanaged, IPixel<TPixelBg>
6565
{
66-
var visitor = new ProcessorFactoryVisitor<TPixelBg>(configuration, this, source, sourceRectangle);
66+
ProcessorFactoryVisitor<TPixelBg> visitor = new(configuration, this, source, sourceRectangle);
6767
this.Image.AcceptVisitor(visitor);
6868
return visitor.Result;
6969
}
@@ -88,8 +88,7 @@ public ProcessorFactoryVisitor(Configuration configuration, DrawImageProcessor d
8888

8989
public void Visit<TPixelFg>(Image<TPixelFg> image)
9090
where TPixelFg : unmanaged, IPixel<TPixelFg>
91-
{
92-
this.Result = new DrawImageProcessor<TPixelBg, TPixelFg>(
91+
=> this.Result = new DrawImageProcessor<TPixelBg, TPixelFg>(
9392
this.configuration,
9493
image,
9594
this.source,
@@ -98,6 +97,5 @@ public void Visit<TPixelFg>(Image<TPixelFg> image)
9897
this.definition.ColorBlendingMode,
9998
this.definition.AlphaCompositionMode,
10099
this.definition.Opacity);
101-
}
102100
}
103101
}

src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
8989

9090
int width = maxX - minX;
9191

92-
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
92+
Rectangle workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
9393

9494
// Not a valid operation because rectangle does not overlap with this image.
9595
if (workingRect.Width <= 0 || workingRect.Height <= 0)
@@ -98,7 +98,7 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
9898
"Cannot draw image because the source image does not overlap the target image.");
9999
}
100100

101-
var operation = new RowOperation(source.PixelBuffer, targetImage.Frames.RootFrame.PixelBuffer, blender, configuration, minX, width, locationY, targetX, this.Opacity);
101+
DrawImageProcessor<TPixelBg, TPixelFg>.RowOperation operation = new(source.PixelBuffer, targetImage.Frames.RootFrame.PixelBuffer, blender, configuration, minX, width, locationY, targetX, this.Opacity);
102102
ParallelRowIterator.IterateRows(
103103
configuration,
104104
workingRect,

tests/ImageSharp.Tests/Drawing/DrawImageTests.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ public void DrawImageOfDifferentPixelType<TPixel>(TestImageProvider<TPixel> prov
119119
public void WorksWithDifferentLocations(TestImageProvider<Rgba32> provider, int x, int y)
120120
{
121121
using Image<Rgba32> background = provider.GetImage();
122-
using Image<Rgba32> overlay = new(50, 50);
123-
Assert.True(overlay.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> overlayMem));
124-
overlayMem.Span.Fill(Color.Black);
122+
using Image<Rgba32> overlay = new(50, 50, Color.Black.ToRgba32());
125123

126124
background.Mutate(c => c.DrawImage(overlay, new Point(x, y), PixelColorBlendingMode.Normal, 1F));
127125

@@ -138,6 +136,31 @@ public void WorksWithDifferentLocations(TestImageProvider<Rgba32> provider, int
138136
appendSourceFileOrDescription: false);
139137
}
140138

139+
[Theory]
140+
[WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 10, 10)]
141+
[WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 50, 25)]
142+
[WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 25, 50)]
143+
[WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, 50, 50)]
144+
public void WorksWithDifferentBounds(TestImageProvider<Rgba32> provider, int width, int height)
145+
{
146+
using Image<Rgba32> background = provider.GetImage();
147+
using Image<Rgba32> overlay = new(50, 50, Color.Black.ToRgba32());
148+
149+
background.Mutate(c => c.DrawImage(overlay, new Rectangle(0, 0, width, height), PixelColorBlendingMode.Normal, 1F));
150+
151+
background.DebugSave(
152+
provider,
153+
testOutputDetails: $"{width}_{height}",
154+
appendPixelTypeToFileName: false,
155+
appendSourceFileOrDescription: false);
156+
157+
background.CompareToReferenceOutput(
158+
provider,
159+
testOutputDetails: $"{width}_{height}",
160+
appendPixelTypeToFileName: false,
161+
appendSourceFileOrDescription: false);
162+
}
163+
141164
[Theory]
142165
[WithFile(TestImages.Png.Splash, PixelTypes.Rgba32)]
143166
public void DrawTransformed<TPixel>(TestImageProvider<TPixel> provider)
@@ -158,12 +181,12 @@ public void DrawTransformed<TPixel>(TestImageProvider<TPixel> provider)
158181
Point position = new((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2);
159182
image.Mutate(x => x.DrawImage(blend, position, .75F));
160183

161-
image.DebugSave(provider, appendSourceFileOrDescription: false, appendPixelTypeToFileName: false);
184+
image.DebugSave(provider, appendPixelTypeToFileName: false, appendSourceFileOrDescription: false);
162185
image.CompareToReferenceOutput(
163186
ImageComparer.TolerantPercentage(0.002f),
164187
provider,
165-
appendSourceFileOrDescription: false,
166-
appendPixelTypeToFileName: false);
188+
appendPixelTypeToFileName: false,
189+
appendSourceFileOrDescription: false);
167190
}
168191

169192
[Theory]
@@ -179,9 +202,6 @@ public void NonOverlappingImageThrows(TestImageProvider<Rgba32> provider, int x,
179202

180203
Assert.Contains("does not overlap", ex.ToString());
181204

182-
void Test()
183-
{
184-
background.Mutate(context => context.DrawImage(overlay, new Point(x, y), new GraphicsOptions()));
185-
}
205+
void Test() => background.Mutate(context => context.DrawImage(overlay, new Point(x, y), new GraphicsOptions()));
186206
}
187207
}
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)