diff --git a/SFI.Formats/Images/ImageAnalyzer.cs b/SFI.Formats/Images/ImageAnalyzer.cs index 44a2e616..434f953e 100644 --- a/SFI.Formats/Images/ImageAnalyzer.cs +++ b/SFI.Formats/Images/ImageAnalyzer.cs @@ -1,5 +1,4 @@ -using IS4.SFI.MediaAnalysis.Images; -using IS4.SFI.Services; +using IS4.SFI.Services; using IS4.SFI.Tags; using IS4.SFI.Tools; using IS4.SFI.Vocabulary; @@ -7,7 +6,6 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; -using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -26,7 +24,7 @@ public class ImageAnalyzer : MediaObjectAnalyzer /// [ComponentCollection("image-hash")] [Description("A collection of image-based hash algorithms that produce hashes from the low-detail form of the image.")] - public ICollection> LowFrequencyImageHashAlgorithms { get; } = new List>(); + public ICollection> LowFrequencyImageHashAlgorithms { get; } = new List>(); /// /// A collection of byte-based hash algorithms producing hashes @@ -97,10 +95,10 @@ public async override ValueTask Analyze(IImage image, AnalysisCo node.Set(Properties.Height, image.Height); node.Set(Properties.HorizontalResolution, (decimal)image.HorizontalResolution); node.Set(Properties.VerticalResolution, (decimal)image.VerticalResolution); - int paletteSize = image.Palette.Count; + var paletteSize = image.PaletteSize; int bpp = image.BitDepth; - if(bpp != 0) node.Set(paletteSize > 0 ? Properties.BitDepth : Properties.ColorDepth, bpp); - if(paletteSize > 0) node.Set(Properties.PaletteSize, paletteSize); + if(bpp != 0) node.Set(paletteSize == 0 ? Properties.ColorDepth : Properties.BitDepth, bpp); + if(paletteSize is int size && size > 0) node.Set(Properties.PaletteSize, size); } if(!storedAsData) diff --git a/SFI.Formats/Images/SharpImage.cs b/SFI.Formats/Images/SharpImage.cs index 83f68a9a..065fb160 100644 --- a/SFI.Formats/Images/SharpImage.cs +++ b/SFI.Formats/Images/SharpImage.cs @@ -4,6 +4,7 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; @@ -62,7 +63,25 @@ private double ResolutionUnit{ public override int BitDepth => PixelType.BitsPerPixel; - public override IReadOnlyList Palette => Array.Empty(); // TODO + public override IReadOnlyList Palette => Array.Empty(); + + public override int? PaletteSize{ + get{ + if(Metadata.GetPngMetadata() is { } png) + { + return png.ColorType == PngColorType.Palette ? null : 0; + } + if(Metadata.GetGifMetadata() is { } gif) + { + return gif.GlobalColorTableLength; + } + if(Metadata.GetJpegMetadata() != null) + { + return 0; + } + return null; + } + } [DynamicDependency(nameof(GetImagePixel), typeof(SharpImage))] public override Color GetPixel(int x, int y) diff --git a/SFI/Services/IImage.cs b/SFI/Services/IImage.cs index 9a8718ad..0940a6ac 100644 --- a/SFI/Services/IImage.cs +++ b/SFI/Services/IImage.cs @@ -44,6 +44,11 @@ public interface IImage : IIdentityKey, IDisposable /// IReadOnlyList Palette { get; } + /// + /// The size of the color palette of the image. + /// + int? PaletteSize { get; } + /// /// The format of the image. /// @@ -234,6 +239,9 @@ protected ImageBase(TUnderlying underlyingImage, IFileFormat format /// public abstract IReadOnlyList Palette { get; } + /// + public virtual int? PaletteSize => Palette.Count; + /// public abstract int BitDepth { get; }