Пример #1
0
        public async ValueTask InvokeAsync(TiffImageEncoderContext <TPixel> context, ITiffImageEncoderPipelineNode <TPixel> next)
        {
            MemoryPool <byte> memoryPool = context.MemoryPool ?? MemoryPool <byte> .Shared;
            TiffSize          imageSize  = context.ImageSize;
            int arraySize = imageSize.Width * imageSize.Height;

            using (IMemoryOwner <byte> pixelData = memoryPool.Rent(arraySize))
            {
                using (var writer = new TiffMemoryPixelBufferWriter <TiffGray8>(memoryPool, pixelData.Memory, imageSize.Width, imageSize.Height))
                    using (TiffPixelBufferWriter <TPixel> convertedWriter = context.ConvertWriter(writer.AsPixelBufferWriter()))
                    {
                        await context.GetReader().ReadAsync(convertedWriter, context.CancellationToken).ConfigureAwait(false);
                    }

                context.PhotometricInterpretation = TiffPhotometricInterpretation.BlackIsZero;
                context.BitsPerSample             = TiffValueCollection.Single <ushort>(8);
                context.UncompressedData          = pixelData.Memory.Slice(0, arraySize);

                await next.RunAsync(context).ConfigureAwait(false);

                context.UncompressedData = default;
            }

            TiffImageFileDirectoryWriter?ifdWriter = context.IfdWriter;

            if (!(ifdWriter is null))
            {
                using (await context.LockAsync().ConfigureAwait(false))
                {
                    await ifdWriter.WriteTagAsync(TiffTag.PhotometricInterpretation, TiffValueCollection.Single((ushort)context.PhotometricInterpretation)).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.BitsPerSample, context.BitsPerSample).ConfigureAwait(false);
                }
            }
        }
            public ValueTask InvokeAsync(TiffImageEncoderContext <TPixel> context, ITiffImageEncoderPipelineNode <TPixel> next)
            {
                TiffImageFileDirectoryWriter?ifdWriter = context.IfdWriter;

                if (!(ifdWriter is null) && (_useSharedHuffmanTables || _useSharedQuantizationTables))
                {
                    return(new ValueTask(WriteTablesAndContinueAsync(context, next)));
                }

                return(next.RunAsync(context));
            }
Пример #3
0
        public async ValueTask InvokeAsync(TiffImageEncoderContext <TPixel> context, ITiffImageEncoderPipelineNode <TPixel> next)
        {
            MemoryPool <byte> memoryPool = context.MemoryPool ?? MemoryPool <byte> .Shared;
            TiffSize          imageSize  = context.ImageSize;
            int arraySize = 3 * imageSize.Width * imageSize.Height;

            using (IMemoryOwner <byte> pixelData = memoryPool.Rent(arraySize))
            {
                Memory <byte> pixelDataMemory = pixelData.Memory;
                using (var writer = new TiffMemoryPixelBufferWriter <TiffRgb24>(memoryPool, pixelDataMemory, imageSize.Width, imageSize.Height))
                    using (TiffPixelBufferWriter <TPixel> convertedWriter = context.ConvertWriter(writer.AsPixelBufferWriter()))
                    {
                        await context.GetReader().ReadAsync(convertedWriter, context.CancellationToken).ConfigureAwait(false);
                    }

                TiffYCbCrConverter8.CreateDefault().ConvertFromRgb24(MemoryMarshal.Cast <byte, TiffRgb24>(pixelDataMemory.Span), pixelDataMemory.Span, imageSize.Width * imageSize.Height);

                context.PhotometricInterpretation = TiffPhotometricInterpretation.YCbCr;
                context.BitsPerSample             = TiffValueCollection.UnsafeWrap(s_bitsPerSample);
                context.UncompressedData          = pixelDataMemory.Slice(0, arraySize);

                await next.RunAsync(context).ConfigureAwait(false);

                context.UncompressedData = default;
            }

            TiffImageFileDirectoryWriter?ifdWriter = context.IfdWriter;

            if (!(ifdWriter is null))
            {
                using (await context.LockAsync().ConfigureAwait(false))
                {
                    await ifdWriter.WriteTagAsync(TiffTag.PhotometricInterpretation, TiffValueCollection.Single((ushort)context.PhotometricInterpretation)).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.BitsPerSample, context.BitsPerSample).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.SamplesPerPixel, TiffValueCollection.Single <ushort>(3)).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.YCbCrCoefficients, TiffYCbCrConverter8.DefaultLuma).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.ReferenceBlackWhite, TiffYCbCrConverter8.DefaultReferenceBlackWhite).ConfigureAwait(false);
                }
            }
        }
            private async Task WriteTablesAndContinueAsync(TiffImageEncoderContext <TPixel> context, ITiffImageEncoderPipelineNode <TPixel> next)
            {
                TiffImageFileDirectoryWriter?ifdWriter = context.IfdWriter;

                using (await context.LockAsync().ConfigureAwait(false))
                {
                    if (_jpegTables is null)
                    {
                        InitializeTables();
                    }
                    byte[]? jpegTables = _jpegTables;

                    Debug.Assert(ifdWriter != null);
                    Debug.Assert(jpegTables != null);
                    await ifdWriter !.WriteTagAsync(TiffTag.JPEGTables, TiffFieldType.Undefined, TiffValueCollection.UnsafeWrap(jpegTables !)).ConfigureAwait(false);
                }

                await next.RunAsync(context).ConfigureAwait(false);
            }
