Browse Source

Use pattern matching

pull/1552/head
Brian Popow 5 years ago
parent
commit
4ec66a2762
  1. 4
      src/ImageSharp/Formats/Webp/AlphaDecoder.cs
  2. 2
      src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs
  3. 15
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

4
src/ImageSharp/Formats/Webp/AlphaDecoder.cs

@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
int totalPixels = width * height; int totalPixels = width * height;
var compression = (WebpAlphaCompressionMethod)(alphaChunkHeader & 0x03); var compression = (WebpAlphaCompressionMethod)(alphaChunkHeader & 0x03);
if (compression != WebpAlphaCompressionMethod.NoCompression && compression != WebpAlphaCompressionMethod.WebpLosslessCompression) if (compression is not WebpAlphaCompressionMethod.NoCompression and not WebpAlphaCompressionMethod.WebpLosslessCompression)
{ {
WebpThrowHelper.ThrowImageFormatException($"unexpected alpha compression method {compression} found"); WebpThrowHelper.ThrowImageFormatException($"unexpected alpha compression method {compression} found");
} }
@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Formats.Webp
// The filtering method used. Only values between 0 and 3 are valid. // The filtering method used. Only values between 0 and 3 are valid.
int filter = (alphaChunkHeader >> 2) & 0x03; int filter = (alphaChunkHeader >> 2) & 0x03;
if (filter < (int)WebpAlphaFilterType.None || filter > (int)WebpAlphaFilterType.Gradient) if (filter is < (int)WebpAlphaFilterType.None or > (int)WebpAlphaFilterType.Gradient)
{ {
WebpThrowHelper.ThrowImageFormatException($"unexpected alpha filter method {filter} found"); WebpThrowHelper.ThrowImageFormatException($"unexpected alpha filter method {filter} found");
} }

2
src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs

@ -221,7 +221,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
public void PutI16Mode(int mode) public void PutI16Mode(int mode)
{ {
if (this.PutBit(mode == TM_PRED || mode == H_PRED, 156)) if (this.PutBit(mode is TM_PRED or H_PRED, 156))
{ {
this.PutBit(mode == TM_PRED, 128); // TM or HE this.PutBit(mode == TM_PRED, 128); // TM or HE
} }

15
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

@ -289,12 +289,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
{ {
bgra.CopyTo(encodedData); bgra.CopyTo(encodedData);
bool useCache = true; bool useCache = true;
this.UsePalette = crunchConfig.EntropyIdx == EntropyIx.Palette || this.UsePalette = crunchConfig.EntropyIdx is EntropyIx.Palette or EntropyIx.PaletteAndSpatial;
crunchConfig.EntropyIdx == EntropyIx.PaletteAndSpatial; this.UseSubtractGreenTransform = crunchConfig.EntropyIdx is EntropyIx.SubGreen or EntropyIx.SpatialSubGreen;
this.UseSubtractGreenTransform = crunchConfig.EntropyIdx == EntropyIx.SubGreen || this.UsePredictorTransform = crunchConfig.EntropyIdx is EntropyIx.Spatial or EntropyIx.SpatialSubGreen;
crunchConfig.EntropyIdx == EntropyIx.SpatialSubGreen;
this.UsePredictorTransform = crunchConfig.EntropyIdx == EntropyIx.Spatial ||
crunchConfig.EntropyIdx == EntropyIx.SpatialSubGreen;
if (lowEffort) if (lowEffort)
{ {
this.UseCrossColorTransform = false; this.UseCrossColorTransform = false;
@ -427,7 +424,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
this.TransformBits = GetTransformBits(this.method, this.HistoBits); this.TransformBits = GetTransformBits(this.method, this.HistoBits);
// Try out multiple LZ77 on images with few colors. // Try out multiple LZ77 on images with few colors.
int nlz77s = this.PaletteSize > 0 && this.PaletteSize <= 16 ? 2 : 1; int nlz77s = this.PaletteSize is > 0 and <= 16 ? 2 : 1;
EntropyIx entropyIdx = this.AnalyzeEntropy(bgra, width, height, usePalette, this.PaletteSize, this.TransformBits, out redAndBlueAlwaysZero); EntropyIx entropyIdx = this.AnalyzeEntropy(bgra, width, height, usePalette, this.PaletteSize, this.TransformBits, out redAndBlueAlwaysZero);
bool doNotCache = false; bool doNotCache = false;
@ -863,7 +860,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
while (i-- > 0) while (i-- > 0)
{ {
int ix = tokens[i].Code; int ix = tokens[i].Code;
if (ix == 0 || ix == 17 || ix == 18) if (ix is 0 or 17 or 18)
{ {
trimmedLength--; // Discount trailing zeros. trimmedLength--; // Discount trailing zeros.
trailingZeroBits += codeLengthBitDepth[ix]; trailingZeroBits += codeLengthBitDepth[ix];
@ -1345,7 +1342,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
} }
} }
if (i == 0 || i == 1 || i == 2) if (i is 0 or 1 or 2)
{ {
ApplyPaletteFor(width, height, palette, i, src, srcStride, dst, dstStride, tmpRow, buffer, xBits); ApplyPaletteFor(width, height, palette, i, src, srcStride, dst, dstStride, tmpRow, buffer, xBits);
} }

Loading…
Cancel
Save