Browse Source

Fix accuracy of progressive decoding

af/merge-core
James Jackson-South 8 years ago
parent
commit
c550225378
  1. 16
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs

16
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs

@ -842,7 +842,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
while (k <= e) while (k <= e)
{ {
ref byte z = ref PdfJsQuantizationTables.DctZigZag[k]; int offsetZ = offset + PdfJsQuantizationTables.DctZigZag[k];
int sign = Unsafe.Add(ref blockDataRef, offsetZ) < 0 ? -1 : 1;
switch (this.successiveACState) switch (this.successiveACState)
{ {
case 0: // Initial state case 0: // Initial state
@ -881,7 +883,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
continue; continue;
case 1: // Skipping r zero items case 1: // Skipping r zero items
case 2: case 2:
ref short blockRef = ref Unsafe.Add(ref blockDataRef, offset + z); ref short blockRef = ref Unsafe.Add(ref blockDataRef, offsetZ);
if (blockRef != 0) if (blockRef != 0)
{ {
int bit = this.ReadBit(stream); int bit = this.ReadBit(stream);
@ -890,7 +892,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
return; return;
} }
blockRef += (short)(bit << this.successiveState); blockRef += (short)(sign * (bit << this.successiveState));
} }
else else
{ {
@ -903,7 +905,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
break; break;
case 3: // Set value for a zero item case 3: // Set value for a zero item
ref short blockRef2 = ref Unsafe.Add(ref blockDataRef, offset + z); ref short blockRef2 = ref Unsafe.Add(ref blockDataRef, offsetZ);
if (blockRef2 != 0) if (blockRef2 != 0)
{ {
int bit = this.ReadBit(stream); int bit = this.ReadBit(stream);
@ -912,7 +914,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
return; return;
} }
blockRef2 += (short)(bit << this.successiveState); blockRef2 += (short)(sign * (bit << this.successiveState));
} }
else else
{ {
@ -922,7 +924,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
break; break;
case 4: // Eob case 4: // Eob
ref short blockRef3 = ref Unsafe.Add(ref blockDataRef, offset + z); ref short blockRef3 = ref Unsafe.Add(ref blockDataRef, offsetZ);
if (blockRef3 != 0) if (blockRef3 != 0)
{ {
int bit = this.ReadBit(stream); int bit = this.ReadBit(stream);
@ -931,7 +933,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
return; return;
} }
blockRef3 += (short)(bit << this.successiveState); blockRef3 += (short)(sign * (bit << this.successiveState));
} }
break; break;

Loading…
Cancel
Save