Browse Source

Improved flush logic after main encode methods run

pull/1632/head
Dmitry Pentin 5 years ago
parent
commit
81979e0f29
  1. 32
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

32
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

@ -108,9 +108,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
}
}
// Pad the last byte with 1's.
this.Emit(0x7f, 7);
this.target.Write(this.emitBuffer, 0, this.emitLen);
this.FlushInternalBuffer();
}
/// <summary>
@ -181,9 +179,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
}
}
// Pad the last byte with 1's.
this.Emit(0x7f, 7);
this.target.Write(this.emitBuffer, 0, this.emitLen);
this.FlushInternalBuffer();
}
/// <summary>
@ -224,9 +220,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
}
}
// Pad the last byte with 1's.
this.Emit(0x7f, 7);
this.target.Write(this.emitBuffer, 0, this.emitLen);
this.FlushInternalBuffer();
}
/// <summary>
@ -376,5 +370,25 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
this.EmitHuff(index, (runLength << 4) | bt);
this.Emit(b & ((1 << bt) - 1), bt);
}
/// <summary>
/// Writes remaining bytes from internal buffer to the target stream.
/// </summary>
/// <remarks>Pads last byte with 1's if necessary</remarks>
private void FlushInternalBuffer()
{
// pad last byte with 1's
int padBitsCount = 8 - (this.bitCount % 8);
if (padBitsCount != 0)
{
this.Emit(0xff, padBitsCount);
}
// flush remaining bytes
if (this.emitLen != 0)
{
this.target.Write(this.emitBuffer, 0, this.emitLen);
}
}
}
}

Loading…
Cancel
Save