|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Buffers.Binary; |
| 5 | + |
| 6 | +namespace SixLabors.ImageSharp.Formats.Png.Chunks; |
| 7 | + |
| 8 | +internal readonly struct APngFrameControl |
| 9 | +{ |
| 10 | + public const int Size = 26; |
| 11 | + |
| 12 | + public APngFrameControl( |
| 13 | + int sequenceNumber, |
| 14 | + int width, |
| 15 | + int height, |
| 16 | + int xOffset, |
| 17 | + int yOffset, |
| 18 | + short delayNumber, |
| 19 | + short delayDenominator, |
| 20 | + APngDisposeOperation disposeOperation, |
| 21 | + APngBlendOperation blendOperation) |
| 22 | + { |
| 23 | + this.SequenceNumber = sequenceNumber; |
| 24 | + this.Width = width; |
| 25 | + this.Height = height; |
| 26 | + this.XOffset = xOffset; |
| 27 | + this.YOffset = yOffset; |
| 28 | + this.DelayNumber = delayNumber; |
| 29 | + this.DelayDenominator = delayDenominator; |
| 30 | + this.DisposeOperation = disposeOperation; |
| 31 | + this.BlendOperation = blendOperation; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the sequence number of the animation chunk, starting from 0 |
| 36 | + /// </summary> |
| 37 | + public int SequenceNumber { get; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the width of the following frame |
| 41 | + /// </summary> |
| 42 | + public int Width { get; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the height of the following frame |
| 46 | + /// </summary> |
| 47 | + public int Height { get; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the X position at which to render the following frame |
| 51 | + /// </summary> |
| 52 | + public int XOffset { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the Y position at which to render the following frame |
| 56 | + /// </summary> |
| 57 | + public int YOffset { get; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Gets the frame delay fraction numerator |
| 61 | + /// </summary> |
| 62 | + public short DelayNumber { get; } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets the frame delay fraction denominator |
| 66 | + /// </summary> |
| 67 | + public short DelayDenominator { get; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets the type of frame area disposal to be done after rendering this frame |
| 71 | + /// </summary> |
| 72 | + public APngDisposeOperation DisposeOperation { get; } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Gets the type of frame area rendering for this frame |
| 76 | + /// </summary> |
| 77 | + public APngBlendOperation BlendOperation { get; } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Validates the APng fcTL. |
| 81 | + /// </summary> |
| 82 | + /// <exception cref="NotSupportedException"> |
| 83 | + /// Thrown if the image does pass validation. |
| 84 | + /// </exception> |
| 85 | + public void Validate(PngHeader hdr) |
| 86 | + { |
| 87 | + if (this.XOffset < 0) |
| 88 | + { |
| 89 | + throw new NotSupportedException($"Invalid XOffset. Expected >= 0. Was '{this.XOffset}'."); |
| 90 | + } |
| 91 | + |
| 92 | + if (this.YOffset < 0) |
| 93 | + { |
| 94 | + throw new NotSupportedException($"Invalid YOffset. Expected >= 0. Was '{this.YOffset}'."); |
| 95 | + } |
| 96 | + |
| 97 | + if (this.Width <= 0) |
| 98 | + { |
| 99 | + throw new NotSupportedException($"Invalid Width. Expected > 0. Was '{this.Width}'."); |
| 100 | + } |
| 101 | + |
| 102 | + if (this.Height <= 0) |
| 103 | + { |
| 104 | + throw new NotSupportedException($"Invalid Height. Expected > 0. Was '{this.Height}'."); |
| 105 | + } |
| 106 | + |
| 107 | + if (this.XOffset + this.Width > hdr.Width) |
| 108 | + { |
| 109 | + throw new NotSupportedException($"Invalid XOffset or Width. The sum > PngHeader.Width. Was '{this.XOffset + this.Width}'."); |
| 110 | + } |
| 111 | + |
| 112 | + if (this.YOffset + this.Height > hdr.Height) |
| 113 | + { |
| 114 | + throw new NotSupportedException($"Invalid YOffset or Height. The sum > PngHeader.Height. Was '{this.YOffset + this.Height}'."); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Writes the fcTL to the given buffer. |
| 120 | + /// </summary> |
| 121 | + /// <param name="buffer">The buffer to write to.</param> |
| 122 | + public void WriteTo(Span<byte> buffer) |
| 123 | + { |
| 124 | + BinaryPrimitives.WriteInt32BigEndian(buffer[..4], this.SequenceNumber); |
| 125 | + BinaryPrimitives.WriteInt32BigEndian(buffer[4..8], this.Width); |
| 126 | + BinaryPrimitives.WriteInt32BigEndian(buffer[8..12], this.Height); |
| 127 | + BinaryPrimitives.WriteInt32BigEndian(buffer[12..16], this.XOffset); |
| 128 | + BinaryPrimitives.WriteInt32BigEndian(buffer[16..20], this.YOffset); |
| 129 | + BinaryPrimitives.WriteInt32BigEndian(buffer[20..22], this.DelayNumber); |
| 130 | + BinaryPrimitives.WriteInt32BigEndian(buffer[12..24], this.DelayDenominator); |
| 131 | + |
| 132 | + buffer[24] = (byte)this.DisposeOperation; |
| 133 | + buffer[25] = (byte)this.BlendOperation; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Parses the APngFrameControl from the given data buffer. |
| 138 | + /// </summary> |
| 139 | + /// <param name="data">The data to parse.</param> |
| 140 | + /// <returns>The parsed fcTL.</returns> |
| 141 | + public static APngFrameControl Parse(ReadOnlySpan<byte> data) |
| 142 | + => new( |
| 143 | + sequenceNumber: BinaryPrimitives.ReadInt32BigEndian(data[..4]), |
| 144 | + width: BinaryPrimitives.ReadInt32BigEndian(data[4..8]), |
| 145 | + height: BinaryPrimitives.ReadInt32BigEndian(data[8..12]), |
| 146 | + xOffset: BinaryPrimitives.ReadInt32BigEndian(data[12..16]), |
| 147 | + yOffset: BinaryPrimitives.ReadInt32BigEndian(data[16..20]), |
| 148 | + delayNumber: BinaryPrimitives.ReadInt16BigEndian(data[20..22]), |
| 149 | + delayDenominator: BinaryPrimitives.ReadInt16BigEndian(data[22..24]), |
| 150 | + disposeOperation: (APngDisposeOperation)data[24], |
| 151 | + blendOperation: (APngBlendOperation)data[25]); |
| 152 | +} |
0 commit comments