Browse Source

Cleanup

pull/2751/head
James Jackson-South 2 years ago
parent
commit
7103e28d90
  1. 100
      src/ImageSharp/Formats/Bmp/BmpMetadata.cs
  2. 16
      src/ImageSharp/Formats/Gif/GifMetadata.cs
  3. 15
      src/ImageSharp/Formats/Pbm/PbmMetadata.cs

100
src/ImageSharp/Formats/Bmp/BmpMetadata.cs

@ -42,37 +42,25 @@ public class BmpMetadata : IFormatMetadata<BmpMetadata>, IFormatFrameMetadata<Bm
public static BmpMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) public static BmpMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
{ {
int bpp = metadata.PixelTypeInfo.BitsPerPixel; int bpp = metadata.PixelTypeInfo.BitsPerPixel;
if (bpp == 1) return bpp switch
{ {
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel1 }; 1 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel1 },
} 2 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel2 },
<= 4 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel4 },
if (bpp == 2) <= 8 => new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel8 },
{ <= 16 => new BmpMetadata
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel2 }; {
} BitsPerPixel = BmpBitsPerPixel.Pixel16, InfoHeaderType = BmpInfoHeaderType.WinVersion3
},
if (bpp <= 4) <= 24 => new BmpMetadata
{ {
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel4 }; BitsPerPixel = BmpBitsPerPixel.Pixel24, InfoHeaderType = BmpInfoHeaderType.WinVersion4
} },
_ => new BmpMetadata
if (bpp <= 8) {
{ BitsPerPixel = BmpBitsPerPixel.Pixel32, InfoHeaderType = BmpInfoHeaderType.WinVersion5
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel8 }; }
} };
if (bpp <= 16)
{
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel16, InfoHeaderType = BmpInfoHeaderType.WinVersion3 };
}
if (bpp <= 24)
{
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel24, InfoHeaderType = BmpInfoHeaderType.WinVersion4 };
}
return new BmpMetadata { BitsPerPixel = BmpBitsPerPixel.Pixel32, InfoHeaderType = BmpInfoHeaderType.WinVersion5 };
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -97,30 +85,42 @@ public class BmpMetadata : IFormatMetadata<BmpMetadata>, IFormatFrameMetadata<Bm
_ => bpp < 32 ? PixelAlphaRepresentation.None : PixelAlphaRepresentation.Unassociated _ => 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), case BmpBitsPerPixel.Pixel1:
BmpBitsPerPixel.Pixel2 => PixelComponentInfo.Create(1, bpp, 2), info = PixelComponentInfo.Create(1, bpp, 1);
BmpBitsPerPixel.Pixel4 => PixelComponentInfo.Create(1, bpp, 4), color = PixelColorType.Indexed;
BmpBitsPerPixel.Pixel8 => PixelComponentInfo.Create(1, bpp, 8), 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 // Could be 555 with padding but 565 is more common in newer bitmaps and offers
// greater accuracy due to extra green precision. // greater accuracy due to extra green precision.
BmpBitsPerPixel.Pixel16 => PixelComponentInfo.Create(3, bpp, 5, 6, 5), case BmpBitsPerPixel.Pixel16:
BmpBitsPerPixel.Pixel24 => PixelComponentInfo.Create(3, bpp, 8, 8, 8), info = PixelComponentInfo.Create(3, bpp, 5, 6, 5);
BmpBitsPerPixel.Pixel32 or _ => PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8), color = PixelColorType.RGB;
}; break;
case BmpBitsPerPixel.Pixel24:
PixelColorType color = this.BitsPerPixel switch info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
{ color = PixelColorType.RGB;
BmpBitsPerPixel.Pixel1 or break;
BmpBitsPerPixel.Pixel2 or case BmpBitsPerPixel.Pixel32 or _:
BmpBitsPerPixel.Pixel4 or info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
BmpBitsPerPixel.Pixel8 => PixelColorType.Indexed, color = PixelColorType.RGB | PixelColorType.Alpha;
BmpBitsPerPixel.Pixel16 or break;
BmpBitsPerPixel.Pixel24 => PixelColorType.RGB, }
BmpBitsPerPixel.Pixel32 or _ => PixelColorType.RGB | PixelColorType.Alpha,
};
return new() return new()
{ {

16
src/ImageSharp/Formats/Gif/GifMetadata.cs

@ -78,11 +78,13 @@ public class GifMetadata : IFormatMetadata<GifMetadata>
ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span; ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span;
for (int i = 0; i < colorTable.Length; i++) for (int i = 0; i < colorTable.Length; i++)
{ {
if (background == colorTable[i]) if (background != colorTable[i])
{ {
index = i; continue;
break;
} }
index = i;
break;
} }
} }
@ -105,11 +107,13 @@ public class GifMetadata : IFormatMetadata<GifMetadata>
ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span; ReadOnlySpan<Color> colorTable = metadata.ColorTable.Value.Span;
for (int i = 0; i < colorTable.Length; i++) for (int i = 0; i < colorTable.Length; i++)
{ {
if (background == colorTable[i]) if (background != colorTable[i])
{ {
index = i; continue;
break;
} }
index = i;
break;
} }
} }

15
src/ImageSharp/Formats/Pbm/PbmMetadata.cs

@ -40,7 +40,7 @@ public class PbmMetadata : IFormatMetadata<PbmMetadata>
/// <summary> /// <summary>
/// Gets or sets the data type of the pixel components. /// Gets or sets the data type of the pixel components.
/// </summary> /// </summary>
public PbmComponentType ComponentType { get; set; } = PbmComponentType.Byte; public PbmComponentType ComponentType { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public static PbmMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) public static PbmMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
@ -69,16 +69,13 @@ public class PbmMetadata : IFormatMetadata<PbmMetadata>
break; break;
} }
PbmComponentType componentType = PbmComponentType.Short;
int bpp = metadata.PixelTypeInfo.BitsPerPixel; int bpp = metadata.PixelTypeInfo.BitsPerPixel;
if (bpp == 1) PbmComponentType componentType = bpp switch
{ {
componentType = PbmComponentType.Bit; 1 => PbmComponentType.Bit,
} <= 8 => PbmComponentType.Byte,
else if (bpp <= 8) _ => PbmComponentType.Short
{ };
componentType = PbmComponentType.Byte;
}
return new PbmMetadata return new PbmMetadata
{ {

Loading…
Cancel
Save