Esempio n. 1
0
        public void Test_FromXDocument()
        {
            using (var image = new MagickImage(Files.InvitationTIF))
            {
                var profile = image.GetXmpProfile();
                Assert.NotNull(profile);

                var doc = profile.ToXDocument();

                Assert.Throws <ArgumentNullException>("document", () =>
                {
                    XmpProfile.FromXDocument(null);
                });

                var newProfile = XmpProfile.FromXDocument(doc);
                image.SetProfile(newProfile);

                doc = profile.ToXDocument();
                TestXDocument(doc);

                profile = image.GetXmpProfile();
                Assert.NotNull(profile);

                doc = profile.ToXDocument();
                TestXDocument(doc);

                Assert.Equal(profile, newProfile);
            }
        }
Esempio n. 2
0
        public void Test_FromIXPathNavigable()
        {
            using (MagickImage image = new MagickImage(Files.InvitationTif))
            {
                XmpProfile profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                IXPathNavigable doc = profile.ToIXPathNavigable();

                ExceptionAssert.Throws <ArgumentNullException>(delegate()
                {
                    XmpProfile.FromIXPathNavigable(null);
                });

                XmpProfile newProfile = XmpProfile.FromIXPathNavigable(doc);
                image.AddProfile(newProfile);

                doc = profile.ToIXPathNavigable();
                TestIXPathNavigable(doc);

                profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                doc = profile.ToIXPathNavigable();
                TestIXPathNavigable(doc);

                Assert.AreEqual(profile, newProfile);
            }
        }
Esempio n. 3
0
        public void Test_FromXDocument()
        {
            using (IMagickImage image = new MagickImage(Files.InvitationTIF))
            {
                XmpProfile profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                XDocument doc = profile.ToXDocument();

                ExceptionAssert.Throws <ArgumentNullException>("document", () =>
                {
                    XmpProfile.FromXDocument(null);
                });

                XmpProfile newProfile = XmpProfile.FromXDocument(doc);
                image.AddProfile(newProfile);

                doc = profile.ToXDocument();
                TestXDocument(doc);

                profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                doc = profile.ToXDocument();
                TestXDocument(doc);

                Assert.AreEqual(profile, newProfile);
            }
        }
 public void ShouldThrowExceptionWhenDocumentIsNull()
 {
     Assert.Throws <ArgumentNullException>("document", () =>
     {
         XmpProfile.FromXDocument(null);
     });
 }
            public void ShouldCreateProfileFromIXDocument()
            {
                var document = XDocument.Parse("<test />");

                var profile = XmpProfile.FromXDocument(document);

                Assert.NotNull(profile);
                Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?><test />", Encoding.UTF8.GetString(profile.ToByteArray()));
            }
Esempio n. 6
0
        public void Test_ToXDocument()
        {
            using (MagickImage image = new MagickImage(Files.InvitationTif))
            {
                XmpProfile profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                XDocument document = profile.ToXDocument();
                TestXDocument(document);
            }
        }
Esempio n. 7
0
        public void Test_ToIXPathNavigable()
        {
            using (MagickImage image = new MagickImage(Files.InvitationTif))
            {
                XmpProfile profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                IXPathNavigable doc = profile.ToIXPathNavigable();
                TestIXPathNavigable(doc);
            }
        }
            public void ShouldCreateProfileFromIXPathNavigable()
            {
                var document = new XmlDocument();

                document.LoadXml("<test />");

                var profile = XmpProfile.FromIXPathNavigable(document);

                Assert.NotNull(profile);
                Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?><test />", Encoding.UTF8.GetString(profile.ToByteArray()));
            }
Esempio n. 9
0
        public void Test_CreateReader()
        {
            using (MagickImage image = new MagickImage(Files.InvitationTif))
            {
                XmpProfile profile = image.GetXmpProfile();
                Assert.IsNotNull(profile);

                using (XmlReader reader = profile.CreateReader())
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(reader);
                    TestIXPathNavigable(doc);
                }
            }
        }
