Browse Source

Cleanup JpegFrameComponent

pull/662/head
James Jackson-South 8 years ago
parent
commit
f6ad783aaf
  1. 4
      src/ImageSharp/Formats/Jpeg/Components/Decoder/FastACTables.cs
  2. 17
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
  3. 4
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs
  4. 24
      src/ImageSharp/Formats/Jpeg/Components/Decoder/ScanDecoder.cs
  5. 8
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  6. 14
      tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs
  7. 2
      tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs
  8. 2
      tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs

4
src/ImageSharp/Formats/Jpeg/Components/Decoder/FastACTables.cs

@ -35,9 +35,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
} }
/// <summary> /// <summary>
/// Gets a reference to the first element of the AC table indexed by <see cref="JpegFrameComponent.ACHuffmanTableId"/> /// </summary> /// Gets a reference to the first element of the AC table indexed by <see cref="JpegComponent.ACHuffmanTableId"/> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref short GetAcTableReference(JpegFrameComponent component) public ref short GetAcTableReference(JpegComponent component)
{ {
return ref this.tables.GetRowSpan(component.ACHuffmanTableId)[0]; return ref this.tables.GetRowSpan(component.ACHuffmanTableId)[0];
} }

17
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrameComponent.cs → src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs

@ -13,11 +13,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// <summary> /// <summary>
/// Represents a single frame component /// Represents a single frame component
/// </summary> /// </summary>
internal class JpegFrameComponent : IDisposable, IJpegComponent internal class JpegComponent : IDisposable, IJpegComponent
{ {
private readonly MemoryAllocator memoryAllocator; private readonly MemoryAllocator memoryAllocator;
public JpegFrameComponent(MemoryAllocator memoryAllocator, JpegFrame frame, byte id, int horizontalFactor, int verticalFactor, byte quantizationTableIndex, int index) public JpegComponent(MemoryAllocator memoryAllocator, JpegFrame frame, byte id, int horizontalFactor, int verticalFactor, byte quantizationTableIndex, int index)
{ {
this.memoryAllocator = memoryAllocator; this.memoryAllocator = memoryAllocator;
this.Frame = frame; this.Frame = frame;
@ -123,7 +123,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
} }
else else
{ {
JpegFrameComponent c0 = this.Frame.Components[0]; JpegComponent c0 = this.Frame.Components[0];
this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors); this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors);
} }
@ -138,16 +138,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetBlockBufferOffset(int row, int col) public ref short GetBlockDataReference(int column, int row)
{ {
return 64 * (((this.WidthInBlocks + 1) * row) + col); ref Block8x8 blockRef = ref this.GetBlockReference(column, row);
}
// 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); return ref Unsafe.As<Block8x8, short>(ref blockRef);
} }
} }

4
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs

@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// <summary> /// <summary>
/// Gets or sets the frame component collection /// Gets or sets the frame component collection
/// </summary> /// </summary>
public JpegFrameComponent[] Components { get; set; } public JpegComponent[] Components { get; set; }
/// <summary> /// <summary>
/// Gets or sets the maximum horizontal sampling factor /// Gets or sets the maximum horizontal sampling factor
@ -94,7 +94,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
for (int i = 0; i < this.ComponentCount; i++) for (int i = 0; i < this.ComponentCount; i++)
{ {
JpegFrameComponent component = this.Components[i]; JpegComponent component = this.Components[i];
component.Init(); component.Init();
} }
} }

24
src/ImageSharp/Formats/Jpeg/Components/Decoder/ScanDecoder.cs

