@@ -240,7 +240,7 @@ public Vp8LEncoder(
240240 public void EncodeHeader < TPixel > ( Image < TPixel > image , Stream stream , bool hasAnimation )
241241 where TPixel : unmanaged, IPixel < TPixel >
242242 {
243- // Write bytes from the bitwriter buffer to the stream.
243+ // Write bytes from the bit-writer buffer to the stream.
244244 ImageMetadata metadata = image . Metadata ;
245245 metadata . SyncProfiles ( ) ;
246246
@@ -267,7 +267,7 @@ public void EncodeHeader<TPixel>(Image<TPixel> image, Stream stream, bool hasAni
267267 public void EncodeFooter < TPixel > ( Image < TPixel > image , Stream stream )
268268 where TPixel : unmanaged, IPixel < TPixel >
269269 {
270- // Write bytes from the bitwriter buffer to the stream.
270+ // Write bytes from the bit-writer buffer to the stream.
271271 ImageMetadata metadata = image . Metadata ;
272272
273273 ExifProfile exifProfile = this . skipMetadata ? null : metadata . ExifProfile ;
@@ -280,48 +280,44 @@ public void EncodeFooter<TPixel>(Image<TPixel> image, Stream stream)
280280 /// Encodes the image as lossless webp to the specified stream.
281281 /// </summary>
282282 /// <typeparam name="TPixel">The pixel format.</typeparam>
283- /// <param name="frame">The <see cref="ImageFrame{TPixel}"/> to encode from.</param>
283+ /// <param name="frame">The image frame to encode from.</param>
284+ /// <param name="bounds">The region of interest within the frame to encode.</param>
285+ /// <param name="frameMetadata">The frame metadata.</param>
284286 /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
285287 /// <param name="hasAnimation">Flag indicating, if an animation parameter is present.</param>
286- public void Encode < TPixel > ( ImageFrame < TPixel > frame , Stream stream , bool hasAnimation )
288+ public void Encode < TPixel > ( ImageFrame < TPixel > frame , Rectangle bounds , WebpFrameMetadata frameMetadata , Stream stream , bool hasAnimation )
287289 where TPixel : unmanaged, IPixel < TPixel >
288290 {
289- int width = frame . Width ;
290- int height = frame . Height ;
291-
292291 // Convert image pixels to bgra array.
293- bool hasAlpha = this . ConvertPixelsToBgra ( frame , width , height ) ;
292+ bool hasAlpha = this . ConvertPixelsToBgra ( frame . PixelBuffer . GetRegion ( bounds ) ) ;
294293
295294 // Write the image size.
296- this . WriteImageSize ( width , height ) ;
295+ this . WriteImageSize ( bounds . Width , bounds . Height ) ;
297296
298297 // Write the non-trivial Alpha flag and lossless version.
299298 this . WriteAlphaAndVersion ( hasAlpha ) ;
300299
301300 // Encode the main image stream.
302- this . EncodeStream ( frame ) ;
301+ this . EncodeStream ( bounds . Width , bounds . Height ) ;
303302
304303 this . bitWriter . Finish ( ) ;
305304
306305 long prevPosition = 0 ;
307306
308307 if ( hasAnimation )
309308 {
310- WebpFrameMetadata frameMetadata = WebpCommonUtils . GetWebpFrameMetadata ( frame ) ;
311-
312- // TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
313309 prevPosition = new WebpFrameData (
314- 0 ,
315- 0 ,
316- ( uint ) frame . Width ,
317- ( uint ) frame . Height ,
310+ ( uint ) bounds . Left ,
311+ ( uint ) bounds . Top ,
312+ ( uint ) bounds . Width ,
313+ ( uint ) bounds . Height ,
318314 frameMetadata . FrameDelay ,
319315 frameMetadata . BlendMethod ,
320316 frameMetadata . DisposalMethod )
321317 . WriteHeaderTo ( stream ) ;
322318 }
323319
324- // Write bytes from the bitwriter buffer to the stream.
320+ // Write bytes from the bit-writer buffer to the stream.
325321 this . bitWriter . WriteEncodedImageToStream ( stream ) ;
326322
327323 if ( hasAnimation )
@@ -334,23 +330,23 @@ public void Encode<TPixel>(ImageFrame<TPixel> frame, Stream stream, bool hasAnim
334330 /// Encodes the alpha image data using the webp lossless compression.
335331 /// </summary>
336332 /// <typeparam name="TPixel">The type of the pixel.</typeparam>
337- /// <param name="frame">The <see cref="ImageFrame{TPixel}"/> to encode from.</param>
333+ /// <param name="frame">The alpha-pixel data to encode from.</param>
338334 /// <param name="alphaData">The destination buffer to write the encoded alpha data to.</param>
339335 /// <returns>The size of the compressed data in bytes.
340336 /// If the size of the data is the same as the pixel count, the compression would not yield in smaller data and is left uncompressed.
341337 /// </returns>
342- public int EncodeAlphaImageData < TPixel > ( ImageFrame < TPixel > frame , IMemoryOwner < byte > alphaData )
338+ public int EncodeAlphaImageData < TPixel > ( Buffer2DRegion < TPixel > frame , IMemoryOwner < byte > alphaData )
343339 where TPixel : unmanaged, IPixel < TPixel >
344340 {
345341 int width = frame . Width ;
346342 int height = frame . Height ;
347343 int pixelCount = width * height ;
348344
349345 // Convert image pixels to bgra array.
350- this . ConvertPixelsToBgra ( frame , width , height ) ;
346+ this . ConvertPixelsToBgra ( frame ) ;
351347
352348 // The image-stream will NOT contain any headers describing the image dimension, the dimension is already known.
353- this . EncodeStream ( frame ) ;
349+ this . EncodeStream ( width , height ) ;
354350 this . bitWriter . Finish ( ) ;
355351 int size = this . bitWriter . NumBytes ;
356352 if ( size >= pixelCount )
@@ -364,7 +360,7 @@ public int EncodeAlphaImageData<TPixel>(ImageFrame<TPixel> frame, IMemoryOwner<b
364360 }
365361
366362 /// <summary>
367- /// Writes the image size to the bitwriter buffer.
363+ /// Writes the image size to the bit writer buffer.
368364 /// </summary>
369365 /// <param name="inputImgWidth">The input image width.</param>
370366 /// <param name="inputImgHeight">The input image height.</param>
@@ -381,7 +377,7 @@ private void WriteImageSize(int inputImgWidth, int inputImgHeight)
381377 }
382378
383379 /// <summary>
384- /// Writes a flag indicating if alpha channel is used and the VP8L version to the bitwriter buffer.
380+ /// Writes a flag indicating if alpha channel is used and the VP8L version to the bit-writer buffer.
385381 /// </summary>
386382 /// <param name="hasAlpha">Indicates if a alpha channel is present.</param>
387383 private void WriteAlphaAndVersion ( bool hasAlpha )
@@ -393,14 +389,10 @@ private void WriteAlphaAndVersion(bool hasAlpha)
393389 /// <summary>
394390 /// Encodes the image stream using lossless webp format.
395391 /// </summary>
396- /// <typeparam name="TPixel">The pixel type.</typeparam>
397- /// <param name="frame">The frame to encode.</param>
398- private void EncodeStream < TPixel > ( ImageFrame < TPixel > frame )
399- where TPixel : unmanaged, IPixel < TPixel >
392+ /// <param name="width">The image frame width.</param>
393+ /// <param name="height">The image frame height.</param>
394+ private void EncodeStream ( int width , int height )
400395 {
401- int width = frame . Width ;
402- int height = frame . Height ;
403-
404396 Span < uint > bgra = this . Bgra . GetSpan ( ) ;
405397 Span < uint > encodedData = this . EncodedData . GetSpan ( ) ;
406398 bool lowEffort = this . method == 0 ;
@@ -508,23 +500,20 @@ private void EncodeStream<TPixel>(ImageFrame<TPixel> frame)
508500 /// Converts the pixels of the image to bgra.
509501 /// </summary>
510502 /// <typeparam name="TPixel">The type of the pixels.</typeparam>
511- /// <param name="frame">The frame to convert.</param>
512- /// <param name="width">The width of the image.</param>
513- /// <param name="height">The height of the image.</param>
503+ /// <param name="pixels">The frame pixel buffer to convert.</param>
514504 /// <returns>true, if the image is non opaque.</returns>
515- private bool ConvertPixelsToBgra < TPixel > ( ImageFrame < TPixel > frame , int width , int height )
505+ private bool ConvertPixelsToBgra < TPixel > ( Buffer2DRegion < TPixel > pixels )
516506 where TPixel : unmanaged, IPixel < TPixel >
517507 {
518- Buffer2D < TPixel > imageBuffer = frame . PixelBuffer ;
519508 bool nonOpaque = false ;
520509 Span < uint > bgra = this . Bgra . GetSpan ( ) ;
521510 Span < byte > bgraBytes = MemoryMarshal . Cast < uint , byte > ( bgra ) ;
522- int widthBytes = width * 4 ;
523- for ( int y = 0 ; y < height ; y ++ )
511+ int widthBytes = pixels . Width * 4 ;
512+ for ( int y = 0 ; y < pixels . Height ; y ++ )
524513 {
525- Span < TPixel > rowSpan = imageBuffer . DangerousGetRowSpan ( y ) ;
514+ Span < TPixel > rowSpan = pixels . DangerousGetRowSpan ( y ) ;
526515 Span < byte > rowBytes = bgraBytes . Slice ( y * widthBytes , widthBytes ) ;
527- PixelOperations < TPixel > . Instance . ToBgra32Bytes ( this . configuration , rowSpan , rowBytes , width ) ;
516+ PixelOperations < TPixel > . Instance . ToBgra32Bytes ( this . configuration , rowSpan , rowBytes , pixels . Width ) ;
528517 if ( ! nonOpaque )
529518 {
530519 Span < Bgra32 > rowBgra = MemoryMarshal . Cast < byte , Bgra32 > ( rowBytes ) ;
0 commit comments