Browse Source

gfoidl review, removed obsolete enum

pull/2120/head
Dmitry Pentin 4 years ago
parent
commit
893bc201bd
  1. 2
      src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
  2. 13
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.ScaledCopy.cs
  3. 6
      src/ImageSharp/Formats/Jpeg/Components/Encoder/Component.cs
  4. 11
      src/ImageSharp/Formats/Jpeg/Components/Encoder/ComponentProcessor.cs
  5. 32
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs
  6. 21
      src/ImageSharp/Formats/Jpeg/Components/Encoder/QuantIndex.cs

2
src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

@ -984,7 +984,7 @@ namespace SixLabors.ImageSharp
Vector256<float> r, g, b; Vector256<float> r, g, b;
const int bytesPerRgbStride = 24; const int bytesPerRgbStride = 24;
int count = source.Length / 8; int count = (int)((uint)source.Length / 8);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
rgb = Avx2.PermuteVar8x32(Unsafe.AddByteOffset(ref rgbByteSpan, (IntPtr)(bytesPerRgbStride * i)).AsUInt32(), extractToLanesMask).AsByte(); rgb = Avx2.PermuteVar8x32(Unsafe.AddByteOffset(ref rgbByteSpan, (IntPtr)(bytesPerRgbStride * i)).AsUInt32(), extractToLanesMask).AsByte();

13
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.ScaledCopy.cs

