From 411f474bfce013673716763e07e9a454c0309f25 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Jul 2020 22:10:30 +0100 Subject: [PATCH] Remove most pointer usage --- .../Formats/Jpeg/Components/Block8x8F.cs | 21 +++--- .../Jpeg/Components/Encoder/BlockQuad.cs | 4 +- .../Formats/Jpeg/JpegEncoderCore.cs | 69 +++++++++---------- .../Formats/Jpg/Block8x8FTests.cs | 2 +- 4 files changed, 45 insertions(+), 51 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index 8809f890fd..26d3e0dbb3 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -378,20 +378,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// The quantization table /// Pointer to elements of 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); } /// @@ -399,13 +396,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// /// The destination block. /// The source block. - 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); diff --git a/src/ImageSharp/Formats/Jpeg/Components/Encoder/BlockQuad.cs b/src/ImageSharp/Formats/Jpeg/Components/Encoder/BlockQuad.cs index cda149d29e..57f9a60cb1 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Encoder/BlockQuad.cs +++ b/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 /// public fixed float Data[4 * Block8x8F.Size]; } -} \ No newline at end of file +} diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index f755028dbd..08ffd53343 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/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); } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs index 3482662698..4c7277e5bc 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs +++ b/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++) {