Browse Source

Moved IJpegComponent.GetBlockReference(...) to an extension method

af/merge-core
Anton Firszov 8 years ago
parent
commit
cec2d1d67d
  1. 11
      src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
  2. 18
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
  3. 39
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentExtensions.cs

11
src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
@ -43,16 +42,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// <summary>
/// Gets the <see cref="Buffer2D{Block8x8}"/> storing the "raw" frequency-domain decoded + unzigged blocks.
/// We need to apply IDCT and dequantiazition to transform them into color-space blocks.
/// We need to apply IDCT and dequantization to transform them into color-space blocks.
/// </summary>
Buffer2D<Block8x8> SpectralBlocks { get; }
/// <summary>
/// Gets a reference to the <see cref="Block8x8"/> at the given row and column index from <see cref="SpectralBlocks"/>
/// </summary>
/// <param name="column">The column</param>
/// <param name="row">The row</param>
/// <returns>The <see cref="Block8x8"/></returns>
ref Block8x8 GetBlockReference(int column, int row);
}
}

18
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs

@ -128,21 +128,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors);
}
this.SpectralBlocks = this.memoryAllocator.Allocate2D<Block8x8>(blocksPerColumnForMcu, blocksPerLineForMcu + 1, AllocationOptions.Clean);
}
int totalNumberOfBlocks = blocksPerColumnForMcu * (blocksPerLineForMcu + 1);
int width = this.WidthInBlocks + 1;
int height = totalNumberOfBlocks / width;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref Block8x8 GetBlockReference(int column, int row)
{
int offset = ((this.WidthInBlocks + 1) * row) + column;
return ref Unsafe.Add(ref MemoryMarshal.GetReference(this.SpectralBlocks.GetSpan()), offset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref short GetBlockDataReference(int column, int row)
{
ref Block8x8 blockRef = ref this.GetBlockReference(column, row);
return ref Unsafe.As<Block8x8, short>(ref blockRef);
this.SpectralBlocks = this.memoryAllocator.Allocate2D<Block8x8>(width, height, AllocationOptions.Clean);
}
}
}

39
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentExtensions.cs

@ -0,0 +1,39 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
{
/// <summary>
/// Extension methods for <see cref="IJpegComponent"/>
/// </summary>
internal static class JpegComponentExtensions
{
/// <summary>
/// Gets a reference to the <see cref="Block8x8"/> at the given row and column index from <see cref="IJpegComponent.SpectralBlocks"/>
/// </summary>
/// <param name="component">The <see cref="IJpegComponent"/></param>
/// <param name="column">The column</param>
/// <param name="row">The row</param>
/// <returns>The <see cref="Block8x8"/></returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static ref Block8x8 GetBlockReference(this IJpegComponent component, int column, int row)
{
return ref component.SpectralBlocks.GetRowSpan(row)[column];
}
/// <summary>
/// Gets a reference to the first item in a block
/// at the given row and column index from <see cref="IJpegComponent.SpectralBlocks"/>
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
public static ref short GetBlockDataReference(this IJpegComponent component, int column, int row)
{
ref Block8x8 blockRef = ref component.GetBlockReference(column, row);
return ref Unsafe.As<Block8x8, short>(ref blockRef);
}
}
}
Loading…
Cancel
Save