@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
private void CopyTo2x2Scale(ref float areaOrigin, int areaStride) private void CopyTo2x2Scale(ref float areaOrigin, int areaStride)
{ {
ref Vector2 destBase = ref Unsafe.As<float, Vector2>(ref areaOrigin); ref Vector2 destBase = ref Unsafe.As<float, Vector2>(ref areaOrigin);
int destStride = areaStride / 2; int destStride = (int)((uint)areaStride / 2);
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 0, destStride); WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 0, destStride);
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 1, destStride); WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 1, destStride);
@ -48,12 +48,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 7, destStride); WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 7, destStride);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static void WidenCopyRowImpl2x2(ref Vector4 selfBase, ref Vector2 destBase, int row, int destStride) static void WidenCopyRowImpl2x2(ref Vector4 selfBase, ref Vector2 destBase, nint row, nint destStride)
{ {
ref Vector4 sLeft = ref Unsafe.Add(ref selfBase, 2 * row); ref Vector4 sLeft = ref Unsafe.Add(ref selfBase, 2 * row);
ref Vector4 sRight = ref Unsafe.Add(ref sLeft, 1); ref Vector4 sRight = ref Unsafe.Add(ref sLeft, 1);
int offset = 2 * row * destStride; nint offset = 2 * row * destStride;
ref Vector4 dTopLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset)); ref Vector4 dTopLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset));
ref Vector4 dBottomLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset + destStride)); ref Vector4 dBottomLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset + destStride));
@ -98,12 +98,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
int xx = x * horizontalScale; int xx = x * horizontalScale;
float value = this[y8 + x]; float value = this[y8 + x];
nint baseIdx = (yy * areaStride) + xx;
for (int i = 0; i < verticalScale; i++) for (nint i = 0; i < verticalScale; i++, baseIdx += areaStride)
{ {
int baseIdx = ((yy + i) * areaStride) + xx; for (nint j = 0; j < horizontalScale; j++)
for (int j = 0; j < horizontalScale; j++)
{ {
// area[xx + j, yy + i] = value; // area[xx + j, yy + i] = value;
Unsafe.Add(ref areaOrigin, baseIdx + j) = value; Unsafe.Add(ref areaOrigin, baseIdx + j) = value;

6
src/ImageSharp/Formats/Jpeg/Components/Encoder/Component.cs

@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
this.HorizontalSamplingFactor = horizontalFactor; this.HorizontalSamplingFactor = horizontalFactor;
this.VerticalSamplingFactor = verticalFactor; this.VerticalSamplingFactor = verticalFactor;
this.SamplingFactors = new Size(this.HorizontalSamplingFactor, this.VerticalSamplingFactor); this.SamplingFactors = new Size(horizontalFactor, verticalFactor);
this.QuantizationTableIndex = quantizationTableIndex; this.QuantizationTableIndex = quantizationTableIndex;
} }
@ -85,10 +85,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
public void Init(JpegFrame frame, int maxSubFactorH, int maxSubFactorV) public void Init(JpegFrame frame, int maxSubFactorH, int maxSubFactorV)
{ {
this.WidthInBlocks = (int)MathF.Ceiling( this.WidthInBlocks = (int)MathF.Ceiling(
MathF.Ceiling(frame.PixelWidth / 8F) * this.HorizontalSamplingFactor / maxSubFactorH); ((uint)frame.PixelWidth + 7) / 8 * this.HorizontalSamplingFactor / maxSubFactorH);
this.HeightInBlocks = (int)MathF.Ceiling( this.HeightInBlocks = (int)MathF.Ceiling(
MathF.Ceiling(frame.PixelHeight / 8F) * this.VerticalSamplingFactor / maxSubFactorV); ((uint)frame.PixelHeight + 7) / 8 * this.VerticalSamplingFactor / maxSubFactorV);
int blocksPerLineForMcu = frame.McusPerLine * this.HorizontalSamplingFactor; int blocksPerLineForMcu = frame.McusPerLine * this.HorizontalSamplingFactor;
int blocksPerColumnForMcu = frame.McusPerColumn * this.VerticalSamplingFactor; int blocksPerColumnForMcu = frame.McusPerColumn * this.VerticalSamplingFactor;

11
src/ImageSharp/Formats/Jpeg/Components/Encoder/ComponentProcessor.cs

@ -168,20 +168,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
// log2(2) == 2 / 2 == 1 // log2(2) == 2 / 2 == 1
// log2(4) == 4 / 2 == 2 // log2(4) == 4 / 2 == 2
int haddIterationsCount = (int)((uint)factor / 2); int haddIterationsCount = (int)((uint)factor / 2);
int length = target.Length / Vector256<float>.Count; uint length = (uint)target.Length / (uint)Vector256<float>.Count;
for (int i = 0; i < haddIterationsCount; i++) for (int i = 0; i < haddIterationsCount; i++)
{ {
length /= 2; length /= 2;
for (int j = 0; j < length; j++)
for (nuint j = 0; j < length; j++)
{ {
int indexLeft = j * 2; nuint indexLeft = j * 2;
int indexRight = indexLeft + 1; nuint indexRight = indexLeft + 1;
Vector256<float> sum = Avx.HorizontalAdd(Unsafe.Add(ref targetRef, indexLeft), Unsafe.Add(ref targetRef, indexRight)); Vector256<float> sum = Avx.HorizontalAdd(Unsafe.Add(ref targetRef, indexLeft), Unsafe.Add(ref targetRef, indexRight));
Unsafe.Add(ref targetRef, j) = Avx2.Permute4x64(sum.AsDouble(), 0b11_01_10_00).AsSingle(); Unsafe.Add(ref targetRef, j) = Avx2.Permute4x64(sum.AsDouble(), 0b11_01_10_00).AsSingle();
} }
} }
int summedCount = length * factor * Vector256<float>.Count; int summedCount = (int)(length * factor * Vector256<float>.Count);
target = target.Slice(summedCount); target = target.Slice(summedCount);
} }

32
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

@ -183,7 +183,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: 0); Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: 0);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (int k = 0; k < w; k++) for (nint k = 0; k < w; k++)
{ {
this.WriteBlock( this.WriteBlock(
component, component,
@ -222,7 +222,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: i); Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: i);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (int k = 0; k < w; k++) for (nint k = 0; k < w; k++)
{ {
this.WriteBlock( this.WriteBlock(
component, component,
@ -249,9 +249,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
private void EncodeScanBaselineInterleaved<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken) private void EncodeScanBaselineInterleaved<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int mcu = 0; nint mcu = 0;
int mcusPerColumn = frame.McusPerColumn; nint mcusPerColumn = frame.McusPerColumn;
int mcusPerLine = frame.McusPerLine; nint mcusPerLine = frame.McusPerLine;
for (int j = 0; j < mcusPerColumn; j++) for (int j = 0; j < mcusPerColumn; j++)
{ {
@ -261,20 +261,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
converter.ConvertStrideBaseline(); converter.ConvertStrideBaseline();
// Encode spectral to binary // Encode spectral to binary
for (int i = 0; i < mcusPerLine; i++) for (nint i = 0; i < mcusPerLine; i++)
{ {
// Scan an interleaved mcu... process components in order // Scan an interleaved mcu... process components in order
int mcuCol = mcu % mcusPerLine; nint mcuCol = mcu % mcusPerLine;
for (int k = 0; k < frame.Components.Length; k++) for (nint k = 0; k < frame.Components.Length; k++)
{ {
Component component = frame.Components[k]; Component component = frame.Components[k];
ref HuffmanLut dcHuffmanTable = ref this.dcHuffmanTables[component.DcTableId]; ref HuffmanLut dcHuffmanTable = ref this.dcHuffmanTables[component.DcTableId];
ref HuffmanLut acHuffmanTable = ref this.acHuffmanTables[component.AcTableId]; ref HuffmanLut acHuffmanTable = ref this.acHuffmanTables[component.AcTableId];
int h = component.HorizontalSamplingFactor; nint h = component.HorizontalSamplingFactor;
int v = component.VerticalSamplingFactor; int v = component.VerticalSamplingFactor;
nint blockColBase = mcuCol * h;
// Scan out an mcu's worth of this component; that's just determined // Scan out an mcu's worth of this component; that's just determined
// by the basic H and V specified for the component // by the basic H and V specified for the component
for (int y = 0; y < v; y++) for (int y = 0; y < v; y++)
@ -282,9 +284,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y); Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (int x = 0; x < h; x++) for (nint x = 0; x < h; x++)
{ {
int blockCol = (mcuCol * h) + x; nint blockCol = blockColBase + x;
this.WriteBlock( this.WriteBlock(
component, component,
@ -316,8 +318,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
private void EncodeThreeComponentScanBaselineInterleaved444<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken) private void EncodeThreeComponentScanBaselineInterleaved444<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int mcusPerColumn = frame.McusPerColumn; nint mcusPerColumn = frame.McusPerColumn;
int mcusPerLine = frame.McusPerLine; nint mcusPerLine = frame.McusPerLine;
Component c2 = frame.Components[2]; Component c2 = frame.Components[2];
Component c1 = frame.Components[1]; Component c1 = frame.Components[1];
@ -334,7 +336,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
ref Block8x8 c1BlockRef = ref MemoryMarshal.GetReference(c1.SpectralBlocks.DangerousGetRowSpan(y: 0)); ref Block8x8 c1BlockRef = ref MemoryMarshal.GetReference(c1.SpectralBlocks.DangerousGetRowSpan(y: 0));
ref Block8x8 c2BlockRef = ref MemoryMarshal.GetReference(c2.SpectralBlocks.DangerousGetRowSpan(y: 0)); ref Block8x8 c2BlockRef = ref MemoryMarshal.GetReference(c2.SpectralBlocks.DangerousGetRowSpan(y: 0));
for (int j = 0; j < mcusPerColumn; j++) for (nint j = 0; j < mcusPerColumn; j++)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -342,7 +344,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
converter.ConvertStrideBaseline(); converter.ConvertStrideBaseline();
// Encode spectral to binary // Encode spectral to binary
for (int i = 0; i < mcusPerLine; i++) for (nint i = 0; i < mcusPerLine; i++)
{ {
this.WriteBlock( this.WriteBlock(
c0, c0,

21
src/ImageSharp/Formats/Jpeg/Components/Encoder/QuantIndex.cs

@ -1,21 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
{
/// <summary>
/// Enumerates the quantization tables.
/// </summary>
internal enum QuantIndex
{
/// <summary>
/// The luminance quantization table index.
/// </summary>
Luminance = 0,
/// <summary>
/// The chrominance quantization table index.
/// </summary>
Chrominance = 1,
}
}
Loading…
Cancel
Save