Skip to content

Commit d2358a5

Browse files
committed
specific ImageSize property since ImageSharp does not provide the palette entries (SixLabors/ImageSharp#2404)
1 parent cf270cd commit d2358a5

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

SFI.Formats/Images/ImageAnalyzer.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using IS4.SFI.MediaAnalysis.Images;
2-
using IS4.SFI.Services;
1+
using IS4.SFI.Services;
32
using IS4.SFI.Tags;
43
using IS4.SFI.Tools;
54
using IS4.SFI.Vocabulary;
65
using System;
76
using System.Collections.Generic;
87
using System.ComponentModel;
98
using System.Drawing;
10-
using System.Drawing.Imaging;
119
using System.IO;
1210
using System.Linq;
1311
using System.Threading.Tasks;
@@ -26,7 +24,7 @@ public class ImageAnalyzer : MediaObjectAnalyzer<IImage>
2624
/// </summary>
2725
[ComponentCollection("image-hash")]
2826
[Description("A collection of image-based hash algorithms that produce hashes from the low-detail form of the image.")]
29-
public ICollection<IObjectHashAlgorithm<IImage>> LowFrequencyImageHashAlgorithms { get; } = new List<IObjectHashAlgorithm<Image>>();
27+
public ICollection<IObjectHashAlgorithm<IImage>> LowFrequencyImageHashAlgorithms { get; } = new List<IObjectHashAlgorithm<IImage>>();
3028

3129
/// <summary>
3230
/// A collection of byte-based hash algorithms producing hashes
@@ -97,10 +95,10 @@ public async override ValueTask<AnalysisResult> Analyze(IImage image, AnalysisCo
9795
node.Set(Properties.Height, image.Height);
9896
node.Set(Properties.HorizontalResolution, (decimal)image.HorizontalResolution);
9997
node.Set(Properties.VerticalResolution, (decimal)image.VerticalResolution);
100-
int paletteSize = image.Palette.Count;
98+
var paletteSize = image.PaletteSize;
10199
int bpp = image.BitDepth;
102-
if(bpp != 0) node.Set(paletteSize > 0 ? Properties.BitDepth : Properties.ColorDepth, bpp);
103-
if(paletteSize > 0) node.Set(Properties.PaletteSize, paletteSize);
100+
if(bpp != 0) node.Set(paletteSize == 0 ? Properties.ColorDepth : Properties.BitDepth, bpp);
101+
if(paletteSize is int size && size > 0) node.Set(Properties.PaletteSize, size);
104102
}
105103

106104
if(!storedAsData)

SFI.Formats/Images/SharpImage.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SixLabors.ImageSharp;
55
using SixLabors.ImageSharp.Advanced;
66
using SixLabors.ImageSharp.Formats;
7+
using SixLabors.ImageSharp.Formats.Png;
78
using SixLabors.ImageSharp.Metadata;
89
using SixLabors.ImageSharp.PixelFormats;
910
using SixLabors.ImageSharp.Processing;
@@ -62,7 +63,25 @@ private double ResolutionUnit{
6263

6364
public override int BitDepth => PixelType.BitsPerPixel;
6465

65-
public override IReadOnlyList<Color> Palette => Array.Empty<Color>(); // TODO
66+
public override IReadOnlyList<Color> Palette => Array.Empty<Color>();
67+
68+
public override int? PaletteSize{
69+
get{
70+
if(Metadata.GetPngMetadata() is { } png)
71+
{
72+
return png.ColorType == PngColorType.Palette ? null : 0;
73+
}
74+
if(Metadata.GetGifMetadata() is { } gif)
75+
{
76+
return gif.GlobalColorTableLength;
77+
}
78+
if(Metadata.GetJpegMetadata() != null)
79+
{
80+
return 0;
81+
}
82+
return null;
83+
}
84+
}
6685

6786
[DynamicDependency(nameof(GetImagePixel), typeof(SharpImage))]
6887
public override Color GetPixel(int x, int y)

SFI/Services/IImage.cs

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public interface IImage : IIdentityKey, IDisposable
4444
/// </summary>
4545
IReadOnlyList<Color> Palette { get; }
4646

47+
/// <summary>
48+
/// The size of the color palette of the image.
49+
/// </summary>
50+
int? PaletteSize { get; }
51+
4752
/// <summary>
4853
/// The format of the image.
4954
/// </summary>
@@ -234,6 +239,9 @@ protected ImageBase(TUnderlying underlyingImage, IFileFormat<TUnderlying> format
234239
/// <inheritdoc/>
235240
public abstract IReadOnlyList<Color> Palette { get; }
236241

242+
/// <inheritdoc/>
243+
public virtual int? PaletteSize => Palette.Count;
244+
237245
/// <inheritdoc/>
238246
public abstract int BitDepth { get; }
239247

0 commit comments

Comments
 (0)