|
1 | 1 | // Copyright (c) Six Labors.
|
2 | 2 | // Licensed under the Six Labors Split License.
|
3 | 3 |
|
| 4 | +using System.Buffers; |
| 5 | +using System.Buffers.Binary; |
| 6 | +using System.Runtime.CompilerServices; |
4 | 7 | using SixLabors.ImageSharp.Formats.Icon;
|
| 8 | +using SixLabors.ImageSharp.Formats.Png; |
5 | 9 | using SixLabors.ImageSharp.IO;
|
| 10 | +using SixLabors.ImageSharp.Memory.Internals; |
| 11 | +using SixLabors.ImageSharp.Memory; |
| 12 | +using SixLabors.ImageSharp.Metadata; |
6 | 13 |
|
7 | 14 | namespace SixLabors.ImageSharp.Formats.Ani;
|
8 | 15 |
|
9 | 16 | internal class AniDecoderCore : ImageDecoderCore
|
10 | 17 | {
|
| 18 | + /// <summary> |
| 19 | + /// The general decoder options. |
| 20 | + /// </summary> |
| 21 | + private readonly Configuration configuration; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The stream to decode from. |
| 25 | + /// </summary> |
| 26 | + private BufferedReadStream currentStream = null!; |
| 27 | + |
| 28 | + private AniHeader header; |
| 29 | + |
11 | 30 | public AniDecoderCore(DecoderOptions options)
|
12 |
| - : base(options) |
13 |
| - { |
14 |
| - } |
| 31 | + : base(options) => this.configuration = options.Configuration; |
15 | 32 |
|
16 | 33 | protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
|
17 | 34 | {
|
18 |
| - this.ReadHeader(stream); |
19 |
| - Span<byte> buffer = stackalloc byte[4]; |
20 |
| - _ = stream.Read(buffer); |
21 |
| - uint type = BitConverter.ToUInt32(buffer); |
22 |
| - switch (type) |
| 35 | + this.currentStream = stream; |
| 36 | + this.ReadHeader(); |
| 37 | + ImageMetadata metadata = new(); |
| 38 | + AniMetadata aniMetadata = metadata.GetAniMetadata(); |
| 39 | + Image<TPixel>? image = null; |
| 40 | + |
| 41 | + Span<byte> buffer = stackalloc byte[20]; |
| 42 | + |
| 43 | + try |
23 | 44 | {
|
24 |
| - case 0x73_65_71_20: // seq |
25 |
| - break; |
26 |
| - case 0x72_61_74_65: // rate |
27 |
| - break; |
28 |
| - case 0x4C_49_53_54: // list |
29 |
| - break; |
30 |
| - default: |
31 |
| - break; |
| 45 | + while (this.TryReadChunk(buffer, out AniChunk chunk)) |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + switch (chunk.Type) |
| 50 | + { |
| 51 | + case AniChunkType.Seq: |
| 52 | + |
| 53 | + break; |
| 54 | + case AniChunkType.Rate: |
| 55 | + |
| 56 | + break; |
| 57 | + case AniChunkType.List: |
| 58 | + |
| 59 | + break; |
| 60 | + default: |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + chunk.Data?.Dispose(); |
| 67 | + } |
| 68 | + } |
32 | 69 | }
|
| 70 | + catch |
| 71 | + { |
| 72 | + image?.Dispose(); |
| 73 | + throw; |
| 74 | + } |
| 75 | + |
33 | 76 |
|
34 | 77 | throw new NotImplementedException();
|
35 | 78 | }
|
36 | 79 |
|
| 80 | + private void ReadSeq() |
| 81 | + { |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + private bool TryReadChunk(Span<byte> buffer, out AniChunk chunk) |
| 86 | + { |
| 87 | + if (!this.TryReadChunkLength(buffer, out int length)) |
| 88 | + { |
| 89 | + // IEND |
| 90 | + chunk = default; |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + while (length < 0) |
| 95 | + { |
| 96 | + // Not a valid chunk so try again until we reach a known chunk. |
| 97 | + if (!this.TryReadChunkLength(buffer, out length)) |
| 98 | + { |
| 99 | + // IEND |
| 100 | + chunk = default; |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + AniChunkType type = this.ReadChunkType(buffer); |
| 106 | + |
| 107 | + // A chunk might report a length that exceeds the length of the stream. |
| 108 | + // Take the minimum of the two values to ensure we don't read past the end of the stream. |
| 109 | + long position = this.currentStream.Position; |
| 110 | + chunk = new AniChunk( |
| 111 | + length: (int)Math.Min(length, this.currentStream.Length - position), |
| 112 | + type: type, |
| 113 | + data: this.ReadChunkData(length)); |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + |
37 | 119 | protected override ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
|
38 | 120 | {
|
39 | 121 | throw new NotImplementedException();
|
40 | 122 | }
|
41 | 123 |
|
42 |
| - private void ReadHeader(Stream stream) |
| 124 | + private void ReadHeader() |
43 | 125 | {
|
44 | 126 | // Skip the identifier
|
45 |
| - stream.Skip(12); |
| 127 | + this.currentStream.Skip(12); |
46 | 128 | Span<byte> buffer = stackalloc byte[36];
|
47 |
| - _ = stream.Read(buffer); |
48 |
| - AniHeader header = AniHeader.Parse(buffer); |
| 129 | + _ = this.currentStream.Read(buffer); |
| 130 | + this.header = AniHeader.Parse(buffer); |
| 131 | + } |
| 132 | + |
| 133 | + private void ReadSeq(Stream stream) |
| 134 | + { |
| 135 | + Span<byte> buffer = stackalloc byte[4]; |
| 136 | + int length = BinaryPrimitives.ReadInt32BigEndian(buffer); |
| 137 | + |
49 | 138 | }
|
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Attempts to read the length of the next chunk. |
| 142 | + /// </summary> |
| 143 | + /// <param name="buffer">Temporary buffer.</param> |
| 144 | + /// <param name="result">The result length. If the return type is <see langword="false"/> this parameter is passed uninitialized.</param> |
| 145 | + /// <returns> |
| 146 | + /// Whether the length was read. |
| 147 | + /// </returns> |
| 148 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 149 | + private bool TryReadChunkLength(Span<byte> buffer, out int result) |
| 150 | + { |
| 151 | + if (this.currentStream.Read(buffer, 0, 4) == 4) |
| 152 | + { |
| 153 | + result = BinaryPrimitives.ReadInt32BigEndian(buffer); |
| 154 | + |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + result = 0; |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Identifies the chunk type from the chunk. |
| 164 | + /// </summary> |
| 165 | + /// <param name="buffer">Temporary buffer.</param> |
| 166 | + /// <exception cref="ImageFormatException"> |
| 167 | + /// Thrown if the input stream is not valid. |
| 168 | + /// </exception> |
| 169 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 170 | + private AniChunkType ReadChunkType(Span<byte> buffer) |
| 171 | + { |
| 172 | + if (this.currentStream.Read(buffer, 0, 4) == 4) |
| 173 | + { |
| 174 | + return (AniChunkType)BinaryPrimitives.ReadUInt32BigEndian(buffer); |
| 175 | + } |
| 176 | + |
| 177 | + PngThrowHelper.ThrowInvalidChunkType(); |
| 178 | + |
| 179 | + // The IDE cannot detect the throw here. |
| 180 | + return default; |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Reads the chunk data from the stream. |
| 185 | + /// </summary> |
| 186 | + /// <param name="length">The length of the chunk data to read.</param> |
| 187 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 188 | + private IMemoryOwner<byte> ReadChunkData(int length) |
| 189 | + { |
| 190 | + if (length == 0) |
| 191 | + { |
| 192 | + return new BasicArrayBuffer<byte>([]); |
| 193 | + } |
| 194 | + |
| 195 | + // We rent the buffer here to return it afterwards in Decode() |
| 196 | + // We don't want to throw a degenerated memory exception here as we want to allow partial decoding |
| 197 | + // so limit the length. |
| 198 | + length = (int)Math.Min(length, this.currentStream.Length - this.currentStream.Position); |
| 199 | + IMemoryOwner<byte> buffer = this.configuration.MemoryAllocator.Allocate<byte>(length, AllocationOptions.Clean); |
| 200 | + |
| 201 | + this.currentStream.Read(buffer.GetSpan(), 0, length); |
| 202 | + |
| 203 | + return buffer; |
| 204 | + } |
| 205 | + |
50 | 206 | }
|
0 commit comments