private static WebPNative.MetadataParams CreateWebPMetadata(Document doc) { byte[] iccProfileBytes = null; byte[] exifBytes = null; byte[] xmpBytes = null; string colorProfile = doc.Metadata.GetUserValue(WebPMetadataNames.ColorProfile); if (!string.IsNullOrEmpty(colorProfile)) { iccProfileBytes = Convert.FromBase64String(colorProfile); } string exif = doc.Metadata.GetUserValue(WebPMetadataNames.EXIF); if (!string.IsNullOrEmpty(exif)) { exifBytes = Convert.FromBase64String(exif); } string xmp = doc.Metadata.GetUserValue(WebPMetadataNames.XMP); if (!string.IsNullOrEmpty(xmp)) { xmpBytes = Convert.FromBase64String(xmp); } if (iccProfileBytes == null || exifBytes == null) { Dictionary <ExifPropertyPath, ExifValue> propertyItems = GetMetadataFromDocument(doc); if (propertyItems != null) { ExifColorSpace exifColorSpace = ExifColorSpace.Srgb; if (propertyItems.TryGetValue(ExifPropertyKeys.Photo.ColorSpace.Path, out ExifValue value)) { propertyItems.Remove(ExifPropertyKeys.Photo.ColorSpace.Path); if (MetadataHelpers.TryDecodeShort(value, out ushort colorSpace)) { exifColorSpace = (ExifColorSpace)colorSpace; } } if (iccProfileBytes != null) { exifColorSpace = ExifColorSpace.Uncalibrated; } else { ExifPropertyPath iccProfileKey = ExifPropertyKeys.Image.InterColorProfile.Path; if (propertyItems.TryGetValue(iccProfileKey, out ExifValue iccProfileItem)) { iccProfileBytes = iccProfileItem.Data.ToArrayEx(); propertyItems.Remove(iccProfileKey); exifColorSpace = ExifColorSpace.Uncalibrated; } } if (iccProfileBytes != null) { // Remove the InteroperabilityIndex and related tags, these tags should // not be written if the image has an ICC color profile. propertyItems.Remove(ExifPropertyKeys.Interop.InteroperabilityIndex.Path); propertyItems.Remove(ExifPropertyKeys.Interop.InteroperabilityVersion.Path); } if (exifBytes == null) { exifBytes = new ExifWriter(doc, propertyItems, exifColorSpace).CreateExifBlob(); } } } if (xmpBytes == null) { XmpPacket xmpPacket = doc.Metadata.TryGetXmpPacket(); if (xmpPacket != null) { string xmpPacketAsString = xmpPacket.ToString(); xmpBytes = System.Text.Encoding.UTF8.GetBytes(xmpPacketAsString); } } if (iccProfileBytes != null || exifBytes != null || xmpBytes != null) { return(new WebPNative.MetadataParams(iccProfileBytes, exifBytes, xmpBytes)); } return(null); }
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); }