Skip to content

Commit

Permalink
specific PaletteSize property since ImageSharp does not provide the p…
Browse files Browse the repository at this point in the history
…alette entries (SixLabors/ImageSharp#2404)
  • Loading branch information
IS4Code committed Jan 17, 2024
1 parent cf270cd commit 33ae687
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
12 changes: 5 additions & 7 deletions SFI.Formats/Images/ImageAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -26,7 +24,7 @@ public class ImageAnalyzer : MediaObjectAnalyzer<IImage>
/// </summary>
[ComponentCollection("image-hash")]
[Description("A collection of image-based hash algorithms that produce hashes from the low-detail form of the image.")]
public ICollection<IObjectHashAlgorithm<IImage>> LowFrequencyImageHashAlgorithms { get; } = new List<IObjectHashAlgorithm<Image>>();
public ICollection<IObjectHashAlgorithm<IImage>> LowFrequencyImageHashAlgorithms { get; } = new List<IObjectHashAlgorithm<IImage>>();

/// <summary>
/// A collection of byte-based hash algorithms producing hashes
Expand Down Expand Up @@ -97,10 +95,10 @@ public async override ValueTask<AnalysisResult> 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)
Expand Down
21 changes: 20 additions & 1 deletion SFI.Formats/Images/SharpImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +63,25 @@ public SharpImage(IImage underlyingImage, IFileFormat<IImage> format) : base(und

public override int BitDepth => PixelType.BitsPerPixel;

public override IReadOnlyList<Color> Palette => Array.Empty<Color>(); // TODO
public override IReadOnlyList<Color> Palette => Array.Empty<Color>();

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)
Expand Down
8 changes: 8 additions & 0 deletions SFI/Services/IImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public interface IImage : IIdentityKey, IDisposable
/// </summary>
IReadOnlyList<Color> Palette { get; }

/// <summary>
/// The size of the color palette of the image.
/// </summary>
int? PaletteSize { get; }

/// <summary>
/// The format of the image.
/// </summary>
Expand Down Expand Up @@ -234,6 +239,9 @@ protected ImageBase(TUnderlying underlyingImage, IFileFormat<TUnderlying> format
/// <inheritdoc/>
public abstract IReadOnlyList<Color> Palette { get; }

/// <inheritdoc/>
public virtual int? PaletteSize => Palette.Count;

/// <inheritdoc/>
public abstract int BitDepth { get; }

Expand Down

0 comments on commit 33ae687

Please sign in to comment.