/// <summary> /// Crop a sub region from <paramref name="buffer"/>. /// </summary> /// <typeparam name="TPixel">The pixel type.</typeparam> /// <param name="buffer">The original pixel buffer.</param> /// <param name="offset">The number of columns and rows to skip.</param> /// <param name="size">The number of columns and rows to take.</param> /// <returns>A <see cref="TiffPixelBuffer{TPixel}"/> representing the cropped region.</returns> public static TiffPixelBuffer <TPixel> Crop <TPixel>(this TiffPixelBuffer <TPixel> buffer, TiffPoint offset, TiffSize size) where TPixel : unmanaged { if ((uint)offset.X > (uint)buffer._size.Width) { throw new ArgumentOutOfRangeException(nameof(offset)); } if ((uint)offset.Y > (uint)buffer._size.Height) { throw new ArgumentOutOfRangeException(nameof(offset)); } int offsetX = buffer._offset.X + offset.X; int offsetY = buffer._offset.Y + offset.Y; if ((uint)offsetX > (uint)buffer._size.Width) { throw new ArgumentOutOfRangeException(nameof(offset)); } if ((uint)offsetY > (uint)buffer._size.Height) { throw new ArgumentOutOfRangeException(nameof(offset)); } int sizeWidth = buffer._size.Width - offset.X; int sizeHeight = buffer._size.Height - offset.Y; if ((uint)size.Width > (uint)sizeWidth) { throw new ArgumentOutOfRangeException(nameof(size)); } if ((uint)size.Height > (uint)sizeHeight) { throw new ArgumentOutOfRangeException(nameof(size)); } return(new TiffPixelBuffer <TPixel>(buffer._buffer, new TiffPoint(offsetX, offsetY), size)); }
/// <summary> /// Decode the image into the specified pixel buffer. /// </summary> /// <typeparam name="TPixel">The pixel type.</typeparam> /// <param name="decoder">The image decoder.</param> /// <param name="buffer">The pixel buffer to write pixels into.</param> public static void Decode <TPixel>(this TiffImageDecoder decoder, TiffPixelBuffer <TPixel> buffer) where TPixel : unmanaged { if (decoder is null) { throw new ArgumentNullException(nameof(decoder)); } if (buffer.IsEmpty) { return; } ITiffPixelBuffer <TPixel> innerBuffer = TiffPixelBufferUnsafeMarshal.GetBuffer(buffer, out TiffPoint offset, out TiffSize size); decoder.Decode(default, size, offset, innerBuffer);
/// <summary> /// Decode the image into the specified pixel buffer. /// </summary> /// <typeparam name="TPixel">The pixel type.</typeparam> /// <param name="decoder">The image decoder.</param> /// <param name="buffer">The pixel buffer to write pixels into.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user has requested to abort the decoding pipeline.</param> /// <returns>A <see cref="Task"/> that completes when the image has been decoded.</returns> public static Task DecodeAsync <TPixel>(this TiffImageDecoder decoder, TiffPixelBuffer <TPixel> buffer, CancellationToken cancellationToken = default) where TPixel : unmanaged { if (decoder is null) { throw new ArgumentNullException(nameof(decoder)); } if (buffer.IsEmpty) { return(Task.CompletedTask); } ITiffPixelBuffer <TPixel> innerBuffer = TiffPixelBufferUnsafeMarshal.GetBuffer(buffer, out TiffPoint offset, out TiffSize size); return(decoder.DecodeAsync(default, size, offset, innerBuffer, cancellationToken));