-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
GifEncoder.LzwEncoder.cs
549 lines (427 loc) · 20.1 KB
/
GifEncoder.LzwEncoder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#region Copyright
///////////////////////////////////////////////////////////////////////////////
// File: GifEncoder.LzwEncoder.cs
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) KGy SOFT, 2005-2021 - All Rights Reserved
//
// You should have received a copy of the LICENSE file at the top-level
// directory of this distribution.
//
// Please refer to the LICENSE file if you want to use this source code.
///////////////////////////////////////////////////////////////////////////////
#endregion
#region Usings
using System;
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
using System.Buffers;
#endif
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security;
using KGySoft.Collections;
#endregion
namespace KGySoft.Drawing.Imaging
{
public partial class GifEncoder
{
/// <summary>
/// The LZW Encoder based on the specification as per chapter 22 and Appendix F in https://www.w3.org/Graphics/GIF/spec-gif89a.txt
/// The detailed LZW algorithm is written here: http://giflib.sourceforge.net/whatsinagif/lzw_image_data.html
/// </summary>
[SecuritySafeCritical]
private static class LzwEncoder
{
#region Nested structs
#region IndexBuffer struct
/// <summary>
/// Represents a span of a byte array and has specialized GetHashCode/Equals.
/// It could also contain a single <see cref="ArraySegment{T}"/> or <see cref="ArraySection{T}"/> field
/// but it is faster if we can mutate only the length directly.
/// </summary>
private struct IndexBuffer : IEquatable<IndexBuffer>
{
#region Fields
internal readonly byte[] Buffer;
internal readonly int Offset;
internal int Length;
#endregion
#region Constructors
internal IndexBuffer(ArraySection<byte> buffer)
{
Buffer = buffer.UnderlyingArray!;
Offset = buffer.Offset;
Length = buffer.Length;
}
internal IndexBuffer(byte[] buffer, int offset, int length)
{
Buffer = buffer;
Offset = offset;
Length = length;
}
internal IndexBuffer(byte[] buffer, int offset)
{
Buffer = buffer;
Offset = offset;
Length = 1;
}
#endregion
#region Methods
#region Public Methods
public bool Equals(IndexBuffer other)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
return new ReadOnlySpan<byte>(Buffer, Offset, Length).SequenceEqual(new ReadOnlySpan<byte>(other.Buffer, other.Offset, other.Length));
#else
if (Length != other.Length)
return false;
unsafe
{
fixed (byte* pThis = &Buffer[Offset])
fixed (byte* pOther = &other.Buffer[other.Offset])
return MemoryHelper.CompareMemory(pThis, pOther, Length);
}
#endif
}
public override bool Equals(object? obj) => obj is IndexBuffer other && Equals(other);
#if NETCOREAPP3_0_OR_GREATER
public override int GetHashCode()
{
Debug.Assert(Length > 1, "Obtaining hashes are expected for non-single sequences");
int result;
ref byte pos = ref Buffer[Offset];
ref byte end = ref Unsafe.Add(ref pos, Length);
// 2 or 3 length
if (Length < 4)
{
result = 13;
while (Unsafe.IsAddressLessThan(ref pos, ref end))
{
result = result * 4099 + pos;
pos = ref Unsafe.Add(ref pos, 1);
}
return result;
}
// Including only the first and last 4 bytes (overlapping is allowed) to avoid high hash code cost.
// Prefixes of large single color areas are still differentiated by length.
result = 8209 * Length;
result = result * 2053 + Unsafe.ReadUnaligned<int>(ref pos);
return Length == 4 ? result : result * 1031 + Unsafe.ReadUnaligned<int>(ref Unsafe.Add(ref end, -4));
}
#else
public override unsafe int GetHashCode()
{
Debug.Assert(Length > 1, "Obtaining hashes are expected for non-single sequences");
fixed (byte* pBuf = Buffer)
{
int result;
byte* pos = &pBuf[Offset];
byte* end = pos + Length;
// 2 or 3 length
if (Length < 4)
{
result = 13;
while (pos < end)
{
result = result * 4099 + *pos;
pos += 1;
}
return result;
}
// Including only the first and last 4 bytes (overlapping is allowed) to avoid high hash code cost.
// Prefixes of large single color areas are still differentiated by length.
result = 8209 * Length;
result = result * 2053 + *(int*)pos;
return Length == 4 ? result : result * 1031 + *(int*)(end - 4);
}
}
#endif
#endregion
#region Internal Methods
internal void AddNext() => Length += 1;
#endregion
#endregion
}
#endregion
#region CodeTable struct
private struct CodeTable : IDisposable
{
#region Constants
private const int maxCodeCount = 1 << 12;
#endregion
#region Fields
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
private readonly bool ownBuffer;
#endif
private readonly Dictionary<IndexBuffer, int>? codeTable;
private readonly IndexBuffer indices;
private readonly GifCompressionMode compressionMode;
private int currentPosition;
private int nextFreeCode;
#endregion
#region Properties
internal int MinimumCodeSize { get; }
internal int ClearCode => 1 << MinimumCodeSize;
internal int EndInformationCode => ClearCode + 1;
internal int FirstAvailableCode => ClearCode + 2;
internal int CurrentCodeSize { get; private set; }
internal int NextSizeLimit => 1 << CurrentCodeSize;
internal byte CurrentIndex => indices.Buffer[indices.Offset + currentPosition];
#endregion
#region Constructors
public CodeTable(IReadableBitmapData imageData, GifCompressionMode compressionMode) : this()
{
Debug.Assert(imageData.Palette?.Count <= 256);
currentPosition = -1;
this.compressionMode = compressionMode;
// According to Appendix F in https://www.w3.org/Graphics/GIF/spec-gif89a.txt
// the minimum code size is 2 "because of some algorithmic constraints" (preserved code values)
MinimumCodeSize = Math.Max(2, imageData.Palette!.Count.ToBitsPerPixel());
CurrentCodeSize = MinimumCodeSize + 1;
// In uncompressed mode we will only need code size. Even pixel enumeration will be different
// so we can spare allocating indices.
if (compressionMode == GifCompressionMode.Uncompressed)
return;
int size = imageData.Width * imageData.Height;
codeTable = new Dictionary<IndexBuffer, int>(Math.Min(size,
(compressionMode == GifCompressionMode.DoNotIncreaseBitSize ? NextSizeLimit : maxCodeCount) - FirstAvailableCode));
// Trying to re-use the actual buffer if it is an 8-bit managed bitmap data; otherwise, allocating a new buffer.
// We need this to prevent allocating a huge memory for the code table keys with the segments: all segments will just be
// spans over the original sequence, which often will contain overlapping memory
if (imageData is ManagedBitmapData<byte, ManagedBitmapDataRow8I> managed8BitBitmapData)
{
indices = new IndexBuffer(managed8BitBitmapData.Buffer.Buffer);
return;
}
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
ownBuffer = true;
indices = new IndexBuffer(ArrayPool<byte>.Shared.Rent(size), 0, size);
#else
indices = new IndexBuffer(new byte[size], 0, size);
#endif
// If we could not obtain the actual buffer, then copying the palette indices into the newly allocated one.
// Not using parallel processing at this level
int i = 0;
IReadableBitmapDataRow rowSrc = imageData.FirstRow;
for (int y = 0; y < imageData.Height; y++, rowSrc.MoveNextRow())
{
// we can index directly the array here without an offset because we allocated/rented it
for (int x = 0; x < imageData.Width; x++)
indices.Buffer[i++] = (byte)rowSrc.GetColorIndex(x);
}
}
#endregion
#region Methods
#region Public Method
public void Dispose()
{
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
if (ownBuffer)
ArrayPool<byte>.Shared.Return(indices.Buffer);
#endif
}
#endregion
#region Internal Methods
[MethodImpl(MethodImpl.AggressiveInlining)]
internal void Reset()
{
Debug.Assert(codeTable != null, "Should not be called in Uncompressed mode");
codeTable!.Clear();
CurrentCodeSize = MinimumCodeSize + 1;
nextFreeCode = FirstAvailableCode;
}
[MethodImpl(MethodImpl.AggressiveInlining)]
internal bool MoveNextIndex()
{
if (currentPosition == indices.Length - 1)
return false;
currentPosition += 1;
return true;
}
internal IndexBuffer GetInitialSegment() => new IndexBuffer(indices.Buffer, indices.Offset + currentPosition);
[MethodImpl(MethodImpl.AggressiveInlining)]
internal bool TryGetCode(IndexBuffer key, out int code)
{
Debug.Assert(codeTable != null, "Should not be called in Uncompressed mode");
return codeTable!.TryGetValue(key, out code);
}
[MethodImpl(MethodImpl.AggressiveInlining)]
internal bool TryAddCode(IndexBuffer key)
{
Debug.Assert(codeTable != null, "Should not be called in Uncompressed mode");
if (nextFreeCode == maxCodeCount || compressionMode == GifCompressionMode.DoNotIncreaseBitSize && nextFreeCode + 1 == NextSizeLimit)
return false;
if (nextFreeCode == NextSizeLimit)
CurrentCodeSize += 1;
codeTable!.Add(key, nextFreeCode);
nextFreeCode += 1;
return true;
}
#endregion
#endregion
}
#endregion
#region BitWriter struct
private unsafe ref struct BitWriter
{
#region Constants
private const int bufferCapacity = 255;
#endregion
#region Fields
private readonly BinaryWriter writer;
private int accumulator;
private int accumulatorSize;
private int bufferLength;
#pragma warning disable CS0649 // field is never assigned - false alarm, a fixed buffer should not be assigned
private fixed byte buffer[bufferCapacity];
#pragma warning restore CS0649
#endregion
#region Constructors
internal BitWriter(BinaryWriter writer) : this() => this.writer = writer;
#endregion
#region Methods
#region Internal Methods
internal void WriteByte(byte value) => writer.Write(value);
internal void WriteCode(int code, int bitSize)
{
Debug.Assert(bitSize + accumulatorSize <= sizeof(int) * 8);
if (BitConverter.IsLittleEndian)
{
if (accumulatorSize == 0)
accumulator = code;
else
accumulator |= code << accumulatorSize;
accumulatorSize += bitSize;
}
else
{
// we must use little endian order regardless of current architecture
int remainingSize = bitSize;
while (remainingSize > 8)
{
accumulator |= (code & 0xFF) << accumulatorSize;
accumulatorSize += 8;
remainingSize -= 8;
code >>= 8;
}
if (remainingSize > 0)
{
accumulator |= (code & 0xFF) << accumulatorSize;
accumulatorSize += remainingSize;
}
}
while (accumulatorSize > 8)
{
Append((byte)(accumulator & 0xFF));
accumulatorSize -= 8;
accumulator >>= 8;
}
}
internal void Flush()
{
if (accumulatorSize > 0)
Append((byte)(accumulator & 0xFF));
if (bufferLength > 0)
DumpImageDataSubBlock();
}
#endregion
#region Private Methods
private void Append(byte value)
{
Debug.Assert(bufferLength < bufferCapacity);
buffer[bufferLength] = value;
bufferLength += 1;
if (bufferLength == bufferCapacity)
DumpImageDataSubBlock();
}
private void DumpImageDataSubBlock()
{
Debug.Assert(bufferLength is > 0 and <= bufferCapacity);
WriteByte((byte)bufferLength);
// This causes CS1666. TODO: apply when https://github.com/dotnet/roslyn/issues/57583 is fixed
//writer.Write(new ReadOnlySpan<byte>(buffer, bufferLength));
for (int i = 0; i < bufferLength; i++)
WriteByte(buffer[i]);
bufferLength = 0;
}
#endregion
#endregion
}
#endregion
#endregion
#region Methods
#region Internal Methods
internal static void Encode(IReadableBitmapData imageData, BinaryWriter bw, GifCompressionMode compressionMode)
{
if (compressionMode == GifCompressionMode.Uncompressed)
{
EncodeUncompressed(imageData, bw);
return;
}
using var codeTable = new CodeTable(imageData, compressionMode);
var writer = new BitWriter(bw);
writer.WriteByte((byte)codeTable.MinimumCodeSize);
writer.WriteCode(codeTable.ClearCode, codeTable.CurrentCodeSize);
codeTable.Reset();
codeTable.MoveNextIndex();
int previousCode = codeTable.CurrentIndex;
IndexBuffer indexBuffer = codeTable.GetInitialSegment();
while (codeTable.MoveNextIndex())
{
indexBuffer.AddNext();
if (codeTable.TryGetCode(indexBuffer, out int code))
{
previousCode = code;
continue;
}
writer.WriteCode(previousCode, codeTable.CurrentCodeSize);
previousCode = codeTable.CurrentIndex;
if (!codeTable.TryAddCode(indexBuffer) && compressionMode != GifCompressionMode.DoNotClear)
{
writer.WriteCode(codeTable.ClearCode, codeTable.CurrentCodeSize);
codeTable.Reset();
}
indexBuffer = codeTable.GetInitialSegment();
}
writer.WriteCode(previousCode, codeTable.CurrentCodeSize);
writer.WriteCode(codeTable.EndInformationCode, codeTable.CurrentCodeSize);
writer.Flush();
writer.WriteByte(blockTerminator);
}
#endregion
#region Private Methods
private static void EncodeUncompressed(IReadableBitmapData imageData, BinaryWriter bw)
{
using var codeTable = new CodeTable(imageData, GifCompressionMode.Uncompressed);
var writer = new BitWriter(bw);
int codeSize = codeTable.CurrentCodeSize;
writer.WriteByte((byte)codeTable.MinimumCodeSize);
writer.WriteCode(codeTable.ClearCode, codeSize);
// Though we do not build the code table the decoder does so we must regularly send a clear to prevent increasing code size
int maxLength = codeTable.NextSizeLimit - codeTable.FirstAvailableCode;
int currLength = 0;
// Note: Not using CodeTable.MoveNextIndex here because Uncompressed mode does not allocate the 8-bit index buffer for non 8-bit images.
// It would not be the cleanest design if CodeTable was not a private type but it helps avoiding unnecessary allocations.
IReadableBitmapDataRow row = imageData.FirstRow;
do
{
for (int x = 0; x < imageData.Width; x++)
{
writer.WriteCode(row.GetColorIndex(x), codeSize);
currLength += 1;
if (currLength == maxLength)
{
currLength = 0;
writer.WriteCode(codeTable.ClearCode, codeSize);
}
}
} while (row.MoveNextRow());
writer.WriteCode(codeTable.EndInformationCode, codeSize);
writer.Flush();
writer.WriteByte(blockTerminator);
}
#endregion
#endregion
}
}
}