private static void AddAvifMetadataToDocument(Document doc, AvifReader reader, IByteArrayPool arrayPool) { byte[] exifBytes = reader.GetExifData(); if (exifBytes != null) { ExifValueCollection exifValues = ExifParser.Parse(exifBytes, arrayPool); if (exifValues != null) { exifValues.Remove(MetadataKeys.Image.InterColorProfile); // The HEIF specification states that the EXIF orientation tag is only // informational and should not be used to rotate the image. // See https://github.com/strukturag/libheif/issues/227#issuecomment-642165942 exifValues.Remove(MetadataKeys.Image.Orientation); foreach (MetadataEntry entry in exifValues) { doc.Metadata.AddExifPropertyItem(entry.CreateExifPropertyItem()); } } } CICPColorData?imageColorData = reader.ImageColorData; if (imageColorData.HasValue) { string serializedValue = CICPSerializer.TrySerialize(imageColorData.Value); if (serializedValue != null) { doc.Metadata.SetUserValue(CICPMetadataName, serializedValue); } } ImageGridMetadata imageGridMetadata = reader.ImageGridMetadata; if (imageGridMetadata != null) { string serializedValue = imageGridMetadata.SerializeToString(); if (serializedValue != null) { doc.Metadata.SetUserValue(ImageGridName, serializedValue); } } byte[] iccProfileBytes = reader.GetICCProfile(); if (iccProfileBytes != null) { doc.Metadata.AddExifPropertyItem(ExifSection.Image, unchecked ((ushort)ExifTagID.IccProfileData), new ExifValue(ExifValueType.Undefined, iccProfileBytes)); } byte[] xmpBytes = reader.GetXmpData(); if (xmpBytes != null) { XmpPacket xmpPacket = XmpPacket.TryParse(xmpBytes); if (xmpPacket != null) { doc.Metadata.SetXmpPacket(xmpPacket); } } }
protected override Document OnLoad(Stream input) { byte[] bytes = new byte[input.Length]; input.ProperRead(bytes, 0, (int)input.Length); Surface surface = GetOrientedSurface(bytes, out ExifValueCollection exifMetadata); bool disposeSurface = true; Document doc = null; try { doc = new Document(surface.Width, surface.Height); byte[] colorProfileBytes = WebPFile.GetColorProfileBytes(bytes); if (colorProfileBytes != null) { doc.Metadata.AddExifPropertyItem(ExifSection.Image, unchecked ((ushort)ExifTagID.IccProfileData), new ExifValue(ExifValueType.Undefined, colorProfileBytes)); } if (exifMetadata != null && exifMetadata.Count > 0) { ExifValue xResProperty = exifMetadata.GetAndRemoveValue(ExifPropertyKeys.Image.XResolution.Path); ExifValue yResProperty = exifMetadata.GetAndRemoveValue(ExifPropertyKeys.Image.YResolution.Path); ExifValue resUnitProperty = exifMetadata.GetAndRemoveValue(ExifPropertyKeys.Image.ResolutionUnit.Path); if (xResProperty != null && yResProperty != null && resUnitProperty != null) { if (MetadataHelpers.TryDecodeRational(xResProperty, out double xRes) && MetadataHelpers.TryDecodeRational(yResProperty, out double yRes) && MetadataHelpers.TryDecodeShort(resUnitProperty, out ushort resUnit)) { if (xRes > 0.0 && yRes > 0.0) { switch (resUnit) { case TiffConstants.ResolutionUnit.Centimeter: doc.DpuUnit = MeasurementUnit.Centimeter; doc.DpuX = xRes; doc.DpuY = yRes; break; case TiffConstants.ResolutionUnit.Inch: doc.DpuUnit = MeasurementUnit.Inch; doc.DpuX = xRes; doc.DpuY = yRes; break; } } } } foreach (KeyValuePair <ExifPropertyPath, ExifValue> item in exifMetadata) { ExifPropertyPath path = item.Key; doc.Metadata.AddExifPropertyItem(path.Section, path.TagID, item.Value); } } byte[] xmpBytes = WebPFile.GetXmpBytes(bytes); if (xmpBytes != null) { XmpPacket xmpPacket = XmpPacket.TryParse(xmpBytes); if (xmpPacket != null) { doc.Metadata.SetXmpPacket(xmpPacket); } } doc.Layers.Add(Layer.CreateBackgroundLayer(surface, takeOwnership: true)); disposeSurface = false; } finally { if (disposeSurface) { surface?.Dispose(); } } return(doc); }