Esempio n. 10
0
            public void Process(Image image)
            {
                ImageFrame  rootFrame            = image.Frames.RootFrame;
                ExifProfile rootFrameExifProfile = rootFrame.Metadata.ExifProfile ?? new ExifProfile();
                XmpProfile  rootFrameXmpProfile  = rootFrame.Metadata.XmpProfile;

                this.ProcessProfiles(image.Metadata, rootFrameExifProfile, rootFrameXmpProfile);
                this.ProcessMetadata(rootFrameExifProfile);

                if (!this.Collector.Entries.Exists(t => t.Tag == ExifTag.Software))
                {
                    this.Collector.Add(new ExifString(ExifTagValue.Software)
                    {
                        Value = SoftwareValue
                    });
                }
            }
        public void CloneIsDeep()
        {
            // arrange
            var exifProfile = new ExifProfile();

            exifProfile.SetValue(ExifTag.Software, "UnitTest");
            exifProfile.SetValue(ExifTag.Artist, "UnitTest");
            var xmpProfile = new XmpProfile(new byte[0]);
            var iccProfile = new IccProfile()
            {
                Header = new IccProfileHeader()
                {
                    CmmType = "Unittest"
                }
            };
            var iptcProfile = new ImageSharp.Metadata.Profiles.Iptc.IptcProfile();
            var metaData    = new ImageFrameMetadata()
            {
                XmpProfile  = xmpProfile,
                ExifProfile = exifProfile,
                IccProfile  = iccProfile,
                IptcProfile = iptcProfile
            };

            // act
            ImageFrameMetadata clone = metaData.DeepClone();

            // assert
            Assert.NotNull(clone);
            Assert.NotNull(clone.ExifProfile);
            Assert.NotNull(clone.XmpProfile);
            Assert.NotNull(clone.IccProfile);
            Assert.NotNull(clone.IptcProfile);
            Assert.False(metaData.ExifProfile.Equals(clone.ExifProfile));
            Assert.True(metaData.ExifProfile.Values.Count == clone.ExifProfile.Values.Count);
            Assert.False(ReferenceEquals(metaData.XmpProfile, clone.XmpProfile));
            Assert.True(metaData.XmpProfile.Data.Equals(clone.XmpProfile.Data));
            Assert.False(metaData.GetGifMetadata().Equals(clone.GetGifMetadata()));
            Assert.False(metaData.IccProfile.Equals(clone.IccProfile));
            Assert.False(metaData.IptcProfile.Equals(clone.IptcProfile));
        }
Esempio n. 12
0
        private static void Metadata(MagickImage image, ImageDetails details)
        {
            var xmp        = MetadataController.Get(details);
            var xmpBuffer  = XmpMetaFactory.SerializeToBuffer(xmp, new SerializeOptions());
            var xmpProfile = new XmpProfile(xmpBuffer);

            image.AddProfile(xmpProfile);

            var iptcProfile = new IptcProfile();

            iptcProfile.SetValue(IptcTag.City, "London");
            iptcProfile.SetValue(IptcTag.Country, "UK");
            iptcProfile.SetValue(IptcTag.Contact, "[email protected], +447740424810,http://www.mcandrewphoto.co.uk");

            iptcProfile.SetValue(IptcTag.CopyrightNotice, "Attribution 3.0 Unported (CC BY 3.0)");
            iptcProfile.SetValue(IptcTag.Title, $"{details.GivenName} {details.FamilyName}");

            iptcProfile.SetValue(IptcTag.Source, "Chris McAndrew / UK Parliament");
            iptcProfile.SetValue(IptcTag.Credit, "Chris McAndrew / UK Parliament (Attribution 3.0 Unported (CC BY 3.0))");

            image.AddProfile(iptcProfile);
        }
Esempio n. 13
0
        /// <summary>
        /// Writes the encoded image to the stream.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="exifProfile">The exif profile.</param>
        /// <param name="xmpProfile">The XMP profile.</param>
        /// <param name="width">The width of the image.</param>
        /// <param name="height">The height of the image.</param>
        /// <param name="hasAlpha">Flag indicating, if a alpha channel is present.</param>
        public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, XmpProfile xmpProfile, uint width, uint height, bool hasAlpha)
        {
            bool isVp8X = false;

            byte[] exifBytes = null;
            byte[] xmpBytes  = null;
            uint   riffSize  = 0;

            if (exifProfile != null)
            {
                isVp8X    = true;
                riffSize += ExtendedFileChunkSize;
                exifBytes = exifProfile.ToByteArray();
                riffSize += this.MetadataChunkSize(exifBytes);
            }

            if (xmpProfile != null)
            {
                isVp8X    = true;
                riffSize += ExtendedFileChunkSize;
                xmpBytes  = xmpProfile.Data;
                riffSize += this.MetadataChunkSize(xmpBytes);
            }

            this.Finish();
            uint size = (uint)this.NumBytes();

            size++; // One byte extra for the VP8L signature.

            // Write RIFF header.
            uint pad = size & 1;

            riffSize += WebpConstants.TagSize + WebpConstants.ChunkHeaderSize + size + pad;
            this.WriteRiffHeader(stream, riffSize);

            // Write VP8X, header if necessary.
            if (isVp8X)
            {
                this.WriteVp8XHeader(stream, exifProfile, xmpProfile, width, height, hasAlpha);
            }

            // Write magic bytes indicating its a lossless webp.
            stream.Write(WebpConstants.Vp8LMagicBytes);

            // Write Vp8 Header.
            BinaryPrimitives.WriteUInt32LittleEndian(this.scratchBuffer, size);
            stream.Write(this.scratchBuffer.AsSpan(0, 4));
            stream.WriteByte(WebpConstants.Vp8LHeaderMagicByte);

            // Write the encoded bytes of the image to the stream.
            this.WriteToStream(stream);
            if (pad == 1)
            {
                stream.WriteByte(0);
            }

            if (exifProfile != null)
            {
                this.WriteMetadataProfile(stream, exifBytes, WebpChunkType.Exif);
            }

            if (xmpProfile != null)
            {
                this.WriteMetadataProfile(stream, xmpBytes, WebpChunkType.Xmp);
            }
        }
