From 2b4d2a735d8e5998dc34ff43b73ce9df9e1afb09 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 23 Mar 2018 09:49:59 -0700 Subject: [PATCH] Eliminate allocation in WriteGammaChunk --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index e438975a4..a0e09e911 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -519,14 +519,10 @@ namespace SixLabors.ImageSharp.Formats.Png { if (this.writeGamma) { - int gammaValue = (int)(this.gamma * 100000F); + // 4-byte unsigned integer of gamma * 100,000. + uint gammaValue = (uint)(this.gamma * 100_000F); - byte[] size = BitConverter.GetBytes(gammaValue); - - this.chunkDataBuffer[0] = size[3]; - this.chunkDataBuffer[1] = size[2]; - this.chunkDataBuffer[2] = size[1]; - this.chunkDataBuffer[3] = size[0]; + BinaryPrimitives.WriteUInt32BigEndian(new Span(this.chunkDataBuffer, 0, 4), gammaValue); this.WriteChunk(stream, PngChunkTypes.Gamma, this.chunkDataBuffer, 0, 4); }