protected override Document OnLoad(Stream input) { byte[] bytes = new byte[input.Length]; input.ProperRead(bytes, 0, (int)input.Length); Document doc = GetOrientedDocument(bytes, out ExifValueCollection exifMetadata); byte[] colorProfileBytes = WebPFile.GetColorProfileBytes(bytes); if (colorProfileBytes != null) { #if PDN_3_5_X System.Drawing.Imaging.PropertyItem colorProfileItem = PaintDotNet.SystemLayer.PdnGraphics.CreatePropertyItem(); colorProfileItem.Id = unchecked ((ushort)ExifTagID.IccProfileData); colorProfileItem.Type = (short)ExifTagType.Undefined; colorProfileItem.Len = colorProfileBytes.Length; colorProfileItem.Value = colorProfileBytes.CloneT(); doc.Metadata.AddExifValues(new System.Drawing.Imaging.PropertyItem[] { colorProfileItem }); #else doc.Metadata.AddExifPropertyItem(PaintDotNet.Imaging.ExifSection.Image, unchecked ((ushort)ExifTagID.IccProfileData), new PaintDotNet.Imaging.ExifValue(PaintDotNet.Imaging.ExifValueType.Undefined, colorProfileBytes.CloneT())); #endif } if (exifMetadata != null) { foreach (MetadataEntry entry in exifMetadata.Distinct()) { #if PDN_3_5_X System.Drawing.Imaging.PropertyItem propertyItem = entry.TryCreateGdipPropertyItem(); if (propertyItem != null) { doc.Metadata.AddExifValues(new System.Drawing.Imaging.PropertyItem[] { propertyItem }); } #else doc.Metadata.AddExifPropertyItem(entry.CreateExifPropertyItem()); #endif } } byte[] xmpBytes = WebPFile.GetXmpBytes(bytes); if (xmpBytes != null) { #if PDN_3_5_X doc.Metadata.SetUserValue(WebPMetadataNames.XMP, Convert.ToBase64String(xmpBytes, Base64FormattingOptions.None)); #else PaintDotNet.Imaging.XmpPacket xmpPacket = PaintDotNet.Imaging.XmpPacket.TryParse(xmpBytes); if (xmpPacket != null) { doc.Metadata.SetXmpPacket(xmpPacket); } #endif } return(doc); }
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); }