Skip to content

Commit 992361f

Browse files
Cleanup
1 parent ea2c4fa commit 992361f

File tree

9 files changed

+115
-116
lines changed

9 files changed

+115
-116
lines changed

.gitattributes

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,19 @@
6464
# Set explicit file behavior to:
6565
# treat as text
6666
# normalize to Unix-style line endings and
67-
# use a union merge when resoling conflicts
67+
# use a union merge when resolving conflicts
6868
###############################################################################
6969
*.csproj text eol=lf merge=union
7070
*.dbproj text eol=lf merge=union
7171
*.fsproj text eol=lf merge=union
7272
*.ncrunchproject text eol=lf merge=union
7373
*.vbproj text eol=lf merge=union
74+
*.shproj text eol=lf merge=union
7475
###############################################################################
7576
# Set explicit file behavior to:
7677
# treat as text
7778
# normalize to Windows-style line endings and
78-
# use a union merge when resoling conflicts
79+
# use a union merge when resolving conflicts
7980
###############################################################################
8081
*.sln text eol=crlf merge=union
8182
###############################################################################

src/ImageSharp/Formats/Webp/AlphaDecoder.cs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AlphaDecoder(int width, int height, IMemoryOwner<byte> data, byte alphaCh
3838
this.LastRow = 0;
3939
int totalPixels = width * height;
4040

41-
var compression = (WebpAlphaCompressionMethod)(alphaChunkHeader & 0x03);
41+
WebpAlphaCompressionMethod compression = (WebpAlphaCompressionMethod)(alphaChunkHeader & 0x03);
4242
if (compression is not WebpAlphaCompressionMethod.NoCompression and not WebpAlphaCompressionMethod.WebpLosslessCompression)
4343
{
4444
WebpThrowHelper.ThrowImageFormatException($"unexpected alpha compression method {compression} found");
@@ -59,7 +59,7 @@ public AlphaDecoder(int width, int height, IMemoryOwner<byte> data, byte alphaCh
5959

6060
if (this.Compressed)
6161
{
62-
var bitReader = new Vp8LBitReader(data);
62+
Vp8LBitReader bitReader = new(data);
6363
this.LosslessDecoder = new WebpLosslessDecoder(bitReader, memoryAllocator, configuration);
6464
this.LosslessDecoder.DecodeImageStream(this.Vp8LDec, width, height, true);
6565

@@ -174,17 +174,14 @@ public void Decode()
174174
dst = dst[this.Width..];
175175
}
176176
}
177+
else if (this.Use8BDecode)
178+
{
179+
this.LosslessDecoder.DecodeAlphaData(this);
180+
}
177181
else
178182
{
179-
if (this.Use8BDecode)
180-
{
181-
this.LosslessDecoder.DecodeAlphaData(this);
182-
}
183-
else
184-
{
185-
this.LosslessDecoder.DecodeImageData(this.Vp8LDec, this.Vp8LDec.Pixels.Memory.Span);
186-
this.ExtractAlphaRows(this.Vp8LDec);
187-
}
183+
this.LosslessDecoder.DecodeImageData(this.Vp8LDec, this.Vp8LDec.Pixels.Memory.Span);
184+
this.ExtractAlphaRows(this.Vp8LDec);
188185
}
189186
}
190187

@@ -262,8 +259,7 @@ private void ExtractAlphaRows(Vp8LDecoder dec)
262259
{
263260
int numRowsToProcess = dec.Height;
264261
int width = dec.Width;
265-
Span<uint> pixels = dec.Pixels.Memory.Span;
266-
Span<uint> input = pixels;
262+
Span<uint> input = dec.Pixels.Memory.Span;
267263
Span<byte> output = this.Alpha.Memory.Span;
268264

269265
// Extract alpha (which is stored in the green plane).
@@ -328,7 +324,7 @@ private static void HorizontalUnfilter(Span<byte> prev, Span<byte> input, Span<b
328324
ref byte srcRef = ref MemoryMarshal.GetReference(input);
329325
for (i = 1; i + 8 <= width; i += 8)
330326
{
331-
var a0 = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref srcRef, i)), 0);
327+
Vector128<long> a0 = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref srcRef, i)), 0);
332328
Vector128<byte> a1 = Sse2.Add(a0.AsByte(), last.AsByte());
333329
Vector128<byte> a2 = Sse2.ShiftLeftLogical128BitLane(a1, 1);
334330
Vector128<byte> a3 = Sse2.Add(a1, a2);
@@ -366,32 +362,29 @@ private static void VerticalUnfilter(Span<byte> prev, Span<byte> input, Span<byt
366362
{
367363
HorizontalUnfilter(null, input, dst, width);
368364
}
369-
else
365+
else if (Avx2.IsSupported)
370366
{
371-
if (Avx2.IsSupported)
367+
nint i;
368+
int maxPos = width & ~31;
369+
for (i = 0; i < maxPos; i += 32)
372370
{
373-
nint i;
374-
int maxPos = width & ~31;
375-
for (i = 0; i < maxPos; i += 32)
376-
{
377-
Vector256<int> a0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(input), i));
378-
Vector256<int> b0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(prev), i));
379-
Vector256<byte> c0 = Avx2.Add(a0.AsByte(), b0.AsByte());
380-
ref byte outputRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(dst), i);
381-
Unsafe.As<byte, Vector256<byte>>(ref outputRef) = c0;
382-
}
371+
Vector256<int> a0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(input), i));
372+
Vector256<int> b0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(prev), i));
373+
Vector256<byte> c0 = Avx2.Add(a0.AsByte(), b0.AsByte());
374+
ref byte outputRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(dst), i);
375+
Unsafe.As<byte, Vector256<byte>>(ref outputRef) = c0;
376+
}
383377

