public void EncoderOptions_SetPhotometricInterpretationAndCompression_Works(
            TiffPhotometricInterpretation?photometricInterpretation,
            TiffCompression compression,
            TiffBitsPerPixel expectedBitsPerPixel,
            TiffCompression expectedCompression)
        {
            // arrange
            var tiffEncoder = new TiffEncoder {
                PhotometricInterpretation = photometricInterpretation, Compression = compression
            };

            using Image input   = new Image <Rgb24>(10, 10);
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata rootFrameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(expectedBitsPerPixel, rootFrameMetaData.BitsPerPixel);
            Assert.Equal(expectedCompression, rootFrameMetaData.Compression);
        }
Example #2
0
 private static void VerifyExpectedTiffFrameMetaDataIsPresent(TiffFrameMetadata frameMetaData)
 {
     Assert.NotNull(frameMetaData);
     Assert.NotNull(frameMetaData.BitsPerPixel);
     Assert.Equal(TiffBitsPerPixel.Bit4, frameMetaData.BitsPerPixel);
     Assert.Equal(TiffCompression.Lzw, frameMetaData.Compression);
     Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frameMetaData.PhotometricInterpretation);
     Assert.Equal(TiffPredictor.None, frameMetaData.Predictor);
 }
        public void TiffEncoder_PreservesPredictor <TPixel>(TestImageProvider <TPixel> provider, TiffPredictor?expectedPredictor)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // arrange
            var tiffEncoder = new TiffEncoder();

            using Image <TPixel> input = provider.GetImage();
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata frameMetadata = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(expectedPredictor, frameMetadata.Predictor);
        }
        public void TiffEncoder_PreservesBitsPerPixel_WhenInputIsL8()
        {
            // arrange
            var tiffEncoder = new TiffEncoder();

            using Image input   = new Image <L8>(10, 10);
            using var memStream = new MemoryStream();
            TiffBitsPerPixel expectedBitsPerPixel = TiffBitsPerPixel.Bit8;

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel);
        }
        public void EncoderOptions_UnsupportedBitPerPixel_DefaultTo24Bits(TiffBitsPerPixel bitsPerPixel)
        {
            // arrange
            var tiffEncoder = new TiffEncoder {
                BitsPerPixel = bitsPerPixel
            };

            using Image input   = new Image <Rgb24>(10, 10);
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);

            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(TiffBitsPerPixel.Bit24, frameMetaData.BitsPerPixel);
        }
        public void EncoderOptions_SetBitPerPixel_Works(TiffBitsPerPixel bitsPerPixel)
        {
            // arrange
            var tiffEncoder = new TiffEncoder {
                BitsPerPixel = bitsPerPixel
            };

            using Image input   = new Image <Rgb24>(10, 10);
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);

            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(bitsPerPixel, frameMetaData.BitsPerPixel);
            Assert.Equal(TiffCompression.None, frameMetaData.Compression);
        }
Example #7
0
        public void TiffFrameMetadata_CloneIsDeep <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using (Image <TPixel> image = provider.GetImage(TiffDecoder))
            {
                TiffFrameMetadata meta        = image.Frames.RootFrame.Metadata.GetTiffMetadata();
                var cloneSameAsSampleMetaData = (TiffFrameMetadata)meta.DeepClone();
                VerifyExpectedTiffFrameMetaDataIsPresent(cloneSameAsSampleMetaData);

                var clone = (TiffFrameMetadata)meta.DeepClone();

                clone.BitsPerPixel = TiffBitsPerPixel.Bit8;
                clone.Compression  = TiffCompression.None;
                clone.PhotometricInterpretation = TiffPhotometricInterpretation.CieLab;
                clone.Predictor = TiffPredictor.Horizontal;

                Assert.False(meta.BitsPerPixel == clone.BitsPerPixel);
                Assert.False(meta.Compression == clone.Compression);
                Assert.False(meta.PhotometricInterpretation == clone.PhotometricInterpretation);
                Assert.False(meta.Predictor == clone.Predictor);
            }
        }
        public void TiffEncoder_EncodesWithCorrectBiColorModeCompression <TPixel>(TestImageProvider <TPixel> provider, TiffCompression compression, TiffCompression expectedCompression)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // arrange
            var encoder = new TiffEncoder()
            {
                Compression = compression, BitsPerPixel = TiffBitsPerPixel.Bit1
            };

            using Image <TPixel> input = provider.GetImage();
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, encoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(TiffBitsPerPixel.Bit1, frameMetaData.BitsPerPixel);
            Assert.Equal(expectedCompression, frameMetaData.Compression);
        }
