Browse Source

Move PngHeader writing logic to struct

pull/717/head
Jason Nelson 8 years ago
parent
commit
3ca9163746
  1. 21
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  2. 18
      src/ImageSharp/Formats/Png/PngHeader.cs

21
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -606,16 +606,9 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <param name="header">The <see cref="PngHeader"/>.</param>
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);
}
/// <summary>
@ -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);

18
src/ImageSharp/Formats/Png/PngHeader.cs

@ -11,6 +11,8 @@ namespace SixLabors.ImageSharp.Formats.Png
/// </summary>
internal readonly struct PngHeader
{
public const int Size = 13;
public PngHeader(
int width,
int height,
@ -78,6 +80,22 @@ namespace SixLabors.ImageSharp.Formats.Png
/// </summary>
public PngInterlaceMode InterlaceMethod { get; }
/// <summary>
/// Writes the header to the given buffer.
/// </summary>
/// <param name="buffer">The buffer to write to.</param>
public void WriteTo(Span<byte> 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;
}
/// <summary>
/// Parses the PngHeader from the given data buffer.
/// </summary>

Loading…
Cancel
Save