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);
        }
Exemple #2
0
        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 <MetadataKey, MetadataEntry> propertyItems = GetMetadataFromDocument(doc);

                if (propertyItems != null)
                {
                    ExifColorSpace exifColorSpace = ExifColorSpace.Srgb;

                    if (iccProfileBytes != null)
                    {
                        exifColorSpace = ExifColorSpace.Uncalibrated;
                    }
                    else
                    {
                        MetadataKey iccProfileKey = MetadataKeys.Image.InterColorProfile;

                        if (propertyItems.TryGetValue(iccProfileKey, out MetadataEntry iccProfileItem))
                        {
                            iccProfileBytes = iccProfileItem.GetData();
                            propertyItems.Remove(iccProfileKey);
                            exifColorSpace = ExifColorSpace.Uncalibrated;
                        }
                    }

                    if (exifBytes == null)
                    {
                        exifBytes = new ExifWriter(doc, propertyItems, exifColorSpace).CreateExifBlob();
                    }
                }
            }

#if !PDN_3_5_X
            if (xmpBytes == null)
            {
                PaintDotNet.Imaging.XmpPacket xmpPacket = doc.Metadata.TryGetXmpPacket();

                if (xmpPacket != null)
                {
                    string xmpPacketAsString = xmpPacket.ToString();

                    xmpBytes = System.Text.Encoding.UTF8.GetBytes(xmpPacketAsString);
                }
            }
#endif

            if (iccProfileBytes != null || exifBytes != null || xmpBytes != null)
            {
                return(new WebPNative.MetadataParams(iccProfileBytes, exifBytes, xmpBytes));
            }

            return(null);
        }