Example #9
0
        public void Encode_PreservesMetadata <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // Load Tiff image
            using Image <TPixel> image = provider.GetImage(new TiffDecoder()
            {
                IgnoreMetadata = false
            });

            ImageMetadata       inputMetaData  = image.Metadata;
            ImageFrame <TPixel> rootFrameInput = image.Frames.RootFrame;
            TiffFrameMetadata   frameMetaInput = rootFrameInput.Metadata.GetTiffMetadata();

            byte[]      xmpProfileInput  = rootFrameInput.Metadata.XmpProfile;
            ExifProfile exifProfileInput = rootFrameInput.Metadata.ExifProfile;

            Assert.Equal(TiffCompression.Lzw, frameMetaInput.Compression);
            Assert.Equal(TiffBitsPerPixel.Bit4, frameMetaInput.BitsPerPixel);

            // Save to Tiff
            var tiffEncoder = new TiffEncoder()
            {
                PhotometricInterpretation = TiffPhotometricInterpretation.Rgb
            };

            using var ms = new MemoryStream();
            image.Save(ms, tiffEncoder);

            // Assert
            ms.Position            = 0;
            using var encodedImage = Image.Load <Rgba32>(ms);

            ImageMetadata       encodedImageMetaData         = encodedImage.Metadata;
            ImageFrame <Rgba32> rootFrameEncodedImage        = encodedImage.Frames.RootFrame;
            TiffFrameMetadata   tiffMetaDataEncodedRootFrame = rootFrameEncodedImage.Metadata.GetTiffMetadata();
            ExifProfile         encodedImageExifProfile      = rootFrameEncodedImage.Metadata.ExifProfile;

            byte[] encodedImageXmpProfile = rootFrameEncodedImage.Metadata.XmpProfile;

            Assert.Equal(TiffBitsPerPixel.Bit4, tiffMetaDataEncodedRootFrame.BitsPerPixel);
            Assert.Equal(TiffCompression.Lzw, tiffMetaDataEncodedRootFrame.Compression);

            Assert.Equal(inputMetaData.HorizontalResolution, encodedImageMetaData.HorizontalResolution);
            Assert.Equal(inputMetaData.VerticalResolution, encodedImageMetaData.VerticalResolution);
            Assert.Equal(inputMetaData.ResolutionUnits, encodedImageMetaData.ResolutionUnits);

            Assert.Equal(rootFrameInput.Width, rootFrameEncodedImage.Width);
            Assert.Equal(rootFrameInput.Height, rootFrameEncodedImage.Height);

            PixelResolutionUnit resolutionUnitInput   = UnitConverter.ExifProfileToResolutionUnit(exifProfileInput);
            PixelResolutionUnit resolutionUnitEncoded = UnitConverter.ExifProfileToResolutionUnit(encodedImageExifProfile);

            Assert.Equal(resolutionUnitInput, resolutionUnitEncoded);
            Assert.Equal(exifProfileInput.GetValue(ExifTag.XResolution).Value.ToDouble(), encodedImageExifProfile.GetValue(ExifTag.XResolution).Value.ToDouble());
            Assert.Equal(exifProfileInput.GetValue(ExifTag.YResolution).Value.ToDouble(), encodedImageExifProfile.GetValue(ExifTag.YResolution).Value.ToDouble());

            Assert.Equal(xmpProfileInput, encodedImageXmpProfile);

            Assert.Equal("IrfanView", exifProfileInput.GetValue(ExifTag.Software).Value);
            Assert.Equal("This is Название", exifProfileInput.GetValue(ExifTag.ImageDescription).Value);
            Assert.Equal("This is Изготовитель камеры", exifProfileInput.GetValue(ExifTag.Make).Value);
            Assert.Equal("This is Авторские права", exifProfileInput.GetValue(ExifTag.Copyright).Value);

            Assert.Equal(exifProfileInput.GetValue(ExifTag.ImageDescription).Value, encodedImageExifProfile.GetValue(ExifTag.ImageDescription).Value);
            Assert.Equal(exifProfileInput.GetValue(ExifTag.Make).Value, encodedImageExifProfile.GetValue(ExifTag.Make).Value);
            Assert.Equal(exifProfileInput.GetValue(ExifTag.Copyright).Value, encodedImageExifProfile.GetValue(ExifTag.Copyright).Value);

            // Note that the encoded profile has PlanarConfiguration explicitly set, which is missing in the original image profile.
            Assert.Equal((ushort)TiffPlanarConfiguration.Chunky, encodedImageExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value);
            Assert.Equal(exifProfileInput.Values.Count + 1, encodedImageExifProfile.Values.Count);
        }
