public static Document Load(Stream input, IByteArrayPool arrayPool) { Document doc = null; using (AvifReader reader = new AvifReader(input, leaveOpen: true, arrayPool)) { Surface surface = null; bool disposeSurface = true; try { surface = reader.Decode(); doc = new Document(surface.Width, surface.Height); AddAvifMetadataToDocument(doc, reader, arrayPool); doc.Layers.Add(Layer.CreateBackgroundLayer(surface, takeOwnership: true)); disposeSurface = false; } finally { if (disposeSurface) { surface?.Dispose(); } } } return(doc); }
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); } } }