Browse Source

Remove most pointer usage

pull/1574/head
James Jackson-South 6 years ago
parent
commit
411f474bfc
  1. 21
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs
  2. 4
      src/ImageSharp/Formats/Jpeg/Components/Encoder/BlockQuad.cs
  3. 69
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
  4. 2
      tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs

21
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

@ -378,20 +378,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
/// <param name="qt">The quantization table</param>
/// <param name="unzigPtr">Pointer to elements of <see cref="ZigZag"/></param>
public static unsafe void Quantize(
Block8x8F* block,
Block8x8F* dest,
Block8x8F* qt,
ref Block8x8F block,
ref Block8x8F dest,
ref Block8x8F qt,
byte* unzigPtr)
{
float* s = (float*)block;
float* d = (float*)dest;
for (int zig = 0; zig < Size; zig++)
{
d[zig] = s[unzigPtr[zig]];
dest[zig] = block[unzigPtr[zig]];
}
DivideRoundAll(ref *dest, ref *qt);
DivideRoundAll(ref dest, ref qt);
}
/// <summary>
@ -399,13 +396,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
/// </summary>
/// <param name="destination">The destination block.</param>
/// <param name="source">The source block.</param>
public static unsafe void Scale16X16To8X8(Block8x8F* destination, Block8x8F* source)
public static unsafe void Scale16X16To8X8(ref Block8x8F destination, Block8x8F* source)
{
float* d = (float*)destination;
for (int i = 0; i < 4; i++)
{
int dstOff = ((i & 2) << 4) | ((i & 1) << 2);
float* iSource = (float*)(source + i);
for (int y = 0; y < 4; y++)
@ -414,7 +409,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
{
int j = (16 * y) + (2 * x);
float sum = iSource[j] + iSource[j + 1] + iSource[j + 8] + iSource[j + 9];
d[(8 * y) + x + dstOff] = (sum + 2) / 4;
destination[(8 * y) + x + dstOff] = (sum + 2) * .25F;
}
}
}
@ -589,7 +584,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components
private static Vector4 DivideRound(Vector4 dividend, Vector4 divisor)
{
// sign(dividend) = max(min(dividend, 1), -1)
var sign = Vector4Utilities.FastClamp(dividend, NegativeOne, Vector4.One);
Vector4 sign = Vector4Utilities.FastClamp(dividend, NegativeOne, Vector4.One);
// AlmostRound(dividend/divisor) = dividend/divisor + 0.5*sign(dividend)
return (dividend / divisor) + (sign * Offset);

4
src/ImageSharp/Formats/Jpeg/Components/Encoder/BlockQuad.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
@ -14,4 +14,4 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
/// </summary>
public fixed float Data[4 * Block8x8F.Size];
}
}
}

69
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

@ -427,26 +427,26 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
prevDCY = this.WriteBlock(
QuantIndex.Luminance,
prevDCY,
&pixelConverter.Y,
&temp1,
&temp2,
&onStackLuminanceQuantTable,
ref pixelConverter.Y,
ref temp1,
ref temp2,
ref onStackLuminanceQuantTable,
unzig.Data);
prevDCCb = this.WriteBlock(
QuantIndex.Chrominance,
prevDCCb,
&pixelConverter.Cb,
&temp1,
&temp2,
&onStackChrominanceQuantTable,
ref pixelConverter.Cb,
ref temp1,
ref temp2,
ref onStackChrominanceQuantTable,
unzig.Data);
prevDCCr = this.WriteBlock(
QuantIndex.Chrominance,
prevDCCr,
&pixelConverter.Cr,
&temp1,
&temp2,
&onStackChrominanceQuantTable,
ref pixelConverter.Cr,
ref temp1,
ref temp2,
ref onStackChrominanceQuantTable,
unzig.Data);
}
}
@ -519,18 +519,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
private int WriteBlock(
QuantIndex index,
int prevDC,
Block8x8F* src,
Block8x8F* tempDest1,
Block8x8F* tempDest2,
Block8x8F* quant,
ref Block8x8F src,
ref Block8x8F tempDest1,
ref Block8x8F tempDest2,
ref Block8x8F quant,
byte* unzigPtr)
{
FastFloatingPointDCT.TransformFDCT(ref *src, ref *tempDest1, ref *tempDest2);
FastFloatingPointDCT.TransformFDCT(ref src, ref tempDest1, ref tempDest2);
Block8x8F.Quantize(tempDest1, tempDest2, quant, unzigPtr);
float* unziggedDestPtr = (float*)tempDest2;
Block8x8F.Quantize(ref tempDest1, ref tempDest2, ref quant, unzigPtr);
int dc = (int)unziggedDestPtr[0];
int dc = (int)tempDest2[0];
// Emit the DC delta.
this.EmitHuffRLE((HuffIndex)((2 * (int)index) + 0), 0, dc - prevDC);
@ -541,7 +540,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
for (int zig = 1; zig < Block8x8F.Size; zig++)
{
int ac = (int)unziggedDestPtr[zig];
int ac = (int)tempDest2[zig];
if (ac == 0)
{
@ -1016,31 +1015,31 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
prevDCY = this.WriteBlock(
QuantIndex.Luminance,
prevDCY,
&pixelConverter.Y,
&temp1,
&temp2,
&onStackLuminanceQuantTable,
ref pixelConverter.Y,
ref temp1,
ref temp2,
ref onStackLuminanceQuantTable,
unzig.Data);
}
Block8x8F.Scale16X16To8X8(&b, cbPtr);
Block8x8F.Scale16X16To8X8(ref b, cbPtr);
prevDCCb = this.WriteBlock(
QuantIndex.Chrominance,
prevDCCb,
&b,
&temp1,
&temp2,
&onStackChrominanceQuantTable,
ref b,
ref temp1,
ref temp2,
ref onStackChrominanceQuantTable,
unzig.Data);
Block8x8F.Scale16X16To8X8(&b, crPtr);
Block8x8F.Scale16X16To8X8(ref b, crPtr);
prevDCCr = this.WriteBlock(
QuantIndex.Chrominance,
prevDCCr,
&b,
&temp1,
&temp2,
&onStackChrominanceQuantTable,
ref b,
ref temp1,
ref temp2,
ref onStackChrominanceQuantTable,
unzig.Data);
}
}

2
tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs

@ -282,7 +282,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var actualResults = default(Block8x8F);
Block8x8F.Quantize(&block, &actualResults, &qt, unzig.Data);
Block8x8F.Quantize(ref block, ref actualResults, ref qt, unzig.Data);
for (int i = 0; i < Block8x8F.Size; i++)
{

Loading…
Cancel
Save