Skip to content

Commit 05d33f2

Browse files
committed
Remove IconFrameMetadata
Signed-off-by: 舰队的偶像-岛风酱! <frg2089@outlook.com>
1 parent 43ea25f commit 05d33f2

6 files changed

Lines changed: 140 additions & 148 deletions

File tree

src/ImageSharp/Formats/Icon/Cur/CurDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ public CurDecoderCore(DecoderOptions options)
1414
{
1515
}
1616

17-
protected override IconFrameMetadata GetFrameMetadata(ImageFrameMetadata metadata) => metadata.GetCurMetadata();
17+
protected override void SetFrameMetadata(ImageFrameMetadata metadata, in IconDirEntry entry) => metadata.GetCurMetadata().FromIconDirEntry(entry);
1818
}

src/ImageSharp/Formats/Icon/Cur/CurFrameMetadata.cs

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
namespace SixLabors.ImageSharp.Formats.Icon.Cur;
55

66
/// <summary>
7-
/// IcoFrameMetadata. TODO: Remove base class and merge into this class.
7+
/// IcoFrameMetadata.
88
/// </summary>
9-
public class CurFrameMetadata : IconFrameMetadata, IDeepCloneable<CurFrameMetadata>, IDeepCloneable
9+
public class CurFrameMetadata : IDeepCloneable<CurFrameMetadata>, IDeepCloneable
1010
{
1111
/// <summary>
1212
/// Initializes a new instance of the <see cref="CurFrameMetadata"/> class.
@@ -15,38 +15,92 @@ public CurFrameMetadata()
1515
{
1616
}
1717

18-
/// <summary>
19-
/// Initializes a new instance of the <see cref="CurFrameMetadata"/> class.
20-
/// </summary>
21-
/// <param name="metadata">metadata</param>
22-
public CurFrameMetadata(IconFrameMetadata metadata)
23-
: base(metadata)
24-
{
25-
}
26-
2718
/// <summary>
2819
/// Initializes a new instance of the <see cref="CurFrameMetadata"/> class.
2920
/// </summary>
3021
/// <param name="width">width</param>
3122
/// <param name="height">height</param>
3223
/// <param name="colorCount">colorCount</param>
33-
/// <param name="field1">field1</param>
34-
/// <param name="field2">field2</param>
35-
public CurFrameMetadata(byte width, byte height, byte colorCount, ushort field1, ushort field2)
36-
: base(width, height, colorCount, field1, field2)
24+
/// <param name="hotspotX">hotspotX</param>
25+
/// <param name="hotspotY">hotspotY</param>
26+
public CurFrameMetadata(byte width, byte height, byte colorCount, ushort hotspotX, ushort hotspotY)
3727
{
28+
this.EncodingWidth = width;
29+
this.EncodingHeight = height;
30+
this.ColorCount = colorCount;
31+
this.HotspotX = hotspotX;
32+
this.HotspotY = hotspotY;
3833
}
3934

35+
/// <inheritdoc cref="CurFrameMetadata()"/>
36+
public CurFrameMetadata(CurFrameMetadata metadata)
37+
{
38+
this.EncodingWidth = metadata.EncodingWidth;
39+
this.EncodingHeight = metadata.EncodingHeight;
40+
this.ColorCount = metadata.ColorCount;
41+
this.HotspotX = metadata.HotspotX;
42+
this.HotspotY = metadata.HotspotY;
43+
this.Compression = metadata.Compression;
44+
}
45+
46+
/// <summary>
47+
/// Gets or sets icoFrameCompression.
48+
/// </summary>
49+
public IconFrameCompression Compression { get; set; }
50+
51+
/// <summary>
52+
/// Gets or sets ColorCount field. <br />
53+
/// Specifies number of colors in the color palette. Should be 0 if the image does not use a color palette.
54+
/// </summary>
55+
// TODO: BmpMetadata does not supported palette yet.
56+
public byte ColorCount { get; set; }
57+
4058
/// <summary>
4159
/// Gets or sets Specifies the horizontal coordinates of the hotspot in number of pixels from the left.
4260
/// </summary>
43-
public ushort HotspotX { get => this.Field1; set => this.Field1 = value; }
61+
public ushort HotspotX { get; set; }
4462

4563
/// <summary>
4664
/// Gets or sets Specifies the vertical coordinates of the hotspot in number of pixels from the top.
4765
/// </summary>
48-
public ushort HotspotY { get => this.Field2; set => this.Field2 = value; }
66+
public ushort HotspotY { get; set; }
67+
68+
/// <summary>
69+
/// Gets or sets Height field. <br />
70+
/// Specifies image height in pixels. Can be any number between 0 and 255. Value 0 means image height is 256 pixels.
71+
/// </summary>
72+
public byte EncodingHeight { get; set; }
73+
74+
/// <summary>
75+
/// Gets or sets Width field. <br />
76+
/// Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels.
77+
/// </summary>
78+
public byte EncodingWidth { get; set; }
79+
80+
/// <inheritdoc cref="Bmp.BmpMetadata.BitsPerPixel" />
81+
public Bmp.BmpBitsPerPixel BitsPerPixel { get; set; } = Bmp.BmpBitsPerPixel.Pixel24;
4982

5083
/// <inheritdoc/>
51-
public override CurFrameMetadata DeepClone() => new(this);
84+
public CurFrameMetadata DeepClone() => new(this);
85+
86+
/// <inheritdoc/>
87+
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
88+
89+
internal void FromIconDirEntry(in IconDirEntry entry)
90+
{
91+
this.EncodingWidth = entry.Width;
92+
this.EncodingHeight = entry.Height;
93+
this.ColorCount = entry.ColorCount;
94+
this.HotspotX = entry.Planes;
95+
this.HotspotY = entry.BitCount;
96+
}
97+
98+
internal IconDirEntry ToIconDirEntry() => new()
99+
{
100+
Width = this.EncodingWidth,
101+
Height = this.EncodingHeight,
102+
ColorCount = this.ColorCount,
103+
Planes = this.HotspotX,
104+
BitCount = this.HotspotY,
105+
};
52106
}

