Ejemplo n.º 1
0
        protected override void OnSaveT(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)
        {
            int        quality = token.GetProperty <Int32Property>(PropertyNames.Quality).Value;
            WebPPreset preset  = (WebPPreset)token.GetProperty(PropertyNames.Preset).Value;

            WebPFile.Save(input, output, quality, preset, scratchSurface, progressCallback);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        private static Surface GetOrientedSurface(byte[] bytes, out ExifValueCollection exifMetadata)
        {
            exifMetadata = null;

            Surface surface = WebPFile.Load(bytes);

            byte[] exifBytes = WebPFile.GetExifBytes(bytes);
            if (exifBytes != null)
            {
                exifMetadata = ExifParser.Parse(exifBytes);

                if (exifMetadata != null)
                {
                    ExifValue orientationProperty = exifMetadata.GetAndRemoveValue(ExifPropertyKeys.Image.Orientation.Path);
                    if (orientationProperty != null)
                    {
                        MetadataHelpers.ApplyOrientationTransform(orientationProperty, ref surface);
                    }
                }
            }

            return(surface);
        }
Ejemplo n.º 4
0
        private static Document GetOrientedDocument(byte[] bytes, out ExifValueCollection exifMetadata)
        {
            exifMetadata = null;

            Document doc = null;

            // Load the image into a Bitmap so the EXIF orientation transform can be applied.

            using (Bitmap image = WebPFile.Load(bytes))
            {
                byte[] exifBytes = WebPFile.GetExifBytes(bytes);
                if (exifBytes != null)
                {
                    exifMetadata = ExifParser.Parse(exifBytes);

                    if (exifMetadata.Count > 0)
                    {
                        MetadataEntry orientationProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.Orientation);
                        if (orientationProperty != null)
                        {
                            RotateFlipType transform = MetadataHelpers.GetOrientationTransform(orientationProperty);
                            if (transform != RotateFlipType.RotateNoneFlipNone)
                            {
                                image.RotateFlip(transform);
                            }
                        }

                        MetadataEntry xResProperty    = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.XResolution);
                        MetadataEntry yResProperty    = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.YResolution);
                        MetadataEntry resUnitProperty = exifMetadata.GetAndRemoveValue(MetadataKeys.Image.ResolutionUnit);
                        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)
                                {
                                    double dpiX, dpiY;

                                    switch ((MeasurementUnit)resUnit)
                                    {
                                    case MeasurementUnit.Centimeter:
                                        dpiX = Document.DotsPerCmToDotsPerInch(xRes);
                                        dpiY = Document.DotsPerCmToDotsPerInch(yRes);
                                        break;

                                    case MeasurementUnit.Inch:
                                        dpiX = xRes;
                                        dpiY = yRes;
                                        break;

                                    default:
                                        // Unknown ResolutionUnit value.
                                        dpiX = 0.0;
                                        dpiY = 0.0;
                                        break;
                                    }

                                    if (dpiX > 0.0 && dpiY > 0.0)
                                    {
                                        try
                                        {
                                            image.SetResolution((float)dpiX, (float)dpiY);
                                        }
                                        catch
                                        {
                                            // Ignore any errors when setting the resolution.
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                doc = Document.FromGdipImage(image);
            }

            return(doc);
        }
Ejemplo n.º 5
0
        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);
        }