384-
for (; i < width; i++)
385-
{
386-
dst[(int)i] = (byte)(prev[(int)i] + input[(int)i]);
387-
}
378+
for (; i < width; i++)
379+
{
380+
dst[(int)i] = (byte)(prev[(int)i] + input[(int)i]);
388381
}
389-
else
382+
}
383+
else
384+
{
385+
for (int i = 0; i < width; i++)
390386
{
391-
for (int i = 0; i < width; i++)
392-
{
393-
dst[i] = (byte)(prev[i] + input[i]);
394-
}
387+
dst[i] = (byte)(prev[i] + input[i]);
395388
}
396389
}
397390
}

src/ImageSharp/Formats/Webp/AlphaEncoder.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ namespace SixLabors.ImageSharp.Formats.Webp;
1212
/// <summary>
1313
/// Methods for encoding the alpha data of a VP8 image.
1414
/// </summary>
15-
internal class AlphaEncoder : IDisposable
15+
internal static class AlphaEncoder
1616
{
17-
private IMemoryOwner<byte>? alphaData;
18-
1917
/// <summary>
2018
/// Encodes the alpha channel data.
2119
/// Data is either compressed as lossless webp image or uncompressed.
@@ -28,12 +26,18 @@ internal class AlphaEncoder : IDisposable
2826
/// <param name="compress">Indicates, if the data should be compressed with the lossless webp compression.</param>
2927
/// <param name="size">The size in bytes of the alpha data.</param>
3028
/// <returns>The encoded alpha data.</returns>
31-
public IMemoryOwner<byte> EncodeAlpha<TPixel>(Image<TPixel> image, Configuration configuration, MemoryAllocator memoryAllocator, bool skipMetadata, bool compress, out int size)
29+
public static IMemoryOwner<byte> EncodeAlpha<TPixel>(
30+
Image<TPixel> image,
31+
Configuration configuration,
32+
MemoryAllocator memoryAllocator,
33+
bool skipMetadata,
34+
bool compress,
35+
out int size)
3236
where TPixel : unmanaged, IPixel<TPixel>
3337
{
3438
int width = image.Width;
3539
int height = image.Height;
36-
this.alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator);
40+
IMemoryOwner<byte> alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator);
3741

