From ec32e422759b98cf47e701d9f69556ffaff07f09 Mon Sep 17 00:00:00 2001 From: winscripter <142818255+winscripter@users.noreply.github.com> Date: Wed, 9 Sep 2026 00:26:29 +0400 Subject: [PATCH] Refactor --- .../Formats/Jxl/IO/Jpeg/JpegBitWriter.cs | 13 ++--- .../Jxl/IO/Jpeg/JpegSerializationStage.cs | 2 +- .../Formats/Jxl/IO/Jpeg/JpegWriter.cs | 56 +++++++++---------- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegBitWriter.cs b/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegBitWriter.cs index bb97c941b3..805eb934f6 100644 --- a/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegBitWriter.cs +++ b/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegBitWriter.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Buffers.Binary; + namespace SixLabors.ImageSharp.Formats.Jxl.IO.Jpeg; internal sealed class JpegBitWriter @@ -62,14 +64,9 @@ internal sealed class JpegBitWriter public void StoreBe64(ulong value) { - this.buffer[this.pos++] = (byte)(value >> 56); - this.buffer[this.pos++] = (byte)(value >> 48); - this.buffer[this.pos++] = (byte)(value >> 40); - this.buffer[this.pos++] = (byte)(value >> 32); - this.buffer[this.pos++] = (byte)(value >> 24); - this.buffer[this.pos++] = (byte)(value >> 16); - this.buffer[this.pos++] = (byte)(value >> 8); - this.buffer[this.pos++] = (byte)value; + Span span = this.buffer.AsSpan(this.pos); + BinaryPrimitives.WriteUInt64BigEndian(span, value); + this.pos += 8; } public void DischargeBitBuffer(int nBits, ulong bits) diff --git a/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegSerializationStage.cs b/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegSerializationStage.cs index 47d1b2d3ad..4beb4e51cf 100644 --- a/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegSerializationStage.cs +++ b/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegSerializationStage.cs @@ -3,7 +3,7 @@ namespace SixLabors.ImageSharp.Formats.Jxl.IO.Jpeg; -internal enum JpegSerializationStage +internal enum JpegSerializationStage : byte { Initialize, SerializeSection, diff --git a/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegWriter.cs b/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegWriter.cs index 62ea5230e5..0df15a4bea 100644 --- a/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegWriter.cs +++ b/src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegWriter.cs @@ -226,18 +226,18 @@ internal class JpegWriter state.OutputQueue.Add([(byte)(markerLength + 2)]); Span data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); - int pos = 0; - - data[pos++] = 0xff; - data[pos++] = marker; - data[pos++] = (byte)(markerLength >> 8); - data[pos++] = (byte)markerLength; - data[pos++] = JpegPrecision; - data[pos++] = (byte)(jpg.Height >> 8); - data[pos++] = (byte)jpg.Height; - data[pos++] = (byte)(jpg.Width >> 8); - data[pos++] = (byte)jpg.Width; - data[pos++] = (byte)componentCount; + int pos = 10; + + data[0] = 0xff; + data[1] = marker; + data[2] = (byte)(markerLength >> 8); + data[3] = (byte)markerLength; + data[4] = JpegPrecision; + data[5] = (byte)(jpg.Height >> 8); + data[6] = (byte)jpg.Height; + data[7] = (byte)(jpg.Width >> 8); + data[8] = (byte)jpg.Width; + data[9] = (byte)componentCount; for (int i = 0; i < componentCount; ++i) { @@ -267,13 +267,13 @@ internal class JpegWriter state.OutputQueue.Add([(byte)(markerLength + 2)]); Span data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); - int pos = 0; + int pos = 5; - data[pos++] = 0xff; - data[pos++] = 0xda; - data[pos++] = (byte)(markerLength >> 8); - data[pos++] = (byte)markerLength; - data[pos++] = (byte)scanCount; + data[0] = 0xff; + data[1] = 0xda; + data[2] = (byte)(markerLength >> 8); + data[3] = (byte)markerLength; + data[4] = (byte)scanCount; for (int i = 0; i < scanCount; ++i) { @@ -327,12 +327,12 @@ internal class JpegWriter state.OutputQueue.Add([(byte)(markerLength + 2)]); Span data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); - int pos = 0; + int pos = 4; - data[pos++] = 0xff; - data[pos++] = 0xc4; - data[pos++] = (byte)(markerLength >> 8); - data[pos++] = (byte)markerLength; + data[0] = 0xff; + data[1] = 0xc4; + data[2] = (byte)(markerLength >> 8); + data[3] = (byte)markerLength; while (true) { @@ -428,12 +428,12 @@ internal class JpegWriter state.OutputQueue.Add([(byte)(markerLength + 2)]); Span data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); - int pos = 0; + int pos = 4; - data[pos++] = 0xff; - data[pos++] = 0xdb; - data[pos++] = (byte)(markerLength >> 8); - data[pos++] = (byte)markerLength; + data[0] = 0xff; + data[1] = 0xdb; + data[2] = (byte)(markerLength >> 8); + data[3] = (byte)markerLength; while (true) {