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;
const int bytesPerRgbStride = 24;
int count = source.Length / 8;
int count = (int)((uint)source.Length / 8);
for (int i = 0; i < count; i++)
{
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)
{
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, 1, destStride);
@ -48,12 +48,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 7, destStride);
[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 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 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;
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 (int j = 0; j < horizontalScale; j++)
for (nint j = 0; j < horizontalScale; j++)
{
// area[xx + j, yy + i] = 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.VerticalSamplingFactor = verticalFactor;
this.SamplingFactors = new Size(this.HorizontalSamplingFactor, this.VerticalSamplingFactor);
this.SamplingFactors = new Size(horizontalFactor, verticalFactor);
this.QuantizationTableIndex = quantizationTableIndex;
}
@ -85,10 +85,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
public void Init(JpegFrame frame, int maxSubFactorH, int maxSubFactorV)
{
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(
MathF.Ceiling(frame.PixelHeight / 8F) * this.VerticalSamplingFactor / maxSubFactorV);
((uint)frame.PixelHeight + 7) / 8 * this.VerticalSamplingFactor / maxSubFactorV);
int blocksPerLineForMcu = frame.McusPerLine * this.HorizontalSamplingFactor;
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(4) == 4 / 2 == 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++)
{
length /= 2;
for (int j = 0; j < length; j++)
for (nuint j = 0; j < length; j++)
{
int indexLeft = j * 2;
int indexRight = indexLeft + 1;
nuint indexLeft = j * 2;
nuint indexRight = indexLeft + 1;
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();
}
}
int summedCount = length * factor * Vector256<float>.Count;
int summedCount = (int)(length * factor * Vector256<float>.Count);
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);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (int k = 0; k < w; k++)
for (nint k = 0; k < w; k++)
{
this.WriteBlock(
component,
@ -222,7 +222,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: i);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (int k = 0; k < w; k++)
for (nint k = 0; k < w; k++)
{
this.WriteBlock(
component,
@ -249,9 +249,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
private void EncodeScanBaselineInterleaved<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
int mcu = 0;
int mcusPerColumn = frame.McusPerColumn;
int mcusPerLine = frame.McusPerLine;
nint mcu = 0;
nint mcusPerColumn = frame.McusPerColumn;
nint mcusPerLine = frame.McusPerLine;
for (int j = 0; j < mcusPerColumn; j++)
{
@ -261,20 +261,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
converter.ConvertStrideBaseline();
// 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
int mcuCol = mcu % mcusPerLine;
for (int k = 0; k < frame.Components.Length; k++)
nint mcuCol = mcu % mcusPerLine;
for (nint k = 0; k < frame.Components.Length; k++)
{
Component component = frame.Components[k];
ref HuffmanLut dcHuffmanTable = ref this.dcHuffmanTables[component.DcTableId];
ref HuffmanLut acHuffmanTable = ref this.acHuffmanTables[component.AcTableId];
int h = component.HorizontalSamplingFactor;
nint h = component.HorizontalSamplingFactor;
int v = component.VerticalSamplingFactor;
nint blockColBase = mcuCol * h;
// Scan out an mcu's worth of this component; that's just determined
// by the basic H and V specified for the component
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);
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(
component,
@ -316,8 +318,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
private void EncodeThreeComponentScanBaselineInterleaved444<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
int mcusPerColumn = frame.McusPerColumn;
int mcusPerLine = frame.McusPerLine;
nint mcusPerColumn = frame.McusPerColumn;
nint mcusPerLine = frame.McusPerLine;
Component c2 = frame.Components[2];
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 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();
@ -342,7 +344,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
converter.ConvertStrideBaseline();
// Encode spectral to binary
for (int i = 0; i < mcusPerLine; i++)
for (nint i = 0; i < mcusPerLine; i++)
{
this.WriteBlock(
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