/// <summary>
 /// Crop a sub region from <paramref name="reader"/>.
 /// </summary>
 /// <typeparam name="TPixel">The pixel type.</typeparam>
 /// <param name="reader">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 TiffPixelBufferReader <TPixel> Crop <TPixel>(this ITiffPixelBufferReader <TPixel> reader, TiffPoint offset, TiffSize size) where TPixel : unmanaged
 {
     if (reader is TiffPixelBufferReader <TPixel> structReader)
     {
         return(structReader.Crop(offset, size));
     }
     return(reader.AsPixelBufferReader().Crop(offset, size));
 }
 /// <summary>
 /// Wraps <paramref name="reader"/> in <see cref="TiffPixelBufferReader{TPixel}"/> structure.
 /// </summary>
 /// <typeparam name="TPixel">The pixel type.</typeparam>
 /// <param name="reader">The pixel buffer reader.</param>
 /// <returns>A <see cref="TiffPixelBufferReader{TPixel}"/> wrapping <paramref name="reader"/>.</returns>
 public static TiffPixelBufferReader <TPixel> AsPixelBufferReader <TPixel>(this ITiffPixelBufferReader <TPixel> reader) where TPixel : unmanaged
 {
     if (reader is TiffPixelBufferReader <TPixel> structReader)
     {
         return(structReader);
     }
     return(new TiffPixelBufferReader <TPixel>(reader));
 }
Exemplo n.º 3
0
 internal TiffPixelBufferReader(ITiffPixelBufferReader <TPixel> reader, TiffPoint offset, TiffSize size)
 {
     _reader = reader;
     if (_reader is TiffPixelBufferReader <TPixel> structReader)
     {
         _reader = structReader._reader;
         _offset = structReader._offset;
         _size   = structReader._size;
     }
     else
     {
         _offset = offset;
         _size   = size;
     }
 }
Exemplo n.º 4
0
 public ImageSharpConversionPixelBufferReader(ITiffPixelBufferReader <TImageSharpPixel> source)
 {
     Debug.Assert(Unsafe.SizeOf <TIntermediate>() == Unsafe.SizeOf <TTiffPixel>());
     _source = source;
 }
 public TiffPassthroughPixelBufferReader(ITiffPixelBufferReader <TSource> source)
 {
     Debug.Assert(Unsafe.SizeOf <TSource>() == Unsafe.SizeOf <TDestination>());
     _source = source;
 }
Exemplo n.º 6
0
        public override Task EncodeAsync(TiffImageFileDirectoryWriter writer, TiffPoint offset, TiffSize size, ITiffPixelBufferReader <TExposedPixel> reader, CancellationToken cancellationToken = default)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (typeof(TExposedPixel) == typeof(TIntermediate))
            {
                return(_encoder.EncodeAsync(writer, offset, size, new TiffPassthroughPixelBufferReader <TExposedPixel, TTiffPixel>(reader), cancellationToken));
            }

            return(_encoder.EncodeAsync(writer, offset, size, new ImageSharpConversionPixelBufferReader <TExposedPixel, TIntermediate, TTiffPixel>(reader), cancellationToken));
        }
Exemplo n.º 7
0
 /// <summary>
 /// Encode an image as well as associated tag fields into TIFF stream.
 /// </summary>
 /// <param name="writer">The <see cref="TiffImageFileDirectoryWriter"/> object to write encoded image data and fields to.</param>
 /// <param name="offset">The number of columns and rows to skip in <paramref name="reader"/>.</param>
 /// <param name="size">The number of columns and rows to encode in <paramref name="reader"/>.</param>
 /// <param name="reader">The pixel buffer reader.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user has requested to abort the encoding pipeline.</param>
 /// <returns>A <see cref="Task"/> that completes when the image and fields have been encoded.</returns>
 public abstract Task EncodeAsync(TiffImageFileDirectoryWriter writer, TiffPoint offset, TiffSize size, ITiffPixelBufferReader <TPixel> reader, CancellationToken cancellationToken = default);
