Exemple #1
0
        private ImageInfo GetImageInfo(BaseItem item, ItemImageInfo info, int?imageIndex)
        {
            int? width  = null;
            int? height = null;
            long length = 0;

            try
            {
                if (info.IsLocalFile)
                {
                    var fileInfo = _fileSystem.GetFileInfo(info.Path);
                    length = fileInfo.Length;

                    ImageDimensions size = _imageProcessor.GetImageDimensions(item, info);
                    _libraryManager.UpdateImages(item);
                    width  = size.Width;
                    height = size.Height;

                    if (width <= 0 || height <= 0)
                    {
                        width  = null;
                        height = null;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error getting image information for {Item}", item.Name);
            }

            try
            {
                return(new ImageInfo
                {
                    Path = info.Path,
                    ImageIndex = imageIndex,
                    ImageType = info.Type,
                    ImageTag = _imageProcessor.GetImageCacheTag(item, info),
                    Size = length,
                    Width = width,
                    Height = height
                });
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error getting image information for {Path}", info.Path);

                return(null);
            }
        }
Exemple #2
0
        public double?GetPrimaryImageAspectRatio(BaseItem item)
        {
            var imageInfo = item.GetImageInfo(ImageType.Primary, 0);

            if (imageInfo == null)
            {
                return(null);
            }

            ImageDimensions size;

            var defaultAspectRatio = item.GetDefaultPrimaryImageAspectRatio();

            if (defaultAspectRatio > 0)
            {
                return(defaultAspectRatio);
            }

            if (!imageInfo.IsLocalFile)
            {
                return(null);
            }

            try
            {
                size = _imageProcessor.GetImageDimensions(item, imageInfo);

                if (size.Width <= 0 || size.Height <= 0)
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to determine primary image aspect ratio for {0}", imageInfo.Path);
                return(null);
            }

            var width  = size.Width;
            var height = size.Height;

            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            return(width / height);
        }
Exemple #3
0
        public double?GetPrimaryImageAspectRatio(BaseItem item)
        {
            var imageInfo = item.GetImageInfo(ImageType.Primary, 0);

            if (imageInfo == null)
            {
                return(null);
            }

            var supportedEnhancers = _imageProcessor.GetSupportedEnhancers(item, ImageType.Primary);

            ImageDimensions size;

            var defaultAspectRatio = item.GetDefaultPrimaryImageAspectRatio();

            if (defaultAspectRatio > 0)
            {
                if (supportedEnhancers.Length == 0)
                {
                    return(defaultAspectRatio);
                }

                int dummyWidth  = 200;
                int dummyHeight = Convert.ToInt32(dummyWidth / defaultAspectRatio);
                size = new ImageDimensions(dummyWidth, dummyHeight);
            }
            else
            {
                if (!imageInfo.IsLocalFile)
                {
                    return(null);
                }

                try
                {
                    size = _imageProcessor.GetImageDimensions(item, imageInfo);

                    if (size.Width <= 0 || size.Height <= 0)
                    {
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to determine primary image aspect ratio for {0}", imageInfo.Path);
                    return(null);
                }
            }

            foreach (var enhancer in supportedEnhancers)
            {
                try
                {
                    size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error in image enhancer: {0}", enhancer.GetType().Name);
                }
            }

            var width  = size.Width;
            var height = size.Height;

            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            return(width / height);
        }
Exemple #4
0
        /// <inheritdoc />
        public Task <ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
        {
            item.SetImagePath(ImageType.Primary, item.Path);

            // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
            if (_includeExtensions.Contains(Path.GetExtension(item.Path), StringComparer.OrdinalIgnoreCase))
            {
                try
                {
                    using (var file = TagLib.File.Create(item.Path))
                    {
                        if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
                        {
                            var structure = tag.Structure;
                            if (structure != null &&
                                structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
                            {
                                var exifStructure = exif.Structure;
                                if (exifStructure != null)
                                {
                                    var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
                                    if (entry != null)
                                    {
                                        item.Aperture = (double)entry.Value.Numerator / entry.Value.Denominator;
                                    }

                                    entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
                                    if (entry != null)
                                    {
                                        item.ShutterSpeed = (double)entry.Value.Numerator / entry.Value.Denominator;
                                    }
                                }
                            }
                        }

                        if (file is TagLib.Image.File image)
                        {
                            item.CameraMake  = image.ImageTag.Make;
                            item.CameraModel = image.ImageTag.Model;

                            item.Width  = image.Properties.PhotoWidth;
                            item.Height = image.Properties.PhotoHeight;

                            var rating = image.ImageTag.Rating;
                            item.CommunityRating = rating.HasValue ? rating : null;

                            item.Overview = image.ImageTag.Comment;

                            if (!string.IsNullOrWhiteSpace(image.ImageTag.Title) &&
                                !item.LockedFields.Contains(MetadataFields.Name))
                            {
                                item.Name = image.ImageTag.Title;
                            }

                            var dateTaken = image.ImageTag.DateTime;
                            if (dateTaken.HasValue)
                            {
                                item.DateCreated    = dateTaken.Value;
                                item.PremiereDate   = dateTaken.Value;
                                item.ProductionYear = dateTaken.Value.Year;
                            }

                            item.Genres   = image.ImageTag.Genres;
                            item.Tags     = image.ImageTag.Keywords;
                            item.Software = image.ImageTag.Software;

                            if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
                            {
                                item.Orientation = null;
                            }
                            else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
                            {
                                item.Orientation = orientation;
                            }

                            item.ExposureTime = image.ImageTag.ExposureTime;
                            item.FocalLength  = image.ImageTag.FocalLength;

                            item.Latitude  = image.ImageTag.Latitude;
                            item.Longitude = image.ImageTag.Longitude;
                            item.Altitude  = image.ImageTag.Altitude;

                            if (image.ImageTag.ISOSpeedRatings.HasValue)
                            {
                                item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
                            }
                            else
                            {
                                item.IsoSpeedRating = null;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
                }
            }

            if (item.Width <= 0 || item.Height <= 0)
            {
                var img = item.GetImageInfo(ImageType.Primary, 0);

                try
                {
                    var size = _imageProcessor.GetImageDimensions(item, img, false);

                    if (size.Width > 0 && size.Height > 0)
                    {
                        item.Width  = size.Width;
                        item.Height = size.Height;
                    }
                }
                catch (ArgumentException)
                {
                    // format not supported
                }
            }

            const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;

            return(Task.FromResult(Result));
        }