Skip to content

Commit 7103e28

Browse files
Cleanup
1 parent 4f6bd17 commit 7103e28

3 files changed

Lines changed: 66 additions & 65 deletions

File tree

src/ImageSharp/Formats/Bmp/BmpMetadata.cs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,25 @@ private BmpMetadata(BmpMetadata other)
4242
public static BmpMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
4343
{
4444
int bpp = metadata.PixelTypeInfo.BitsPerPixel;
45-
if (bpp == 1)
45+
return bpp switch
4646
{
47-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel1 };
48-
}
49-
50-
if (bpp == 2)
51-
{
52-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel2 };
53-
}
54-
55-
if (bpp <= 4)
56-
{
57-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel4 };
58-
}
59-
60-
if (bpp <= 8)
61-
{
62-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel8 };
63-
}
64-
65-
if (bpp <= 16)
66-
{
67-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel16, InfoHeaderType = BmpInfoHeaderType.WinVersion3 };
68-
}
69-
70-
if (bpp <= 24)
71-
{
72-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel24, InfoHeaderType = BmpInfoHeaderType.WinVersion4 };
73-
}
74-
75-
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel32, InfoHeaderType = BmpInfoHeaderType.WinVersion5 };
47+
1 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel1 },
48+
2 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel2 },
49+
<= 4 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel4 },
50+
<= 8 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel8 },
51+
<= 16 => new BmpMetadata
52+
{
53+
BitsPerPixel = BmpBitsPerPixel.Pixel16, InfoHeaderType = BmpInfoHeaderType.WinVersion3
54+
},
55+
<= 24 => new BmpMetadata
56+
{
57+
BitsPerPixel = BmpBitsPerPixel.Pixel24, InfoHeaderType = BmpInfoHeaderType.WinVersion4
58+
},
59+
_ => new BmpMetadata
60+
{
61+
BitsPerPixel = BmpBitsPerPixel.Pixel32, InfoHeaderType = BmpInfoHeaderType.WinVersion5
62+
}
63+
};
7664
}
7765

7866
/// <inheritdoc/>
@@ -97,30 +85,42 @@ BmpInfoHeaderType.WinVersion5 or
9785
_ => bpp < 32 ? PixelAlphaRepresentation.None : PixelAlphaRepresentation.Unassociated
9886
};
9987

100-
PixelComponentInfo info = this.BitsPerPixel switch
88+
PixelComponentInfo info;
89+
PixelColorType color;
90+
switch (this.BitsPerPixel)
10191
{
102-
BmpBitsPerPixel.Pixel1 => PixelComponentInfo.Create(1, bpp, 1),
103-
BmpBitsPerPixel.Pixel2 => PixelComponentInfo.Create(1, bpp, 2),
104-
BmpBitsPerPixel.Pixel4 => PixelComponentInfo.Create(1, bpp, 4),
105-
BmpBitsPerPixel.Pixel8 => PixelComponentInfo.Create(1, bpp, 8),
92+
case BmpBitsPerPixel.Pixel1:
93+
info = PixelComponentInfo.Create(1, bpp, 1);
94+
color = PixelColorType.Indexed;
95+
break;
96+
case BmpBitsPerPixel.Pixel2:
97+
info = PixelComponentInfo.Create(1, bpp, 2);
98+
color = PixelColorType.Indexed;
99+
break;
100+
case BmpBitsPerPixel.Pixel4:
101+
info = PixelComponentInfo.Create(1, bpp, 4);
102+
color = PixelColorType.Indexed;
103+
break;
104+
case BmpBitsPerPixel.Pixel8:
105+
info = PixelComponentInfo.Create(1, bpp, 8);
106+
color = PixelColorType.Indexed;
107+
break;
106108

107109
// Could be 555 with padding but 565 is more common in newer bitmaps and offers
108110
// greater accuracy due to extra green precision.
109-
BmpBitsPerPixel.Pixel16 => PixelComponentInfo.Create(3, bpp, 5, 6, 5),
110-
BmpBitsPerPixel.Pixel24 => PixelComponentInfo.Create(3, bpp, 8, 8, 8),
111-
BmpBitsPerPixel.Pixel32 or _ => PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8),
112-
};
113-
114-
PixelColorType color = this.BitsPerPixel switch
115-
{
116-
BmpBitsPerPixel.Pixel1 or
117-
BmpBitsPerPixel.Pixel2 or
118-
BmpBitsPerPixel.Pixel4 or
119-
BmpBitsPerPixel.Pixel8 => PixelColorType.Indexed,
120-
BmpBitsPerPixel.Pixel16 or
121-
BmpBitsPerPixel.Pixel24 => PixelColorType.RGB,
122-
BmpBitsPerPixel.Pixel32 or _ => PixelColorType.RGB | PixelColorType.Alpha,
123-
};
111+
case BmpBitsPerPixel.Pixel16:
112+
info = PixelComponentInfo.Create(3, bpp, 5, 6, 5);
113+
color = PixelColorType.RGB;
114+
break;
115+
case BmpBitsPerPixel.Pixel24:
116+
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
117+
color = PixelColorType.RGB;
118+
break;
119+
case BmpBitsPerPixel.Pixel32 or _:
120+
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
121+
color = PixelColorType.RGB | PixelColorType.Alpha;
122+
break;
123+
}
124124

125125
return new()
126126
{

src/ImageSharp/Formats/Gif/GifMetadata.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ internal static GifMetadata FromAnimatedMetadata(AnimatedImageMetadata metadata)
7878
ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span;
7979
for (int i = 0; i < colorTable.Length; i++)
8080
{
81-
if (background == colorTable[i])
81+
if (background != colorTable[i])
8282
{
83-
index = i;
84-
break;
83+
continue;
8584
}
85+
86+
index = i;
87+
break;
8688
}
8789
}
8890

@@ -105,11 +107,13 @@ public static GifMetadata FromFormatConnectingMetadata(FormatConnectingMetadata
105107
ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span;
106108
for (int i = 0; i < colorTable.Length; i++)
107109
{
108-
if (background == colorTable[i])
110+
if (background != colorTable[i])
109111
{
110-
index = i;
111-
break;
112+
continue;
112113
}
114+
115+
index = i;
116+
break;
113117
}
114118
}
115119

src/ImageSharp/Formats/Pbm/PbmMetadata.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private PbmMetadata(PbmMetadata other)
4040
/// <summary>
4141
/// Gets or sets the data type of the pixel components.
4242
/// </summary>
43-
public PbmComponentType ComponentType { get; set; } = PbmComponentType.Byte;
43+
public PbmComponentType ComponentType { get; set; }
4444

4545
/// <inheritdoc/>
4646
public static PbmMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
@@ -69,16 +69,13 @@ public static PbmMetadata FromFormatConnectingMetadata(FormatConnectingMetadata
6969
break;
7070
}
7171

72-
PbmComponentType componentType = PbmComponentType.Short;
7372
int bpp = metadata.PixelTypeInfo.BitsPerPixel;
74-
if (bpp == 1)
73+
PbmComponentType componentType = bpp switch
7574
{
76-
componentType = PbmComponentType.Bit;
77-
}
78-
else if (bpp <= 8)
79-
{
80-
componentType = PbmComponentType.Byte;
81-
}
75+
1 => PbmComponentType.Bit,
76+
<= 8 => PbmComponentType.Byte,
77+
_ => PbmComponentType.Short
78+
};
8279

8380
return new PbmMetadata
8481
{

0 commit comments

Comments
 (0)