From 4ec66a27625eb104a0bca30cafbe4c44c239f7db Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 20 Oct 2021 18:19:57 +0200 Subject: [PATCH] Use pattern matching --- src/ImageSharp/Formats/Webp/AlphaDecoder.cs | 4 ++-- .../Formats/Webp/BitWriter/Vp8BitWriter.cs | 2 +- .../Formats/Webp/Lossless/Vp8LEncoder.cs | 15 ++++++--------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/AlphaDecoder.cs b/src/ImageSharp/Formats/Webp/AlphaDecoder.cs index c2a6a261f..e63cd27b5 100644 --- a/src/ImageSharp/Formats/Webp/AlphaDecoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaDecoder.cs @@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.Formats.Webp int totalPixels = width * height; 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"); } @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Formats.Webp // The filtering method used. Only values between 0 and 3 are valid. 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"); } diff --git a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs index 76766f67e..7628247fd 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs @@ -221,7 +221,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter 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 } diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index c95b8e362..2ebd3a270 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -289,12 +289,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless { bgra.CopyTo(encodedData); bool useCache = true; - this.UsePalette = crunchConfig.EntropyIdx == EntropyIx.Palette || - crunchConfig.EntropyIdx == EntropyIx.PaletteAndSpatial; - this.UseSubtractGreenTransform = crunchConfig.EntropyIdx == EntropyIx.SubGreen || - crunchConfig.EntropyIdx == EntropyIx.SpatialSubGreen; - this.UsePredictorTransform = crunchConfig.EntropyIdx == EntropyIx.Spatial || - crunchConfig.EntropyIdx == EntropyIx.SpatialSubGreen; + this.UsePalette = crunchConfig.EntropyIdx is EntropyIx.Palette or EntropyIx.PaletteAndSpatial; + this.UseSubtractGreenTransform = crunchConfig.EntropyIdx is EntropyIx.SubGreen or EntropyIx.SpatialSubGreen; + this.UsePredictorTransform = crunchConfig.EntropyIdx is EntropyIx.Spatial or EntropyIx.SpatialSubGreen; if (lowEffort) { this.UseCrossColorTransform = false; @@ -427,7 +424,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless this.TransformBits = GetTransformBits(this.method, this.HistoBits); // 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); bool doNotCache = false; @@ -863,7 +860,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless while (i-- > 0) { int ix = tokens[i].Code; - if (ix == 0 || ix == 17 || ix == 18) + if (ix is 0 or 17 or 18) { trimmedLength--; // Discount trailing zeros. 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); }