Exemplo n.º 1
0
        public async Task TestRoundtrip(bool isTile)
        {
            TiffGray8[] refImage  = new TiffGray8[2048 * 2048];
            TiffGray8[] testImage = new TiffGray8[2048 * 2048];

            var rand = new Random(42);

            rand.NextBytes(MemoryMarshal.AsBytes(refImage.AsSpan()));

            var builder = new TiffImageEncoderBuilder();

            builder.PhotometricInterpretation = TiffPhotometricInterpretation.BlackIsZero;
            builder.Compression = TiffCompression.NoCompression;
            builder.IsTiled     = isTile;
            // Make sure we have at least CacheSize strips or tiles
            // Cache size in the current implementation is 256
            // Any value greater than 256 should be OK.
            builder.RowsPerStrip = 2;                    // 1024 strips
            builder.TileSize     = new TiffSize(16, 16); // 16384 tiles

            var ms = new MemoryStream();

            await GenerateImageAsync(ms, builder, TiffPixelBuffer.WrapReadOnly(refImage, 2048, 2048));

            ms.Seek(0, SeekOrigin.Begin);

            await using TiffFileReader tiff = await TiffFileReader.OpenAsync(ms, leaveOpen : true);

            TiffImageDecoder decoder = await tiff.CreateImageDecoderAsync();

            await decoder.DecodeAsync(TiffPixelBuffer.Wrap(testImage, 2048, 2048));

            Assert.True(refImage.AsSpan().SequenceEqual(testImage.AsSpan()));
        }
Exemplo n.º 2
0
        public async Task TestRoundtrip(bool isTile, int maxDegreeOfParallelism, bool isYCbCr)
        {
            TiffGray8[] refImage  = new TiffGray8[4096 * 4096];
            TiffGray8[] testImage = new TiffGray8[4096 * 4096];

            var rand = new Random(42);

            rand.NextBytes(MemoryMarshal.AsBytes(refImage.AsSpan()));

            var builder = new TiffImageEncoderBuilder();

            if (isYCbCr)
            {
                builder.PhotometricInterpretation   = TiffPhotometricInterpretation.YCbCr;
                builder.HorizontalChromaSubSampling = 2;
                builder.VerticalChromaSubSampling   = 2;
            }
            else
            {
                builder.PhotometricInterpretation = TiffPhotometricInterpretation.BlackIsZero;
                builder.Predictor = TiffPredictor.HorizontalDifferencing;
            }

            builder.Compression            = TiffCompression.Lzw;
            builder.IsTiled                = isTile;
            builder.RowsPerStrip           = 64;
            builder.TileSize               = new TiffSize(64, 64);
            builder.MaxDegreeOfParallelism = maxDegreeOfParallelism;

            var ms = new MemoryStream();

            await GenerateImageAsync(ms, builder, TiffPixelBuffer.WrapReadOnly(refImage, 4096, 4096));

            ms.Seek(0, SeekOrigin.Begin);

            await using TiffFileReader tiff = await TiffFileReader.OpenAsync(ms, leaveOpen : true);

            TiffImageDecoder decoder = await tiff.CreateImageDecoderAsync();

            await decoder.DecodeAsync(TiffPixelBuffer.Wrap(testImage, 4096, 4096));

            Assert.True(refImage.AsSpan().SequenceEqual(testImage.AsSpan()));
        }