Browse Source

Using Binary serialization for Vp8Residual

pull/2432/head
Brian Popow 3 years ago
parent
commit
39f8c776de
  1. 8
      src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs
  2. 10
      src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs
  3. 10
      src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs
  4. 10
      src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs
  5. 13
      src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs
  6. 10
      src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs
  7. 10
      src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs
  8. 32
      tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs
  9. 2
      tests/ImageSharp.Tests/ImageSharp.Tests.csproj
  10. BIN
      tests/ImageSharp.Tests/TestDataWebp/Vp8Residual.bin

8
src/ImageSharp/Formats/Webp/Lossy/Vp8BandProbas.cs

@ -8,6 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
/// <summary> /// <summary>
/// All the probabilities associated to one band. /// All the probabilities associated to one band.
/// </summary> /// </summary>
[Serializable]
internal class Vp8BandProbas internal class Vp8BandProbas
{ {
/// <summary> /// <summary>
@ -22,13 +23,6 @@ internal class Vp8BandProbas
} }
} }
/// <summary>
/// Initializes a new instance of the <see cref="Vp8BandProbas"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8BandProbas(Vp8ProbaArray[] probabilities) => this.Probabilities = probabilities;
/// <summary> /// <summary>
/// Gets the Probabilities. /// Gets the Probabilities.
/// </summary> /// </summary>

10
src/ImageSharp/Formats/Webp/Lossy/Vp8CostArray.cs

@ -1,10 +1,9 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Text.Json.Serialization;
namespace SixLabors.ImageSharp.Formats.Webp.Lossy; namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
[Serializable]
internal class Vp8CostArray internal class Vp8CostArray
{ {
/// <summary> /// <summary>
@ -12,12 +11,5 @@ internal class Vp8CostArray
/// </summary> /// </summary>
public Vp8CostArray() => this.Costs = new ushort[67 + 1]; public Vp8CostArray() => this.Costs = new ushort[67 + 1];
/// <summary>
/// Initializes a new instance of the <see cref="Vp8CostArray"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8CostArray(ushort[] costs) => this.Costs = costs;
public ushort[] Costs { get; } public ushort[] Costs { get; }
} }

10
src/ImageSharp/Formats/Webp/Lossy/Vp8Costs.cs

@ -1,10 +1,9 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Text.Json.Serialization;
namespace SixLabors.ImageSharp.Formats.Webp.Lossy; namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
[Serializable]
internal class Vp8Costs internal class Vp8Costs
{ {
/// <summary> /// <summary>
@ -19,13 +18,6 @@ internal class Vp8Costs
} }
} }
/// <summary>
/// Initializes a new instance of the <see cref="Vp8Costs"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8Costs(Vp8CostArray[] costs) => this.Costs = costs;
/// <summary> /// <summary>
/// Gets the Costs. /// Gets the Costs.
/// </summary> /// </summary>

10
src/ImageSharp/Formats/Webp/Lossy/Vp8ProbaArray.cs

@ -1,13 +1,12 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Text.Json.Serialization;
namespace SixLabors.ImageSharp.Formats.Webp.Lossy; namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
/// <summary> /// <summary>
/// Probabilities associated to one of the contexts. /// Probabilities associated to one of the contexts.
/// </summary> /// </summary>
[Serializable]
internal class Vp8ProbaArray internal class Vp8ProbaArray
{ {
/// <summary> /// <summary>
@ -15,13 +14,6 @@ internal class Vp8ProbaArray
/// </summary> /// </summary>
public Vp8ProbaArray() => this.Probabilities = new byte[WebpConstants.NumProbas]; public Vp8ProbaArray() => this.Probabilities = new byte[WebpConstants.NumProbas];
/// <summary>
/// Initializes a new instance of the <see cref="Vp8ProbaArray"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8ProbaArray(byte[] probabilities) => this.Probabilities = probabilities;
/// <summary> /// <summary>
/// Gets the probabilities. /// Gets the probabilities.
/// </summary> /// </summary>

13
src/ImageSharp/Formats/Webp/Lossy/Vp8Residual.cs

@ -14,24 +14,13 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
/// <summary> /// <summary>
/// On-the-fly info about the current set of residuals. /// On-the-fly info about the current set of residuals.
/// </summary> /// </summary>
[Serializable]
internal class Vp8Residual internal class Vp8Residual
{ {
public Vp8Residual() public Vp8Residual()
{ {
} }
[JsonConstructor]
public Vp8Residual(int first, int last, int coeffType, short[] coeffs, Vp8BandProbas[] prob, Vp8Stats[] stats, Vp8Costs[] costs)
{
this.First = first;
this.Last = last;
this.CoeffType = coeffType;
this.Coeffs = coeffs;
this.Prob = prob;
this.Stats = stats;
this.Costs = costs;
}
public int First { get; set; } public int First { get; set; }
public int Last { get; set; } public int Last { get; set; }

10
src/ImageSharp/Formats/Webp/Lossy/Vp8Stats.cs

@ -1,10 +1,9 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Text.Json.Serialization;
namespace SixLabors.ImageSharp.Formats.Webp.Lossy; namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
[Serializable]
internal class Vp8Stats internal class Vp8Stats
{ {
/// <summary> /// <summary>
@ -19,12 +18,5 @@ internal class Vp8Stats
} }
} }
/// <summary>
/// Initializes a new instance of the <see cref="Vp8Stats"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8Stats(Vp8StatsArray[] stats) => this.Stats = stats;
public Vp8StatsArray[] Stats { get; } public Vp8StatsArray[] Stats { get; }
} }

