Browse Source

Refactor

pull/3153/head
winscripter 4 weeks ago
parent
commit
ec32e42275
  1. 13
      src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegBitWriter.cs
  2. 2
      src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegSerializationStage.cs
  3. 56
      src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegWriter.cs

13
src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegBitWriter.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers.Binary;
namespace SixLabors.ImageSharp.Formats.Jxl.IO.Jpeg; namespace SixLabors.ImageSharp.Formats.Jxl.IO.Jpeg;
internal sealed class JpegBitWriter internal sealed class JpegBitWriter
@ -62,14 +64,9 @@ internal sealed class JpegBitWriter
public void StoreBe64(ulong value) public void StoreBe64(ulong value)
{ {
this.buffer[this.pos++] = (byte)(value >> 56); Span<byte> span = this.buffer.AsSpan(this.pos);
this.buffer[this.pos++] = (byte)(value >> 48); BinaryPrimitives.WriteUInt64BigEndian(span, value);
this.buffer[this.pos++] = (byte)(value >> 40); this.pos += 8;
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;
} }
public void DischargeBitBuffer(int nBits, ulong bits) public void DischargeBitBuffer(int nBits, ulong bits)

2
src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegSerializationStage.cs

@ -3,7 +3,7 @@
namespace SixLabors.ImageSharp.Formats.Jxl.IO.Jpeg; namespace SixLabors.ImageSharp.Formats.Jxl.IO.Jpeg;
internal enum JpegSerializationStage internal enum JpegSerializationStage : byte
{ {
Initialize, Initialize,
SerializeSection, SerializeSection,

56
src/ImageSharp/Formats/Jxl/IO/Jpeg/JpegWriter.cs

@ -226,18 +226,18 @@ internal class JpegWriter
state.OutputQueue.Add([(byte)(markerLength + 2)]); state.OutputQueue.Add([(byte)(markerLength + 2)]);
Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]);
int pos = 0; int pos = 10;
data[pos++] = 0xff; data[0] = 0xff;
data[pos++] = marker; data[1] = marker;
data[pos++] = (byte)(markerLength >> 8); data[2] = (byte)(markerLength >> 8);
data[pos++] = (byte)markerLength; data[3] = (byte)markerLength;
data[pos++] = JpegPrecision; data[4] = JpegPrecision;
data[pos++] = (byte)(jpg.Height >> 8); data[5] = (byte)(jpg.Height >> 8);
data[pos++] = (byte)jpg.Height; data[6] = (byte)jpg.Height;
data[pos++] = (byte)(jpg.Width >> 8); data[7] = (byte)(jpg.Width >> 8);
data[pos++] = (byte)jpg.Width; data[8] = (byte)jpg.Width;
data[pos++] = (byte)componentCount; data[9] = (byte)componentCount;
for (int i = 0; i < componentCount; ++i) for (int i = 0; i < componentCount; ++i)
{ {
@ -267,13 +267,13 @@ internal class JpegWriter
state.OutputQueue.Add([(byte)(markerLength + 2)]); state.OutputQueue.Add([(byte)(markerLength + 2)]);
Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]);
int pos = 0; int pos = 5;
data[pos++] = 0xff; data[0] = 0xff;
data[pos++] = 0xda; data[1] = 0xda;
data[pos++] = (byte)(markerLength >> 8); data[2] = (byte)(markerLength >> 8);
data[pos++] = (byte)markerLength; data[3] = (byte)markerLength;
data[pos++] = (byte)scanCount; data[4] = (byte)scanCount;
for (int i = 0; i < scanCount; ++i) for (int i = 0; i < scanCount; ++i)
{ {
@ -327,12 +327,12 @@ internal class JpegWriter
state.OutputQueue.Add([(byte)(markerLength + 2)]); state.OutputQueue.Add([(byte)(markerLength + 2)]);
Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]);
int pos = 0; int pos = 4;
data[pos++] = 0xff; data[0] = 0xff;
data[pos++] = 0xc4; data[1] = 0xc4;
data[pos++] = (byte)(markerLength >> 8); data[2] = (byte)(markerLength >> 8);
data[pos++] = (byte)markerLength; data[3] = (byte)markerLength;
while (true) while (true)
{ {
@ -428,12 +428,12 @@ internal class JpegWriter
state.OutputQueue.Add([(byte)(markerLength + 2)]); state.OutputQueue.Add([(byte)(markerLength + 2)]);
Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]); Span<byte> data = CollectionsMarshal.AsSpan(state.OutputQueue[^1]);
int pos = 0; int pos = 4;
data[pos++] = 0xff; data[0] = 0xff;
data[pos++] = 0xdb; data[1] = 0xdb;
data[pos++] = (byte)(markerLength >> 8); data[2] = (byte)(markerLength >> 8);
data[pos++] = (byte)markerLength; data[3] = (byte)markerLength;
while (true) while (true)
{ {

Loading…
Cancel
Save