@@ -310,7 +310,7 @@ public Vp8Encoder(
310310 /// </summary>
311311 private int MbHeaderLimit { get ; }
312312
313- public void EncodeHeader < TPixel > ( Image < TPixel > image , Stream stream , bool hasAlpha , bool hasAnimation )
313+ public WebpVp8X EncodeHeader < TPixel > ( Image < TPixel > image , Stream stream , bool hasAlpha , bool hasAnimation )
314314 where TPixel : unmanaged, IPixel < TPixel >
315315 {
316316 // Write bytes from the bitwriter buffer to the stream.
@@ -320,7 +320,7 @@ public void EncodeHeader<TPixel>(Image<TPixel> image, Stream stream, bool hasAlp
320320 ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
321321 XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
322322
323- BitWriterBase . WriteTrunksBeforeData (
323+ WebpVp8X vp8x = BitWriterBase . WriteTrunksBeforeData (
324324 stream ,
325325 ( uint ) image . Width ,
326326 ( uint ) image . Height ,
@@ -335,9 +335,11 @@ public void EncodeHeader<TPixel>(Image<TPixel> image, Stream stream, bool hasAlp
335335 WebpMetadata webpMetadata = WebpCommonUtils . GetWebpMetadata ( image ) ;
336336 BitWriterBase . WriteAnimationParameter ( stream , webpMetadata . BackgroundColor , webpMetadata . RepeatCount ) ;
337337 }
338+
339+ return vp8x ;
338340 }
339341
340- public void EncodeFooter < TPixel > ( Image < TPixel > image , Stream stream )
342+ public void EncodeFooter < TPixel > ( Image < TPixel > image , in WebpVp8X vp8x , bool hasAlpha , Stream stream , long initialPosition )
341343 where TPixel : unmanaged, IPixel < TPixel >
342344 {
343345 // Write bytes from the bitwriter buffer to the stream.
@@ -346,7 +348,9 @@ public void EncodeFooter<TPixel>(Image<TPixel> image, Stream stream)
346348 ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
347349 XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
348350
349- BitWriterBase . WriteTrunksAfterData ( stream , exifProfile , xmpProfile ) ;
351+ bool updateVp8x = hasAlpha && vp8x != default ;
352+ WebpVp8X updated = updateVp8x ? vp8x . WithAlpha ( true ) : vp8x ;
353+ BitWriterBase . WriteTrunksAfterData ( stream , in updated , updateVp8x , initialPosition , exifProfile , xmpProfile ) ;
350354 }
351355
352356 /// <summary>
@@ -357,9 +361,10 @@ public void EncodeFooter<TPixel>(Image<TPixel> image, Stream stream)
357361 /// <param name="stream">The stream to encode the image data to.</param>
358362 /// <param name="bounds">The region of interest within the frame to encode.</param>
359363 /// <param name="frameMetadata">The frame metadata.</param>
360- public void EncodeAnimation < TPixel > ( ImageFrame < TPixel > frame , Stream stream , Rectangle bounds , WebpFrameMetadata frameMetadata )
361- where TPixel : unmanaged, IPixel < TPixel > =>
362- this . Encode ( stream , frame , bounds , frameMetadata , true , null ) ;
364+ /// <returns>A <see cref="bool"/> indicating whether the frame contains an alpha channel.</returns>
365+ public bool EncodeAnimation < TPixel > ( ImageFrame < TPixel > frame , Stream stream , Rectangle bounds , WebpFrameMetadata frameMetadata )
366+ where TPixel : unmanaged, IPixel < TPixel >
367+ => this . Encode ( stream , frame , bounds , frameMetadata , true , null ) ;
363368
364369 /// <summary>
365370 /// Encodes the static image frame to the specified stream.
@@ -384,7 +389,8 @@ public void EncodeStatic<TPixel>(Stream stream, Image<TPixel> image)
384389 /// <param name="frameMetadata">The frame metadata.</param>
385390 /// <param name="hasAnimation">Flag indicating, if an animation parameter is present.</param>
386391 /// <param name="image">The image to encode from.</param>
387- private void Encode < TPixel > ( Stream stream , ImageFrame < TPixel > frame , Rectangle bounds , WebpFrameMetadata frameMetadata , bool hasAnimation , Image < TPixel > image )
392+ /// <returns>A <see cref="bool"/> indicating whether the frame contains an alpha channel.</returns>
393+ private bool Encode < TPixel > ( Stream stream , ImageFrame < TPixel > frame , Rectangle bounds , WebpFrameMetadata frameMetadata , bool hasAnimation , Image < TPixel > image )
388394 where TPixel : unmanaged, IPixel < TPixel >
389395 {
390396 int width = bounds . Width ;
@@ -514,6 +520,8 @@ private void Encode<TPixel>(Stream stream, ImageFrame<TPixel> frame, Rectangle b
514520 {
515521 encodedAlphaData ? . Dispose ( ) ;
516522 }
523+
524+ return hasAlpha ;
517525 }
518526
519527 /// <inheritdoc/>
0 commit comments