Example #10
0
        public void BaselineTags <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using (Image <TPixel> image = provider.GetImage(TiffDecoder))
            {
                ImageFrame <TPixel> rootFrame = image.Frames.RootFrame;
                Assert.Equal(32, rootFrame.Width);
                Assert.Equal(32, rootFrame.Height);
                Assert.NotNull(rootFrame.Metadata.XmpProfile);
                Assert.Equal(2599, rootFrame.Metadata.XmpProfile.Length);

                ExifProfile       exifProfile       = rootFrame.Metadata.ExifProfile;
                TiffFrameMetadata tiffFrameMetadata = rootFrame.Metadata.GetTiffMetadata();
                Assert.NotNull(exifProfile);

                // The original exifProfile has 30 values, but 4 of those values will be stored in the TiffFrameMetaData
                // and removed from the profile on decode.
                Assert.Equal(26, exifProfile.Values.Count);
                Assert.Equal(TiffBitsPerPixel.Bit4, tiffFrameMetadata.BitsPerPixel);
                Assert.Equal(TiffCompression.Lzw, tiffFrameMetadata.Compression);
                Assert.Equal("This is Название", exifProfile.GetValue(ExifTag.ImageDescription).Value);
                Assert.Equal("This is Изготовитель камеры", exifProfile.GetValue(ExifTag.Make).Value);
                Assert.Equal("This is Модель камеры", exifProfile.GetValue(ExifTag.Model).Value);
                Assert.Equal("IrfanView", exifProfile.GetValue(ExifTag.Software).Value);
                Assert.Null(exifProfile.GetValue(ExifTag.DateTime)?.Value);
                Assert.Equal("This is author1;Author2", exifProfile.GetValue(ExifTag.Artist).Value);
                Assert.Null(exifProfile.GetValue(ExifTag.HostComputer)?.Value);
                Assert.Equal("This is Авторские права", exifProfile.GetValue(ExifTag.Copyright).Value);
                Assert.Equal(4, exifProfile.GetValue(ExifTag.Rating).Value);
                Assert.Equal(75, exifProfile.GetValue(ExifTag.RatingPercent).Value);
                var expectedResolution = new Rational(10000, 1000, simplify: false);
                Assert.Equal(expectedResolution, exifProfile.GetValue(ExifTag.XResolution).Value);
                Assert.Equal(expectedResolution, exifProfile.GetValue(ExifTag.YResolution).Value);
                Assert.Equal(new Number[] { 8u }, exifProfile.GetValue(ExifTag.StripOffsets)?.Value, new NumberComparer());
                Assert.Equal(new Number[] { 297u }, exifProfile.GetValue(ExifTag.StripByteCounts)?.Value, new NumberComparer());
                Assert.Null(exifProfile.GetValue(ExifTag.ExtraSamples)?.Value);
                Assert.Equal(32u, exifProfile.GetValue(ExifTag.RowsPerStrip).Value);
                Assert.Null(exifProfile.GetValue(ExifTag.SampleFormat));
                Assert.Equal(TiffPredictor.None, tiffFrameMetadata.Predictor);
                Assert.Equal(PixelResolutionUnit.PixelsPerInch, UnitConverter.ExifProfileToResolutionUnit(exifProfile));
                ushort[] colorMap = exifProfile.GetValue(ExifTag.ColorMap)?.Value;
                Assert.NotNull(colorMap);
                Assert.Equal(48, colorMap.Length);
                Assert.Equal(10537, colorMap[0]);
                Assert.Equal(14392, colorMap[1]);
                Assert.Equal(58596, colorMap[46]);
                Assert.Equal(3855, colorMap[47]);
                Assert.Equal(TiffPhotometricInterpretation.PaletteColor, tiffFrameMetadata.PhotometricInterpretation);
                Assert.Equal(1u, exifProfile.GetValue(ExifTag.SamplesPerPixel).Value);

                ImageMetadata imageMetaData = image.Metadata;
                Assert.NotNull(imageMetaData);
                Assert.Equal(PixelResolutionUnit.PixelsPerInch, imageMetaData.ResolutionUnits);
                Assert.Equal(10, imageMetaData.HorizontalResolution);
                Assert.Equal(10, imageMetaData.VerticalResolution);

                TiffMetadata tiffMetaData = image.Metadata.GetTiffMetadata();
                Assert.NotNull(tiffMetaData);
                Assert.Equal(ByteOrder.LittleEndian, tiffMetaData.ByteOrder);
            }
        }
        protected static void TestStripLength <TPixel>(
            TestImageProvider <TPixel> provider,
            TiffPhotometricInterpretation photometricInterpretation,
            TiffCompression compression,
            bool useExactComparer  = true,
            float compareTolerance = 0.01f)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // arrange
            var tiffEncoder = new TiffEncoder()
            {
                PhotometricInterpretation = photometricInterpretation, Compression = compression
            };

            using Image <TPixel> input = provider.GetImage();
            using var memStream = new MemoryStream();
            TiffFrameMetadata inputMeta        = input.Frames.RootFrame.Metadata.GetTiffMetadata();
            TiffCompression   inputCompression = inputMeta.Compression ?? TiffCompression.None;

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            ExifProfile         exifProfileOutput = output.Frames.RootFrame.Metadata.ExifProfile;
            TiffFrameMetadata   outputMeta        = output.Frames.RootFrame.Metadata.GetTiffMetadata();
            ImageFrame <Rgba32> rootFrame         = output.Frames.RootFrame;

            Number rowsPerStrip = exifProfileOutput.GetValue(ExifTag.RowsPerStrip) != null?exifProfileOutput.GetValue(ExifTag.RowsPerStrip).Value : TiffConstants.RowsPerStripInfinity;

            Assert.True(output.Height > (int)rowsPerStrip);
            Assert.True(exifProfileOutput.GetValue(ExifTag.StripOffsets)?.Value.Length > 1);
            Number[] stripByteCounts = exifProfileOutput.GetValue(ExifTag.StripByteCounts)?.Value;
            Assert.NotNull(stripByteCounts);
            Assert.True(stripByteCounts.Length > 1);
            Assert.NotNull(outputMeta.BitsPerPixel);

            foreach (Number sz in stripByteCounts)
            {
                Assert.True((uint)sz <= TiffConstants.DefaultStripSize);
            }

            // For uncompressed more accurate test.
            if (compression == TiffCompression.None)
            {
                for (int i = 0; i < stripByteCounts.Length - 1; i++)
                {
                    // The difference must be less than one row.
                    int stripBytes = (int)stripByteCounts[i];
                    int widthBytes = ((int)outputMeta.BitsPerPixel + 7) / 8 * rootFrame.Width;

                    Assert.True((TiffConstants.DefaultStripSize - stripBytes) < widthBytes);
                }
            }

            // Compare with reference.
            TestTiffEncoderCore(
                provider,
                inputMeta.BitsPerPixel,
                photometricInterpretation,
                inputCompression,
                useExactComparer: useExactComparer,
                compareTolerance: compareTolerance);
        }