Skip to content

Commit aada974

Browse files
Refactor and cleanup
1 parent 5a711a8 commit aada974

File tree

7 files changed

+309
-347
lines changed

7 files changed

+309
-347
lines changed

src/ImageSharp/Formats/Png/Chunks/FrameControl.cs

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ internal readonly struct FrameControl
1010
public const int Size = 26;
1111

1212
public FrameControl(
13-
int sequenceNumber,
14-
int width,
15-
int height,
16-
int xOffset,
17-
int yOffset,
18-
short delayNumber,
19-
short delayDenominator,
20-
PngDisposeOperation disposeOperation,
21-
PngBlendOperation blendOperation)
13+
uint sequenceNumber,
14+
uint width,
15+
uint height,
16+
uint xOffset,
17+
uint yOffset,
18+
ushort delayNumerator,
19+
ushort delayDenominator,
20+
PngDisposalMethod disposeOperation,
21+
PngBlendMethod blendOperation)
2222
{
2323
this.SequenceNumber = sequenceNumber;
2424
this.Width = width;
2525
this.Height = height;
2626
this.XOffset = xOffset;
2727
this.YOffset = yOffset;
28-
this.DelayNumber = delayNumber;
28+
this.DelayNumerator = delayNumerator;
2929
this.DelayDenominator = delayDenominator;
3030
this.DisposeOperation = disposeOperation;
3131
this.BlendOperation = blendOperation;
@@ -34,130 +34,101 @@ public FrameControl(
3434
/// <summary>
3535
/// Gets the sequence number of the animation chunk, starting from 0
3636
/// </summary>
37-
public int SequenceNumber { get; }
37+
public uint SequenceNumber { get; }
3838

3939
/// <summary>
4040
/// Gets the width of the following frame
4141
/// </summary>
42-
public int Width { get; }
42+
public uint Width { get; }
4343

4444
/// <summary>
4545
/// Gets the height of the following frame
4646
/// </summary>
47-
public int Height { get; }
47+
public uint Height { get; }
4848

4949
/// <summary>
5050
/// Gets the X position at which to render the following frame
5151
/// </summary>
52-
public int XOffset { get; }
52+
public uint XOffset { get; }
5353

5454
/// <summary>
5555
/// Gets the Y position at which to render the following frame
5656
/// </summary>
57-
public int YOffset { get; }
57+
public uint YOffset { get; }
5858

5959
/// <summary>
6060
/// Gets the X limit at which to render the following frame
6161
/// </summary>
62-
public uint XLimit => (uint)(this.XOffset + this.Width);
62+
public uint XMax => this.XOffset + this.Width;
6363

6464
/// <summary>
6565
/// Gets the Y limit at which to render the following frame
6666
/// </summary>
67-
public uint YLimit => (uint)(this.YOffset + this.Height);
67+
public uint YMax => this.YOffset + this.Height;
6868

6969
/// <summary>
7070
/// Gets the frame delay fraction numerator
7171
/// </summary>
72-
public short DelayNumber { get; }
72+
public ushort DelayNumerator { get; }
7373

7474
/// <summary>
7575
/// Gets the frame delay fraction denominator
7676
/// </summary>
77-
public short DelayDenominator { get; }
77+
public ushort DelayDenominator { get; }
7878

7979
/// <summary>
8080
/// Gets the type of frame area disposal to be done after rendering this frame
8181
/// </summary>
82-
public PngDisposeOperation DisposeOperation { get; }
82+
public PngDisposalMethod DisposeOperation { get; }
8383

8484
/// <summary>
8585
/// Gets the type of frame area rendering for this frame
8686
/// </summary>
87-
public PngBlendOperation BlendOperation { get; }
87+
public PngBlendMethod BlendOperation { get; }
8888

8989
/// <summary>
9090
/// Validates the APng fcTL.
9191
/// </summary>
92+
/// <param name="header">The header.</param>
9293
/// <exception cref="NotSupportedException">
9394
/// Thrown if the image does pass validation.
9495
/// </exception>
95-
public void Validate(PngHeader hdr)
96+
public void Validate(PngHeader header)
9697
{
97-
if (this.XOffset < 0)
98-
{
99-
PngThrowHelper.ThrowInvalidParameter(this.XOffset, "Expected >= 0");
100-
}
101-
102-
if (this.YOffset < 0)
103-
{
104-
PngThrowHelper.ThrowInvalidParameter(this.YOffset, "Expected >= 0");
105-
}
106-
107-
if (this.Width <= 0)
98+
if (this.Width == 0)
10899
{
109100
PngThrowHelper.ThrowInvalidParameter(this.Width, "Expected > 0");
110101
}
111102

112-
if (this.Height <= 0)
103+
if (this.Height == 0)
113104
{
114105
PngThrowHelper.ThrowInvalidParameter(this.Height, "Expected > 0");
115106
}
116107

117-
if (this.XLimit > hdr.Width)
108+
if (this.XMax > header.Width)
118109
{
119-
PngThrowHelper.ThrowInvalidParameter(this.XOffset, this.Width, $"The sum of them > {nameof(PngHeader)}.{nameof(PngHeader.Width)}");
110+
PngThrowHelper.ThrowInvalidParameter(this.XOffset, this.Width, $"The x-offset plus width > {nameof(PngHeader)}.{nameof(PngHeader.Width)}");
120111
}
121112

122-
if (this.YLimit > hdr.Height)
113+
if (this.YMax > header.Height)
123114
{
124-
PngThrowHelper.ThrowInvalidParameter(this.YOffset, this.Height, $"The sum of them > {nameof(PngHeader)}.{nameof(PngHeader.Height)}");
115+
PngThrowHelper.ThrowInvalidParameter(this.YOffset, this.Height, $"The y-offset plus height > {nameof(PngHeader)}.{nameof(PngHeader.Height)}");
125116
}
126117
}
127118

128-
/// <summary>
129-
/// Parses the APngFrameControl from the given metadata.
130-
/// </summary>
131-
/// <param name="frameMetadata">The metadata to parse.</param>
132-
/// <param name="sequenceNumber">Sequence number.</param>
133-
public static FrameControl FromMetadata(PngFrameMetadata frameMetadata, int sequenceNumber)
134-
{
135-
FrameControl fcTL = new(
136-
sequenceNumber,
137-
frameMetadata.Width,
138-
frameMetadata.Height,
139-
frameMetadata.XOffset,
140-
frameMetadata.YOffset,
141-
frameMetadata.DelayNumber,
142-
frameMetadata.DelayDenominator,
143-
frameMetadata.DisposeOperation,
144-
frameMetadata.BlendOperation);
145-
return fcTL;
146-
}
147-
148119
/// <summary>
149120
/// Writes the fcTL to the given buffer.
150121
/// </summary>
151122
/// <param name="buffer">The buffer to write to.</param>
152123
public void WriteTo(Span<byte> buffer)
153124
{
154-
BinaryPrimitives.WriteInt32BigEndian(buffer[..4], this.SequenceNumber);
155-
BinaryPrimitives.WriteInt32BigEndian(buffer[4..8], this.Width);
156-
BinaryPrimitives.WriteInt32BigEndian(buffer[8..12], this.Height);
157-
BinaryPrimitives.WriteInt32BigEndian(buffer[12..16], this.XOffset);
158-
BinaryPrimitives.WriteInt32BigEndian(buffer[16..20], this.YOffset);
159-
BinaryPrimitives.WriteInt16BigEndian(buffer[20..22], this.DelayNumber);
160-
BinaryPrimitives.WriteInt16BigEndian(buffer[22..24], this.DelayDenominator);
125+
BinaryPrimitives.WriteUInt32BigEndian(buffer[..4], this.SequenceNumber);
126+
BinaryPrimitives.WriteUInt32BigEndian(buffer[4..8], this.Width);
127+
BinaryPrimitives.WriteUInt32BigEndian(buffer[8..12], this.Height);
128+
BinaryPrimitives.WriteUInt32BigEndian(buffer[12..16], this.XOffset);
129+
BinaryPrimitives.WriteUInt32BigEndian(buffer[16..20], this.YOffset);
130+
BinaryPrimitives.WriteUInt16BigEndian(buffer[20..22], this.DelayNumerator);
131+
BinaryPrimitives.WriteUInt16BigEndian(buffer[22..24], this.DelayDenominator);
161132

162133
buffer[24] = (byte)this.DisposeOperation;
163134
buffer[25] = (byte)this.BlendOperation;
@@ -170,13 +141,13 @@ public void WriteTo(Span<byte> buffer)
170141
/// <returns>The parsed fcTL.</returns>
171142
public static FrameControl Parse(ReadOnlySpan<byte> data)
172143
=> new(
173-
sequenceNumber: BinaryPrimitives.ReadInt32BigEndian(data[..4]),
174-
width: BinaryPrimitives.ReadInt32BigEndian(data[4..8]),
175-
height: BinaryPrimitives.ReadInt32BigEndian(data[8..12]),
176-
xOffset: BinaryPrimitives.ReadInt32BigEndian(data[12..16]),
177-
yOffset: BinaryPrimitives.ReadInt32BigEndian(data[16..20]),
178-
delayNumber: BinaryPrimitives.ReadInt16BigEndian(data[20..22]),
179-
delayDenominator: BinaryPrimitives.ReadInt16BigEndian(data[22..24]),
180-
disposeOperation: (PngDisposeOperation)data[24],
181-
blendOperation: (PngBlendOperation)data[25]);
144+
sequenceNumber: BinaryPrimitives.ReadUInt32BigEndian(data[..4]),
145+
width: BinaryPrimitives.ReadUInt32BigEndian(data[4..8]),
146+
height: BinaryPrimitives.ReadUInt32BigEndian(data[8..12]),
147+
xOffset: BinaryPrimitives.ReadUInt32BigEndian(data[12..16]),
148+
yOffset: BinaryPrimitives.ReadUInt32BigEndian(data[16..20]),
149+
delayNumerator: BinaryPrimitives.ReadUInt16BigEndian(data[20..22]),
150+
delayDenominator: BinaryPrimitives.ReadUInt16BigEndian(data[22..24]),
151+
disposeOperation: (PngDisposalMethod)data[24],
152+
blendOperation: (PngBlendMethod)data[25]);
182153
}

src/ImageSharp/Formats/Png/PngBlendOperation.cs renamed to src/ImageSharp/Formats/Png/PngBlendMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

44
namespace SixLabors.ImageSharp.Formats.Png;
55

66
/// <summary>
77
/// Specifies whether the frame is to be alpha blended into the current output buffer content, or whether it should completely replace its region in the output buffer.
88
/// </summary>
9-
public enum PngBlendOperation
9+
public enum PngBlendMethod
1010
{
1111
/// <summary>
1212
/// All color components of the frame, including alpha, overwrite the current contents of the frame's output buffer region.

0 commit comments

Comments
 (0)