diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
index e5431cf34f..2186444739 100644
--- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
@@ -336,7 +336,7 @@ namespace SixLabors.ImageSharp.Formats.Png
this.previousScanline?.Dispose();
}
- if (this.header == null)
+ if (this.header.Width == 0 && this.header.Height == 0)
{
throw new ImageFormatException("PNG Image does not contain a header chunk");
}
@@ -1157,16 +1157,15 @@ namespace SixLabors.ImageSharp.Formats.Png
/// The containing data.
private void ReadHeaderChunk(ReadOnlySpan data)
{
- this.header = new PngHeader
- {
- Width = BinaryPrimitives.ReadInt32BigEndian(data.Slice(0, 4)),
- Height = BinaryPrimitives.ReadInt32BigEndian(data.Slice(4, 4)),
- BitDepth = data[8],
- ColorType = (PngColorType)data[9],
- CompressionMethod = data[10],
- FilterMethod = data[11],
- InterlaceMethod = (PngInterlaceMode)data[12]
- };
+ this.header = new PngHeader(
+ width: BinaryPrimitives.ReadInt32BigEndian(data.Slice(0, 4)),
+ height: BinaryPrimitives.ReadInt32BigEndian(data.Slice(4, 4)),
+ bitDepth: data[8],
+ colorType: (PngColorType)data[9],
+ compressionMethod: data[10],
+ filterMethod: data[11],
+ interlaceMethod: (PngInterlaceMode)data[12]
+ );
}
///
diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
index 0cded1d80c..5f7cd9b23d 100644
--- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
@@ -212,16 +212,15 @@ namespace SixLabors.ImageSharp.Formats.Png
this.bytesPerPixel = this.CalculateBytesPerPixel();
- var header = new PngHeader
- {
- Width = image.Width,
- Height = image.Height,
- ColorType = this.pngColorType,
- BitDepth = this.bitDepth,
- FilterMethod = 0, // None
- CompressionMethod = 0,
- InterlaceMethod = 0
- };
+ var header = new PngHeader(
+ width: image.Width,
+ height: image.Height,
+ colorType: this.pngColorType,
+ bitDepth: this.bitDepth,
+ filterMethod: 0, // None
+ compressionMethod: 0,
+ interlaceMethod: 0
+ );
this.WriteHeaderChunk(stream, header);
@@ -415,7 +414,7 @@ namespace SixLabors.ImageSharp.Formats.Png
///
/// The containing image data.
/// The .
- private void WriteHeaderChunk(Stream stream, PngHeader header)
+ private void WriteHeaderChunk(Stream stream, in PngHeader header)
{
BinaryPrimitives.WriteInt32BigEndian(new Span(this.chunkDataBuffer, 0, 4), header.Width);
BinaryPrimitives.WriteInt32BigEndian(new Span(this.chunkDataBuffer, 4, 4), header.Height);
@@ -436,7 +435,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// The containing image data.
/// The .
/// The quantized frame.
- private void WritePaletteChunk(Stream stream, PngHeader header, QuantizedFrame quantized)
+ private void WritePaletteChunk(Stream stream, in PngHeader header, QuantizedFrame quantized)
where TPixel : struct, IPixel
{
// Grab the palette and write it to the stream.
diff --git a/src/ImageSharp/Formats/Png/PngHeader.cs b/src/ImageSharp/Formats/Png/PngHeader.cs
index a70032ce3c..df85642bed 100644
--- a/src/ImageSharp/Formats/Png/PngHeader.cs
+++ b/src/ImageSharp/Formats/Png/PngHeader.cs
@@ -6,55 +6,73 @@ namespace SixLabors.ImageSharp.Formats.Png
///
/// Represents the png header chunk.
///
- internal sealed class PngHeader
+ internal readonly struct PngHeader
{
+ public PngHeader(
+ int width,
+ int height,
+ byte bitDepth,
+ PngColorType colorType,
+ byte compressionMethod,
+ byte filterMethod,
+ PngInterlaceMode interlaceMethod)
+ {
+ this.Width = width;
+ this.Height = height;
+ this.BitDepth = bitDepth;
+ this.ColorType = colorType;
+ this.CompressionMethod = compressionMethod;
+ this.FilterMethod = filterMethod;
+ this.InterlaceMethod = interlaceMethod;
+ }
+
///
- /// Gets or sets the dimension in x-direction of the image in pixels.
+ /// Gets the dimension in x-direction of the image in pixels.
///
- public int Width { get; set; }
+ public int Width { get; }
///
- /// Gets or sets the dimension in y-direction of the image in pixels.
+ /// Gets the dimension in y-direction of the image in pixels.
///
- public int Height { get; set; }
+ public int Height { get; }
///
- /// Gets or sets the bit depth.
+ /// Gets the bit depth.
/// Bit depth is a single-byte integer giving the number of bits per sample
/// or per palette index (not per pixel). Valid values are 1, 2, 4, 8, and 16,
/// although not all values are allowed for all color types.
///
- public byte BitDepth { get; set; }
+ public byte BitDepth { get; }
///
- /// Gets or sets the color type.
+ /// Gets the color type.
/// Color type is a integer that describes the interpretation of the
/// image data. Color type codes represent sums of the following values:
/// 1 (palette used), 2 (color used), and 4 (alpha channel used).
///
- public PngColorType ColorType { get; set; }
+ public PngColorType ColorType { get; }
///
- /// Gets or sets the compression method.
+ /// Gets the compression method.
/// Indicates the method used to compress the image data. At present,
/// only compression method 0 (deflate/inflate compression with a sliding
/// window of at most 32768 bytes) is defined.
///
- public byte CompressionMethod { get; set; }
+ public byte CompressionMethod { get; }
///
- /// Gets or sets the preprocessing method.
+ /// Gets the preprocessing method.
/// Indicates the preprocessing method applied to the image
/// data before compression. At present, only filter method 0
/// (adaptive filtering with five basic filter types) is defined.
///
- public byte FilterMethod { get; set; }
+ public byte FilterMethod { get; }
///
- /// Gets or sets the transmission order.
+ /// Gets the transmission order.
/// Indicates the transmission order of the image data.
/// Two values are currently defined: 0 (no interlace) or 1 (Adam7 interlace).
///
- public PngInterlaceMode InterlaceMethod { get; set; }
+ public PngInterlaceMode InterlaceMethod { get; }
}
}