From 7103e28d9002071c1fb46007192c3725bbcd1564 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 30 May 2024 19:59:21 +1000 Subject: [PATCH] Cleanup --- src/ImageSharp/Formats/Bmp/BmpMetadata.cs | 100 +++++++++++----------- src/ImageSharp/Formats/Gif/GifMetadata.cs | 16 ++-- src/ImageSharp/Formats/Pbm/PbmMetadata.cs | 15 ++-- 3 files changed, 66 insertions(+), 65 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpMetadata.cs b/src/ImageSharp/Formats/Bmp/BmpMetadata.cs index 00c5910d4b..bb6b87e585 100644 --- a/src/ImageSharp/Formats/Bmp/BmpMetadata.cs +++ b/src/ImageSharp/Formats/Bmp/BmpMetadata.cs @@ -42,37 +42,25 @@ public class BmpMetadata : IFormatMetadata, IFormatFrameMetadata new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel1 }, + 2 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel2 }, + <= 4 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel4 }, + <= 8 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel8 }, + <= 16 => new BmpMetadata + { + BitsPerPixel = BmpBitsPerPixel.Pixel16, InfoHeaderType = BmpInfoHeaderType.WinVersion3 + }, + <= 24 => new BmpMetadata + { + BitsPerPixel = BmpBitsPerPixel.Pixel24, InfoHeaderType = BmpInfoHeaderType.WinVersion4 + }, + _ => new BmpMetadata + { + BitsPerPixel = BmpBitsPerPixel.Pixel32, InfoHeaderType = BmpInfoHeaderType.WinVersion5 + } + }; } /// @@ -97,30 +85,42 @@ public class BmpMetadata : IFormatMetadata, IFormatFrameMetadata bpp < 32 ? PixelAlphaRepresentation.None : PixelAlphaRepresentation.Unassociated }; - PixelComponentInfo info = this.BitsPerPixel switch + PixelComponentInfo info; + PixelColorType color; + switch (this.BitsPerPixel) { - BmpBitsPerPixel.Pixel1 => PixelComponentInfo.Create(1, bpp, 1), - BmpBitsPerPixel.Pixel2 => PixelComponentInfo.Create(1, bpp, 2), - BmpBitsPerPixel.Pixel4 => PixelComponentInfo.Create(1, bpp, 4), - BmpBitsPerPixel.Pixel8 => PixelComponentInfo.Create(1, bpp, 8), + case BmpBitsPerPixel.Pixel1: + info = PixelComponentInfo.Create(1, bpp, 1); + color = PixelColorType.Indexed; + break; + case BmpBitsPerPixel.Pixel2: + info = PixelComponentInfo.Create(1, bpp, 2); + color = PixelColorType.Indexed; + break; + case BmpBitsPerPixel.Pixel4: + info = PixelComponentInfo.Create(1, bpp, 4); + color = PixelColorType.Indexed; + break; + case BmpBitsPerPixel.Pixel8: + info = PixelComponentInfo.Create(1, bpp, 8); + color = PixelColorType.Indexed; + break; // Could be 555 with padding but 565 is more common in newer bitmaps and offers // greater accuracy due to extra green precision. - BmpBitsPerPixel.Pixel16 => PixelComponentInfo.Create(3, bpp, 5, 6, 5), - BmpBitsPerPixel.Pixel24 => PixelComponentInfo.Create(3, bpp, 8, 8, 8), - BmpBitsPerPixel.Pixel32 or _ => PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8), - }; - - PixelColorType color = this.BitsPerPixel switch - { - BmpBitsPerPixel.Pixel1 or - BmpBitsPerPixel.Pixel2 or - BmpBitsPerPixel.Pixel4 or - BmpBitsPerPixel.Pixel8 => PixelColorType.Indexed, - BmpBitsPerPixel.Pixel16 or - BmpBitsPerPixel.Pixel24 => PixelColorType.RGB, - BmpBitsPerPixel.Pixel32 or _ => PixelColorType.RGB | PixelColorType.Alpha, - }; + case BmpBitsPerPixel.Pixel16: + info = PixelComponentInfo.Create(3, bpp, 5, 6, 5); + color = PixelColorType.RGB; + break; + case BmpBitsPerPixel.Pixel24: + info = PixelComponentInfo.Create(3, bpp, 8, 8, 8); + color = PixelColorType.RGB; + break; + case BmpBitsPerPixel.Pixel32 or _: + info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8); + color = PixelColorType.RGB | PixelColorType.Alpha; + break; + } return new() { diff --git a/src/ImageSharp/Formats/Gif/GifMetadata.cs b/src/ImageSharp/Formats/Gif/GifMetadata.cs index 9a234aa9ba..64c7edbaf5 100644 --- a/src/ImageSharp/Formats/Gif/GifMetadata.cs +++ b/src/ImageSharp/Formats/Gif/GifMetadata.cs @@ -78,11 +78,13 @@ public class GifMetadata : IFormatMetadata ReadOnlySpan colorTable = metadata.ColorTable.Value.Span; for (int i = 0; i < colorTable.Length; i++) { - if (background == colorTable[i]) + if (background != colorTable[i]) { - index = i; - break; + continue; } + + index = i; + break; } } @@ -105,11 +107,13 @@ public class GifMetadata : IFormatMetadata ReadOnlySpan colorTable = metadata.ColorTable.Value.Span; for (int i = 0; i < colorTable.Length; i++) { - if (background == colorTable[i]) + if (background != colorTable[i]) { - index = i; - break; + continue; } + + index = i; + break; } } diff --git a/src/ImageSharp/Formats/Pbm/PbmMetadata.cs b/src/ImageSharp/Formats/Pbm/PbmMetadata.cs index 4e6b6f2650..49a13d5c33 100644 --- a/src/ImageSharp/Formats/Pbm/PbmMetadata.cs +++ b/src/ImageSharp/Formats/Pbm/PbmMetadata.cs @@ -40,7 +40,7 @@ public class PbmMetadata : IFormatMetadata /// /// Gets or sets the data type of the pixel components. /// - public PbmComponentType ComponentType { get; set; } = PbmComponentType.Byte; + public PbmComponentType ComponentType { get; set; } /// public static PbmMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) @@ -69,16 +69,13 @@ public class PbmMetadata : IFormatMetadata break; } - PbmComponentType componentType = PbmComponentType.Short; int bpp = metadata.PixelTypeInfo.BitsPerPixel; - if (bpp == 1) + PbmComponentType componentType = bpp switch { - componentType = PbmComponentType.Bit; - } - else if (bpp <= 8) - { - componentType = PbmComponentType.Byte; - } + 1 => PbmComponentType.Bit, + <= 8 => PbmComponentType.Byte, + _ => PbmComponentType.Short + }; return new PbmMetadata {