Browse Source

Enhance DrawImage functionality by adding repeatCount parameter for animation control in DrawImageExtensions and DrawImageProcessor classes.

pull/2995/head
予纾 4 months ago
parent
commit
60149fc64f
  1. 80
      src/ImageSharp/Processing/Extensions/Drawing/DrawImageExtensions.cs
  2. 19
      src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs
  3. 20
      src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs

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

@ -21,10 +21,11 @@ public static class DrawImageExtensions
public static IImageProcessingContext DrawImage(
this IImageProcessingContext source,
Image foreground,
float opacity)
float opacity,
int repeatCount)
{
GraphicsOptions options = source.GetGraphicsOptions();
return DrawImage(source, foreground, options.ColorBlendingMode, options.AlphaCompositionMode, opacity);
return DrawImage(source, foreground, options.ColorBlendingMode, options.AlphaCompositionMode, opacity, repeatCount);
}
/// <summary>
@ -39,10 +40,11 @@ public static class DrawImageExtensions
this IImageProcessingContext source,
Image foreground,
Rectangle foregroundRectangle,
float opacity)
float opacity,
int repeatCount)
{
GraphicsOptions options = source.GetGraphicsOptions();
return DrawImage(source, foreground, foregroundRectangle, options.ColorBlendingMode, options.AlphaCompositionMode, opacity);
return DrawImage(source, foreground, foregroundRectangle, options.ColorBlendingMode, options.AlphaCompositionMode, opacity, repeatCount);
}
/// <summary>
@ -57,8 +59,9 @@ public static class DrawImageExtensions
this IImageProcessingContext source,
Image foreground,
PixelColorBlendingMode colorBlending,
float opacity)
=> DrawImage(source, foreground, Point.Empty, colorBlending, opacity);
float opacity,
int repeatCount)
=> DrawImage(source, foreground, Point.Empty, colorBlending, opacity, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -74,8 +77,9 @@ public static class DrawImageExtensions
Image foreground,
Rectangle foregroundRectangle,
PixelColorBlendingMode colorBlending,
float opacity)
=> DrawImage(source, foreground, foregroundRectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity);
float opacity,
int repeatCount)
=> DrawImage(source, foreground, Point.Empty, foregroundRectangle, colorBlending, opacity, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -91,8 +95,9 @@ public static class DrawImageExtensions
Image foreground,
PixelColorBlendingMode colorBlending,
PixelAlphaCompositionMode alphaComposition,
float opacity)
=> DrawImage(source, foreground, Point.Empty, colorBlending, alphaComposition, opacity);
float opacity,
int repeatCount)
=> DrawImage(source, foreground, Point.Empty, colorBlending, alphaComposition, opacity, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -110,8 +115,9 @@ public static class DrawImageExtensions
Rectangle foregroundRectangle,
PixelColorBlendingMode colorBlending,
PixelAlphaCompositionMode alphaComposition,
float opacity)
=> DrawImage(source, foreground, Point.Empty, foregroundRectangle, colorBlending, alphaComposition, opacity);
float opacity,
int repeatCount)
=> DrawImage(source, foreground, Point.Empty, foregroundRectangle, colorBlending, alphaComposition, opacity, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -123,8 +129,9 @@ public static class DrawImageExtensions
public static IImageProcessingContext DrawImage(
this IImageProcessingContext source,
Image foreground,
GraphicsOptions options)
=> DrawImage(source, foreground, Point.Empty, options);
GraphicsOptions options,
int repeatCount)
=> DrawImage(source, foreground, Point.Empty, options, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -138,8 +145,9 @@ public static class DrawImageExtensions
this IImageProcessingContext source,
Image foreground,
Rectangle foregroundRectangle,
GraphicsOptions options)
=> DrawImage(source, foreground, Point.Empty, foregroundRectangle, options);
GraphicsOptions options,
int repeatCount)
=> DrawImage(source, foreground, Point.Empty, foregroundRectangle, options, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -153,10 +161,11 @@ public static class DrawImageExtensions
this IImageProcessingContext source,
Image foreground,
Point backgroundLocation,
float opacity)
float opacity,
int repeatCount)
{
GraphicsOptions options = source.GetGraphicsOptions();
return DrawImage(source, foreground, backgroundLocation, options.ColorBlendingMode, options.AlphaCompositionMode, opacity);
return DrawImage(source, foreground, backgroundLocation, options.ColorBlendingMode, options.AlphaCompositionMode, opacity, repeatCount);
}
/// <summary>
@ -173,10 +182,11 @@ public static class DrawImageExtensions
Image foreground,
Point backgroundLocation,
Rectangle foregroundRectangle,
float opacity)
float opacity,
int repeatCount)
{
GraphicsOptions options = source.GetGraphicsOptions();
return DrawImage(source, foreground, backgroundLocation, foregroundRectangle, options.ColorBlendingMode, options.AlphaCompositionMode, opacity);
return DrawImage(source, foreground, backgroundLocation, foregroundRectangle, options.ColorBlendingMode, options.AlphaCompositionMode, opacity, repeatCount);
}
/// <summary>
@ -193,8 +203,9 @@ public static class DrawImageExtensions
Image foreground,
Point backgroundLocation,
PixelColorBlendingMode colorBlending,
float opacity)
=> DrawImage(source, foreground, backgroundLocation, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity);
float opacity,
int repeatCount)
=> DrawImage(source, foreground, backgroundLocation, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -212,8 +223,9 @@ public static class DrawImageExtensions
Point backgroundLocation,
Rectangle foregroundRectangle,
PixelColorBlendingMode colorBlending,
float opacity)
=> DrawImage(source, foreground, backgroundLocation, foregroundRectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity);
float opacity,
int repeatCount)
=> DrawImage(source, foreground, backgroundLocation, foregroundRectangle, colorBlending, source.GetGraphicsOptions().AlphaCompositionMode, opacity, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -227,8 +239,9 @@ public static class DrawImageExtensions
this IImageProcessingContext source,
Image foreground,
Point backgroundLocation,
GraphicsOptions options)
=> DrawImage(source, foreground, backgroundLocation, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage);
GraphicsOptions options,
int repeatCount)
=> DrawImage(source, foreground, backgroundLocation, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -244,8 +257,9 @@ public static class DrawImageExtensions
Image foreground,
Point backgroundLocation,
Rectangle foregroundRectangle,
GraphicsOptions options)
=> DrawImage(source, foreground, backgroundLocation, foregroundRectangle, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage);
GraphicsOptions options,
int repeatCount)
=> DrawImage(source, foreground, backgroundLocation, foregroundRectangle, options.ColorBlendingMode, options.AlphaCompositionMode, options.BlendPercentage, repeatCount);
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -263,8 +277,9 @@ public static class DrawImageExtensions
Point backgroundLocation,
PixelColorBlendingMode colorBlending,
PixelAlphaCompositionMode alphaComposition,
float opacity)
=> source.ApplyProcessor(new DrawImageProcessor(foreground, backgroundLocation, foreground.Bounds, colorBlending, alphaComposition, opacity));
float opacity,
int repeatCount)
=> source.ApplyProcessor(new DrawImageProcessor(foreground, backgroundLocation, foreground.Bounds, colorBlending, alphaComposition, opacity, repeatCount));
/// <summary>
/// Draws the given image together with the currently processing image by blending their pixels.
@ -284,8 +299,9 @@ public static class DrawImageExtensions
Rectangle foregroundRectangle,
PixelColorBlendingMode colorBlending,
PixelAlphaCompositionMode alphaComposition,
float opacity) =>
float opacity,
int repeatCount) =>
source.ApplyProcessor(
new DrawImageProcessor(foreground, backgroundLocation, foregroundRectangle, colorBlending, alphaComposition, opacity),
new DrawImageProcessor(foreground, backgroundLocation, foregroundRectangle, colorBlending, alphaComposition, opacity, repeatCount),
foregroundRectangle);
}

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

