@@ -17,6 +17,12 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
1717 where TPixelBg : unmanaged, IPixel < TPixelBg >
1818 where TPixelFg : unmanaged, IPixel < TPixelFg >
1919{
20+ /// <summary>
21+ /// Counts how many times <see cref="OnFrameApply"/> has been called for this processor instance.
22+ /// Used to select the current foreground frame.
23+ /// </summary>
24+ private int foregroundFrameCounter ;
25+
2026 /// <summary>
2127 /// Initializes a new instance of the <see cref="DrawImageProcessor{TPixelBg, TPixelFg}"/> class.
2228 /// </summary>
@@ -28,6 +34,10 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
2834 /// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param>
2935 /// <param name="alphaCompositionMode">The alpha blending mode to use when drawing the image.</param>
3036 /// <param name="opacity">The opacity of the image to blend. Must be between 0 and 1.</param>
37+ /// <param name="foregroundRepeatCount">
38+ /// The number of times the foreground frames are allowed to loop while applying this processor across successive frames.
39+ /// A value of 0 means loop indefinitely.
40+ /// </param>
3141 public DrawImageProcessor (
3242 Configuration configuration ,
3343 Image < TPixelFg > foregroundImage ,
@@ -36,16 +46,19 @@ public DrawImageProcessor(
3646 Rectangle foregroundRectangle ,
3747 PixelColorBlendingMode colorBlendingMode ,
3848 PixelAlphaCompositionMode alphaCompositionMode ,
39- float opacity )
49+ float opacity ,
50+ int foregroundRepeatCount )
4051 : base ( configuration , backgroundImage , backgroundImage . Bounds )
4152 {
53+ Guard . MustBeGreaterThanOrEqualTo ( foregroundRepeatCount , 0 , nameof ( foregroundRepeatCount ) ) ;
4254 Guard . MustBeBetweenOrEqualTo ( opacity , 0 , 1 , nameof ( opacity ) ) ;
4355
4456 this . ForegroundImage = foregroundImage ;
4557 this . ForegroundRectangle = foregroundRectangle ;
4658 this . Opacity = opacity ;
4759 this . Blender = PixelOperations < TPixelBg > . Instance . GetPixelBlender ( colorBlendingMode , alphaCompositionMode ) ;
4860 this . BackgroundLocation = backgroundLocation ;
61+ this . ForegroundRepeatCount = foregroundRepeatCount ;
4962 }
5063
5164 /// <summary>
@@ -73,6 +86,12 @@ public DrawImageProcessor(
7386 /// </summary>
7487 public Point BackgroundLocation { get ; }
7588
89+ /// <summary>
90+ /// Gets the number of times the foreground frames are allowed to loop while applying this processor across
91+ /// successive frames. A value of 0 means loop indefinitely.
92+ /// </summary>
93+ public int ForegroundRepeatCount { get ; }
94+
7695 /// <inheritdoc/>
7796 protected override void OnFrameApply ( ImageFrame < TPixelBg > source )
7897 {
@@ -114,12 +133,13 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
114133 // Sanitize the dimensions so that we don't try and sample outside the image.
115134 Rectangle backgroundRectangle = Rectangle . Intersect ( new Rectangle ( left , top , width , height ) , this . SourceRectangle ) ;
116135 Configuration configuration = this . Configuration ;
136+ int currentFrameIndex = this . foregroundFrameCounter % this . ForegroundImage . Frames . Count ;
117137
118- DrawImageProcessor < TPixelBg , TPixelFg > . RowOperation operation =
138+ RowOperation operation =
119139 new (
120140 configuration ,
121141 source . PixelBuffer ,
122- this . ForegroundImage . Frames . RootFrame . PixelBuffer ,
142+ this . ForegroundImage . Frames [ currentFrameIndex ] . PixelBuffer ,
123143 backgroundRectangle ,
124144 foregroundRectangle ,
125145 this . Blender ,
@@ -129,6 +149,13 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
129149 configuration ,
130150 new Rectangle ( 0 , 0 , foregroundRectangle . Width , foregroundRectangle . Height ) ,
131151 in operation ) ;
152+
153+ // The repeat count only affects how the foreground frame advances across successive background frames.
154+ // When exhausted, the selected foreground frame stops advancing.
155+ if ( this . ForegroundRepeatCount is 0 || this . foregroundFrameCounter / this . ForegroundImage . Frames . Count < this . ForegroundRepeatCount )
156+ {
157+ this . foregroundFrameCounter ++ ;
158+ }
132159 }
133160
134161 /// <summary>
0 commit comments