Browse Source

Fix warnings

pull/1552/head
Brian Popow 5 years ago
parent
commit
3c2cc0c52e
  1. 1
      src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs
  2. 2
      src/ImageSharp/Formats/WebP/Lossless/BackwardReferenceEncoder.cs
  3. 211
      src/ImageSharp/Formats/WebP/Lossless/LosslessUtils.cs
  4. 4
      src/ImageSharp/Formats/WebP/Lossless/NearLosslessEnc.cs
  5. 10
      src/ImageSharp/Formats/WebP/Lossless/PredictorEncoder.cs
  6. 3
      src/ImageSharp/Formats/WebP/Lossless/WebpLosslessDecoder.cs
  7. 3
      src/ImageSharp/Formats/WebP/Lossy/Vp8EncIterator.cs
  8. 1
      src/ImageSharp/Formats/WebP/Lossy/Vp8Encoder.cs
  9. 1
      src/ImageSharp/Formats/WebP/Lossy/WebpLossyDecoder.cs
  10. 22
      src/ImageSharp/Formats/WebP/Lossy/YuvConversion.cs
  11. 1
      src/ImageSharp/Formats/WebP/WebpDecoder.cs
  12. 2
      src/ImageSharp/Formats/WebP/WebpLookupTables.cs
  13. 7
      tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs

1
src/ImageSharp/Formats/WebP/BitReader/BitReaderBase.cs

@ -4,7 +4,6 @@
using System;
using System.Buffers;
using System.IO;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Webp.BitReader

2
src/ImageSharp/Formats/WebP/Lossless/BackwardReferenceEncoder.cs