src/ImageSharp/Formats/Icon/Ico/IcoDecoderCore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public IcoDecoderCore(DecoderOptions options)
1414
{
1515
}
1616

17-
protected override IconFrameMetadata GetFrameMetadata(ImageFrameMetadata metadata) => metadata.GetIcoMetadata();
17+
protected override void SetFrameMetadata(ImageFrameMetadata metadata, in IconDirEntry entry)
18+
=> metadata.GetIcoMetadata().FromIconDirEntry(entry);
1819
}

src/ImageSharp/Formats/Icon/Ico/IcoFrameMetadata.cs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.Icon.Ico;
66
/// <summary>
77
/// IcoFrameMetadata. TODO: Remove base class and merge into this class.
88
/// </summary>
9-
public class IcoFrameMetadata : IconFrameMetadata, IDeepCloneable<IcoFrameMetadata>, IDeepCloneable
9+
public class IcoFrameMetadata : IDeepCloneable<IcoFrameMetadata>, IDeepCloneable
1010
{
1111
/// <summary>
1212
/// Initializes a new instance of the <see cref="IcoFrameMetadata"/> class.
@@ -15,34 +15,78 @@ public IcoFrameMetadata()
1515
{
1616
}
1717

18-
/// <summary>
19-
/// Initializes a new instance of the <see cref="IcoFrameMetadata"/> class.
20-
/// </summary>
21-
/// <param name="metadata">metadata</param>
22-
public IcoFrameMetadata(IconFrameMetadata metadata)
23-
: base(metadata)
24-
{
25-
}
26-
2718
/// <summary>
2819
/// Initializes a new instance of the <see cref="IcoFrameMetadata"/> class.
2920
/// </summary>
3021
/// <param name="width">width</param>
3122
/// <param name="height">height</param>
3223
/// <param name="colorCount">colorCount</param>
33-
/// <param name="field1">field1</param>
34-
/// <param name="field2">field2</param>
35-
public IcoFrameMetadata(byte width, byte height, byte colorCount, ushort field1, ushort field2)
36-
: base(width, height, colorCount, field1, field2)
24+
public IcoFrameMetadata(byte width, byte height, byte colorCount)
3725
{
26+
this.EncodingWidth = width;
27+
this.EncodingHeight = height;
28+
this.ColorCount = colorCount;
3829
}
3930

31+
/// <inheritdoc cref="IcoFrameMetadata()"/>
32+
public IcoFrameMetadata(IcoFrameMetadata metadata)
33+
{
34+
this.EncodingWidth = metadata.EncodingWidth;
35+
this.EncodingHeight = metadata.EncodingHeight;
36+
this.ColorCount = metadata.ColorCount;
37+
this.Compression = metadata.Compression;
38+
}
39+
40+
/// <summary>
41+
/// Gets or sets icoFrameCompression.
42+
/// </summary>
43+
public IconFrameCompression Compression { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets ColorCount field. <br />
47+
/// Specifies number of colors in the color palette. Should be 0 if the image does not use a color palette.
48+
/// </summary>
49+
// TODO: BmpMetadata does not supported palette yet.
50+
public byte ColorCount { get; set; }
51+
4052
/// <summary>
41-
/// Gets or sets the bits per pixel.
42-
/// TODO: This needs to be constrained and calculated using the metadata returned from the png/bmp.
53+
/// Gets or sets Height field. <br />
54+
/// Specifies image height in pixels. Can be any number between 0 and 255. Value 0 means image height is 256 pixels.
4355
/// </summary>
44-
public ushort BitCount { get => this.Field2; set => this.Field2 = value; }
56+
public byte EncodingHeight { get; set; }
57+
58+
/// <summary>
59+
/// Gets or sets Width field. <br />
60+
/// Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels.
61+
/// </summary>
62+
public byte EncodingWidth { get; set; }
63+
64+
/// <inheritdoc cref="Bmp.BmpMetadata.BitsPerPixel" />
65+
public Bmp.BmpBitsPerPixel BitsPerPixel { get; set; } = Bmp.BmpBitsPerPixel.Pixel24;
4566

4667
/// <inheritdoc/>
47-
public override IcoFrameMetadata DeepClone() => new(this);
68+
public IcoFrameMetadata DeepClone() => new(this);
69+
70+
/// <inheritdoc/>
71+
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
72+
73+
internal void FromIconDirEntry(in IconDirEntry entry)
74+
{
75+
this.EncodingWidth = entry.Width;
76+
this.EncodingHeight = entry.Height;
77+
this.ColorCount = entry.ColorCount;
78+
}
79+
80+
internal IconDirEntry ToIconDirEntry() => new()
81+
{
82+
Width = this.EncodingWidth,
83+
Height = this.EncodingHeight,
84+
ColorCount = this.ColorCount,
85+
Planes = 1,
86+
BitCount = this.Compression switch
87+
{
88+
IconFrameCompression.Bmp => (ushort)this.BitsPerPixel,
89+
_ => 0,
90+
},
91+
};
4892
}

src/ImageSharp/Formats/Icon/IconDecoderCore.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
9393
bmpMetadata = x.Image.Metadata.GetBmpMetadata();
9494
}
9595

