|
| 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 FrameControl |
| 9 | +{ |
| 10 | + public const int Size = 26; |
| 11 | + |
| 12 | + public FrameControl(uint width, uint height) |
| 13 | + : this(0, width, height, 0, 0, 0, 0, default, default) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + public FrameControl( |
| 18 | + uint sequenceNumber, |
| 19 | + uint width, |
| 20 | + uint height, |
| 21 | + uint xOffset, |
| 22 | + uint yOffset, |
| 23 | + ushort delayNumerator, |
| 24 | + ushort delayDenominator, |
| 25 | + PngDisposalMethod disposeOperation, |
| 26 | + PngBlendMethod blendOperation) |
| 27 | + { |
| 28 | + this.SequenceNumber = sequenceNumber; |
| 29 | + this.Width = width; |
| 30 | + this.Height = height; |
| 31 | + this.XOffset = xOffset; |
| 32 | + this.YOffset = yOffset; |
| 33 | + this.DelayNumerator = delayNumerator; |
| 34 | + this.DelayDenominator = delayDenominator; |
| 35 | + this.DisposeOperation = disposeOperation; |
| 36 | + this.BlendOperation = blendOperation; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the sequence number of the animation chunk, starting from 0 |
| 41 | + /// </summary> |
| 42 | + public uint SequenceNumber { get; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the width of the following frame |
| 46 | + /// </summary> |
| 47 | + public uint Width { get; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the height of the following frame |
| 51 | + /// </summary> |
| 52 | + public uint Height { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the X position at which to render the following frame |
| 56 | + /// </summary> |
| 57 | + public uint XOffset { get; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Gets the Y position at which to render the following frame |
| 61 | + /// </summary> |
| 62 | + public uint YOffset { get; } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets the X limit at which to render the following frame |
| 66 | + /// </summary> |
| 67 | + public uint XMax => this.XOffset + this.Width; |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets the Y limit at which to render the following frame |
| 71 | + /// </summary> |
| 72 | + public uint YMax => this.YOffset + this.Height; |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Gets the frame delay fraction numerator |
| 76 | + /// </summary> |
| 77 | + public ushort DelayNumerator { get; } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Gets the frame delay fraction denominator |
| 81 | + /// </summary> |
| 82 | + public ushort DelayDenominator { get; } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Gets the type of frame area disposal to be done after rendering this frame |
| 86 | + /// </summary> |
| 87 | + public PngDisposalMethod DisposeOperation { get; } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Gets the type of frame area rendering for this frame |
| 91 | + /// </summary> |
| 92 | + public PngBlendMethod BlendOperation { get; } |
| 93 | + |
| 94 | + public Rectangle Bounds => new((int)this.XOffset, (int)this.YOffset, (int)this.Width, (int)this.Height); |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Validates the APng fcTL. |
| 98 | + /// </summary> |
| 99 | + /// <param name="header">The header.</param> |
| 100 | + /// <exception cref="NotSupportedException"> |
| 101 | + /// Thrown if the image does pass validation. |
| 102 | + /// </exception> |
| 103 | + public void Validate(PngHeader header) |
| 104 | + { |
| 105 | + if (this.Width == 0) |
| 106 | + { |
| 107 | + PngThrowHelper.ThrowInvalidParameter(this.Width, "Expected > 0"); |
| 108 | + } |
| 109 | + |
| 110 | + if (this.Height == 0) |
| 111 | + { |
| 112 | + PngThrowHelper.ThrowInvalidParameter(this.Height, "Expected > 0"); |
| 113 | + } |
| 114 | + |
| 115 | + if (this.XMax > header.Width) |
| 116 | + { |
| 117 | + PngThrowHelper.ThrowInvalidParameter(this.XOffset, this.Width, $"The x-offset plus width > {nameof(PngHeader)}.{nameof(PngHeader.Width)}"); |
| 118 | + } |
| 119 | + |
| 120 | + if (this.YMax > header.Height) |
| 121 | + { |
| 122 | + PngThrowHelper.ThrowInvalidParameter(this.YOffset, this.Height, $"The y-offset plus height > {nameof(PngHeader)}.{nameof(PngHeader.Height)}"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Writes the fcTL to the given buffer. |
| 128 | + /// </summary> |
| 129 | + /// <param name="buffer">The buffer to write to.</param> |
| 130 | + public void WriteTo(Span<byte> buffer) |
| 131 | + { |
| 132 | + BinaryPrimitives.WriteUInt32BigEndian(buffer[..4], this.SequenceNumber); |
| 133 | + BinaryPrimitives.WriteUInt32BigEndian(buffer[4..8], this.Width); |
| 134 | + BinaryPrimitives.WriteUInt32BigEndian(buffer[8..12], this.Height); |
| 135 | + BinaryPrimitives.WriteUInt32BigEndian(buffer[12..16], this.XOffset); |
| 136 | + BinaryPrimitives.WriteUInt32BigEndian(buffer[16..20], this.YOffset); |
| 137 | + BinaryPrimitives.WriteUInt16BigEndian(buffer[20..22], this.DelayNumerator); |
| 138 | + BinaryPrimitives.WriteUInt16BigEndian(buffer[22..24], this.DelayDenominator); |
| 139 | + |
| 140 | + buffer[24] = (byte)this.DisposeOperation; |
| 141 | + buffer[25] = (byte)this.BlendOperation; |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Parses the APngFrameControl from the given data buffer. |
| 146 | + /// </summary> |
| 147 | + /// <param name="data">The data to parse.</param> |
| 148 | + /// <returns>The parsed fcTL.</returns> |
| 149 | + public static FrameControl Parse(ReadOnlySpan<byte> data) |
| 150 | + => new( |
| 151 | + sequenceNumber: BinaryPrimitives.ReadUInt32BigEndian(data[..4]), |
| 152 | + width: BinaryPrimitives.ReadUInt32BigEndian(data[4..8]), |
| 153 | + height: BinaryPrimitives.ReadUInt32BigEndian(data[8..12]), |
| 154 | + xOffset: BinaryPrimitives.ReadUInt32BigEndian(data[12..16]), |
| 155 | + yOffset: BinaryPrimitives.ReadUInt32BigEndian(data[16..20]), |
| 156 | + delayNumerator: BinaryPrimitives.ReadUInt16BigEndian(data[20..22]), |
| 157 | + delayDenominator: BinaryPrimitives.ReadUInt16BigEndian(data[22..24]), |
| 158 | + disposeOperation: (PngDisposalMethod)data[24], |
| 159 | + blendOperation: (PngBlendMethod)data[25]); |
| 160 | +} |
0 commit comments