@ -19,13 +19,15 @@ public class DrawImageProcessor : IImageProcessor
/// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param>
/// <param name="alphaCompositionMode">The Alpha blending mode to use when drawing the image.</param>
/// <param name="opacity">The opacity of the image to blend.</param>
/// <param name="repeatCount">The loop count. The number of times to loop the animation. 0 means infinitely.</param>
public DrawImageProcessor(
Image foreground,
Point backgroundLocation,
PixelColorBlendingMode colorBlendingMode,
PixelAlphaCompositionMode alphaCompositionMode,
float opacity)
: this(foreground, backgroundLocation, foreground.Bounds, colorBlendingMode, alphaCompositionMode, opacity)
float opacity,
int repeatCount)
: this(foreground, backgroundLocation, foreground.Bounds, colorBlendingMode, alphaCompositionMode, opacity, repeatCount)
{
}
@ -38,13 +40,15 @@ public class DrawImageProcessor : IImageProcessor
/// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param>
/// <param name="alphaCompositionMode">The Alpha blending mode to use when drawing the image.</param>
/// <param name="opacity">The opacity of the image to blend.</param>
/// <param name="repeatCount">The loop count. The number of times to loop the animation. 0 means infinitely.</param>
public DrawImageProcessor(
Image foreground,
Point backgroundLocation,
Rectangle foregroundRectangle,
PixelColorBlendingMode colorBlendingMode,
PixelAlphaCompositionMode alphaCompositionMode,
float opacity)
float opacity,
int repeatCount)
{
this.ForeGround = foreground;
this.BackgroundLocation = backgroundLocation;
@ -52,6 +56,7 @@ public class DrawImageProcessor : IImageProcessor
this.ColorBlendingMode = colorBlendingMode;
this.AlphaCompositionMode = alphaCompositionMode;
this.Opacity = opacity;
this.RepeatCount = repeatCount;
}
/// <summary>
@ -84,6 +89,11 @@ public class DrawImageProcessor : IImageProcessor
/// </summary>
public float Opacity { get; }
/// <summary>
/// Gets the loop count. The number of times to loop the animation. 0 means infinitely.
/// </summary>
public int RepeatCount { get; }
/// <inheritdoc />
public IImageProcessor<TPixelBg> CreatePixelSpecificProcessor<TPixelBg>(Configuration configuration, Image<TPixelBg> source, Rectangle sourceRectangle)
where TPixelBg : unmanaged, IPixel<TPixelBg>
@ -122,6 +132,7 @@ public class DrawImageProcessor : IImageProcessor
this.definition.ForegroundRectangle,
this.definition.ColorBlendingMode,
this.definition.AlphaCompositionMode,
this.definition.Opacity);
this.definition.Opacity,
this.definition.RepeatCount);
}
}

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

