protected override MediaTransformResult TransformStream(Stream stream, out Stream transformedStream)
        {
            var bitmap   = BitmapFrame.Create(stream);
            var exifData = _exifParser.Parse(bitmap);

            transformedStream = new MemoryStream();
            Serialize(transformedStream, exifData);
            transformedStream.Seek(0, SeekOrigin.Begin);
            return(MediaTransformResult.Success);
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #4
0
        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);
                }
            }
        }