Browse Source

Make BmpFileHeader readonly

af/merge-core
Jason Nelson 8 years ago
parent
commit
a12c7c06d5
  1. 13
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 18
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  3. 28
      src/ImageSharp/Formats/Bmp/BmpFileHeader.cs

13
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -581,13 +581,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.currentStream.Read(data, 0, BmpFileHeader.Size);
this.fileHeader = new BmpFileHeader
{
Type = BitConverter.ToInt16(data, 0),
FileSize = BitConverter.ToInt32(data, 2),
Reserved = BitConverter.ToInt32(data, 6),
Offset = BitConverter.ToInt32(data, 10)
};
this.fileHeader = new BmpFileHeader(
type: BitConverter.ToInt16(data, 0),
fileSize: BitConverter.ToInt32(data, 2),
reserved: BitConverter.ToInt32(data, 6),
offset: BitConverter.ToInt32(data, 10)
);
}
/// <summary>

18
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -55,9 +55,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.padding = bytesPerLine - (image.Width * (int)this.bitsPerPixel);
// Do not use IDisposable pattern here as we want to preserve the stream.
EndianBinaryWriter writer = new EndianBinaryWriter(Endianness.LittleEndian, stream);
var writer = new EndianBinaryWriter(Endianness.LittleEndian, stream);
BmpInfoHeader infoHeader = new BmpInfoHeader
var infoHeader = new BmpInfoHeader
{
HeaderSize = BmpInfoHeader.BitmapInfoHeaderSize,
Height = image.Height,
@ -69,12 +69,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp
ClrImportant = 0
};
BmpFileHeader fileHeader = new BmpFileHeader
{
Type = 19778, // BM
Offset = 54,
FileSize = 54 + infoHeader.ImageSize
};
var fileHeader = new BmpFileHeader(
type: 19778, // BM
offset: 54,
reserved: 0,
fileSize: 54 + infoHeader.ImageSize
);
WriteHeader(writer, fileHeader);
this.WriteInfo(writer, infoHeader);
@ -92,7 +92,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="fileHeader">
/// The <see cref="BmpFileHeader"/> containing the header data.
/// </param>
private static void WriteHeader(EndianBinaryWriter writer, BmpFileHeader fileHeader)
private static void WriteHeader(EndianBinaryWriter writer, in BmpFileHeader fileHeader)
{
writer.Write(fileHeader.Type);
writer.Write(fileHeader.FileSize);

28
src/ImageSharp/Formats/Bmp/BmpFileHeader.cs

@ -13,35 +13,43 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// All of the other integer values are stored in little-endian format
/// (i.e. least-significant byte first).
/// </remarks>
internal sealed class BmpFileHeader
internal readonly struct BmpFileHeader
{
/// <summary>
/// Defines of the data structure in the bitmap file.
/// </summary>
public const int Size = 14;
public BmpFileHeader(short type, int fileSize, int reserved, int offset)
{
this.Type = type;
this.FileSize = fileSize;
this.Reserved = reserved;
this.Offset = offset;
}
/// <summary>
/// Gets or sets the Bitmap identifier.
/// Gets the Bitmap identifier.
/// The field used to identify the bitmap file: 0x42 0x4D
/// (Hex code points for B and M)
/// </summary>
public short Type { get; set; }
public short Type { get; }
/// <summary>
/// Gets or sets the size of the bitmap file in bytes.
/// Gets the size of the bitmap file in bytes.
/// </summary>
public int FileSize { get; set; }
public int FileSize { get; }
/// <summary>
/// Gets or sets any reserved data; actual value depends on the application
/// Gets any reserved data; actual value depends on the application
/// that creates the image.
/// </summary>
public int Reserved { get; set; }
public int Reserved { get; }
/// <summary>
/// Gets or sets the offset, i.e. starting address, of the byte where
/// Gets the offset, i.e. starting address, of the byte where
/// the bitmap data can be found.
/// </summary>
public int Offset { get; set; }
public int Offset { get; }
}
}
}
Loading…
Cancel
Save