@@ -311,7 +311,7 @@ public Vp8Encoder(
311311 /// </summary>
312312 private int MbHeaderLimit { get ; }
313313
314- public void EncodeHeader < TPixel > ( Image < TPixel > image , Stream stream , bool hasAlpha , bool hasAnimation )
314+ public WebpVp8X EncodeHeader < TPixel > ( Image < TPixel > image , Stream stream , bool hasAlpha , bool hasAnimation )
315315 where TPixel : unmanaged, IPixel < TPixel >
316316 {
317317 // Write bytes from the bitwriter buffer to the stream.
@@ -321,7 +321,7 @@ public void EncodeHeader<TPixel>(Image<TPixel> image, Stream stream, bool hasAlp
321321 ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
322322 XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
323323
324- BitWriterBase . WriteTrunksBeforeData (
324+ WebpVp8X vp8x = BitWriterBase . WriteTrunksBeforeData (
325325 stream ,
326326 ( uint ) image . Width ,
327327 ( uint ) image . Height ,
@@ -336,9 +336,11 @@ public void EncodeHeader<TPixel>(Image<TPixel> image, Stream stream, bool hasAlp
336336 WebpMetadata webpMetadata = WebpCommonUtils . GetWebpMetadata ( image ) ;
337337 BitWriterBase . WriteAnimationParameter ( stream , webpMetadata . BackgroundColor , webpMetadata . RepeatCount ) ;
338338 }
339+
340+ return vp8x ;
339341 }
340342
341- public void EncodeFooter < TPixel > ( Image < TPixel > image , Stream stream )
343+ public void EncodeFooter < TPixel > ( Image < TPixel > image , in WebpVp8X vp8x , bool hasAlpha , Stream stream , long initialPosition )
342344 where TPixel : unmanaged, IPixel < TPixel >
343345 {
344346 // Write bytes from the bitwriter buffer to the stream.
@@ -347,7 +349,9 @@ public void EncodeFooter<TPixel>(Image<TPixel> image, Stream stream)
347349 ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
348350 XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
349351
350- BitWriterBase . WriteTrunksAfterData ( stream , exifProfile , xmpProfile ) ;
352+ bool updateVp8x = hasAlpha && vp8x != default ;
353+ WebpVp8X updated = updateVp8x ? vp8x . WithAlpha ( true ) : vp8x ;
354+ BitWriterBase . WriteTrunksAfterData ( stream , in updated , updateVp8x , initialPosition , exifProfile , xmpProfile ) ;
351355 }
352356
353357 /// <summary>
@@ -358,9 +362,10 @@ public void EncodeFooter<TPixel>(Image<TPixel> image, Stream stream)
358362 /// <param name="stream">The stream to encode the image data to.</param>
359363 /// <param name="bounds">The region of interest within the frame to encode.</param>
360364 /// <param name="frameMetadata">The frame metadata.</param>
361- public void EncodeAnimation < TPixel > ( ImageFrame < TPixel > frame , Stream stream , Rectangle bounds , WebpFrameMetadata frameMetadata )
362- where TPixel : unmanaged, IPixel < TPixel > =>
363- this . Encode ( stream , frame , bounds , frameMetadata , true , null ) ;
365+ /// <returns>A <see cref="bool"/> indicating whether the frame contains an alpha channel.</returns>
366+ public bool EncodeAnimation < TPixel > ( ImageFrame < TPixel > frame , Stream stream , Rectangle bounds , WebpFrameMetadata frameMetadata )
367+ where TPixel : unmanaged, IPixel < TPixel >
368+ => this . Encode ( stream , frame , bounds , frameMetadata , true , null ) ;
364369
365370 /// <summary>
366371 /// Encodes the static image frame to the specified stream.
@@ -385,7 +390,8 @@ public void EncodeStatic<TPixel>(Stream stream, Image<TPixel> image)
385390 /// <param name="frameMetadata">The frame metadata.</param>
386391 /// <param name="hasAnimation">Flag indicating, if an animation parameter is present.</param>
387392 /// <param name="image">The image to encode from.</param>
388- private void Encode < TPixel > ( Stream stream , ImageFrame < TPixel > frame , Rectangle bounds , WebpFrameMetadata frameMetadata , bool hasAnimation , Image < TPixel > image )
393+ /// <returns>A <see cref="bool"/> indicating whether the frame contains an alpha channel.</returns>
394+ private bool Encode < TPixel > ( Stream stream , ImageFrame < TPixel > frame , Rectangle bounds , WebpFrameMetadata frameMetadata , bool hasAnimation , Image < TPixel > image )
389395 where TPixel : unmanaged, IPixel < TPixel >
390396 {
391397 int width = bounds . Width ;
@@ -515,6 +521,8 @@ private void Encode<TPixel>(Stream stream, ImageFrame<TPixel> frame, Rectangle b
515521 {
516522 encodedAlphaData ? . Dispose ( ) ;
517523 }
524+
525+ return hasAlpha ;
518526 }
519527
520528 /// <inheritdoc/>
0 commit comments