Browse Source

simplify + uniformize blockDataRef retrieval

pull/643/head
Anton Firszov 8 years ago
parent
commit
ff1a24c02a
  1. 8
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs
  2. 49
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs

8
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs

@ -144,5 +144,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
{ {
return 64 * (((this.WidthInBlocks + 1) * row) + col); return 64 * (((this.WidthInBlocks + 1) * row) + col);
} }
// TODO: we need consistence in (row, col) VS (col, row) ordering
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref short GetBlockDataReference(int row, int col)
{
ref Block8x8 blockRef = ref this.GetBlockReference(col, row);
return ref Unsafe.As<Block8x8, short>(ref blockRef);
}
} }
} }

49
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs

@ -179,8 +179,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
for (int k = 0; k < this.componentsLength; k++) for (int k = 0; k < this.componentsLength; k++)
{ {
PdfJsFrameComponent component = this.components[k]; PdfJsFrameComponent component = this.components[k];
ref short blockDataRef = ref MemoryMarshal.GetReference(
MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId];
ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId];
ref short fastACRef = ref short fastACRef =
@ -203,10 +202,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
int mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
int blockRow = (mcuRow * v) + y; int blockRow = (mcuRow * v) + y;
int blockCol = (mcuCol * h) + x; int blockCol = (mcuCol * h) + x;
int offset = component.GetBlockBufferOffset(blockRow, blockCol);
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockDataRef, offset), blockRow,
blockCol,
ref dcHuffmanTable, ref dcHuffmanTable,
ref acHuffmanTable, ref acHuffmanTable,
ref fastACRef); ref fastACRef);
@ -240,8 +240,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
int w = component.WidthInBlocks; int w = component.WidthInBlocks;
int h = component.HeightInBlocks; int h = component.HeightInBlocks;
ref short blockDataRef =
ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId];
ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId];
ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId));
@ -258,10 +257,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
int blockRow = mcu / w; int blockRow = mcu / w;
int blockCol = mcu % w; int blockCol = mcu % w;
int offset = component.GetBlockBufferOffset(blockRow, blockCol);
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockDataRef, offset), blockRow,
blockCol,
ref dcHuffmanTable, ref dcHuffmanTable,
ref acHuffmanTable, ref acHuffmanTable,
ref fastACRef); ref fastACRef);
@ -330,7 +330,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
int offset = component.GetBlockBufferOffset(blockRow, blockCol); int offset = component.GetBlockBufferOffset(blockRow, blockCol);
this.DecodeBlockProgressiveDC( this.DecodeBlockProgressiveDC(
component, component,
ref Unsafe.Add(ref blockDataRef, offset), blockRow,
blockCol,
ref dcHuffmanTable); ref dcHuffmanTable);
} }
} }
@ -362,8 +363,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
int w = component.WidthInBlocks; int w = component.WidthInBlocks;
int h = component.HeightInBlocks; int h = component.HeightInBlocks;
ref short blockDataRef =
ref MemoryMarshal.GetReference(MemoryMarshal.Cast<Block8x8, short>(component.SpectralBlocks.Span));
ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId];
ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId];
ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId)); ref short fastACRef = ref MemoryMarshal.GetReference(fastACTables.GetTableSpan(component.ACHuffmanTableId));
@ -380,16 +380,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
int blockRow = mcu / w; int blockRow = mcu / w;
int blockCol = mcu % w; int blockCol = mcu % w;
int offset = component.GetBlockBufferOffset(blockRow, blockCol);
if (this.spectralStart == 0) if (this.spectralStart == 0)
{ {
this.DecodeBlockProgressiveDC(component, ref Unsafe.Add(ref blockDataRef, offset), ref dcHuffmanTable); this.DecodeBlockProgressiveDC(
component,
blockRow,
blockCol,
ref dcHuffmanTable);
} }
else else
{ {
this.DecodeBlockProgressiveAC( this.DecodeBlockProgressiveAC(
ref Unsafe.Add(ref blockDataRef, offset), component,
blockRow,
blockCol,
ref acHuffmanTable, ref acHuffmanTable,
ref fastACRef); ref fastACRef);
} }
@ -406,7 +411,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
private void DecodeBlockBaseline( private void DecodeBlockBaseline(
PdfJsFrameComponent component, PdfJsFrameComponent component,
ref short blockDataRef, int row,
int col,
ref PdfJsHuffmanTable dcTable, ref PdfJsHuffmanTable dcTable,
ref PdfJsHuffmanTable acTable, ref PdfJsHuffmanTable acTable,
ref short fastACRef) ref short fastACRef)
@ -419,6 +425,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
JpegThrowHelper.ThrowBadHuffmanCode(); JpegThrowHelper.ThrowBadHuffmanCode();
} }
ref short blockDataRef = ref component.GetBlockDataReference(row, col);
int diff = t != 0 ? this.ExtendReceive(t) : 0; int diff = t != 0 ? this.ExtendReceive(t) : 0;
int dc = component.DcPredictor + diff; int dc = component.DcPredictor + diff;
component.DcPredictor = dc; component.DcPredictor = dc;
@ -482,7 +490,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
private void DecodeBlockProgressiveDC( private void DecodeBlockProgressiveDC(
PdfJsFrameComponent component, PdfJsFrameComponent component,
ref short blockDataRef, int row,
int col,
ref PdfJsHuffmanTable dcTable) ref PdfJsHuffmanTable dcTable)
{ {
if (this.spectralEnd != 0) if (this.spectralEnd != 0)
@ -492,6 +501,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
this.CheckBits(); this.CheckBits();
ref short blockDataRef = ref component.GetBlockDataReference(row, col);
if (this.successiveHigh == 0) if (this.successiveHigh == 0)
{ {
// First scan for DC coefficient, must be first // First scan for DC coefficient, must be first
@ -514,7 +525,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
} }
private void DecodeBlockProgressiveAC( private void DecodeBlockProgressiveAC(
ref short blockDataRef, PdfJsFrameComponent component,
int row,
int col,
ref PdfJsHuffmanTable acTable, ref PdfJsHuffmanTable acTable,
ref short fastACRef) ref short fastACRef)
{ {
@ -523,6 +536,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC."); JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC.");
} }
ref short blockDataRef = ref component.GetBlockDataReference(row, col);
if (this.successiveHigh == 0) if (this.successiveHigh == 0)
{ {
// MCU decoding for AC initial scan (either spectral selection, // MCU decoding for AC initial scan (either spectral selection,

Loading…
Cancel
Save