Exemplo n.º 8
0
 /// <summary>
 /// Encode a single image without writing any IFD tag fields.
 /// </summary>
 /// <param name="writer">The <see cref="TiffFileWriter"/> object to write encoded image data to.</param>
 /// <param name="offset">The number of columns and rows to skip in <paramref name="reader"/>.</param>
 /// <param name="size">The number of columns and rows to encode in <paramref name="reader"/>.</param>
 /// <param name="reader">The pixel buffer reader object.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user has requested to abort the encoding pipeline.</param>
 /// <returns>A <see cref="Task{TiffStreamRegion}"/> that completes and return the position and length written into the stream when the image has been encoded.</returns>
 public abstract Task <TiffStreamRegion> EncodeAsync(TiffFileWriter writer, TiffPoint offset, TiffSize size, ITiffPixelBufferReader <TPixel> reader, CancellationToken cancellationToken = default);
Exemplo n.º 9
0
 /// <summary>
 /// Initialize <see cref="TiffPixelBufferReader{TPixel}"/> to wrap <paramref name="reader"/>.
 /// </summary>
 /// <param name="reader">The reader to wrap.</param>
 public TiffPixelBufferReader(ITiffPixelBufferReader <TPixel> reader)
 {
     _reader = reader ?? TiffEmptyPixelBufferReader <TPixel> .Default;
     _offset = default;
     _size   = new TiffSize(_reader.Width, _reader.Height);
 }
Exemplo n.º 10
0
        /// <inheritdoc />
        public override async Task EncodeAsync(TiffImageFileDirectoryWriter writer, TiffPoint offset, TiffSize size, ITiffPixelBufferReader <TPixel> reader, CancellationToken cancellationToken)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (_ifdEncoder is null)
            {
                throw new InvalidOperationException("Ifd encoder is not configured.");
            }

            TiffPixelBufferReader <TPixel> structReader = reader.AsPixelBufferReader();

            size = new TiffSize(Math.Max(0, Math.Min(structReader.Width - offset.X, structReader.Width)), Math.Max(0, Math.Min(structReader.Height - offset.Y, structReader.Height)));
            if (size.IsAreaEmpty)
            {
                throw new ArgumentOutOfRangeException(nameof(size), "The image size is zero.");
            }

            var context = new TiffDefaultImageEncoderContext <TPixel>
            {
                MemoryPool            = _memoryPool ?? MemoryPool <byte> .Shared,
                CancellationToken     = cancellationToken,
                FileWriter            = writer.FileWriter,
                IfdWriter             = writer,
                ImageSize             = size,
                PixelConverterFactory = TiffDefaultPixelConverterFactory.Instance,
                PixelBufferReader     = structReader
            };

            await _ifdEncoder.RunAsync(context).ConfigureAwait(false);
        }
        /// <summary>
        /// Encode a single image without writing any IFD fields.
        /// </summary>
        /// <typeparam name="TPixel">The pixel type.</typeparam>
        /// <param name="encoder">The image encoder.</param>
        /// <param name="writer">The <see cref="TiffFileWriter"/> instance of the output TIFF file.</param>
        /// <param name="reader">The pixel buffer reader object.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that fires if the user has requested to abort the encoding pipeline.</param>
        /// <returns>A <see cref="Task{TiffStreamRegion}"/> that completes and return the position and length written into the stream when the image has been encoded.</returns>
        public static Task <TiffStreamRegion> EncodeAsync <TPixel>(this TiffImageEncoder <TPixel> encoder, TiffFileWriter writer, ITiffPixelBufferReader <TPixel> reader, CancellationToken cancellationToken = default) where TPixel : unmanaged
        {
            if (encoder is null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            return(encoder.EncodeAsync(writer, default, new TiffSize(reader.Width, reader.Height), reader, cancellationToken));