10
src/ImageSharp/Formats/Webp/Lossy/Vp8StatsArray.cs

@ -1,10 +1,9 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Text.Json.Serialization;
namespace SixLabors.ImageSharp.Formats.Webp.Lossy; namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
[Serializable]
internal class Vp8StatsArray internal class Vp8StatsArray
{ {
/// <summary> /// <summary>
@ -12,12 +11,5 @@ internal class Vp8StatsArray
/// </summary> /// </summary>
public Vp8StatsArray() => this.Stats = new uint[WebpConstants.NumProbas]; public Vp8StatsArray() => this.Stats = new uint[WebpConstants.NumProbas];
/// <summary>
/// Initializes a new instance of the <see cref="Vp8StatsArray"/> class.
/// Only used for unit tests.
/// </summary>
[JsonConstructor]
public Vp8StatsArray(uint[] stats) => this.Stats = stats;
public uint[] Stats { get; } public uint[] Stats { get; }
} }

32
tests/ImageSharp.Tests/Formats/WebP/Vp8ResidualTests.cs

@ -1,11 +1,10 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Runtime.Intrinsics.X86; using System.Runtime.Serialization.Formatters.Binary;
using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.Formats.Webp.Lossy;
using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace SixLabors.ImageSharp.Tests.Formats.Webp; namespace SixLabors.ImageSharp.Tests.Formats.Webp;
@ -15,12 +14,6 @@ public class Vp8ResidualTests
[Fact] [Fact]
public void Vp8Residual_Serialization_Works() public void Vp8Residual_Serialization_Works()
{ {
if (!Sse2.IsSupported)
{
// JsonSerializer without SSE2 does not seem to work, skip test then.
return;
}
// arrange // arrange
Vp8Residual expected = new(); Vp8Residual expected = new();
Vp8EncProba encProb = new(); Vp8EncProba encProb = new();
@ -34,8 +27,12 @@ public class Vp8ResidualTests
} }
// act // act
string jsonString = JsonSerializer.Serialize(expected); BinaryFormatter formatter = new();
Vp8Residual actual = JsonSerializer.Deserialize<Vp8Residual>(jsonString); using MemoryStream ms = new();
formatter.Serialize(ms, expected);
ms.Position = 0;
object obj = formatter.Deserialize(ms);
Vp8Residual actual = (Vp8Residual)obj;
// assert // assert
Assert.Equal(expected.CoeffType, actual.CoeffType); Assert.Equal(expected.CoeffType, actual.CoeffType);
@ -82,17 +79,14 @@ public class Vp8ResidualTests
[Fact] [Fact]
public void GetResidualCost_Works() public void GetResidualCost_Works()
{ {
if (!Sse2.IsSupported)
{
// JsonSerializer without SSE2 does not seem to work, skip test then.
return;
}
// arrange // arrange
int ctx0 = 0; int ctx0 = 0;
int expected = 20911; int expected = 20911;
string jsonString = File.ReadAllText(Path.Combine("TestDataWebp", "Vp8Residual.json")); byte[] data = File.ReadAllBytes(Path.Combine("TestDataWebp", "Vp8Residual.bin"));
Vp8Residual residual = JsonSerializer.Deserialize<Vp8Residual>(jsonString); BinaryFormatter formatter = new();
using Stream stream = new MemoryStream(data);
object obj = formatter.Deserialize(stream);
Vp8Residual residual = (Vp8Residual)obj;
// act // act
int actual = residual.GetResidualCost(ctx0); int actual = residual.GetResidualCost(ctx0);
@ -101,8 +95,6 @@ public class Vp8ResidualTests
Assert.Equal(expected, actual); Assert.Equal(expected, actual);
} }
private static void CreateRandomProbas(Vp8EncProba probas, Random rand) private static void CreateRandomProbas(Vp8EncProba probas, Random rand)
{ {
for (int t = 0; t < WebpConstants.NumTypes; ++t) for (int t = 0; t < WebpConstants.NumTypes; ++t)

2
tests/ImageSharp.Tests/ImageSharp.Tests.csproj

@ -56,7 +56,7 @@
<None Update="TestFonts\SixLaborsSampleAB.woff"> <None Update="TestFonts\SixLaborsSampleAB.woff">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="TestDataWebp\Vp8Residual.json"> <None Update="TestDataWebp\Vp8Residual.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="xunit.runner.json"> <None Update="xunit.runner.json">

BIN
tests/ImageSharp.Tests/TestDataWebp/Vp8Residual.bin

Binary file not shown.
Loading…
Cancel
Save