@ -17,6 +17,8 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
where TPixelBg : unmanaged, IPixel<TPixelBg>
where TPixelFg : unmanaged, IPixel<TPixelFg>
{
private int currentFrameLoop;
/// <summary>
/// Initializes a new instance of the <see cref="DrawImageProcessor{TPixelBg, TPixelFg}"/> class.
/// </summary>
@ -28,6 +30,7 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
/// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param>
/// <param name="alphaCompositionMode">The alpha blending mode to use when drawing the image.</param>
/// <param name="opacity">The opacity of the image to blend. Must be between 0 and 1.</param>
/// <param name="repeatCount">The loop count. The number of times to loop the animation. 0 means infinitely.</param>
public DrawImageProcessor(
Configuration configuration,
Image<TPixelFg> foregroundImage,
@ -36,9 +39,11 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
Rectangle foregroundRectangle,
PixelColorBlendingMode colorBlendingMode,
PixelAlphaCompositionMode alphaCompositionMode,
float opacity)
float opacity,
int repeatCount)
: base(configuration, backgroundImage, backgroundImage.Bounds)
{
Guard.MustBeGreaterThanOrEqualTo(repeatCount, 0, nameof(repeatCount));
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));
this.ForegroundImage = foregroundImage;
@ -73,6 +78,11 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
/// </summary>
public Point BackgroundLocation { get; }
/// <summary>
/// Gets the loop count. The number of times to loop the animation. 0 means infinitely.
/// </summary>
public int RepeatCount { get; }
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixelBg> source)
{
@ -114,12 +124,13 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
// Sanitize the dimensions so that we don't try and sample outside the image.
Rectangle backgroundRectangle = Rectangle.Intersect(new Rectangle(left, top, width, height), this.SourceRectangle);
Configuration configuration = this.Configuration;
int currentFrameIndex = this.currentFrameLoop % this.ForegroundImage.Frames.Count;
DrawImageProcessor<TPixelBg, TPixelFg>.RowOperation operation =
new(
configuration,
source.PixelBuffer,
this.ForegroundImage.Frames.RootFrame.PixelBuffer,
this.ForegroundImage.Frames[currentFrameIndex].PixelBuffer,
backgroundRectangle,
foregroundRectangle,
this.Blender,
@ -129,6 +140,11 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
configuration,
new Rectangle(0, 0, foregroundRectangle.Width, foregroundRectangle.Height),
in operation);
if (this.RepeatCount is 0 || this.currentFrameLoop / this.ForegroundImage.Frames.Count <= this.RepeatCount)
{
this.currentFrameLoop++;
}
}
/// <summary>

Loading…
Cancel
Save