public ValueTask ReadAsync(TiffPoint offset, TiffPixelBufferWriter <TDestination> destination, CancellationToken cancellationToken)
        {
            ITiffPixelBufferWriter <TDestination> destinationWriter = TiffPixelBufferUnsafeMarshal.GetBuffer(destination, out TiffPoint destinationOffset, out TiffSize size);
            TiffPixelBufferWriter <TSource>       newDestination    = new TiffPassthroughPixelBufferWriter <TSource, TDestination>(destinationWriter).AsPixelBufferWriter().Crop(destinationOffset, size);

            return(_source.ReadAsync(offset, newDestination, cancellationToken));
        }
示例#2
0
        public ValueTask ReadAsync(TiffPoint offset, TiffPixelBufferWriter <TTiffPixel> destination, CancellationToken cancellationToken)
        {
            ITiffPixelBufferWriter <TTiffPixel>      destinationWriter = TiffPixelBufferUnsafeMarshal.GetBuffer(destination, out TiffPoint destinationOffset, out TiffSize size);
            TiffPixelBufferWriter <TImageSharpPixel> newDestination    = new ImageSharpConversionPixelBufferWriter <TImageSharpPixel, TIntermediate, TTiffPixel>(destinationWriter).AsPixelBufferWriter().Crop(destinationOffset, size);

            return(_source.ReadAsync(offset, newDestination, cancellationToken));
        }
        public void TestCrop()
        {
            ITiffPixelBuffer <TiffGray8> pixelBuffer = PixelBufferTests.InitializePixelBuffer(out TiffGray8[] pixels);
            var writer = new TiffPixelBufferWriterAdapter <TiffGray8>(pixelBuffer);

            TiffPixelBufferWriter <TiffGray8> structWriter = writer.AsPixelBufferWriter();

            Assert.Equal(3, structWriter.Width);
            Assert.Equal(2, structWriter.Height);
            Assert.False(structWriter.IsEmpty);

            Assert.Throws <ArgumentOutOfRangeException>("offset", () => structWriter.Crop(new TiffPoint(4, 0)));
            Assert.Throws <ArgumentOutOfRangeException>("offset", () => structWriter.Crop(new TiffPoint(0, 3)));
            Assert.Throws <ArgumentOutOfRangeException>("offset", () => structWriter.Crop(new TiffPoint(4, 3)));

            Assert.Throws <ArgumentOutOfRangeException>("size", () => structWriter.Crop(new TiffPoint(0, 0), new TiffSize(4, 1)));
            Assert.Throws <ArgumentOutOfRangeException>("size", () => structWriter.Crop(new TiffPoint(0, 0), new TiffSize(1, 3)));
            Assert.Throws <ArgumentOutOfRangeException>("size", () => structWriter.Crop(new TiffPoint(0, 0), new TiffSize(4, 3)));

            structWriter = writer.Crop(new TiffPoint(1, 1), new TiffSize(1, 1));
            Assert.Equal(1, structWriter.Width);
            Assert.Equal(1, structWriter.Height);

            structWriter = writer.AsPixelBufferWriter().Crop(new TiffPoint(1, 1), new TiffSize(1, 1));
            Assert.Equal(1, structWriter.Width);
            Assert.Equal(1, structWriter.Height);

            structWriter = writer.Crop(new TiffPoint(1, 1));
            Assert.Equal(2, structWriter.Width);
            Assert.Equal(1, structWriter.Height);

            structWriter = writer.AsPixelBufferWriter().Crop(new TiffPoint(1, 1));
            Assert.Equal(2, structWriter.Width);
            Assert.Equal(1, structWriter.Height);

            Assert.Throws <ArgumentOutOfRangeException>("rowIndex", () => structWriter.GetRowSpan(-1, 0, 1));
            Assert.Throws <ArgumentOutOfRangeException>("rowIndex", () => structWriter.GetRowSpan(2, 0, 1));
            Assert.Throws <ArgumentOutOfRangeException>("start", () => structWriter.GetRowSpan(0, -1, 1));
            Assert.Throws <ArgumentOutOfRangeException>("start", () => structWriter.GetRowSpan(0, 3, 1));
            Assert.Throws <ArgumentOutOfRangeException>("length", () => structWriter.GetRowSpan(0, 1, -1));
            Assert.Throws <ArgumentOutOfRangeException>("length", () => structWriter.GetRowSpan(0, 1, 2));

            Assert.Throws <ArgumentOutOfRangeException>("colIndex", () => structWriter.GetColumnSpan(-1, 0, 1));
            Assert.Throws <ArgumentOutOfRangeException>("colIndex", () => structWriter.GetColumnSpan(2, 0, 1));
            Assert.Throws <ArgumentOutOfRangeException>("start", () => structWriter.GetColumnSpan(0, -1, 1));
            Assert.Throws <ArgumentOutOfRangeException>("start", () => structWriter.GetColumnSpan(0, 2, 1));
            Assert.Throws <ArgumentOutOfRangeException>("length", () => structWriter.GetColumnSpan(0, 1, -1));
            Assert.Throws <ArgumentOutOfRangeException>("length", () => structWriter.GetColumnSpan(0, 1, 2));

            ITiffPixelBufferWriter <TiffGray8> writer2 = TiffPixelBufferUnsafeMarshal.GetBuffer(structWriter, out TiffPoint offset, out TiffSize size);

            Assert.True(ReferenceEquals(writer, writer2));
            Assert.Equal(1, offset.X);
            Assert.Equal(1, offset.Y);
            Assert.Equal(2, size.Width);
            Assert.Equal(1, size.Height);
        }
