diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
index 603162fbe8..525cc8bd1c 100644
--- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
@@ -606,16 +606,9 @@ namespace SixLabors.ImageSharp.Formats.Png
/// The .
private void WriteHeaderChunk(Stream stream, in PngHeader header)
{
- BinaryPrimitives.WriteInt32BigEndian(this.chunkDataBuffer.AsSpan(0, 4), header.Width);
- BinaryPrimitives.WriteInt32BigEndian(this.chunkDataBuffer.AsSpan(4, 4), header.Height);
+ header.WriteTo(this.chunkDataBuffer);
- this.chunkDataBuffer[8] = header.BitDepth;
- this.chunkDataBuffer[9] = (byte)header.ColorType;
- this.chunkDataBuffer[10] = header.CompressionMethod;
- this.chunkDataBuffer[11] = header.FilterMethod;
- this.chunkDataBuffer[12] = (byte)header.InterlaceMethod;
-
- this.WriteChunk(stream, PngChunkType.Header, this.chunkDataBuffer, 0, 13);
+ this.WriteChunk(stream, PngChunkType.Header, this.chunkDataBuffer, 0, PngHeader.Size);
}
///
@@ -697,28 +690,24 @@ namespace SixLabors.ImageSharp.Formats.Png
switch (meta.ResolutionUnits)
{
case PixelResolutionUnit.AspectRatio:
-
this.chunkDataBuffer[8] = 0;
BinaryPrimitives.WriteInt32BigEndian(hResolution, (int)Math.Round(meta.HorizontalResolution));
BinaryPrimitives.WriteInt32BigEndian(vResolution, (int)Math.Round(meta.VerticalResolution));
break;
case PixelResolutionUnit.PixelsPerInch:
-
this.chunkDataBuffer[8] = 1; // Per meter
BinaryPrimitives.WriteInt32BigEndian(hResolution, (int)Math.Round(UnitConverter.InchToMeter(meta.HorizontalResolution)));
BinaryPrimitives.WriteInt32BigEndian(vResolution, (int)Math.Round(UnitConverter.InchToMeter(meta.VerticalResolution)));
break;
case PixelResolutionUnit.PixelsPerCentimeter:
-
this.chunkDataBuffer[8] = 1; // Per meter
BinaryPrimitives.WriteInt32BigEndian(hResolution, (int)Math.Round(UnitConverter.CmToMeter(meta.HorizontalResolution)));
BinaryPrimitives.WriteInt32BigEndian(vResolution, (int)Math.Round(UnitConverter.CmToMeter(meta.VerticalResolution)));
break;
default:
-
this.chunkDataBuffer[8] = 1; // Per meter
BinaryPrimitives.WriteInt32BigEndian(hResolution, (int)Math.Round(meta.HorizontalResolution));
BinaryPrimitives.WriteInt32BigEndian(vResolution, (int)Math.Round(meta.VerticalResolution));
@@ -782,26 +771,22 @@ namespace SixLabors.ImageSharp.Formats.Png
break;
case PngFilterMethod.Sub:
-
this.sub = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
break;
case PngFilterMethod.Up:
-
this.up = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
break;
case PngFilterMethod.Average:
-
this.average = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
break;
case PngFilterMethod.Paeth:
-
this.paeth = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
break;
- case PngFilterMethod.Adaptive:
+ case PngFilterMethod.Adaptive:
this.sub = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
this.up = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
this.average = this.memoryAllocator.AllocateManagedByteBuffer(resultLength, AllocationOptions.Clean);
diff --git a/src/ImageSharp/Formats/Png/PngHeader.cs b/src/ImageSharp/Formats/Png/PngHeader.cs
index 389ba80aee..ec22f1bb42 100644
--- a/src/ImageSharp/Formats/Png/PngHeader.cs
+++ b/src/ImageSharp/Formats/Png/PngHeader.cs
@@ -11,6 +11,8 @@ namespace SixLabors.ImageSharp.Formats.Png
///
internal readonly struct PngHeader
{
+ public const int Size = 13;
+
public PngHeader(
int width,
int height,
@@ -78,6 +80,22 @@ namespace SixLabors.ImageSharp.Formats.Png
///
public PngInterlaceMode InterlaceMethod { get; }
+ ///
+ /// Writes the header to the given buffer.
+ ///
+ /// The buffer to write to.
+ public void WriteTo(Span buffer)
+ {
+ BinaryPrimitives.WriteInt32BigEndian(buffer.Slice(0, 4), this.Width);
+ BinaryPrimitives.WriteInt32BigEndian(buffer.Slice(4, 4), this.Height);
+
+ buffer[8] = this.BitDepth;
+ buffer[9] = (byte)this.ColorType;
+ buffer[10] = this.CompressionMethod;
+ buffer[11] = this.FilterMethod;
+ buffer[12] = (byte)this.InterlaceMethod;
+ }
+
///
/// Parses the PngHeader from the given data buffer.
///