10
10
using SixLabors . ImageSharp . Memory ;
11
11
using SixLabors . ImageSharp . Metadata ;
12
12
using SixLabors . ImageSharp . Metadata . Profiles . Exif ;
13
- using SixLabors . ImageSharp . Metadata . Profiles . Icc ;
14
13
using SixLabors . ImageSharp . Metadata . Profiles . Xmp ;
15
14
using SixLabors . ImageSharp . PixelFormats ;
16
15
@@ -236,26 +235,59 @@ public Vp8LEncoder(
236
235
/// </summary>
237
236
public Vp8LHashChain HashChain { get ; }
238
237
239
- /// <summary>
240
- /// Encodes the image as lossless webp to the specified stream.
241
- /// </summary>
242
- /// <typeparam name="TPixel">The pixel format.</typeparam>
243
- /// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param>
244
- /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
245
- public void Encode < TPixel > ( Image < TPixel > image , Stream stream )
238
+ public void EncodeHeader < TPixel > ( Image < TPixel > image , Stream stream , bool hasAnimation , uint background = 0 , uint loopCount = 0 )
246
239
where TPixel : unmanaged, IPixel < TPixel >
247
240
{
248
- int width = image . Width ;
249
- int height = image . Height ;
250
-
241
+ // Write bytes from the bitwriter buffer to the stream.
251
242
ImageMetadata metadata = image . Metadata ;
252
243
metadata . SyncProfiles ( ) ;
253
244
254
245
ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
255
246
XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
256
247
248
+ BitWriterBase . WriteTrunksBeforeData (
249
+ stream ,
250
+ ( uint ) image . Width ,
251
+ ( uint ) image . Height ,
252
+ exifProfile ,
253
+ xmpProfile ,
254
+ metadata . IccProfile ,
255
+ false ,
256
+ hasAnimation ) ;
257
+
258
+ if ( hasAnimation )
259
+ {
260
+ BitWriterBase . WriteAnimationParameter ( stream , background , ( ushort ) loopCount ) ;
261
+ }
262
+ }
263
+
264
+ public void EncodeFooter < TPixel > ( Image < TPixel > image , Stream stream )
265
+ where TPixel : unmanaged, IPixel < TPixel >
266
+ {
267
+ // Write bytes from the bitwriter buffer to the stream.
268
+ ImageMetadata metadata = image . Metadata ;
269
+
270
+ ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
271
+ XmpProfile xmpProfile = this . skipMetadata ? null : metadata . XmpProfile ;
272
+
273
+ BitWriterBase . WriteTrunksAfterData ( stream , exifProfile , xmpProfile ) ;
274
+ }
275
+
276
+ /// <summary>
277
+ /// Encodes the image as lossless webp to the specified stream.
278
+ /// </summary>
279
+ /// <typeparam name="TPixel">The pixel format.</typeparam>
280
+ /// <param name="frame">The <see cref="ImageFrame{TPixel}"/> to encode from.</param>
281
+ /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
282
+ /// <param name="hasAnimation">Flag indicating, if an animation parameter is present.</param>
283
+ public void Encode < TPixel > ( ImageFrame < TPixel > frame , Stream stream , bool hasAnimation )
284
+ where TPixel : unmanaged, IPixel < TPixel >
285
+ {
286
+ int width = frame . Width ;
287
+ int height = frame . Height ;
288
+
257
289
// Convert image pixels to bgra array.
258
- bool hasAlpha = this . ConvertPixelsToBgra ( image . Frames . RootFrame , width , height ) ;
290
+ bool hasAlpha = this . ConvertPixelsToBgra ( frame , width , height ) ;
259
291
260
292
// Write the image size.
261
293
this . WriteImageSize ( width , height ) ;
@@ -264,23 +296,28 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
264
296
this . WriteAlphaAndVersion ( hasAlpha ) ;
265
297
266
298
// Encode the main image stream.
267
- this . EncodeStream ( image . Frames . RootFrame ) ;
299
+ this . EncodeStream ( frame ) ;
268
300
269
301
this . bitWriter . Finish ( ) ;
270
- BitWriterBase . WriteTrunksBeforeData (
271
- stream ,
272
- ( uint ) width ,
273
- ( uint ) height ,
274
- exifProfile ,
275
- xmpProfile ,
276
- metadata . IccProfile ,
277
- false /*hasAlpha*/ ,
278
- false ) ;
302
+
303
+ long prevPosition = 0 ;
304
+
305
+ if ( hasAnimation )
306
+ {
307
+ prevPosition = BitWriterBase . WriteAnimationFrame ( stream , new ( )
308
+ {
309
+ Width = ( uint ) frame . Width ,
310
+ Height = ( uint ) frame . Height
311
+ } ) ;
312
+ }
279
313
280
314
// Write bytes from the bitwriter buffer to the stream.
281
315
this . bitWriter . WriteEncodedImageToStream ( stream ) ;
282
316
283
- BitWriterBase . WriteTrunksAfterData ( stream , exifProfile , xmpProfile ) ;
317
+ if ( hasAnimation )
318
+ {
319
+ BitWriterBase . OverwriteFrameSize ( stream , prevPosition ) ;
320
+ }
284
321
}
285
322
286
323
/// <summary>
@@ -1843,9 +1880,9 @@ public void Dispose()
1843
1880
{
1844
1881
this . Bgra . Dispose ( ) ;
1845
1882
this . EncodedData . Dispose ( ) ;
1846
- this . BgraScratch . Dispose ( ) ;
1883
+ this . BgraScratch ? . Dispose ( ) ;
1847
1884
this . Palette . Dispose ( ) ;
1848
- this . TransformData . Dispose ( ) ;
1885
+ this . TransformData ? . Dispose ( ) ;
1849
1886
this . HashChain . Dispose ( ) ;
1850
1887
}
1851
1888
0 commit comments