Browse Source

Merge pull request #1979 from br3aker/dp/jpeg-grayscale-fix

Fixed decoding grayscale jpegs with exotic sampling factors
pull/1980/head
James Jackson-South 4 years ago
committed by GitHub
parent
commit
6d434ea28e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 61
      src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs
  2. 3
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/External/ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_grayscale_sampling22.png
  5. 3
      tests/Images/Input/Jpg/baseline/grayscale_sampling22.jpg

61
src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs

@ -148,11 +148,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
private void ParseBaselineData()
{
if (this.componentsCount == this.frame.ComponentCount)
if (this.componentsCount != 1)
{
this.ParseBaselineDataInterleaved();
this.spectralConverter.CommitConversion();
}
else if (this.frame.ComponentCount == 1)
{
this.ParseBaselineDataSingleComponent();
this.spectralConverter.CommitConversion();
}
else
{
this.ParseBaselineDataNonInterleaved();
@ -161,7 +166,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
private void ParseBaselineDataInterleaved()
{
// Interleaved
int mcu = 0;
int mcusPerColumn = this.frame.McusPerColumn;
int mcusPerLine = this.frame.McusPerLine;
@ -198,7 +202,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
{
if (buffer.NoData)
{
// It is very likely that some spectral data was decoded before we encountered EOI marker
// It is very likely that some spectral data was decoded before we've encountered 'end of scan'
// so we need to decode what's left and return (or maybe throw?)
this.spectralConverter.ConvertStrideBaseline();
return;
@ -221,9 +225,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
this.HandleRestart();
}
// convert from spectral to actual pixels via given converter
// Convert from spectral to actual pixels via given converter
this.spectralConverter.ConvertStrideBaseline();
}
// Stride conversion must be sealed for stride conversion approach
this.spectralConverter.CommitConversion();
}
private void ParseBaselineDataNonInterleaved()
@ -261,6 +268,52 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
}
}
private void ParseBaselineDataSingleComponent()
{
JpegComponent component = this.frame.Components[0];
int mcuLines = this.frame.McusPerColumn;
int w = component.WidthInBlocks;
int h = component.SamplingFactors.Height;
ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId];
ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId];
ref HuffmanScanBuffer buffer = ref this.scanBuffer;
for (int i = 0; i < mcuLines; i++)
{
this.cancellationToken.ThrowIfCancellationRequested();
// decode from binary to spectral
for (int j = 0; j < h; j++)
{
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(j);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (int k = 0; k < w; k++)
{
if (buffer.NoData)
{
// It is very likely that some spectral data was decoded before we've encountered 'end of scan'
// so we need to decode what's left and return (or maybe throw?)
this.spectralConverter.ConvertStrideBaseline();
return;
}
this.DecodeBlockBaseline(
component,
ref Unsafe.Add(ref blockRef, k),
ref dcHuffmanTable,
ref acHuffmanTable);
this.HandleRestart();
}
}
// Convert from spectral to actual pixels via given converter
this.spectralConverter.ConvertStrideBaseline();
}
}
private void CheckProgressiveData()
{
// Validate successive scan parameters.

3
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs

@ -42,6 +42,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
// High depth images
TestImages.Jpeg.Baseline.Testorig12bit,
// Grayscale jpeg with 2x2 sampling factors (not a usual thing to encounter in the wild)
TestImages.Jpeg.Baseline.GrayscaleSampling2x2,
};
public static string[] ProgressiveTestJpegs =

1
tests/ImageSharp.Tests/TestImages.cs

@ -223,6 +223,7 @@ namespace SixLabors.ImageSharp.Tests
public const string Winter444_Interleaved = "Jpg/baseline/winter444_interleaved.jpg";
public const string Metadata = "Jpg/baseline/Metadata-test-file.jpg";
public const string ExtendedXmp = "Jpg/baseline/extended-xmp.jpg";
public const string GrayscaleSampling2x2 = "Jpg/baseline/grayscale_sampling22.jpg";
public static readonly string[] All =
{

3
tests/Images/External/ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_grayscale_sampling22.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ad22f28d20ea0ceda983a138b3bf9503ed836d779ed75a313f668329c910665e
size 168405

3
tests/Images/Input/Jpg/baseline/grayscale_sampling22.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a5cc8572082a54944d48b3e4f49e6c441871f6eb2b616fbbbfb025f20e0aeff5
size 45066
Loading…
Cancel
Save