96-
// TODO: The inheriting decoder should be responsible for setting the actual data (FromIconDirEntry)
97-
// so we can avoid the protected Field1 and Field2 properties and use strong typing.
98-
this.GetFrameMetadata(target.Metadata).FromIconDirEntry(this.Entries[x.Index]);
96+
this.SetFrameMetadata(target.Metadata, this.Entries[x.Index]);
9997

10098
x.Image.Dispose();
10199

@@ -127,15 +125,14 @@ public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellat
127125
// TODO: Use the Identify methods in each decoder to return the
128126
// format specific metadata for the image and frame.
129127
frames[i] = new();
130-
IconFrameMetadata icoFrameMetadata = this.GetFrameMetadata(frames[i]);
131-
icoFrameMetadata.FromIconDirEntry(this.Entries[i]);
128+
this.SetFrameMetadata(frames[i], this.Entries[i]);
132129
}
133130

134131
// TODO: Use real values from the metadata.
135132
return new(new(32), new(0), metadata, frames);
136133
}
137134

138-
protected abstract IconFrameMetadata GetFrameMetadata(ImageFrameMetadata metadata);
135+
protected abstract void SetFrameMetadata(ImageFrameMetadata metadata, in IconDirEntry entry);
139136

140137
protected void ReadHeader(Stream stream)
141138
{

src/ImageSharp/Formats/Icon/IconFrameMetadata.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)