@ -639,7 +639,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
{
// Figure out if we should use the offset/length from the previous pixel
// as an initial guess and therefore only inspect the offsets in windowOffsetsNew[].
bool usePrev = bestLengthPrev > 1 && bestLengthPrev < MaxLength;
bool usePrev = bestLengthPrev is > 1 and < MaxLength;
int numInd = usePrev ? windowOffsetsNewSize : windowOffsetsSize;
bestLength = usePrev ? bestLengthPrev - 1 : 0;
bestOffset = usePrev ? bestOffsetPrev : 0;

211
src/ImageSharp/Formats/WebP/Lossless/LosslessUtils.cs

@ -5,7 +5,6 @@ using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
#if SUPPORTS_RUNTIME_INTRINSICS
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
@ -67,30 +66,26 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
{
if (distance < PrefixLookupIdxMax)
{
(int code, int extraBits) prefixCode = WebpLookupTables.PrefixEncodeCode[distance];
extraBits = prefixCode.extraBits;
return prefixCode.code;
}
else
{
return PrefixEncodeBitsNoLut(distance, ref extraBits);
(int Code, int ExtraBits) prefixCode = WebpLookupTables.PrefixEncodeCode[distance];
extraBits = prefixCode.ExtraBits;
return prefixCode.Code;
}
return PrefixEncodeBitsNoLut(distance, ref extraBits);
}
public static int PrefixEncode(int distance, ref int extraBits, ref int extraBitsValue)
{
if (distance < PrefixLookupIdxMax)
{
(int code, int extraBits) prefixCode = WebpLookupTables.PrefixEncodeCode[distance];
extraBits = prefixCode.extraBits;
(int Code, int ExtraBits) prefixCode = WebpLookupTables.PrefixEncodeCode[distance];
extraBits = prefixCode.ExtraBits;
extraBitsValue = WebpLookupTables.PrefixEncodeExtraBitsValue[distance];
return prefixCode.code;
}
else
{
return PrefixEncodeNoLut(distance, ref extraBits, ref extraBitsValue);
return prefixCode.Code;
}
return PrefixEncodeNoLut(distance, ref extraBits, ref extraBitsValue);
}
/// <summary>
@ -402,9 +397,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
var maskalphagreen = Vector128.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255);
var maskredblue = Vector128.Create(255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0);
byte shufflemask = SimdUtils.Shuffle.MmShuffle(2, 2, 0, 0);
int idx;
fixed (uint* src = data)
{
int idx;
for (idx = 0; idx + 4 <= numPixels; idx += 4)
{
uint* pos = src + idx;
@ -535,104 +530,106 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
Span<uint> outputSpan)
{
fixed (uint* inputFixed = pixelData)
fixed (uint* outputFixed = outputSpan)
{
uint* input = inputFixed;
uint* output = outputFixed;
int width = transform.XSize;
Span<uint> transformData = transform.Data.GetSpan();
// First Row follows the L (mode=1) mode.
PredictorAdd0(input, 1, output);
PredictorAdd1(input + 1, width - 1, output + 1);
input += width;
output += width;
int y = 1;
int yEnd = transform.YSize;
int tileWidth = 1 << transform.Bits;
int mask = tileWidth - 1;
int tilesPerRow = SubSampleSize(width, transform.Bits);
int predictorModeIdxBase = (y >> transform.Bits) * tilesPerRow;
while (y < yEnd)
fixed (uint* outputFixed = outputSpan)
{
int predictorModeIdx = predictorModeIdxBase;
int x = 1;
uint* input = inputFixed;
uint* output = outputFixed;
// First pixel follows the T (mode=2) mode.
PredictorAdd2(input, output - width, 1, output);
int width = transform.XSize;
Span<uint> transformData = transform.Data.GetSpan();
// .. the rest:
while (x < width)
// First Row follows the L (mode=1) mode.
PredictorAdd0(input, 1, output);
PredictorAdd1(input + 1, width - 1, output + 1);
input += width;
output += width;
int y = 1;
int yEnd = transform.YSize;
int tileWidth = 1 << transform.Bits;
int mask = tileWidth - 1;
int tilesPerRow = SubSampleSize(width, transform.Bits);
int predictorModeIdxBase = (y >> transform.Bits) * tilesPerRow;
while (y < yEnd)
{
uint predictorMode = (transformData[predictorModeIdx++] >> 8) & 0xf;
int xEnd = (x & ~mask) + tileWidth;
if (xEnd > width)
{
xEnd = width;
}
int predictorModeIdx = predictorModeIdxBase;
int x = 1;
// First pixel follows the T (mode=2) mode.
PredictorAdd2(input, output - width, 1, output);
// There are 14 different prediction modes.
// In each prediction mode, the current pixel value is predicted from one
// or more neighboring pixels whose values are already known.
switch (predictorMode)
// .. the rest:
while (x < width)
{
case 0:
PredictorAdd0(input + x, xEnd - x, output + x);
break;
case 1:
PredictorAdd1(input + x, xEnd - x, output + x);
break;
case 2:
PredictorAdd2(input + x, output + x - width, xEnd - x, output + x);
break;
case 3:
PredictorAdd3(input + x, output + x - width, xEnd - x, output + x);
break;
case 4:
PredictorAdd4(input + x, output + x - width, xEnd - x, output + x);
break;
case 5:
PredictorAdd5(input + x, output + x - width, xEnd - x, output + x);
break;
case 6:
PredictorAdd6(input + x, output + x - width, xEnd - x, output + x);
break;
case 7:
PredictorAdd7(input + x, output + x - width, xEnd - x, output + x);
break;
case 8:
PredictorAdd8(input + x, output + x - width, xEnd - x, output + x);
break;
case 9:
PredictorAdd9(input + x, output + x - width, xEnd - x, output + x);
break;
case 10:
PredictorAdd10(input + x, output + x - width, xEnd - x, output + x);
break;
case 11:
PredictorAdd11(input + x, output + x - width, xEnd - x, output + x);
break;
case 12:
PredictorAdd12(input + x, output + x - width, xEnd - x, output + x);
break;
case 13:
PredictorAdd13(input + x, output + x - width, xEnd - x, output + x);
break;
uint predictorMode = (transformData[predictorModeIdx++] >> 8) & 0xf;
int xEnd = (x & ~mask) + tileWidth;
if (xEnd > width)
{
xEnd = width;
}
// There are 14 different prediction modes.
// In each prediction mode, the current pixel value is predicted from one
// or more neighboring pixels whose values are already known.
switch (predictorMode)
{
case 0:
PredictorAdd0(input + x, xEnd - x, output + x);
break;
case 1:
PredictorAdd1(input + x, xEnd - x, output + x);
break;
case 2:
PredictorAdd2(input + x, output + x - width, xEnd - x, output + x);
break;
case 3:
PredictorAdd3(input + x, output + x - width, xEnd - x, output + x);
break;
case 4:
PredictorAdd4(input + x, output + x - width, xEnd - x, output + x);
break;
case 5:
PredictorAdd5(input + x, output + x - width, xEnd - x, output + x);
break;
case 6:
PredictorAdd6(input + x, output + x - width, xEnd - x, output + x);
break;
case 7:
PredictorAdd7(input + x, output + x - width, xEnd - x, output + x);
break;
case 8:
PredictorAdd8(input + x, output + x - width, xEnd - x, output + x);
break;
case 9:
PredictorAdd9(input + x, output + x - width, xEnd - x, output + x);
break;
case 10:
PredictorAdd10(input + x, output + x - width, xEnd - x, output + x);
break;
case 11:
PredictorAdd11(input + x, output + x - width, xEnd - x, output + x);
break;
case 12:
PredictorAdd12(input + x, output + x - width, xEnd - x, output + x);
break;
case 13:
PredictorAdd13(input + x, output + x - width, xEnd - x, output + x);
break;
}
x = xEnd;
}
x = xEnd;
}
input += width;
output += width;
++y;
input += width;
output += width;
++y;
if ((y & mask) == 0)
{
// Use the same mask, since tiles are squares.
predictorModeIdxBase += tilesPerRow;
if ((y & mask) == 0)
{
// Use the same mask, since tiles are squares.
predictorModeIdxBase += tilesPerRow;
}
}
}
}
@ -839,10 +836,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
return (float)log2;
}
else
{
return (float)(Log2Reciprocal * Math.Log(v));
}
return (float)(Log2Reciprocal * Math.Log(v));
}
/// <summary>