Esempio n. 14
0
            private void ProcessProfiles(ImageMetadata imageMetadata, ExifProfile exifProfile, XmpProfile xmpProfile)
            {
                if (exifProfile != null && exifProfile.Parts != ExifParts.None)
                {
                    foreach (IExifValue entry in exifProfile.Values)
                    {
                        if (!this.Collector.Entries.Exists(t => t.Tag == entry.Tag) && entry.GetValue() != null)
                        {
                            ExifParts entryPart = ExifTags.GetPart(entry.Tag);
                            if (entryPart != ExifParts.None && exifProfile.Parts.HasFlag(entryPart))
                            {
                                this.Collector.AddOrReplace(entry.DeepClone());
                            }
                        }
                    }
                }
                else
                {
                    exifProfile.RemoveValue(ExifTag.SubIFDOffset);
                }

                if (imageMetadata.IptcProfile != null)
                {
                    imageMetadata.IptcProfile.UpdateData();
                    var iptc = new ExifByteArray(ExifTagValue.IPTC, ExifDataType.Byte)
                    {
                        Value = imageMetadata.IptcProfile.Data
                    };

                    this.Collector.Add(iptc);
                }
                else
                {
                    exifProfile.RemoveValue(ExifTag.IPTC);
                }

                if (imageMetadata.IccProfile != null)
                {
                    var icc = new ExifByteArray(ExifTagValue.IccProfile, ExifDataType.Undefined)
                    {
                        Value = imageMetadata.IccProfile.ToByteArray()
                    };

                    this.Collector.Add(icc);
                }
                else
                {
                    exifProfile.RemoveValue(ExifTag.IccProfile);
                }

                if (xmpProfile != null)
                {
                    var xmp = new ExifByteArray(ExifTagValue.XMP, ExifDataType.Byte)
                    {
                        Value = xmpProfile.Data
                    };

                    this.Collector.Add(xmp);
                }
                else
                {
                    exifProfile.RemoveValue(ExifTag.XMP);
                }
            }
Esempio n. 15
0
        /// <summary>
        /// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="image">The <see cref="Image{TPixel}"/> to encode from.</param>
        /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
        /// <param name="cancellationToken">The token to request cancellation.</param>
        public void Encode <TPixel>(Image <TPixel> image, Stream stream, CancellationToken cancellationToken)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            ImageMetadata metadata    = image.Metadata;
            GifMetadata   gifMetadata = metadata.GetGifMetadata();

            this.colorTableMode ??= gifMetadata.ColorTableMode;
            bool useGlobalTable = this.colorTableMode == GifColorTableMode.Global;

            // Quantize the image returning a palette.
            IndexedImageFrame <TPixel> quantized;

            using (IQuantizer <TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer <TPixel>(this.configuration))
            {
                if (useGlobalTable)
                {
                    frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image);
                    quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds());
                }
                else
                {
                    quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image.Frames.RootFrame, image.Bounds());
                }
            }

            // Get the number of bits.
            this.bitDepth = ColorNumerics.GetBitsNeededForColorDepth(quantized.Palette.Length);

            // Write the header.
            this.WriteHeader(stream);

            // Write the LSD.
            int index = this.GetTransparentIndex(quantized);

            this.WriteLogicalScreenDescriptor(metadata, image.Width, image.Height, index, useGlobalTable, stream);

            if (useGlobalTable)
            {
                this.WriteColorTable(quantized, stream);
            }

            // Write the comments.
            this.WriteComments(gifMetadata, stream);

            // Write application extensions.
            XmpProfile xmpProfile = image.Metadata.XmpProfile ?? image.Frames.RootFrame.Metadata.XmpProfile;

            this.WriteApplicationExtensions(stream, image.Frames.Count, gifMetadata.RepeatCount, xmpProfile);

            if (useGlobalTable)
            {
                this.EncodeGlobal(image, quantized, index, stream);
            }
            else
            {
                this.EncodeLocal(image, quantized, stream);
            }

            // Clean up.
            quantized.Dispose();

            stream.WriteByte(GifConstants.EndIntroducer);
        }