diff --git a/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrEncoder{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrEncoder{TPixel}.cs index 5b63d05882..a10f40b09a 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrEncoder{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrEncoder{TPixel}.cs @@ -13,21 +13,34 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder { internal class YCbCrEncoder + where TPixel : unmanaged, IPixel { + /// + /// Number of bytes cached before being written to target stream via Stream.Write(byte[], offest, count). + /// + /// + /// This is subject to change, 1024 seems to be the best value in terms of performance. + /// expects it to be at least 8 (see comments in method body). + /// private const int EmitBufferSizeInBytes = 1024; /// - /// A buffer for reducing the number of stream writes when emitting Huffman tables. 64 seems to be enough. + /// A buffer for reducing the number of stream writes when emitting Huffman tables. /// private byte[] emitBuffer = new byte[EmitBufferSizeInBytes]; /// - /// The accumulated bits to write to the stream. + /// Number of filled bytes in buffer + /// + private int emitLen = 0; + + /// + /// Emmited bits 'micro buffer' before being transfered to the . /// private uint accumulatedBits; /// - /// The accumulated bit count. + /// Number of jagged bits stored in /// private uint bitCount; @@ -44,10 +57,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder private Block8x8F temporalBlock1; private Block8x8F temporalBlock2; + private ImageFrame source; + /// /// The output stream. All attempted writes after the first error become no-ops. /// - private Stream outputStream; + private Stream target; /// /// Gets the counts the number of bits needed to hold an integer. @@ -118,7 +133,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder public YCbCrEncoder(Stream outputStream, int componentCount, int quality) { - this.outputStream = outputStream; + this.target = outputStream; // Convert from a quality rating to a scaling factor. int scale; @@ -333,7 +348,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder // Pad the last byte with 1's. this.Emit(0x7f, 7); - this.outputStream.Write(this.emitBuffer, 0, this.emitLen); + this.target.Write(this.emitBuffer, 0, this.emitLen); } /// @@ -344,8 +359,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder /// The quantization table index. /// The previous DC value. /// Source block - /// Temporal block to be used as FDCT Destination - /// Temporal block 2 /// Quantization table /// The 8x8 Unzig block. /// The . @@ -401,8 +414,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder return dc; } - private int emitLen = 0; - /// /// Emits the least significant count of bits to the stream write buffer. /// The precondition is bits @@ -444,7 +455,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder // So we must check if emit buffer has extra 8 bytes, if not - call stream.Write if (this.emitLen > EmitBufferSizeInBytes - 8) { - this.outputStream.Write(this.emitBuffer, 0, this.emitLen); + this.target.Write(this.emitBuffer, 0, this.emitLen); this.emitLen = 0; } }