@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
private readonly FastACTables fastACTables; private readonly FastACTables fastACTables;
private readonly DoubleBufferedStreamReader stream; private readonly DoubleBufferedStreamReader stream;
private readonly JpegFrameComponent[] components; private readonly JpegComponent[] components;
private readonly ZigZag dctZigZag; private readonly ZigZag dctZigZag;
// The restart interval. // The restart interval.
@ -175,7 +175,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
// Scan an interleaved mcu... process components in order // Scan an interleaved mcu... process components in order
for (int k = 0; k < this.componentsLength; k++) for (int k = 0; k < this.componentsLength; k++)
{ {
JpegFrameComponent component = this.components[k]; JpegComponent component = this.components[k];
ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId];
ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId]; ref HuffmanTable acHuffmanTable = ref this.acHuffmanTables[component.ACHuffmanTableId];
@ -229,7 +229,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// </summary> /// </summary>
private void ParseBaselineDataNonInterleaved() private void ParseBaselineDataNonInterleaved()
{ {
JpegFrameComponent component = this.components[this.componentIndex]; JpegComponent component = this.components[this.componentIndex];
int w = component.WidthInBlocks; int w = component.WidthInBlocks;
int h = component.HeightInBlocks; int h = component.HeightInBlocks;
@ -294,7 +294,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
// Scan an interleaved mcu... process components in order // Scan an interleaved mcu... process components in order
for (int k = 0; k < this.componentsLength; k++) for (int k = 0; k < this.componentsLength; k++)
{ {
JpegFrameComponent component = this.components[k]; JpegComponent component = this.components[k];
ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId]; ref HuffmanTable dcHuffmanTable = ref this.dcHuffmanTables[component.DCHuffmanTableId];
int h = component.HorizontalSamplingFactor; int h = component.HorizontalSamplingFactor;
int v = component.VerticalSamplingFactor; int v = component.VerticalSamplingFactor;
@ -343,7 +343,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// </summary> /// </summary>
private void ParseProgressiveDataNonInterleaved() private void ParseProgressiveDataNonInterleaved()
{ {
JpegFrameComponent component = this.components[this.componentIndex]; JpegComponent component = this.components[this.componentIndex];
int w = component.WidthInBlocks; int w = component.WidthInBlocks;
int h = component.HeightInBlocks; int h = component.HeightInBlocks;
@ -394,7 +394,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
} }
private void DecodeBlockBaseline( private void DecodeBlockBaseline(
JpegFrameComponent component, JpegComponent component,
int row, int row,
int col, int col,
ref HuffmanTable dcTable, ref HuffmanTable dcTable,
@ -409,7 +409,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
JpegThrowHelper.ThrowBadHuffmanCode(); JpegThrowHelper.ThrowBadHuffmanCode();
} }
ref short blockDataRef = ref component.GetBlockDataReference(row, col); ref short blockDataRef = ref component.GetBlockDataReference(col, row);
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;
@ -473,7 +473,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
} }
private void DecodeBlockProgressiveDC( private void DecodeBlockProgressiveDC(
JpegFrameComponent component, JpegComponent component,
int row, int row,
int col, int col,
ref HuffmanTable dcTable) ref HuffmanTable dcTable)
@ -485,7 +485,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
this.CheckBits(); this.CheckBits();
ref short blockDataRef = ref component.GetBlockDataReference(row, col); ref short blockDataRef = ref component.GetBlockDataReference(col, row);
if (this.successiveHigh == 0) if (this.successiveHigh == 0)
{ {
@ -509,7 +509,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
} }
private void DecodeBlockProgressiveAC( private void DecodeBlockProgressiveAC(
JpegFrameComponent component, JpegComponent component,
int row, int row,
int col, int col,
ref HuffmanTable acTable, ref HuffmanTable acTable,
@ -520,7 +520,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC."); JpegThrowHelper.ThrowImageFormatException("Can't merge DC and AC.");
} }
ref short blockDataRef = ref component.GetBlockDataReference(row, col); ref short blockDataRef = ref component.GetBlockDataReference(col, row);
if (this.successiveHigh == 0) if (this.successiveHigh == 0)
{ {
@ -939,7 +939,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
for (int i = 0; i < this.components.Length; i++) for (int i = 0; i < this.components.Length; i++)
{ {
JpegFrameComponent c = this.components[i]; JpegComponent c = this.components[i];
c.DcPredictor = 0; c.DcPredictor = 0;
} }

8
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -145,7 +145,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// <summary> /// <summary>
/// Gets the components. /// Gets the components.
/// </summary> /// </summary>
public JpegFrameComponent[] Components => this.Frame.Components; public JpegComponent[] Components => this.Frame.Components;
/// <inheritdoc/> /// <inheritdoc/>
IEnumerable<IJpegComponent> IRawJpegData.Components => this.Components; IEnumerable<IJpegComponent> IRawJpegData.Components => this.Components;
@ -666,7 +666,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{ {
// No need to pool this. They max out at 4 // No need to pool this. They max out at 4
this.Frame.ComponentIds = new byte[this.Frame.ComponentCount]; this.Frame.ComponentIds = new byte[this.Frame.ComponentCount];
this.Frame.Components = new JpegFrameComponent[this.Frame.ComponentCount]; this.Frame.Components = new JpegComponent[this.Frame.ComponentCount];
this.ColorSpace = this.DeduceJpegColorSpace(); this.ColorSpace = this.DeduceJpegColorSpace();
for (int i = 0; i < this.Frame.ComponentCount; i++) for (int i = 0; i < this.Frame.ComponentCount; i++)
@ -685,7 +685,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
maxV = v; maxV = v;
} }
var component = new JpegFrameComponent(this.configuration.MemoryAllocator, this.Frame, this.temp[index], h, v, this.temp[index + 2], i); var component = new JpegComponent(this.configuration.MemoryAllocator, this.Frame, this.temp[index], h, v, this.temp[index + 2], i);
this.Frame.Components[i] = component; this.Frame.Components[i] = component;
this.Frame.ComponentIds[i] = component.Id; this.Frame.ComponentIds[i] = component.Id;
@ -793,7 +793,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
throw new ImageFormatException("Unknown component selector"); throw new ImageFormatException("Unknown component selector");
} }
ref JpegFrameComponent component = ref this.Frame.Components[componentIndex]; ref JpegComponent component = ref this.Frame.Components[componentIndex];
int tableSpec = this.InputStream.ReadByte(); int tableSpec = this.InputStream.ReadByte();
component.DCHuffmanTableId = tableSpec >> 4; component.DCHuffmanTableId = tableSpec >> 4;
component.ACHuffmanTableId = tableSpec & 15; component.ACHuffmanTableId = tableSpec & 15;

14
tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs

@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
Assert.Equal(expectedSizeInBlocks, decoder.ImageSizeInMCU); Assert.Equal(expectedSizeInBlocks, decoder.ImageSizeInMCU);
var uniform1 = new Size(1, 1); var uniform1 = new Size(1, 1);
JpegFrameComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
VerifyJpeg.VerifyComponent(c0, expectedSizeInBlocks, uniform1, uniform1); VerifyJpeg.VerifyComponent(c0, expectedSizeInBlocks, uniform1, uniform1);
} }
} }
@ -71,8 +71,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
{ {
sb.AppendLine(imageFile); sb.AppendLine(imageFile);
sb.AppendLine($"Size:{decoder.ImageSizeInPixels} MCU:{decoder.ImageSizeInMCU}"); sb.AppendLine($"Size:{decoder.ImageSizeInPixels} MCU:{decoder.ImageSizeInMCU}");
JpegFrameComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
JpegFrameComponent c1 = decoder.Components[1]; JpegComponent c1 = decoder.Components[1];
sb.AppendLine($"Luma: SAMP: {c0.SamplingFactors} BLOCKS: {c0.SizeInBlocks}"); sb.AppendLine($"Luma: SAMP: {c0.SamplingFactors} BLOCKS: {c0.SizeInBlocks}");
sb.AppendLine($"Chroma: {c1.SamplingFactors} BLOCKS: {c1.SizeInBlocks}"); sb.AppendLine($"Chroma: {c1.SamplingFactors} BLOCKS: {c1.SizeInBlocks}");
@ -107,9 +107,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
Assert.Equal(componentCount, decoder.ComponentCount); Assert.Equal(componentCount, decoder.ComponentCount);
Assert.Equal(componentCount, decoder.Components.Length); Assert.Equal(componentCount, decoder.Components.Length);
JpegFrameComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
JpegFrameComponent c1 = decoder.Components[1]; JpegComponent c1 = decoder.Components[1];
JpegFrameComponent c2 = decoder.Components[2]; JpegComponent c2 = decoder.Components[2];
var uniform1 = new Size(1, 1); var uniform1 = new Size(1, 1);
@ -125,7 +125,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
if (componentCount == 4) if (componentCount == 4)
{ {
JpegFrameComponent c3 = decoder.Components[2]; JpegComponent c3 = decoder.Components[2];
VerifyJpeg.VerifyComponent(c3, expectedLumaSizeInBlocks, fLuma, uniform1); VerifyJpeg.VerifyComponent(c3, expectedLumaSizeInBlocks, fLuma, uniform1);
} }
} }

2
tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs

@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils
this.SpectralBlocks[x, y] = new Block8x8(data); this.SpectralBlocks[x, y] = new Block8x8(data);
} }
public static ComponentData Load(JpegFrameComponent c, int index) public static ComponentData Load(JpegComponent c, int index)
{ {
var result = new ComponentData( var result = new ComponentData(
c.WidthInBlocks, c.WidthInBlocks,

2
tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.SpectralData.cs

@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils
public static SpectralData LoadFromImageSharpDecoder(JpegDecoderCore decoder) public static SpectralData LoadFromImageSharpDecoder(JpegDecoderCore decoder)
{ {
JpegFrameComponent[] srcComponents = decoder.Frame.Components; JpegComponent[] srcComponents = decoder.Frame.Components;
LibJpegTools.ComponentData[] destComponents = srcComponents.Select(LibJpegTools.ComponentData.Load).ToArray(); LibJpegTools.ComponentData[] destComponents = srcComponents.Select(LibJpegTools.ComponentData.Load).ToArray();
return new SpectralData(destComponents); return new SpectralData(destComponents);

Loading…
Cancel
Save