3842
if (compress)
3943
{
@@ -54,15 +58,15 @@ public IMemoryOwner<byte> EncodeAlpha<TPixel>(Image<TPixel> image, Configuration
5458
// The transparency information will be stored in the green channel of the ARGB quadruplet.
5559
// The green channel is allowed extra transformation steps in the specification -- unlike the other channels,
5660
// that can improve compression.
57-
using Image<Rgba32> alphaAsImage = DispatchAlphaToGreen(image, this.alphaData.GetSpan());
61+
using Image<Rgba32> alphaAsImage = DispatchAlphaToGreen(image, alphaData.GetSpan());
5862

59-
size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, this.alphaData);
63+
size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, alphaData);
6064

61-
return this.alphaData;
65+
return alphaData;
6266
}
6367

6468
size = width * height;
65-
return this.alphaData;
69+
return alphaData;
6670
}
6771

6872
/// <summary>
@@ -127,7 +131,4 @@ private static IMemoryOwner<byte> ExtractAlphaChannel<TPixel>(Image<TPixel> imag
127131

128132
return alphaDataBuffer;
129133
}
130-
131-
/// <inheritdoc/>
132-
public void Dispose() => this.alphaData?.Dispose();
133134
}

src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ internal abstract class BitReaderBase : IDisposable
1313
{
1414
private bool isDisposed;
1515

16-
protected BitReaderBase(IMemoryOwner<byte> data) => this.Data = data;
16+
protected BitReaderBase(IMemoryOwner<byte> data)
17+
=> this.Data = data;
1718

18-
protected BitReaderBase(Stream inputStream, int imageDataSize, MemoryAllocator memoryAllocator) => this.Data = ReadImageDataFromStream(inputStream, imageDataSize, memoryAllocator);
19+
protected BitReaderBase(Stream inputStream, int imageDataSize, MemoryAllocator memoryAllocator)
20+
=> this.Data = ReadImageDataFromStream(inputStream, imageDataSize, memoryAllocator);
1921

2022
/// <summary>
21-
/// Gets or sets the raw encoded image data.
23+
/// Gets the raw encoded image data.
2224
/// </summary>
23-
public IMemoryOwner<byte> Data { get; set; }
25+
public IMemoryOwner<byte> Data { get; }
2426

2527
/// <summary>
2628
/// Copies the raw encoded image data from the stream into a byte array.

src/ImageSharp/Formats/Webp/Lossless/ColorCache.cs

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

4-
using System.Diagnostics.CodeAnalysis;
54
using System.Runtime.CompilerServices;
65

76
namespace SixLabors.ImageSharp.Formats.Webp.Lossless;
@@ -28,17 +27,17 @@ public ColorCache(int hashBits)
2827
/// <summary>
2928
/// Gets the color entries.
3029
/// </summary>
31-
public uint[] Colors { get; private set; }
30+
public uint[] Colors { get; }
3231

3332
/// <summary>
3433
/// Gets the hash shift: 32 - hashBits.
3534
/// </summary>
36-
public int HashShift { get; private set; }
35+
public int HashShift { get; }
3736

3837
/// <summary>
3938
/// Gets the hash bits.
4039
/// </summary>
41-
public int HashBits { get; private set; }
40+
public int HashBits { get; }
4241

4342
/// <summary>
4443
/// Inserts a new color into the cache.

src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,18 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
348348
// Extract and encode alpha channel data, if present.
349349
int alphaDataSize = 0;
350350
bool alphaCompressionSucceeded = false;
351-
using AlphaEncoder alphaEncoder = new();
352351
Span<byte> alphaData = Span<byte>.Empty;
353352
if (hasAlpha)
354353
{
355354
// TODO: This can potentially run in an separate task.
356-
IMemoryOwner<byte> encodedAlphaData = alphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.skipMetadata, this.alphaCompression, out alphaDataSize);
355+
using IMemoryOwner<byte> encodedAlphaData = AlphaEncoder.EncodeAlpha(
356+
image,
357+
this.configuration,
358+
this.memoryAllocator,
359+
this.skipMetadata,
360+
this.alphaCompression,
361+
out alphaDataSize);
362+
357363
alphaData = encodedAlphaData.GetSpan();
358364
if (alphaDataSize < pixelCount)
359365
{

0 commit comments

Comments
 (0)