Пример #5
0
        public async ValueTask InvokeAsync(TiffImageEncoderContext <TPixel> context, ITiffImageEncoderPipelineNode <TPixel> next)
        {
            MemoryPool <byte> memoryPool = context.MemoryPool ?? MemoryPool <byte> .Shared;
            TiffSize          imageSize  = context.ImageSize;

            using (IMemoryOwner <byte> pixelData = memoryPool.Rent(imageSize.Width * imageSize.Height))
            {
                Memory <byte> pixelDataMemory = pixelData.Memory;
                using (var writer = new TiffMemoryPixelBufferWriter <TiffMask>(memoryPool, pixelDataMemory, imageSize.Width, imageSize.Height))
                    using (TiffPixelBufferWriter <TPixel> convertedWriter = context.ConvertWriter(writer.AsPixelBufferWriter()))
                    {
                        await context.GetReader().ReadAsync(convertedWriter, context.CancellationToken).ConfigureAwait(false);
                    }

                int count = PackBytesIntoBits(pixelDataMemory.Span, imageSize, _threshold);

                context.PhotometricInterpretation = TiffPhotometricInterpretation.TransparencyMask;
                context.BitsPerSample             = TiffValueCollection.Single <ushort>(1);
                context.UncompressedData          = pixelData.Memory.Slice(0, count);

                await next.RunAsync(context).ConfigureAwait(false);

                context.UncompressedData = default;
            }

            TiffImageFileDirectoryWriter?ifdWriter = context.IfdWriter;

            if (!(ifdWriter is null))
            {
                using (await context.LockAsync().ConfigureAwait(false))
                {
                    await ifdWriter.WriteTagAsync(TiffTag.PhotometricInterpretation, TiffValueCollection.Single((ushort)context.PhotometricInterpretation)).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.BitsPerSample, context.BitsPerSample).ConfigureAwait(false);

                    await ifdWriter.WriteTagAsync(TiffTag.FillOrder, TiffValueCollection.Single((ushort)TiffFillOrder.HigherOrderBitsFirst)).ConfigureAwait(false);
                }
            }
        }
 public ValueTask RunAsync(TiffImageEncoderContext <TPixel> context)
 {
     Assert.True(context.UncompressedData.Span.SequenceEqual(_expected));
     return(default);