Browse Source

[WIP] Moved SOS writing logic to separate class

pull/1632/head
Dmitry Pentin 5 years ago
parent
commit
66b5a8df67
  1. 29
      src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrEncoder{TPixel}.cs
  2. 44
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

29
src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrEncoder{TPixel}.cs

@ -141,7 +141,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
/// <param name="pixels">The pixel accessor providing access to the image pixels.</param>
/// <param name="cancellationToken">The token to monitor for cancellation.</param>
/// <param name="emitBufferBase">The reference to the emit buffer.</param>
public void Encode444<TPixel>(Image<TPixel> pixels, CancellationToken cancellationToken)
private void Encode444<TPixel>(Image<TPixel> pixels, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
// TODO: Need a JpegScanEncoder<TPixel> class or struct that encapsulates the scan-encoding implementation. (Similar to JpegScanDecoder.)
@ -209,7 +209,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
/// <param name="pixels">The pixel accessor providing access to the image pixels.</param>
/// <param name="cancellationToken">The token to monitor for cancellation.</param>
/// <param name="emitBufferBase">The reference to the emit buffer.</param>
public void Encode420<TPixel>(Image<TPixel> pixels, CancellationToken cancellationToken)
private void Encode420<TPixel>(Image<TPixel> pixels, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
// TODO: Need a JpegScanEncoder<TPixel> class or struct that encapsulates the scan-encoding implementation. (Similar to JpegScanDecoder.)
@ -290,7 +290,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
/// <param name="pixels">The pixel accessor providing access to the image pixels.</param>
/// <param name="cancellationToken">The token to monitor for cancellation.</param>
/// <param name="emitBufferBase">The reference to the emit buffer.</param>
public void EncodeGrayscale<TPixel>(Image<TPixel> pixels, CancellationToken cancellationToken)
private void EncodeGrayscale<TPixel>(Image<TPixel> pixels, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
// TODO: Need a JpegScanEncoder<TPixel> class or struct that encapsulates the scan-encoding implementation. (Similar to JpegScanDecoder.)
@ -331,6 +331,29 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
}
}
public void WriteStartOfScan<TPixel>(Image<TPixel> image, JpegColorType? colorType, JpegSubsample? subsample, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
if (colorType == JpegColorType.Luminance)
{
this.EncodeGrayscale(image, cancellationToken);
}
else
{
switch (subsample)
{
case JpegSubsample.Ratio444:
this.Encode444(image, cancellationToken);
break;
case JpegSubsample.Ratio420:
this.Encode420(image, cancellationToken);
break;
}
}
// Pad the last byte with 1's.
this.Emit(0x7f, 7);
}
/// <summary>
/// Writes a block of pixel data using the given quantization table,

44
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

@ -203,7 +203,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.WriteDefineHuffmanTables(componentCount);
// Write the image data.
this.WriteStartOfScan(image, componentCount, cancellationToken);
this.WriteStartOfScan(scanEncoder, image, componentCount, cancellationToken);
// Write the End Of Image marker.
this.buffer[0] = JpegConstants.Markers.XFF;
@ -969,7 +969,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// <param name="image">The pixel accessor providing access to the image pixels.</param>
/// <param name="componentCount">The number of components in a pixel.</param>
/// <param name="cancellationToken">The token to monitor for cancellation.</param>
private void WriteStartOfScan<TPixel>(Image<TPixel> image, int componentCount, CancellationToken cancellationToken)
private void WriteStartOfScan<TPixel>(YCbCrEncoder<TPixel> scanEncoder, Image<TPixel> image, int componentCount, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
// TODO: Need a JpegScanEncoder<TPixel> class or struct that encapsulates the scan-encoding implementation. (Similar to JpegScanDecoder.)
@ -1015,26 +1015,28 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.buffer[sosSize + 1] = 0x00; // Ah + Ah (Successive approximation bit position high + low)
this.outputStream.Write(this.buffer, 0, sosSize + 2);
ref byte emitBufferBase = ref MemoryMarshal.GetReference<byte>(this.emitBuffer);
if (this.colorType == JpegColorType.Luminance)
{
this.EncodeGrayscale(image, cancellationToken, ref emitBufferBase);
}
else
{
switch (this.subsample)
{
case JpegSubsample.Ratio444:
this.Encode444(image, cancellationToken, ref emitBufferBase);
break;
case JpegSubsample.Ratio420:
this.Encode420(image, cancellationToken, ref emitBufferBase);
break;
}
}
// Pad the last byte with 1's.
this.Emit(0x7f, 7, ref emitBufferBase);
scanEncoder.WriteStartOfScan(image, this.colorType, this.subsample, cancellationToken);
//ref byte emitBufferBase = ref MemoryMarshal.GetReference<byte>(this.emitBuffer);
//if (this.colorType == JpegColorType.Luminance)
//{
// scanEncoder.EncodeGrayscale(image, cancellationToken);
//}
//else
//{
// switch (this.subsample)
// {
// case JpegSubsample.Ratio444:
// scanEncoder.Encode444(image, cancellationToken);
// break;
// case JpegSubsample.Ratio420:
// scanEncoder.Encode420(image, cancellationToken);
// break;
// }
//}
//// Pad the last byte with 1's.
//this.Emit(0x7f, 7, ref emitBufferBase);
}
/// <summary>

Loading…
Cancel
Save