示例#4
0
        /// <summary>
        /// A function to get destination buffer in the specified pixel format.
        /// </summary>
        public sealed override TiffPixelBufferWriter <TPixel> GetWriter <TPixel>()
        {
            ITiffPixelConverterFactory?pixelConverterFactory = PixelConverterFactory;

            if (pixelConverterFactory is null)
            {
                throw new InvalidOperationException("Failed to acquire PixelConverterFactory");
            }

            ITiffPixelBufferWriter <TDestinationPixel> buffer = TiffPixelBufferUnsafeMarshal.GetBuffer(DestinationWriter, out TiffPoint offset, out TiffSize size);

            return(pixelConverterFactory.CreateConverter <TPixel, TDestinationPixel>(buffer).Crop(offset, 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>
        /// Converts pixel buffer writer of any pixel format <typeparamref name="TBuffer"/> into <see cref="TiffPixelBufferWriter{TPixel}"/>.
        /// </summary>
        /// <typeparam name="TBuffer">The specified pixel type.</typeparam>
        /// <param name="writer">The writer to be converted.</param>
        /// <returns>The converted writer.</returns>
        public override TiffPixelBufferWriter <TPixel> ConvertWriter <TBuffer>(TiffPixelBufferWriter <TBuffer> writer)
        {
            ITiffPixelConverterFactory?pixelConverterFactory = PixelConverterFactory;

            if (pixelConverterFactory is null)
            {
                throw new InvalidOperationException("Failed to acquire PixelConverterFactory");
            }

            ITiffPixelBufferWriter <TBuffer> innerWriter = TiffPixelBufferUnsafeMarshal.GetBuffer(writer, out TiffPoint offset, out TiffSize size);
            ITiffPixelBufferWriter <TPixel>  converted   = pixelConverterFactory.CreateConverter <TPixel, TBuffer>(innerWriter);

            return(converted.Crop(offset, size));
        }
示例#7
0
        /// <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));
示例#8
0
        public void TestCrop()
        {
            ITiffPixelBuffer <TiffGray8> pixelBuffer  = InitializePixelBuffer(out TiffGray8[] pixels);
            TiffPixelBuffer <TiffGray8>  structBuffer = pixelBuffer.AsPixelBuffer();

            Assert.Equal(3, structBuffer.Width);
            Assert.Equal(2, structBuffer.Height);
            Assert.False(structBuffer.IsEmpty);

            Assert.Throws <ArgumentOutOfRangeException>("offset", () => structBuffer.Crop(new TiffPoint(4, 0)));
            Assert.Throws <ArgumentOutOfRangeException>("offset", () => structBuffer.Crop(new TiffPoint(0, 3)));
            Assert.Throws <ArgumentOutOfRangeException>("offset", () => structBuffer.Crop(new TiffPoint(4, 3)));

            Assert.Throws <ArgumentOutOfRangeException>("size", () => structBuffer.Crop(new TiffPoint(0, 0), new TiffSize(4, 1)));
            Assert.Throws <ArgumentOutOfRangeException>("size", () => structBuffer.Crop(new TiffPoint(0, 0), new TiffSize(1, 3)));
            Assert.Throws <ArgumentOutOfRangeException>("size", () => structBuffer.Crop(new TiffPoint(0, 0), new TiffSize(4, 3)));

            structBuffer = pixelBuffer.Crop(new TiffPoint(1, 1), new TiffSize(1, 1));
            Assert.Equal(1, structBuffer.Width);
            Assert.Equal(1, structBuffer.Height);

            structBuffer = pixelBuffer.AsPixelBuffer().Crop(new TiffPoint(1, 1), new TiffSize(1, 1));
            Assert.Equal(1, structBuffer.Width);
            Assert.Equal(1, structBuffer.Height);

            structBuffer = pixelBuffer.Crop(new TiffPoint(1, 1));
            Assert.Equal(2, structBuffer.Width);
            Assert.Equal(1, structBuffer.Height);

            structBuffer = pixelBuffer.AsPixelBuffer().Crop(new TiffPoint(1, 1));
            Assert.Equal(2, structBuffer.Width);
            Assert.Equal(1, structBuffer.Height);

            ITiffPixelBuffer <TiffGray8> pixelBuffer2 = TiffPixelBufferUnsafeMarshal.GetBuffer(structBuffer, out TiffPoint offset, out TiffSize size);

            Assert.True(ReferenceEquals(pixelBuffer, pixelBuffer2));
            Assert.Equal(1, offset.X);
            Assert.Equal(1, offset.Y);
            Assert.Equal(2, size.Width);
            Assert.Equal(1, size.Height);
        }