4
src/ImageSharp/Formats/WebP/Lossless/NearLosslessEnc.cs

@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
// Adjusts pixel values of image with given maximum error.
private static void NearLossless(int xSize, int ySize, Span<uint> argbSrc, int stride, int limitBits, Span<uint> copyBuffer, Span<uint> argbDst)
{
int x, y;
int y;
int limit = 1 << limitBits;
Span<uint> prevRow = copyBuffer;
Span<uint> currRow = copyBuffer.Slice(xSize, xSize);
@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
argbSrc.Slice(srcOffset + stride, xSize).CopyTo(nextRow);
argbDst[dstOffset] = argbSrc[srcOffset];
argbDst[dstOffset + xSize - 1] = argbSrc[srcOffset + xSize - 1];
for (x = 1; x < xSize - 1; ++x)
for (int x = 1; x < xSize - 1; ++x)
{
if (IsSmooth(prevRow, currRow, nextRow, x, limit))
{

10
src/ImageSharp/Formats/WebP/Lossless/PredictorEncoder.cs

@ -341,6 +341,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
else
{
#pragma warning disable SA1503 // Braces should not be omitted
fixed (uint* currentRow = currentRowSpan)
fixed (uint* upperRow = upperRowSpan)
{
@ -454,6 +455,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
}
}
#pragma warning restore SA1503 // Braces should not be omitted
/// <summary>
/// Quantize every component of the difference between the actual pixel value and
@ -478,7 +480,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
quantization >>= 1;
}
if (value >> 24 == 0 || value >> 24 == 0xff)
if (value >> 24 is 0 or 0xff)
{
// Preserve transparency of fully transparent or fully opaque pixels.
a = NearLosslessDiff((byte)((value >> 24) & 0xff), (byte)((predict >> 24) & 0xff));
@ -649,6 +651,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
Span<uint> upperSpan,
Span<uint> outputSpan)
{
#pragma warning disable SA1503 // Braces should not be omitted
fixed (uint* current = currentSpan)
fixed (uint* upper = upperSpan)
fixed (uint* outputFixed = outputSpan)
@ -727,6 +730,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
}
}
#pragma warning restore SA1503 // Braces should not be omitted
private static void MaxDiffsForRow(int width, int stride, Span<uint> argb, int offset, Span<byte> maxDiffs, bool usedSubtractGreen)
{
@ -990,6 +994,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
for (int y = 0; y < tileHeight; ++y)
{
Span<uint> srcSpan = bgra.Slice(y * stride);
#pragma warning disable SA1503 // Braces should not be omitted
fixed (uint* src = srcSpan)
fixed (ushort* dst = values)
{
@ -1016,6 +1021,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
}
}
#pragma warning restore SA1503 // Braces should not be omitted
int leftOver = tileWidth & (span - 1);
if (leftOver > 0)
@ -1063,6 +1069,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
for (int y = 0; y < tileHeight; ++y)
{
Span<uint> srcSpan = bgra.Slice(y * stride);
#pragma warning disable SA1503 // Braces should not be omitted
fixed (uint* src = srcSpan)
fixed (ushort* dst = values)
{
@ -1092,6 +1099,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
}
}
#pragma warning restore SA1503 // Braces should not be omitted
int leftOver = tileWidth & (span - 1);
if (leftOver > 0)

3
src/ImageSharp/Formats/WebP/Lossless/WebpLosslessDecoder.cs

@ -6,7 +6,6 @@ using System.Buffers;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Webp.BitReader;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@ -147,7 +146,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
// Note: According to webpinfo color cache bits of 11 are valid, even though 10 is defined in the source code as maximum.
// That is why 11 bits is also considered valid here.
bool colorCacheBitsIsValid = colorCacheBits >= 1 && colorCacheBits <= WebpConstants.MaxColorCacheBits + 1;
bool colorCacheBitsIsValid = colorCacheBits is >= 1 and <= WebpConstants.MaxColorCacheBits + 1;
if (!colorCacheBitsIsValid)
{
WebpThrowHelper.ThrowImageFormatException("Invalid color cache bits found");

3
src/ImageSharp/Formats/WebP/Lossy/Vp8EncIterator.cs

@ -400,7 +400,6 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
{
byte[] modes = new byte[16];
int maxMode = MaxIntra4Mode;
int i4Alpha;
var totalHisto = new Vp8Histogram();
int curHisto = 0;
this.StartI4();
@ -433,7 +432,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
}
while (this.RotateI4(this.YuvIn.AsSpan(YOffEnc))); // Note: we reuse the original samples for predictors.
i4Alpha = totalHisto.GetAlpha();
var i4Alpha = totalHisto.GetAlpha();
if (i4Alpha > bestAlpha)
{
this.SetIntra4Mode(modes);

1
src/ImageSharp/Formats/WebP/Lossy/Vp8Encoder.cs

@ -5,7 +5,6 @@ using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats.Webp.BitWriter;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

1
src/ImageSharp/Formats/WebP/Lossy/WebpLossyDecoder.cs

@ -5,7 +5,6 @@ using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Webp.BitReader;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

22
src/ImageSharp/Formats/WebP/Lossy/YuvConversion.cs

@ -140,17 +140,20 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
GammaToLinear(bgra0.R) +
GammaToLinear(bgra1.R) +
GammaToLinear(bgra2.R) +
GammaToLinear(bgra3.R), 0);
GammaToLinear(bgra3.R),
0);
dst[dstIdx + 1] = (ushort)LinearToGamma(
GammaToLinear(bgra0.G) +
GammaToLinear(bgra1.G) +
GammaToLinear(bgra2.G) +
GammaToLinear(bgra3.G), 0);
GammaToLinear(bgra3.G),
0);
dst[dstIdx + 2] = (ushort)LinearToGamma(
GammaToLinear(bgra0.B) +
GammaToLinear(bgra1.B) +
GammaToLinear(bgra2.B) +
GammaToLinear(bgra3.B), 0);
GammaToLinear(bgra3.B),
0);
}
if ((width & 1) != 0)
@ -178,23 +181,26 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
Bgra32 bgra3 = nextRowSpan[j + 1];
uint a = (uint)(bgra0.A + bgra1.A + bgra2.A + bgra3.A);
int r, g, b;
if (a == 4 * 0xff || a == 0)
if (a is 4 * 0xff or 0)
{
r = (ushort)LinearToGamma(
GammaToLinear(bgra0.R) +
GammaToLinear(bgra1.R) +
GammaToLinear(bgra2.R) +
GammaToLinear(bgra3.R), 0);
GammaToLinear(bgra3.R),
0);
g = (ushort)LinearToGamma(
GammaToLinear(bgra0.G) +
GammaToLinear(bgra1.G) +
GammaToLinear(bgra2.G) +
GammaToLinear(bgra3.G), 0);
GammaToLinear(bgra3.G),
0);
b = (ushort)LinearToGamma(
GammaToLinear(bgra0.B) +
GammaToLinear(bgra1.B) +
GammaToLinear(bgra2.B) +
GammaToLinear(bgra3.B), 0);
GammaToLinear(bgra3.B),
0);
}
else
{
@ -215,7 +221,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
bgra1 = nextRowSpan[j];
uint a = (uint)(2u * (bgra0.A + bgra1.A));
int r, g, b;
if (a == 4 * 0xff || a == 0)
if (a is 4 * 0xff or 0)
{
r = (ushort)LinearToGamma(GammaToLinear(bgra0.R) + GammaToLinear(bgra1.R), 1);
g = (ushort)LinearToGamma(GammaToLinear(bgra0.G) + GammaToLinear(bgra1.G), 1);

1
src/ImageSharp/Formats/WebP/WebpDecoder.cs

@ -4,7 +4,6 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

2
src/ImageSharp/Formats/WebP/WebpLookupTables.cs

@ -974,7 +974,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
}
};
public static readonly (int code, int extraBits)[] PrefixEncodeCode = new (int code, int extraBits)[]
public static readonly (int Code, int ExtraBits)[] PrefixEncodeCode =
{
(0, 0), (0, 0), (1, 0), (2, 0), (3, 0), (4, 1), (4, 1), (5, 1),
(5, 1), (6, 2), (6, 2), (6, 2), (6, 2), (7, 2), (7, 2), (7, 2),

7
tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs

@ -2,13 +2,10 @@
// Licensed under the Apache License, Version 2.0.
using System.IO;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
using Xunit;
using static SixLabors.ImageSharp.Tests.TestImages.WebP;
// ReSharper disable InconsistentNaming
@ -18,9 +15,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
[Trait("Format", "Webp")]
public class WebpDecoderTests
{
private static WebpDecoder WebpDecoder => new WebpDecoder();
private static WebpDecoder WebpDecoder => new();
private static MagickReferenceDecoder ReferenceDecoder => new MagickReferenceDecoder();
private static MagickReferenceDecoder ReferenceDecoder => new();
[Theory]
[InlineData(Lossless.GreenTransform1, 1000, 307, 32)]

Loading…
Cancel
Save